-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
122 lines (96 loc) · 3.12 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
if (!process.env.NODE_ENV) process.env.NODE_ENV = 'production';
const PRODUCTION = process.env.NODE_ENV === 'production',
OUTPUTDIR = PRODUCTION ? 'docs' : 'dev',
MODULES_GLOB = process.cwd() + '/node_modules/**';
let cachePlugins = {};
function plugin (name) {
if (!(name in cachePlugins)) cachePlugins[name] = require('rollup-plugin-' + name);
return cachePlugins[name];
}
const gulp = require('gulp'),
newer = require('gulp-newer'),
pug = require('gulp-pug'),
stylus = require('gulp-stylus'),
rollup = require('rollup'),
anymatch = require('anymatch'),
fs = require('fs'),
rmdirSync = require('rimraf').sync,
sync = require('browser-sync').create();
function getPlugins () {
return [
plugin('node-resolve')(),
plugin('commonjs')(),
plugin('json')(),
plugin('replace')({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
}),
plugin('vue')(),
PRODUCTION ? plugin('babel')() : undefined,
PRODUCTION ? plugin('uglify').uglify() : undefined
];
}
gulp.task('clean', function (done) {
if (fs.existsSync(OUTPUTDIR)) rmdirSync(OUTPUTDIR);
fs.mkdirSync(OUTPUTDIR);
done();
});
gulp.task('assets', function () {
return gulp.src('src/assets/**', { since: gulp.lastRun('assets') })
.pipe(newer(OUTPUTDIR + '/assets'))
.pipe(gulp.dest(OUTPUTDIR + '/assets'));
});
gulp.task('html', function () {
return gulp.src('src/*.pug', { since: gulp.lastRun('html') })
.pipe(pug())
.pipe(gulp.dest(OUTPUTDIR));
});
gulp.task('css', function () {
return gulp.src('src/styles/index.styl')
.pipe(stylus({
'inline css': true,
compress: PRODUCTION
}))
.pipe(gulp.dest(OUTPUTDIR));
});
let oldWatchFiles = [], jswatcher, cache,
plugins = getPlugins();
gulp.task('js', async function () {
let bundle = await rollup.rollup({
input: 'src/scripts/main.js',
cache, plugins,
treeshake: PRODUCTION ? {
pureExternalModules: true
} : false
});
cache = bundle.cache;
await bundle.write({
file: OUTPUTDIR + '/app.js',
format: 'iife'
});
if (!jswatcher) return;
let newWatchFiles = bundle.watchFiles.filter(file => !file.startsWith('\u0000')),
watch = newWatchFiles.filter(function (file) {
return !anymatch(MODULES_GLOB, file) && oldWatchFiles.indexOf(file) == -1;
}),
unwatch = oldWatchFiles.filter(file => bundle.watchFiles.indexOf(file) == -1);
jswatcher.unwatch(unwatch);
jswatcher.add(watch);
oldWatchFiles = bundle.watchFiles;
});
gulp.task('watch', function (done) {
gulp.watch('src/assets/**', gulp.series('assets'));
gulp.watch('src/*.pug', gulp.series('html'));
gulp.watch('src/styles/**/*.styl', gulp.series('css'));
jswatcher = gulp.watch('src/scripts/main.js', gulp.series('js'));
done();
});
gulp.task('server', function () {
sync.init({
server: OUTPUTDIR,
files: OUTPUTDIR + '/**/*',
open: false
});
});
gulp.task('build', gulp.series('assets', 'html', 'css', 'js'));
gulp.task('dev', gulp.series('watch', 'build', 'server'));
gulp.task('default', gulp.series('clean', 'build'));