-
Notifications
You must be signed in to change notification settings - Fork 29
/
gulpfile.js
204 lines (176 loc) · 4.89 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* eslint-disable no-use-before-define, no-console */
'use strict';
import BrowserSync from 'browser-sync';
import childProcess from 'child_process';
import config from './config';
import del from 'del';
import gulp from 'gulp';
import gulpLoadPlugins from 'gulp-load-plugins';
import nodemon from 'nodemon';
import notifier from 'node-notifier';
import path from 'path';
import runSequence from 'run-sequence';
import webpack from 'webpack';
import WebpackDevServer from 'webpack-dev-server';
const $ = gulpLoadPlugins();
const env = process.env.NODE_ENV || 'development';
var isWatching = false;
gulp.task('clean', clean);
gulp.task('js:lint', jsLint);
gulp.task('js:bundle', jsBundle);
gulp.task('browser-sync', browserSyncInitialize);
gulp.task('watch', function () {
isWatching = true;
runSequence([ 'js:lint', 'js:bundle', 'browser-sync' ]);
});
gulp.task('nodemon', function () {
nodemon({
ignore: [ 'src/js/**', 'node_modules' ],
exec: 'npm run express',
verbose: false
});
});
gulp.task('dev', function (callback) {
runSequence('js:bundle', [ 'watch', 'nodemon' ], callback);
});
gulp.task('build', function (callback) {
runSequence('js:bundle', callback);
});
function clean() {
del('dist');
}
function jsLint() {
var srcBlob = [ '**/*.@(js|jsx)', '!node_modules/**/*', '!dist/**/*' ];
return (isWatching ? $.watch(srcBlob) : gulp.src(srcBlob))
.pipe($.eslint())
.pipe($.plumber({
errorHandler(err) {
if (isWatching) {
let { fileName, lineNumber, message } = err;
let relativeFilename = path.relative(process.cwd(), fileName);
notifier.notify({
title: 'ESLint Error',
wait: true,
message: `Line ${lineNumber}: ${message} (${relativeFilename})`
}, (err, message) => {
if (err) {
console.error(err);
}
if (message.startsWith('Activate')) {
childProcess.exec(`subl --command open_file ${fileName}:${lineNumber}`);
}
});
}
}
}))
.pipe($.eslint.failOnError())
.pipe($.eslint.formatEach());
}
function jsBundle(callback) {
const { webpackDevServer: { host, port } } = config;
var webpackDevServerUrl = `http://${host}:${port}`;
var babelLoader = {
test: /\.jsx?$/,
loaders: [ 'babel-loader' ],
exclude: [
path.resolve(__dirname, 'node_modules')
]
};
var webpackConfig = {
devtool: '#inline-source-map',
entry: {
main: './src/js/main',
vendor: './src/js/vendor'
},
resolve: {
extensions: [ '', '.jsx', '.js' ],
modulesDirectories: [ 'node_modules' ]
},
target: 'web',
module: {
loaders: [ babelLoader ]
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': `"${env}"`
}
}),
new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.js')
],
output: {
path: path.resolve(__dirname, 'dist/js'),
publicPath: `${webpackDevServerUrl}/js/`,
filename: '[name].js',
chunkFilename: '[id].js'
}
};
var devServerConfig = {
contentBase: path.resolve(__dirname, 'dist'),
publicPath: webpackConfig.output.publicPath,
hot: true,
quiet: true,
noInfo: true,
stats: {
colors: true
}
};
if (env === 'production') {
webpackConfig.devtool = '#source-map';
webpackConfig.plugins.push(
new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin()
);
}
if (!isWatching) {
webpack(webpackConfig).run(function (err) {
if (err) {
handleError(err);
}
if (callback) {
callback();
}
});
} else {
webpackConfig.entry.main = [
`webpack-dev-server/client?${webpackDevServerUrl}`,
'webpack/hot/only-dev-server',
webpackConfig.entry.main
];
babelLoader.loaders.unshift('react-hot');
webpackConfig.plugins.push(
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin()
);
var compiler = webpack(webpackConfig);
var server = new WebpackDevServer(compiler, devServerConfig);
compiler.plugin('done', (stats) => {
if (stats.hasErrors()) {
console.error($.util.colors.red('WebpackError'));
stats.toJson().errors.forEach(err => console.error(err));
}
$.util.log('Finished', $.util.colors.cyan('jsBundle()'));
});
server.listen(config.webpackDevServer.port);
}
}
function browserSyncInitialize() {
const browserSync = BrowserSync.create();
browserSync.init({
files: [ 'dist/**/*' ],
open: false,
ui: false,
logLevel: 'silent',
port: config.browserSyncServer.port
});
}
function handleError(err) {
var { name, message } = err;
console.error($.util.colors.red(name), message);
if (isWatching) {
notifier.notify({
title: 'Build Error',
message: 'Something went wrong.'
});
}
}