-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
162 lines (131 loc) · 4.54 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
'use strict';
const
gulp = require('gulp'),
gutil = require('gulp-util'),
clean = require('gulp-clean'),
merge = require('merge-stream'),
path = require('path'),
run = require('gulp-run'),
browserify = require('browserify'),
watchify = require('watchify'),
source = require('vinyl-source-stream'),
buffer = require('vinyl-buffer'),
uglify = require('gulp-uglify'),
tsify = require('tsify'),
sourcemaps = require('gulp-sourcemaps'),
jsonMinify = require('gulp-json-minify'),
htmlmin = require('gulp-htmlmin'),
sass = require('gulp-sass')(require('sass')),
cleanCSS = require('gulp-clean-css')
;
const config = {
srcBase: 'src',
modules: [
{ src: "src/background.ts", bundle: "background.js" },
{ src: "src/popup/ui.ts", bundle: "popup/ui.js" }
],
jsonGlob: 'src/**/*.json',
imageGlob: 'src/icons/**/*.png',
htmlGlob: 'src/popup/**/*.html',
sassGlob: 'src/**/*.sass',
buildDir: './build'
}
function js(watch, noSourceMaps = false) {
if (noSourceMaps) {
gutil.log('Building without source maps');
}
return merge(config.modules.map((module => {
let bundler = browserify({
basedir: '.',
debug: true,
entries: module.src,
cache: {},
packageCache: {}
});
bundler.plugin(tsify);
if (watch) {
bundler.plugin(watchify, {
delay: 100,
ignoreWatch: ['**/note_mdoules/**'],
poll: 250
});
watches.push(bundler);
}
bundler.on('update', function(){
bundle(bundler, module);
});
bundler.on('log', function(msg) {
gutil.log(msg);
});
return bundle(bundler, module, noSourceMaps);
})));
}
function bundle(bundler, module, noSourceMaps = false) {
return bundler
.bundle()
.pipe(source(module.bundle))
.pipe(buffer())
.pipe(noSourceMaps ? gutil.noop() : sourcemaps.init({ loadMaps: true }))
.pipe(uglify())
.pipe(noSourceMaps ? gutil.noop() : sourcemaps.write('./'))
.pipe(gulp.dest(config.buildDir));
}
let watches = [];
gulp.task('img', function () {
return gulp
.src(config.imageGlob, { base: config.srcBase })
.pipe(gulp.dest(config.buildDir))
.on('error', gutil.log);
});
gulp.task('img:watch', function () {
return watches.push(gulp.watch([config.imageGlob], gulp.parallel('img')));
});
gulp.task('js', function () { return js(false) });
gulp.task('js:watch', function () { return js(true) });
gulp.task('js:release', function () { return js(false, true) });
gulp.task('json', function() {
return gulp
.src(config.jsonGlob, { base: config.srcBase })
.pipe(jsonMinify())
.pipe(gulp.dest(config.buildDir))
.on('error', gutil.log);
});
gulp.task('json:watch', function () {
return watches.push(gulp.watch([config.jsonGlob], gulp.parallel('json')));
});
gulp.task('html', function () {
return gulp
.src(config.htmlGlob, { base: config.srcBase })
.pipe(htmlmin({ collapseWhitespace: true, removeComments: true, caseSensitive: true }))
.pipe(gulp.dest(config.buildDir));
});
gulp.task('html:watch', function () {
return watches.push(gulp.watch(config.htmlGlob, gulp.parallel('html')));
});
gulp.task('style', function () {
return gulp
.src(config.sassGlob, { base: config.srcBase })
.pipe(sass().on('error', sass.logError))
.pipe(cleanCSS({compatibility: '*'}))
.pipe(gulp.dest(config.buildDir));
});
gulp.task('style:watch', function () {
return watches.push(gulp.watch(config.sassGlob, gulp.parallel('style')));
});
gulp.task('run', function (cb) {
run('npm run start:firefox -- --bc -u file:///' + path.resolve('./test/dates.html')).exec(function () {
for (let watch of watches) {
watch.close();
}
cb();
});
});
gulp.task('clean', function () {
return gulp
.src(config.buildDir, { read: false, allowEmpty: true })
.pipe(clean());
});
gulp.task('release', gulp.series('clean', gulp.parallel('js:release', 'json', 'img', 'html', 'style'), function () { return run('npm run pack').exec() }));
gulp.task('lint', function () { return run('npm run lint').exec() });
gulp.task('watch', gulp.parallel('js:watch', 'json:watch', 'img:watch', 'html:watch', 'style:watch', 'run'));
gulp.task('default', gulp.parallel('js', 'json', 'img', 'html', 'style'));