-
Notifications
You must be signed in to change notification settings - Fork 3
/
gulpfile.js
executable file
·162 lines (138 loc) · 4.3 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
/* eslint import/no-extraneous-dependencies:0 */
const gulp = require('gulp');
const del = require('del');
const stylus = require('gulp-stylus');
const base64 = require('gulp-base64');
const nib = require('nib');
const cssmin = require('gulp-cssmin');
const copy = require('gulp-copy');
const path = require('path');
const through = require('through2');
const webpack = require('webpack');
const crc32 = require('crc').crc32;
const _ = require('lodash');
const fs = require('fs');
const webpackConfig = require('./webpack.config');
const uglify = new webpack.optimize.UglifyJsPlugin({
mangle: {
except: ['$super', '$', 'exports', 'require'],
},
});
const definePlugin = new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production'),
},
});
webpackConfig.plugins.push(uglify);
webpackConfig.plugins.push(definePlugin);
webpackConfig.output.filename = '[name].[chunkhash].js';
const assetsPath = 'assets';
// 保存静态文件的crc32版本号
const crc32Versions = {};
function version(opts) {
function addVersion(file, encoding, cb) {
const v = crc32(file.contents);
const extname = path.extname(file.path);
let name = file.path.substring(file.base.length - 1);
if (opts && opts.prefix) {
name = opts.prefix + name;
}
crc32Versions[name] = `${v}`;
/* eslint no-param-reassign:0 */
file.path = file.path.replace(extname, `.${v}${extname}`);
cb(null, file);
}
return through.obj(addVersion);
}
gulp.task('del:assets', () => del([assetsPath]));
gulp.task('del:build', () => del(['build', 'public/bundle']));
gulp.task('clean', ['crc32'], () => del(['build', 'node_modules']));
gulp.task('stylus', ['del:assets', 'del:build'], () => gulp.src('public/**/*.styl')
.pipe(stylus({
use: nib(),
}))
.pipe(base64())
.pipe(cssmin())
.pipe(gulp.dest('build')));
gulp.task('copy:others', ['del:assets', 'del:build'], () => gulp.src([
'public/**/*',
'!public/**/*.styl',
'!public/**/*.js',
]).pipe(copy('build', {
prefix: 1,
})));
gulp.task('copy:source', ['del:assets', 'del:build'], () => gulp.src([
'public/fonts/*',
]).pipe(copy(assetsPath, {
prefix: 1,
})));
gulp.task('static:css', ['stylus', 'copy:others'], () => gulp.src(['build/**/*.css'])
.pipe(base64())
.pipe(cssmin())
.pipe(version())
.pipe(gulp.dest(assetsPath)));
gulp.task('static:js', ['copy:others'], () => gulp.src(['public/bundle/*.js'])
.pipe(version())
.pipe(gulp.dest(assetsPath)));
gulp.task('webpack:bundle', (cb) => {
webpack(webpackConfig, cb);
});
gulp.task('static:webpack-compile', ['webpack:bundle'], () => gulp.src(['public/bundle/*'])
.pipe(copy('build', {
prefix: 1,
})));
gulp.task('static:webpack', ['static:webpack-compile'], () => gulp.src(['build/bundle/*.js'])
.pipe(copy(assetsPath, {
prefix: 1,
})));
gulp.task('static:webpack-sourcemap', ['webpack:bundle'], () => gulp.src(['public/bundle/*.map'])
.pipe(copy(assetsPath, {
prefix: 1,
})));
gulp.task('static:webpack-version', ['static:webpack', 'static:webpack-sourcemap'], () => gulp.src([`${assetsPath}/bundle/*.js`])
.pipe(through.obj((file, encoding, cb) => {
const fileName = file.path.replace(path.join(__dirname, assetsPath), '');
const arr = fileName.split('.');
const v = arr.splice(-2, 1);
crc32Versions[arr.join('.')] = v[0];
cb();
})));
gulp.task('static:img', ['copy:others'], () => {
const maxSize = 10 * 1024;
const sizeLimit = (file, encoding, cb) => {
if (file.stat.size > maxSize) {
const size = Math.ceil(file.stat.size / 1024);
console.error(`Warning, the size of ${file.path} is ${size}KB`);
}
cb(null, file);
};
return gulp.src([
'build/**/*.png',
'build/**/*.jpg',
'build/**/*.gif',
'build/**/*.ico',
])
.pipe(through.obj(sizeLimit))
.pipe(version())
.pipe(gulp.dest(assetsPath));
});
gulp.task('crc32', ['static:css', 'static:js', 'static:img', 'static:webpack', 'static:webpack-version'], (cb) => {
const data = {};
const keys = _.keys(crc32Versions).sort();
_.forEach(keys, (k) => {
data[k] = crc32Versions[k];
});
fs.writeFile(path.join(__dirname, 'versions.json'), JSON.stringify(data, null, 2), cb);
});
gulp.task('default', [
'del:assets',
'del:build',
'stylus',
'copy:source',
'copy:others',
'static:css',
'static:webpack',
'static:js',
'crc32',
'clean',
]);