This repository was archived by the owner on Mar 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgulpfile.js
More file actions
253 lines (229 loc) · 6.61 KB
/
gulpfile.js
File metadata and controls
253 lines (229 loc) · 6.61 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* eslint comma-dangle: ["error", "always-multiline"] */
const fs = require('fs');
const http = require('http');
const gulp = require('gulp');
const babel = require('gulp-babel');
const { colors, log } = require('gulp-util');
const through2 = require('through2');
const replace = require('replacestream');
const gulpif = require('gulp-if');
const uglify = require('gulp-uglify/composer')(require('uglify-es'), require('gulp-uglify/lib/log'));
const concat = require('gulp-concat');
const del = require('del');
const mkdirp = require('mkdirp');
const rename = require('gulp-rename');
const rjs = require('requirejs');
const template = require('gulp-template');
const zip = require('gulp-zip');
const webpack = require('webpack');
const webpackConfig = require('./webpack.config');
const DevServer = require('webpack-dev-server');
const packg = require('./package.json');
const env = process.env.NODE_ENV || 'development';
function cleanLib() {
return del('lib');
}
function cleanBuild() {
return del('build');
}
const clean = gulp.parallel(
cleanLib,
cleanBuild
);
function buildSource(done) {
webpack(webpackConfig, (err, stats) => {
if (err) {
done(err);
} else {
stats.toString({
modules: false,
colors: colors.enabled,
}).split('\n').forEach((line) => {
log(line);
});
done();
}
});
}
function buildLoaderTransform() {
return gulp.src('src/main.js')
.pipe(babel({
presets: [
['extplug', { modules: 'amd' }],
],
}))
.pipe(rename('init.js'))
.pipe(gulp.dest('build'));
}
function buildLoaderBundle(done) {
rjs.optimize({
baseUrl: './',
name: 'extplug/loader',
paths: {
jquery: 'empty:',
underscore: 'empty:',
backbone: 'empty:',
'extplug/loader': 'build/init',
'plug-modules': 'node_modules/plug-modules/plug-modules',
},
optimize: 'none',
out(text) {
mkdirp('build', (e) => {
if (e) {
done(e);
} else {
fs.writeFile('build/loader.js', text, done);
}
});
},
});
}
const buildLoader = gulp.series(buildLoaderTransform, buildLoaderBundle);
function concatSource() {
return gulp.src(['build/loader.js', 'build/source.js'])
.pipe(concat('extplug.code.js'))
.pipe(gulp.dest('build/'));
}
function wrapBuiltSourceInLoader() {
return gulp.src('src/loader.template.js', { buffer: true })
.pipe(through2.obj((file, enc, cb) => {
fs.readFile('build/extplug.code.js', 'utf8', (e, code) => {
if (e) {
cb(e);
} else {
file.contents = Buffer.from(file.contents.toString().replace('CODE', () => code));
cb(null, file);
}
});
}))
.pipe(rename('extplug.js'))
.pipe(gulp.dest('build/'));
}
const build = gulp.series(
gulp.parallel(buildSource, buildLoader),
concatSource,
wrapBuiltSourceInLoader
);
const buildWebExtensionFiles = gulp.parallel(
() => gulp.src(['extensions/webextension/main.js', 'extensions/webextension/manifest.json'])
.pipe(template(packg))
.pipe(gulp.dest('build/webextension/')),
() => gulp.src(['img/icon*.png'])
.pipe(gulp.dest('build/webextension/img/')),
() => gulp.src(['build/extplug.js'])
.pipe(concat('extplug.js'))
.pipe(gulp.dest('build/webextension/'))
);
function packWebExtension() {
return gulp.src('build/webextension/*')
.pipe(zip('extplug.extension.zip'))
.pipe(gulp.dest('build/'));
}
const buildWebExtension = gulp.series(buildWebExtensionFiles, packWebExtension);
function userscriptMeta() {
return gulp.src(['extensions/userscript/extplug.user.js'])
.pipe(template(packg))
.pipe(rename('extplug.meta.user.js'))
.pipe(gulp.dest('build/'));
}
function userscriptConcat() {
let inUserScriptComment = false;
return gulp.src(['build/extplug.meta.user.js', 'build/extplug.js'])
.pipe(gulpif(env === 'production', uglify({
output: {
comments(_, comment) {
if (comment.value.includes('==UserScript==')) {
inUserScriptComment = true;
} else if (comment.value.includes('==/UserScript==')) {
inUserScriptComment = false;
return true;
}
return inUserScriptComment;
},
},
compress: {
pure_getters: true,
unsafe: true,
},
toplevel: true,
})))
.pipe(concat('extplug.user.js'))
.pipe(gulp.dest('build/'));
}
const buildUserscript = gulp.series(
userscriptMeta,
userscriptConcat
);
const buildExtensions = gulp.parallel(
buildWebExtension,
buildUserscript
);
function getPort() {
const server = http.createServer().listen();
const { port } = server.address();
server.close();
return port;
}
function dev(done) {
const port = getPort();
const publicPath = `https://localhost:${port}/`;
const address = `${publicPath}dev.js`;
webpackConfig.mode = 'development';
webpackConfig.entry.unshift(
`${require.resolve('webpack-dev-server/client')}?${publicPath}`,
'webpack/hot/dev-server'
);
webpackConfig.entry.pop();
webpackConfig.entry.push('./hotReloader.js');
webpackConfig.output.publicPath = publicPath;
webpackConfig.output.filename = 'dev.js';
webpackConfig.plugins.push(new webpack.HotModuleReplacementPlugin());
const compiler = webpack(webpackConfig);
const server = new DevServer(compiler, {
port,
publicPath,
contentBase: webpackConfig.output.path,
hot: true,
https: true,
disableHostCheck: true,
headers: {
'Access-Control-Allow-Origin': '*',
},
setup(app) {
app.get('/ok.html', (req, res) => {
res.setHeader('content-type', 'text/html');
fs.createReadStream('src/devCertificateHelper.html').pipe(res);
});
},
});
server.listen(port, () => {
function writeDevStub() {
return fs.createReadStream('src/devStub.js')
.pipe(replace('PUBLIC_PATH', JSON.stringify(publicPath)))
.pipe(replace('ADDRESS', JSON.stringify(address)))
.pipe(fs.createWriteStream('build/source.js'));
}
gulp.series(
gulp.parallel(buildLoader, writeDevStub),
concatSource,
wrapBuiltSourceInLoader,
buildExtensions
)(done);
});
}
exports.default = gulp.series(
cleanBuild,
build,
buildExtensions
);
exports.clean = clean;
exports.loader = buildLoader;
exports.build = build;
exports.dev = dev;
exports.userscript = buildUserscript;
exports.watchWebExtension = () =>
gulp.watch('src/**/*', gulp.series(build, buildWebExtension));
exports.watchUserscript = () =>
gulp.watch('src/**/*', gulp.series(build, buildUserscript));
exports.watch = () =>
gulp.watch('src/**/*', gulp.series(build, buildExtensions));