forked from b3log/pipe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
100 lines (89 loc) · 3.02 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
/**
* @file themes tool.
*
* @author <a href='http://vanessa.b3log.org'>Liyuan Li</a>
* @version 0.1.0.1, Dec 27, 2017
*/
const gulp = require('gulp')
const sass = require('gulp-sass')
const rename = require('gulp-rename')
const composer = require('gulp-uglify/composer')
const uglifyjs = require('uglify-es')
const gulpUtil = require('gulp-util')
const fs = require('fs')
const browserify = require('browserify')
const livereload = require('gulp-livereload')
const source = require('vinyl-source-stream')
const buffer = require('vinyl-buffer')
const sourcemaps = require('gulp-sourcemaps')
const es = require('event-stream')
gulp.task('sass', function () {
gulp.src('./scss/*.scss')
.pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
.pipe(gulp.dest('./scss'))
return gulp.src('./x/*/css/*.scss')
.pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
.pipe(gulp.dest('./x'))
})
gulp.task('dev', function () {
const theme = gulpUtil.env.theme || '9IPHP'
const files = [`./x/${theme}/js/common.js`, `./x/${theme}/js/article.js`]
const tasks = files.map(function (entry) {
return browserify({entries: [entry]})
.transform('babelify', {presets: ['es2015']})
.bundle()
.pipe(source(entry))
.pipe(rename({suffix: '.min'}))
.pipe(buffer())
.pipe(sourcemaps.init())
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('.'))
.pipe(livereload())
})
return es.merge.apply(null, tasks)
})
gulp.task('watch', function () {
livereload.listen()
gulp.watch(['./js/common.js', './js/article.js', './x/*/js/article.js', './x/*/js/common.js', './x/*/js/symbol.js'], ['dev'])
gulp.watch(['./x/*/css/*.scss', './scss/*.scss'], ['sass'])
})
gulp.task('build', function () {
// set static version
const newVersion = (new Date()).getTime()
// set pipe.json
fs.writeFileSync('../pipe.json',
fs.readFileSync('../pipe.json', 'UTF-8')
.replace(/"StaticResourceVersion": "\d{13}"/, `"StaticResourceVersion": "${newVersion}"`), 'UTF-8')
const minify = composer(uglifyjs)
// min sw.js
browserify({entries: `./sw.js`})
.transform('babelify', {presets: ['es2015']})
.bundle()
.pipe(source('sw.min.js'))
.pipe(buffer())
.pipe(minify().on('error', gulpUtil.log))
.pipe(gulp.dest('.'))
// theme js
fs.readdirSync('./x').forEach(function (file) {
const jsPath = `./x/${file}/js/`
try {
fs.statSync(`${jsPath}/common.js`)
browserify({entries: `${jsPath}/common.js`})
.transform('babelify', {presets: ['es2015']})
.bundle()
.pipe(source('common.min.js'))
.pipe(buffer())
.pipe(minify().on('error', gulpUtil.log))
.pipe(gulp.dest(jsPath))
browserify({entries: `${jsPath}/article.js`})
.transform('babelify', {presets: ['es2015']})
.bundle()
.pipe(source('article.min.js'))
.pipe(buffer())
.pipe(minify().on('error', gulpUtil.log))
.pipe(gulp.dest(jsPath))
} catch (e) {
}
})
})
gulp.task('default', ['sass', 'build'])