This repository has been archived by the owner on Jan 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgulpfile.js
188 lines (166 loc) · 4.67 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
'use strict';
var path = require('path'),
del = require('del'),
gulp = require('gulp'),
gutil = require("gulp-util"),
watch = require('gulp-watch'),
stylus = require('gulp-stylus'),
changed = require('gulp-changed'),
notify = require("gulp-notify"),
nib = require('nib'),
imagemin = require('gulp-imagemin'),
rev = require('gulp-rev'),
inject = require('gulp-inject'),
through2 = require('through2'),
runSequence = require('run-sequence'),
express = require('express'),
webpack = require('webpack'),
webpackConfig = require("./webpack.config.js"),
webpackProductionConfig = require('./webpack-production.config.js');
var dest = './public/build';
var config = {
js: {
src: "./app/main.cjsx",
dest: dest
},
stylus: {
files: './css/**/*',
src: "./css/main.styl",
out: 'main.css',
dest: dest
},
html: {
src: "./public/index.html",
dest: dest
},
clean: {
src: dest + '/**/*',
dest: dest + '/'
},
images: {
src: './public/assets/**/*',
dest: dest + '/assets'
},
server: {
port: 3434
}
}
// error handing function, pass to on 'error'
var handleErrors = function() {
var args = Array.prototype.slice.call(arguments);
notify.onError({
title: "Compile Error",
message: "<%= error.message %>"
}).apply(this, args);
this.emit('end'); // Keep gulp from hanging on this task
};
var execWebpack = function(config){
webpack((config), function(err, stats) {
if (err) new gutil.PluginError("execWebpack", err);
gutil.log(stats.toString({colors: true}));
});
}
var createServer = function(port) {
var app = express()
app.use(express.static(path.resolve(dest)))
app.listen(port, function(){
gutil.log("Server started on ", port);
})
}
// remove the build files
gulp.task('clean', function () {
del([dest])
});
// copy / minify images
gulp.task('images', function() {
return gulp.src(config.images.src)
.pipe(changed(config.images.dest))
.pipe(imagemin())
.pipe(gulp.dest(config.images.dest));
});
// copy html to build dir
gulp.task('html', function() {
var target = gulp.src(config.html.src);
var sources = gulp.src([dest + '/*.css'], {read: false});
return target.pipe(inject(sources, {ignorePath: 'public/build', addPrefix: '.', addRootSlash: false}))
.on('error', handleErrors)
.pipe(gulp.dest(config.html.dest));
});
// compile stylus and move to build dir
gulp.task('stylus', function() {
return gulp.src(config.stylus.src)
.pipe(stylus({use: nib(), 'include css': true, errors: true}))
.on('error', handleErrors)
.pipe(gulp.dest(config.stylus.dest));
});
// compile stylus for production build and move to build dir
gulp.task('stylus:build', function() {
return gulp.src(config.stylus.src)
.pipe(stylus({use: nib(), 'include css': true, errors: true}))
.on('error', handleErrors)
.pipe(rev())
.pipe(gulp.dest(config.stylus.dest));
});
// start webpack
gulp.task('webpack', function(callback){
execWebpack(webpackConfig);
callback();
})
// compile js for production build and move to build dir
gulp.task("webpack:build", function(callback) {
// modify some webpack config options
var myConfig = Object.create(webpackProductionConfig);
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.UglifyJsPlugin()
);
// run webpack
webpack(myConfig, function(err, stats) {
if(err) throw new gutil.PluginError("webpack:build", err);
gutil.log("[webpack:build]", stats.toString({
colors: true
})
);
// write the hashed main.js to /public/build/index.html
var jsFilename = stats.toJson().assetsByChunkName['app'];
return gulp.src('./public/build/index.html')
.on('error', handleErrors)
.pipe(through2.obj(function (file, enc, tCb) {
file.contents = new Buffer(String(file.contents)
.replace('app.js', jsFilename));
this.push(file);
tCb();
}))
.pipe(gulp.dest(config.html.dest));
callback();
});
});
gulp.task('watch', function(callback) {
runSequence(
['stylus', 'images'],
'html',
'webpack',
callback
);
gulp.watch(config.stylus.files, ['stylus']);
gulp.watch(config.html.src, ['html']);
gulp.watch(config.images.src, ['images']);
});
// start a dev server
gulp.task('serve', function(){
createServer(config.server.port);
});
gulp.task('build', function(callback){
runSequence(
['stylus:build', 'images'],
'html',
'webpack:build',
callback
);
});
gulp.task('default', ['serve', 'watch']);