-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.js
More file actions
290 lines (259 loc) · 7.98 KB
/
build.js
File metadata and controls
290 lines (259 loc) · 7.98 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
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
/*
* This Source Code is subject to the terms of the Mozilla Public License
* version 2.0 (the "License"). You can obtain a copy of the License at
* http://mozilla.org/MPL/2.0/.
*/
"use strict";
import fs from "fs";
import path from "path";
import alias from "@rollup/plugin-alias";
import babel from "@rollup/plugin-babel";
import commonjs from "@rollup/plugin-commonjs";
import resolve from "@rollup/plugin-node-resolve";
import vue from "rollup-plugin-vue";
import {series, parallel, Files, MemoryFile} from "builder";
import eslint from "./build/eslint.js";
import htmlValidate from "./build/html-validate.js";
import mocha from "./build/mocha.js";
import rollup from "./build/rollup.js";
import sass from "./build/sass.js";
import stylelint from "./build/stylelint.js";
import * as utils from "./build/utils.js";
import zip from "./build/zip.js";
import iife from "./build/rollup/iifeChunks.js";
import localeLoader from "./build/rollup/localeLoader.js";
import replace from "./build/rollup/replacePlugin.js";
import workerLoader from "./build/rollup/workerLoader.js";
import testEnv from "./test-env/setup.js";
const VERSION = JSON.parse(fs.readFileSync("./manifest.json")).version;
function rollupOptions(builder, overrides = {})
{
let prePlugins = overrides.plugins || [];
let postPlugins = overrides.postPlugins || [];
delete overrides.plugins;
delete overrides.postPlugins;
return [{
plugins: [
...prePlugins,
alias({
entries: [
{
find: "vue",
replacement: (
builder.hasFlag("dev")
? "vue/dist/vue.runtime.esm-browser.js"
: "vue/dist/vue.runtime.esm-browser.prod.js"
)
}
]
}),
resolve(),
commonjs({
include: ["node_modules/**"]
}),
vue({
template: {
compilerOptions: {
whitespace: "condense"
}
}
}),
...postPlugins
]
}, Object.assign({
format: "iife",
compact: true
}, overrides)];
}
function addReloader(files)
{
return [
files.pipe(async function*(files)
{
for await (let file of files)
{
if (file.path == "manifest.json")
{
file = await file.read();
let data = JSON.parse(file.contents);
data.background.scripts.push("reloader.js");
yield new MemoryFile(file.path, utils.stringifyObject(data));
}
else
yield file;
}
}),
this.src("lib/reloader.js")
.rename("reloader.js"),
new MemoryFile("random.json", String(Math.random()))
];
}
function eslintTask()
{
return this.src(["*.js", "*.json", "ui/**/*.js", "ui/**/*.vue", "lib/**/*.js",
"test/**/*.js", "test-lib/**/*.js", "web/**/*.js",
"contentScript/**/*.js", "worker/**/*.js",
"locale/**/*.json", "!ui/third-party/**"])
.pipe(eslint);
}
function htmlValidateTask()
{
return this.src(["ui/**/*.html", "ui/**/*.vue", "web/**/*.html", "ui/**/*.vue"])
.pipe(htmlValidate);
}
function stylelintTask()
{
return this.src(["ui/**/*.scss"])
.pipe(stylelint, {
customSyntax: "postcss-scss"
});
}
export {
eslintTask as eslint,
htmlValidateTask as htmlValidate,
stylelintTask as stylelint
};
export let validate = parallel(eslintTask, htmlValidateTask, stylelintTask);
let common = series(validate, function()
{
return [
this.src(["LICENSE.txt", "ui/**/*.html", "ui/images/**", "ui/third-party/**"]),
this.src("contentScript/fillIn.js")
.pipe(rollup, ...rollupOptions(this)),
this.src("ui/*/main.js")
.pipe(rollup, ...rollupOptions(this, {
format: "es",
manualChunks(id)
{
if (/[/\\](vue|clipboard)\.js$|[/\\]ui[/\\]components[/\\]/.test(id))
return "shared";
return null;
},
chunkFileNames: "ui/[name].js",
postPlugins: [iife()]
})),
this.src("ui/**/*.scss")
.pipe(sass),
this.src("locale/**/*.json")
.pipe(utils.combineLocales)
.pipe(utils.toChromeLocale),
this.src("lib/main.js")
.pipe(rollup, ...rollupOptions(this))
.rename("background.js"),
this.src("worker/scrypt.js")
.pipe(rollup, ...rollupOptions(this))
];
});
let chromeMain = series(common, function(common)
{
return [
common,
this.src("manifest.json")
.pipe(utils.jsonModify, data =>
{
delete data.applications;
})
];
});
export let chrome = series(chromeMain, addReloader, files => files.dest("build-chrome"));
export let crx = series(chromeMain, function(files)
{
return files.pipe(zip, `aep-${VERSION}.zip`).dest("build-chrome");
});
export let watchChrome = series(chrome, function()
{
return this.src(["*.js", "*.json", "ui/**/*", "lib/**/*", "contentScript/**/*", "worker/**/*", "locale/**/*"])
.watch(chrome);
});
let firefoxMain = series(common, function(common)
{
return [
common,
this.src("manifest.json")
.pipe(utils.jsonModify, data =>
{
delete data.minimum_chrome_version;
delete data.minimum_opera_version;
delete data.background.persistent;
data.browser_action.browser_style = false;
})
];
});
export let firefox = series(firefoxMain, addReloader, files => files.dest("build-firefox"));
export let xpi = series(firefoxMain, function(files)
{
return files.pipe(zip, `aep-${VERSION}.xpi`).dest("build-firefox");
});
export let watchFirefox = series(firefox, function()
{
return this.src(["*.js", "*.json", "ui/**/*", "lib/**/*", "contentScript/**/*", "worker/**/*", "locale/**/*"])
.watch(firefox);
});
let webMain = series(validate, function()
{
return new Files(
this.src(["LICENSE.txt", "ui/images/**", "ui/third-party/**", "web/**/*.html"]),
this.src(["ui/**/*.scss", "!ui/options/options.scss", "web/**/*.scss"])
.pipe(sass),
this.src("web/index.js")
.pipe(rollup, ...rollupOptions(this, {
plugins: [
replace({
[path.resolve(process.cwd(), "lib", "browserAPI.js")]: path.resolve(process.cwd(), "web", "backgroundBrowserAPI.js"),
[path.resolve(process.cwd(), "ui", "browserAPI.js")]: path.resolve(process.cwd(), "web", "contentBrowserAPI.js")
}),
workerLoader(/[/\\]scrypt\.js$/),
localeLoader(path.resolve(process.cwd(), "locale", "en_US"))
],
postPlugins: [
babel.default({
retainLines: true,
compact: false,
babelrc: false,
babelHelpers: "runtime",
extensions: [".js", ".vue"],
presets: ["@babel/preset-env"],
plugins: [
["@babel/transform-runtime"]
]
})
]
}))
)
.rename(filepath => path.normalize(filepath).includes(path.sep) ? path.normalize(filepath).split(path.sep).slice(1).join(path.sep) : filepath);
});
export let web = series(webMain, files => files.dest("build-web"));
export let webZip = series(webMain, function(files)
{
return files.pipe(zip, `aep-web-${VERSION}.zip`).dest("build-web");
});
export let test = series(validate, function()
{
let testFile = this.getFlag("test");
if (!testFile)
testFile = "**/*.js";
else if (!testFile.endsWith(".js"))
testFile += ".js";
return this.src("test/" + testFile)
.pipe(async function*(files)
{
testEnv.setup();
try
{
yield* await mocha(files, {
timeout: 10000
});
}
finally
{
testEnv.teardown();
}
});
});
export function clean()
{
return this.src(["build-chrome/**", "build-firefox/**", "build-web/**"])
.rm();
}
export let all = parallel(firefox, xpi, chrome, crx, web, webZip);
export default all;