This repository has been archived by the owner on Feb 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
/
gulpfile.babel.js
179 lines (142 loc) · 4.67 KB
/
gulpfile.babel.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
/* eslint-disable import/no-extraneous-dependencies */
import gulp from 'gulp';
import webpack from 'webpack';
import WebpackDevServer from 'webpack-dev-server';
import gutil from 'gulp-util';
import path from 'path';
import runSequence from 'run-sequence';
import del from 'del';
import webpackConfig from './webpack.config.babel';
import pluginsWithoutUglify from './config/webpackPluginsWithoutUglify';
import webpackCommonsChunkPluginConfig from './config/webpackCommonsChunkPluginConfig';
const cache = {};
const config = {
prod: false,
addon: false,
noEval: false,
};
webpackConfig.output.devtoolModuleFilenameTemplate = info =>
`wp:///${path.relative(__dirname, info.resourcePath)}`;
webpackConfig.output.devtoolFallbackModuleFilenameTemplate = info =>
`wp:///${path.relative(__dirname, info.resourcePath)}?${info.hash}`;
require('webpack/lib/ModuleFilenameHelpers').createFooter = () => '';
// pretend it's prod ( still has sourcemaps )
// slowest compilation
gulp.task('webpack-dev-server-prod', () => {
config.prod = true;
config.addon = true;
runSequence('webpack-dev-server');
});
// no eval, faster runtime, slower compilation
gulp.task('webpack-dev-server-no-eval', () => {
config.noEval = true;
runSequence('webpack-dev-server');
});
// fast compilation, low runtime performance
gulp.task('webpack-dev-server', () => {
const host = '0.0.0.0';
const port = 8080;
webpackConfig.cache = cache;
webpackConfig.entry.app = [
`webpack-dev-server/client?http://${host}:${port}`, // WebpackDevServer host and port
'webpack/hot/only-dev-server', // "only" prevents reload on syntax errors
].concat(webpackConfig.entry.app);
webpackConfig.entry.advanced = [
`webpack-dev-server/client?http://${host}:${port}`, // WebpackDevServer host and port
'webpack/hot/only-dev-server', // "only" prevents reload on syntax errors
].concat(webpackConfig.entry.advanced);
webpackConfig.module.loaders.forEach((loader) => {
if (loader.loader === 'babel-loader') {
loader.query.plugins.push('react-hot-loader/babel');
}
});
if (config.prod) {
webpackConfig.devtool = 'source-map';
} else if (config.noEval) {
webpackConfig.devtool = 'cheap-module-source-map';
} else {
webpackConfig.devtool = 'eval-cheap-module-source-map';
}
webpackConfig.plugins = [
new webpack.HotModuleReplacementPlugin(),
webpackCommonsChunkPluginConfig,
];
if (config.prod) {
webpackConfig.plugins.unshift(
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"',
ENABLE_REACT_ADDON_HOOKS: config.addon ? '"true"' : '"false"',
},
}));
webpackConfig.plugins.push(
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false,
},
mangle: true,
}));
}
// Start a webpack-dev-server
const compiler = webpack(webpackConfig);
new WebpackDevServer(compiler, webpackConfig.devServer).listen(port, host, (err) => {
if (err) {
throw new gutil.PluginError('webpack-dev-server', err);
}
// Server listening
gutil.log('[webpack-dev-server]', 'http://localhost:8080/webpack-dev-server/index.html');
// keep the server alive or continue?
// callback();
});
});
// only enable addon integration, everything else in prod settings
gulp.task('build-prod-with-addon', (callback) => {
webpackConfig.plugins.unshift(new webpack.DefinePlugin({
'process.env': {
ENABLE_REACT_ADDON_HOOKS: '"true"',
},
}));
runSequence('build', callback);
});
// also adds sourceMaps too!
gulp.task('build-prod-with-addon-no-mangle', (callback) => {
webpackConfig.devtool = 'source-map';
webpackConfig.plugins = pluginsWithoutUglify.concat([
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false,
},
mangle: false,
}),
]);
runSequence('build-prod-with-addon', callback);
});
// build without production node env
gulp.task('build-dev', (callback) => {
webpackConfig.plugins = [
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false,
},
mangle: true,
}),
];
runSequence('build', callback);
});
gulp.task('clean-pages', () => del('pages/**/!(.git|README.md)'));
gulp.task('copy-assets', () => gulp
.src('assets/**/*')
.pipe(gulp.dest('pages/')));
// just run webpack with default config (prod)
gulp.task('build', ['clean-pages'], (callback) => {
webpack(webpackConfig, (err, stats) => {
if (err) {
throw new gutil.PluginError('webpack', err);
}
gutil.log('[webpack]', stats.toString({
// output options
}));
runSequence('copy-assets', callback);
});
});
gulp.task('default', ['webpack-dev-server']);