-
Notifications
You must be signed in to change notification settings - Fork 17
/
gulpfile.js
125 lines (105 loc) · 3.52 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
'use strict';
var gulp = require('gulp');
var gutil = require('gulp-util');
var runSequence = require('run-sequence');
var del = require('del');
var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var webpackConfig = require('./webpack.config.js');
var path = require('path');
var dist = 'public';
var distJoin = path.join.bind(null, dist);
// The development server (the recommended option for development)
gulp.task('default', function(callback) {
runSequence('clean', 'webpack-dev-server', callback);
});
// Production build
gulp.task('build', function(callback) {
runSequence('clean', 'webpack:build', callback);
});
gulp.task('clean', function(cb) {
del(['./public/**/*'], cb);
});
gulp.task('webpack:build', function(callback) {
// modify some webpack config options
var prodConfig = webpackConfig.map(function(config) {
var myConfig = Object.create(config);
myConfig.plugins = myConfig.plugins || [];
myConfig.plugins = myConfig.plugins.concat(
new webpack.DefinePlugin({
'process.env': {
// This has effect on the react lib size
NODE_ENV: JSON.stringify('production')
}
}),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin()
);
return myConfig;
});
// run webpack
webpack(prodConfig, function(err, stats) {
if (err) {
throw new gutil.PluginError('webpack:build', err);
}
gutil.log('[webpack:build]', stats.toString({
colors: true
}));
callback();
});
});
gulp.task('webpack-dev-server', function(callback) {
var publicPath = 'http://localhost:8080/assets/';
// modify some webpack config options
var browserConfig = webpackConfig.filter(function(config) {
return config.name === 'browser';
})[0];
browserConfig = Object.create(browserConfig);
browserConfig.devtool = 'eval';
browserConfig.debug = true;
browserConfig.plugins = browserConfig.plugins || [];
browserConfig.plugins = browserConfig.plugins.concat([new webpack.HotModuleReplacementPlugin(), new webpack.NoErrorsPlugin()]);
browserConfig.entry = [
'webpack-dev-server/client?http://localhost:8080', 'webpack/hot/only-dev-server', './app/jsx/app.jsx'
];
// don't extract the css to a file for the dev server.
browserConfig.module.loaders = browserConfig.module.loaders.map(function(loader) {
if (loader.test.toString() === /\.styl$/.toString()) {
return {test: /\.styl$/, loader: 'style!css!stylus'};
} else {
return loader;
}
});
browserConfig.output.publicPath = publicPath;
// modify some webpack config options of the server
var serverConfig = Object.create(webpackConfig.filter(function(config) {
return config.name === 'server';
})[0]);
// FIXME we musn't overwrite the plugins, only change the PUBLIC_PATH
serverConfig.plugins = [
new webpack.DefinePlugin({
PUBLIC_PATH: JSON.stringify(publicPath)
})
];
// run webpack
webpack(serverConfig, function(err, stats) {
if (err) {
throw new gutil.PluginError('webpack:build', err);
}
gutil.log('[webpack:build]', stats.toString({
colors: true
}));
// Start a webpack-dev-server
new WebpackDevServer(webpack(browserConfig), {
contentBase: './public',
hot: true,
publicPath: '/assets/',
stats: {
colors: true
}
}).listen(8080, '0.0.0.0', function(err) {
if(err) throw new gutil.PluginError('webpack-dev-server', err);
gutil.log('[webpack-dev-server]', 'http://localhost:8080/webpack-dev-server/index.html');
});
});
});