This repository has been archived by the owner on Jun 11, 2019. It is now read-only.
forked from codeinthedark/editor
-
Notifications
You must be signed in to change notification settings - Fork 4
/
gulpfile.js
155 lines (132 loc) · 4.31 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const _ = require('underscore');
const $ = require('gulp-load-plugins')();
const gulp = require('gulp');
const path = require('path');
const rename = require('gulp-rename');
const merge = require('merge-stream');
const deepExtend = require('deep-extend');
const runSequence = require('run-sequence');
const autoprefixer = require('autoprefixer-core');
const webpack = require('webpack');
const WebpackDevServer = require('webpack-dev-server');
// ###################
// # CONFIG
// ###################
var config = {
paths: {
app: 'app',
tmp: '.tmp',
dist: 'dist',
scripts: 'app/scripts',
styles: 'app/styles',
assets: 'app/assets',
},
serverPort: 9000,
webpack() {
return {
resolveLoader: {
moduleDirectories: ['node_modules'],
},
output: {
path: path.join(__dirname, config.paths.tmp),
filename: 'bundle.js',
},
resolve: {
extensions: ['', '.js', '.css', '.ttf'],
alias: {
assets: path.join(__dirname, config.paths.assets),
},
},
module: {
loaders: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel-loader?presets[]=env'
},
{ test: /\.css$/, loaders: ['style', 'css', 'postcss-loader'] },
{ test: /\.png/, loaders: ['url-loader?mimetype=image/png'] },
{ test: /\.ttf/, loaders: ['url-loader?mimetype=font/ttf'] },
],
},
postcss: [autoprefixer({ browsers: ['last 2 version'] })],
};
},
webpackEnvs() {
return {
development: {
devtool: 'eval',
debug: true,
entry: [
`webpack-dev-server/client?http://0.0.0.0:${config.serverPort}`,
'webpack/hot/only-dev-server',
`./${config.paths.scripts}/app`,
],
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
],
},
distribute: {
entry: [
`./${config.paths.scripts}/app`,
],
plugins: [
new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin({
compressor: { warnings: false },
}),
],
},
};
},
};
config = _(config).mapObject((val) => {
if (_.isFunction(val)) { return val(); } return val;
});
const webpackers = _(config.webpackEnvs).mapObject(val => webpack(deepExtend({}, config.webpack, val)));
// ###################
// # TASKS
// ###################
gulp
.task('copy-assets', () => {
const assets = gulp
.src(path.join(config.paths.assets, '**'))
.pipe(gulp.dest(`${config.paths.tmp}/assets`));
const instructions = gulp
.src(path.join(config.paths.app, 'index.html'))
.pipe(gulp.dest(config.paths.tmp));
return merge(assets, instructions);
}).task('copy-page-files', () => gulp
.src(path.join(config.paths.assets, '{instructions.html,page.png,result.html,beach.jpg}'))
.pipe(gulp.dest(path.join(config.paths.dist, 'assets')))).task('webpack-dev-server', (done) => {
const server = new WebpackDevServer(webpackers.development, {
contentBase: config.paths.tmp,
hot: true,
watchDelay: 100,
noInfo: true,
});
return server.listen(config.serverPort, '0.0.0.0', (err) => {
if (err) { throw new $.util.PluginError('webpack-dev-server', err); }
$.util.log($.util.colors.green(
`[webpack-dev-server] Server running on http://localhost:${config.serverPort}`,
));
return done();
});
}).task('build', done => webpackers.distribute.run((err, stats) => {
if (err) { throw new $.util.PluginError('webpack:build', err); }
return done();
}))
.task('serve', ['copy-assets', 'webpack-dev-server'], () => gulp.watch(['app/assets/**'], ['copy-assets']))
.task('inline', () => gulp
.src(`${config.paths.tmp}/index.html`)
.pipe($.inlineSource({ compress: false }))
.pipe(rename({ basename: 'editor' }))
.pipe(gulp.dest(`${config.paths.dist}`)))
.task('dist', () => runSequence('copy-assets', 'build', 'inline', 'copy-page-files'))
.task('default', ['serve']);