-
Notifications
You must be signed in to change notification settings - Fork 3
/
gulpfile.js
64 lines (56 loc) · 1.85 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
var gulp = require('gulp');
var clean = require('gulp-clean');
var gutil = require('gulp-util');
var fs = require('fs');
var glob = require('glob');
var path = require('path');
var _ = require('lodash');
var webpack = require('webpack');
var webpackConfig = require('./webpack/webpack.production');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var libjs = [
'<script src="http://cdn.staticfile.org/react/0.14.3/react.min.js"></script>',
'<script src="http://cdn.staticfile.org/react/0.14.3/react-dom.min.js"></script>'
];
//清除数据
gulp.task('clean', function () {
return gulp.src(['assets/dist/*', 'assets/assets-map.json'], {read: false})
.pipe(clean())
});
function modifyTemplate(filename) {
var html = fs.readFileSync(filename, 'utf-8');
var script = libjs.join('');
return html.replace('</body>', script + '</body>');
}
gulp.task('default', function () {
var entries = {};
var entryFiles = glob.sync('assets/src/**/*.entry.js');
for (var i = 0; i < entryFiles.length; i++) {
var filePath = entryFiles[i];
var key = filePath.substring(filePath.lastIndexOf(path.sep) + 1, filePath.lastIndexOf('.'));
entries[key] = path.join(__dirname, filePath);
}
var config = _.merge({}, webpackConfig);
config.entry = _.merge({}, config.entry, entries);
config.plugins = config.plugins || [];
for (var i in entries) {
//TODO 读取html,进行修改
var relativePath = path.relative(__dirname, entries[i]);
var htmlFilename = (relativePath + '.ejs').replace('.entry.js', '');
config.plugins.push(new HtmlWebpackPlugin({
filename: (i + '.ejs').replace('entry.', ''),
templateContent: modifyTemplate(htmlFilename),
inject: true,
chunks: [i]
}));
}
webpack(config, function (err, stats) {
if (err) {
throw new gutil.PluginError('webpack-build', err);
}
console.log(stats.toString({
timings: true,
color: true
}));
});
});