-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
105 lines (95 loc) · 3.16 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
/**
*
* Frontend workflow with Gulp, Webpack, Bower and Swig.
*
* @author https://github.com/jaylinski
* @source https://github.com/jaylinski/frontend-webpack
* @license https://github.com/jaylinski/frontend-webpack#copyright-and-license
*
*/
'use strict';
var gulp = require('gulp');
var gulpUtil = require("gulp-util");
var gulpBower = require("gulp-bower");
var gulpChanged = require("gulp-changed");
var gulpImagemin = require("gulp-imagemin");
var webpack = require('webpack');
var webpackServer = require('webpack-dev-server'),
webpackServerInstance;
var rimraf = require('rimraf');
var config = require('./gulp.config.json');
var webpackConfig = require('./webpack.config.js');
gulp.task('default', ['root', 'assets', 'images', 'webpack-dev-server']);
gulp.task('build', ['clean', 'bower', 'root', 'assets', 'images', 'webpack:build']);
gulp.task('clean', function(callback) {
rimraf.sync(config.paths.clean, [], function(err){
gulpUtil.log("[clean] errored");
});
callback();
});
gulp.task('root', function() {
return gulp.src(config.paths.root.src)
.pipe(gulp.dest(config.paths.root.dest));
});
gulp.task('bower', function(callback) {
gulpBower()
.pipe(gulp.dest(config.bowerOptions.paths.dest));
callback();
});
gulp.task('images', function() {
return gulp.src(config.paths.images.src)
.pipe(gulpChanged(config.paths.images.dest))
.pipe(gulpImagemin({
progressive: true,
interlaced: true
}))
.pipe(gulp.dest(config.paths.images.dest));
});
gulp.task('assets', function() {
return gulp.src(config.paths.assets.src)
.pipe(gulpChanged(config.paths.assets.dest))
.pipe(gulp.dest(config.paths.assets.dest));
});
gulp.task("webpack:build", ['bower'], function(callback) {
// modify some webpack config options
var configObj = Object.create(webpackConfig);
configObj.plugins = configObj.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()
);
// run webpackConfig
webpack(configObj, function(err, stats) {
if(err) throw new gutil.PluginError("webpack:build", err);
gulpUtil.log("[webpack:build]", stats.toString({
colors: true
}));
callback();
});
});
gulp.task("webpack-dev-server", function(callback) {
var configObj = Object.create(webpackConfig);
if(config.webpackOptions.server.hot) {
configObj.plugins.unshift(new webpack.HotModuleReplacementPlugin());
configObj.entry.app.unshift("webpack/hot/dev-server");
}
webpackServerInstance = new webpackServer(webpack(webpackConfig), {
contentBase: config.webpackOptions.paths.contentBase,
publicPath: "/" + configObj.output.publicPath,
hot: config.webpackOptions.server.hot,
stats: { colors: true }
}).listen(config.webpackOptions.server.port, config.webpackOptions.server.host, function(err) {
if(err) throw new gulpUtil.PluginError("webpack-dev-server", err);
gulpUtil.log(
"[webpack-dev-server]",
config.webpackOptions.server.protocol +
"://" + config.webpackOptions.server.host +
":" + config.webpackOptions.server.port
);
});
});