-
Notifications
You must be signed in to change notification settings - Fork 14
/
build.js
224 lines (195 loc) · 5.77 KB
/
build.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
/*
* 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 alias from "@rollup/plugin-alias";
import commonjs from "@rollup/plugin-commonjs";
import resolve from "@rollup/plugin-node-resolve";
import vue from "rollup-plugin-vue";
import {series, parallel, MemoryFile, PhysicalFile} from "builder";
import eslint from "./build/eslint.js";
import htmlValidate from "./build/html-validate.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";
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 == "background.js")
{
let reloader = new PhysicalFile("lib/reloader.js");
yield new MemoryFile(file.path, [
(await file.read()).contents,
(await reloader.read()).contents
].join("\n"));
}
else
yield file;
}
}),
new MemoryFile("random.json", String(Math.random()))
];
}
function eslintTask()
{
return this.src(["*.js", "*.json", "ui/**/*.js", "ui/**/*.vue", "lib/**/*.js",
"contentScript/**/*.js",
"locale/**/*.json", "!ui/third-party/**"])
.pipe(eslint);
}
function htmlValidateTask()
{
return this.src(["ui/**/*.html", "ui/**/*.vue", "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("contentScript/upgradeConnection.js"),
this.src("ui/*/main.js")
.pipe(rollup, ...rollupOptions(this, {
format: "es",
manualChunks(id)
{
if (/[/\\](vue|clipboard|recoveryCodes)\.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/background.js")
.pipe(rollup, ...rollupOptions(this))
.rename("background.js")
];
});
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, `pfp-${VERSION}.zip`).dest("build-chrome");
});
export let watchChrome = series(chrome, function()
{
return this.src(["*.js", "*.json", "ui/**/*", "lib/**/*", "contentScript/**/*", "locale/**/*"])
.watch(chrome);
});
let firefoxMain = series(common, function(common)
{
return [
common,
this.src("manifest.json")
.pipe(utils.jsonModify, data =>
{
data.manifest_version = 2;
delete data.minimum_chrome_version;
delete data.minimum_opera_version;
data.permissions = data.permissions.filter(p => p != "scripting");
data.background.scripts = [data.background.service_worker];
delete data.background.service_worker;
data.browser_action = data.action;
data.browser_action.browser_style = false;
delete data.action;
data.commands._execute_browser_action = data.commands._execute_action;
delete data.commands._execute_action;
})
];
});
export let firefox = series(firefoxMain, addReloader, files => files.dest("build-firefox"));
export let xpi = series(firefoxMain, function(files)
{
return files.pipe(zip, `pfp-${VERSION}.xpi`).dest("build-firefox");
});
export let watchFirefox = series(firefox, function()
{
return this.src(["*.js", "*.json", "ui/**/*", "lib/**/*", "contentScript/**/*", "locale/**/*"])
.watch(firefox);
});
export function clean()
{
return this.src(["build-chrome/**", "build-firefox/**"])
.rm();
}
export let all = parallel(firefox, xpi, chrome, crx);
export default all;