-
Notifications
You must be signed in to change notification settings - Fork 44
/
gulpfile.babel.js
350 lines (303 loc) · 11.4 KB
/
gulpfile.babel.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import fs from 'fs'
import path from 'path'
import { exec as execCb } from 'child_process'
import { promisify } from 'util'
import streamToPromise from 'stream-to-promise'
import gulp from 'gulp'
import addsrc from 'gulp-add-src'
import clipEmptyFiles from 'gulp-clip-empty-files'
import concatCss from 'gulp-concat-css'
import identity from 'gulp-identity'
import indefinitely from 'indefinitely'
import source from 'vinyl-source-stream'
import buffer from 'vinyl-buffer'
import eslint from 'gulp-eslint'
import stylelint from 'gulp-stylelint'
import browserify from 'browserify'
import watchify from 'watchify'
import babelify from 'babelify'
import envify from 'loose-envify/custom'
import cssModulesify from 'css-modulesify'
import postcssPresetEnv from 'postcss-preset-env'
import terser from 'gulp-terser'
import markdownToHtml from '@wulechuan/gulp-markdown-to-html'
const exec = promisify((command, callback) => {
// Let exec also display the shell command and its output
console.log('>>>', command)
execCb(command, (error, stdout, stderr) => {
console.log(stdout)
console.error(stderr)
callback(error, stdout, stderr)
})
})
// === Tasks for building the source code; result is put into ./extension ===
const staticFiles = {
'src/manifest.json': 'extension',
'src/*.html': 'extension',
'src/assets/**': 'extension/assets/',
'node_modules/webextension-polyfill/dist/browser-polyfill.js': 'extension/lib',
'node_modules/semantic-ui-css/semantic.min.css': 'extension/lib/semantic-ui',
'node_modules/semantic-ui-css/themes/**/*': 'extension/lib/semantic-ui/themes',
}
const markdownFiles = {
'Changelog.md': 'extension',
}
const sourceFiles = [
'main/background.js',
'main/content_script.js',
'overview/overview.jsx',
'local-page/local-page.jsx',
'popup/popup.jsx',
'options/options.jsx',
]
const browserifySettings = {
debug: true,
extensions: ['.jsx', '.css'],
paths: ['.'],
}
// Define babel config here, as .babelrc is already used for converting this gulpfile itself.
const babelifySettings = {
presets: [
'@babel/preset-react',
['@babel/preset-env', {
targets: {
browsers: [
'last 2 Firefox versions',
'last 2 Chrome versions',
],
},
}],
],
}
const markdownToHtmlOptions = {
conversionPreparations: {
shouldNotAutoInsertTOCPlaceholderIntoMarkdown: true,
},
conversionOptions: {
shouldNotBuildHeadingPermanentLinks: true,
},
manipulationsOverHTML: {
htmlTagLanguage: 'en',
shouldNotInsertBackToTopAnchor: true,
},
}
async function createBundle({ filePath, watch = false, production = false }) {
const { dir, name } = path.parse(filePath)
const entries = [path.join('src', filePath)]
const destination = path.join('extension', dir)
const output = `${name}.js` // ignore original filename extension, to replace jsx with js.
// Hard-code the inclusion of any css file with the same name as the script.
// We append any css-modules imported from the script to this css file.
const cssInputPath = path.join('src', dir, `${name}.css`)
const cssOutput = `${name}.css`
let b = watch
? watchify(browserify({ ...watchify.args, ...browserifySettings, entries }))
.on('update', bundle)
: browserify({ ...browserifySettings, entries })
b.transform(babelify, babelifySettings)
b.transform(envify({
NODE_ENV: production ? 'production' : 'development',
}), { global: true })
b.plugin(cssModulesify, {
global: true, // for importing css modules from e.g. react-datepicker.
rootDir: path.join('src', dir),
// output: path.join(destination, cssOutput), // We read the stream instead (see below)
postcssBefore: [
postcssPresetEnv({
stage: 0,
}),
],
})
b.on('css stream', stream => {
// Append the css-modules output to the script's eponymous plain css file (if any).
// TODO resolve & copy @import and url()s
stream
.pipe(source('css-modules-output.css')) // pretend the streamed data had this filename.
.pipe(buffer()) // concatCss & clipEmptyFiles do not support streamed files.
.pipe(fs.existsSync(cssInputPath) ? addsrc.prepend(cssInputPath) : identity())
.pipe(concatCss(cssOutput, { inlineImports: false }))
.pipe(clipEmptyFiles()) // Drop file if no output was produced (e.g. no background.css)
.pipe(gulp.dest(destination))
})
function bundle(callback) {
let startTime = Date.now()
b.bundle()
.on('error', error => console.error(error.message))
.pipe(source(output))
.pipe(buffer())
.pipe(production ? terser({ output: { ascii_only: true } }) : identity())
.pipe(gulp.dest(destination))
.on('end', () => {
let time = (Date.now() - startTime) / 1000
console.log(`Bundled ${output} in ${time}s.`)
if (!watch) {
callback()
}
})
}
await promisify(bundle)()
}
gulp.task('copyStaticFiles', async () => {
for (let filename in staticFiles) {
console.log(`Copying '${filename}' to '${staticFiles[filename]}'..`)
gulp.src(filename)
.pipe(gulp.dest(staticFiles[filename]))
}
})
gulp.task('copyStaticFiles-watch', gulp.series('copyStaticFiles',
async function watchAndCopyStaticFiles() {
Object.entries(staticFiles).forEach(([filename, destination]) => {
gulp.watch(filename)
.on('all', (event, path) => {
console.log(`Copying '${filename}' to '${staticFiles[filename]}'..`)
return gulp.src(filename)
.pipe(gulp.dest(staticFiles[filename]))
})
})
await indefinitely
}
))
gulp.task('convertMarkdownFiles', async () => {
for (let filename in markdownFiles) {
console.log(`Converting '${filename}' to '${markdownFiles[filename]}'..`)
gulp.src(filename)
.pipe(markdownToHtml(markdownToHtmlOptions))
.pipe(gulp.dest(markdownFiles[filename]))
}
})
gulp.task('convertMarkdownFiles-watch', gulp.series('convertMarkdownFiles',
async function watchAndConvertMarkdownFiles() {
Object.entries(markdownFiles).forEach(([filename, destination]) => {
gulp.watch(filename)
.on('all', (event, path) => {
console.log(`Converting '${filename}' to '${markdownFiles[filename]}'..`)
return gulp.src(filename)
.pipe(markdownToHtml(markdownToHtmlOptions))
.pipe(gulp.dest(markdownFiles[filename]))
})
})
await indefinitely
}
))
gulp.task('build-prod', gulp.parallel('copyStaticFiles', 'convertMarkdownFiles', async function bundleForProduction() {
const ps = sourceFiles.map(filePath => createBundle({ filePath, watch: false, production: true }))
await Promise.all(ps)
}))
gulp.task('build', gulp.parallel('copyStaticFiles', 'convertMarkdownFiles', async function bundleForDevelopment() {
const ps = sourceFiles.map(filePath => createBundle({ filePath, watch: false }))
await Promise.all(ps)
}))
gulp.task('build-watch', gulp.parallel('copyStaticFiles-watch', 'convertMarkdownFiles-watch', async function watchAndBundle() {
const ps = sourceFiles.map(filePath => createBundle({ filePath, watch: true }))
await Promise.all(ps)
}))
// === Tasks for linting the source code ===
const stylelintOptions = {
failAfterError: false,
reporters: [
{ formatter: 'string', console: true },
],
}
gulp.task('lint', async () => {
const eslintStream = gulp.src(['src/**/*.js', 'src/**/*.jsx'])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.results(results => {
// For clarity, also give some output when there are no errors.
if (results.errorCount === 0) {
console.log(`No eslint errors.\n`)
}
}))
await streamToPromise(eslintStream)
const stylelintStream = gulp.src(['src/**/*.css'])
.pipe(stylelint(stylelintOptions))
await streamToPromise(stylelintStream)
})
gulp.task('lint-watch', gulp.series('lint', async function watchAndLint() {
gulp.watch(['src/**/*.js', 'src/**/*.jsx'])
.on('change', path => {
return gulp.src(path)
.pipe(eslint())
.pipe(eslint.format())
})
gulp.watch(['src/**/*.css'])
.on('change', path => {
return gulp.src(path)
.pipe(stylelint(stylelintOptions))
})
await indefinitely
}))
gulp.task('watch', gulp.parallel('build-watch', 'lint-watch'))
// === Tasks for packaging the extension; results go into ./dist/{browser} ===
function getManifest() {
const manifest = JSON.parse(fs.readFileSync('./extension/manifest.json'))
return manifest
}
function getFilename() {
const { name, version } = getManifest()
const filename = `${name.toLowerCase()}-${version}`
return filename
}
gulp.task('package-firefox', async () => {
const filename = getFilename()
const buildXpiCommand = `web-ext -s ./extension -a ./dist/firefox build`
await exec(buildXpiCommand)
// web-ext will have named the file ${filename}.zip. Change .zip to .xpi.
await exec(`mv dist/firefox/${filename}.zip dist/firefox/${filename}.xpi`)
})
gulp.task('package-chromium', async () => {
const filename = getFilename()
const buildCrxCommand = (
`crx pack ./extension`
+ ` -o ./dist/chromium/${filename}.crx`
+ ` -p .chrome-extension-key.pem`
)
// crx fails if the output directory is not there.
await exec(`mkdir -p dist/chromium`)
await exec(buildCrxCommand)
})
// Run sequentially to keep output cleanly separated.
gulp.task('package', gulp.series('package-firefox', 'package-chromium'))
// === Tasks for publishing the extension ===
function readApiKeys() {
try {
return JSON.parse(fs.readFileSync('./.api-keys.json'))
} catch (err) {
throw new Error(
'Expected to find API keys in .api-keys.json.'
+ ' For details, well best just read the gulpfile..'
)
}
}
// Publish to Mozilla Addons
gulp.task('publish-amo', async () => {
const { MozillaAddons } = readApiKeys()
const publishAmoCommand = (
`web-ext sign`
+ ` -s ./extension`
+ ` -a ./dist/amo`
+ ` --api-key ${MozillaAddons.apiKey}`
+ ` --api-secret ${MozillaAddons.apiSecret}`
)
try {
await exec(publishAmoCommand)
} catch (err) {}
})
// Publish to Chrome Web Store
// TODO use gulp-crx-pack instead of using crx through exec()
gulp.task('publish-cws', async () => {
const { ChromeWebStore } = readApiKeys()
const publishCwsCommand = (
`webstore upload --auto-publish`
+ ` --source ./extension`
+ ` --extension-id ${ChromeWebStore.extensionId}`
+ ` --client-id ${ChromeWebStore.clientId}`
+ ` --client-secret ${ChromeWebStore.clientSecret}`
+ ` --refresh-token ${ChromeWebStore.refreshToken}`
)
try {
await exec(publishCwsCommand)
} catch (err) {}
})
// Run sequentially to keep output cleanly separated.
gulp.task('publish', gulp.series('publish-amo', 'publish-cws'))