-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
78 lines (65 loc) · 2.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
// 引入 gulp
var gulp = require('gulp');
// 引入组件
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var replace = require('gulp-replace');
var cleanCSS = require('gulp-clean-css');
var rev = require('gulp-rev');
var path = require('path');
var fs = require('fs');
var del = require('del');
const buildPath = path.join(__dirname, 'build');
const buildPublicPath = path.join(buildPath, 'public');
const buildViewsPath = path.join(buildPath, 'views');
// gulp.task('baseCss', function() {
// return gulp.src([
// './src/fe/css/**/*.css'
// ]).pipe(cleanCSS({ compatibility: 'ie8' }))
// .pipe(rev())
// .pipe(gulp.dest(path.join(buildPublicPath, 'css')))
// .pipe(rev.manifest({
// merge: true
// }))
// .pipe(gulp.dest(buildPublicPath));
// });
const readRevJSON = function(assetsPath) {
let assets = {};
if(!fs.existsSync(assetsPath)) { return assets; }
const assetsContent = fs.readFileSync(assetsPath, { encoding: 'utf8' });
assets = (new Function('return ' + assetsContent))();
return assets;
};
const jsAssetsPath = path.join(buildPath, 'assets.json');
const cssAssetsPath = path.join(buildPath, 'rev-manifest.json');
const jsRevJSON = readRevJSON(jsAssetsPath);
const cssRevJSON = readRevJSON(cssAssetsPath);
const revJSON = Object.assign({}, jsRevJSON, cssRevJSON);
// gulp.task('rev', ['baseCss'], function() {
gulp.task('rev', function() {
return gulp.src(['./src/fe/html/*.pug'])
.pipe(replace(/{\$(.*)}/g, function(match, keyword) {
return revJSON[keyword];
}))
.pipe(gulp.dest(path.join(buildPath,'views')));
});
gulp.task('config', function() {
return gulp.src(['./src/server/config_master.js'])
.pipe(gulp.dest(buildPath));
});
gulp.task('default', [
'rev', 'config'
]);
if(process.env.NODE_ENV === 'development') {
console.log('watch start...');
del.sync(buildPublicPath);
del.sync(buildViewsPath);
var watcher = gulp.watch([
// './src/fe/css/**/*.css',
'./src/fe/html/**/*.pug',
], ['rev']);
watcher.on('change', function(event) {
console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
});
}