forked from darkreader/darkreader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbundle-js.js
More file actions
171 lines (160 loc) · 5.32 KB
/
Copy pathbundle-js.js
File metadata and controls
171 lines (160 loc) · 5.32 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
const fs = require('fs-extra');
const os = require('os');
const rollup = require('rollup');
const rollupPluginCommonjs = require('@rollup/plugin-commonjs');
const rollupPluginNodeResolve = require('@rollup/plugin-node-resolve').default;
const rollupPluginReplace = require('@rollup/plugin-replace');
const rollupPluginTypescript = require('rollup-plugin-typescript2');
const typescript = require('typescript');
const {getDestDir} = require('./paths');
const reload = require('./reload');
const {PORT} = reload;
const {createTask} = require('./task');
async function copyToFF({cwdPath, debug}) {
const destPath = `${getDestDir({debug})}/${cwdPath}`;
const ffDestPath = `${getDestDir({debug, firefox: true})}/${cwdPath}`;
await fs.copy(destPath, ffDestPath);
}
function replace(str, find, replace) {
return str.split(find).join(replace);
}
function patchFirefoxJS(/** @type {string} */code) {
code = replace(code, 'chrome.fontSettings.getFontList', `chrome['font' + 'Settings']['get' + 'Font' + 'List']`);
code = replace(code, 'chrome.fontSettings', `chrome['font' + 'Settings']`);
return code;
}
/**
* @typedef JSEntry
* @property {string} src
* @property {string} dest
* @property {string} reloadType
* @property {({debug}) => Promise<void>} postBuild
* @property {string[]} watchFiles
*/
/** @type {JSEntry[]} */
const jsEntries = [
{
src: 'src/background/index.ts',
dest: 'background/index.js',
reloadType: reload.FULL,
async postBuild({debug}) {
const destPath = `${getDestDir({debug})}/${this.dest}`;
const ffDestPath = `${getDestDir({debug, firefox: true})}/${this.dest}`;
const code = await fs.readFile(destPath, 'utf8');
await fs.outputFile(ffDestPath, patchFirefoxJS(code));
},
watchFiles: null,
},
{
src: 'src/inject/index.ts',
dest: 'inject/index.js',
reloadType: reload.FULL,
async postBuild({debug}) {
await copyToFF({cwdPath: this.dest, debug});
},
watchFiles: null,
},
{
src: 'src/ui/devtools/index.tsx',
dest: 'ui/devtools/index.js',
reloadType: reload.UI,
async postBuild({debug}) {
await copyToFF({cwdPath: this.dest, debug});
},
watchFiles: null,
},
{
src: 'src/ui/popup/index.tsx',
dest: 'ui/popup/index.js',
reloadType: reload.UI,
async postBuild({debug}) {
await copyToFF({cwdPath: this.dest, debug});
},
watchFiles: null,
},
{
src: 'src/ui/stylesheet-editor/index.tsx',
dest: 'ui/stylesheet-editor/index.js',
reloadType: reload.UI,
async postBuild({debug}) {
await copyToFF({cwdPath: this.dest, debug});
},
watchFiles: null,
},
];
async function bundleJS(/** @type {JSEntry} */entry, {debug, watch}) {
const {src, dest} = entry;
const bundle = await rollup.rollup({
input: src,
plugins: [
rollupPluginNodeResolve(),
rollupPluginCommonjs(),
rollupPluginTypescript({
typescript,
tsconfig: 'src/tsconfig.json',
tsconfigOverride: {
compilerOptions: {
removeComments: debug ? false : true,
sourceMap: debug ? true : false,
},
},
clean: debug ? false : true,
cacheRoot: debug ? `${fs.realpathSync(os.tmpdir())}/darkreader_typescript_cache` : null,
}),
rollupPluginReplace({
'__DEBUG__': debug ? 'true' : 'false',
'__PORT__': watch ? String(PORT) : '-1',
'__WATCH__': watch ? 'true' : 'false',
}),
].filter((x) => x)
});
entry.watchFiles = bundle.watchFiles;
await bundle.write({
file: `${getDestDir({debug})}/${dest}`,
strict: true,
format: 'iife',
sourcemap: debug ? 'inline' : false,
});
await entry.postBuild({debug});
}
function getWatchFiles() {
const watchFiles = new Set();
jsEntries.forEach((entry) => {
entry.watchFiles.forEach((file) => watchFiles.add(file));
});
return Array.from(watchFiles);
}
/** @type {string[]} */
let watchFiles;
module.exports = createTask(
'bundle-js',
async ({debug, watch}) => await Promise.all(
jsEntries.map((entry) => bundleJS(entry, {debug, watch}))
),
).addWatcher(
() => {
watchFiles = getWatchFiles();
return watchFiles;
},
async (changedFiles, watcher) => {
const entries = jsEntries.filter((entry) => {
return changedFiles.some((changed) => {
return entry.watchFiles.includes(changed);
});
});
await Promise.all(
entries.map((e) => bundleJS(e, {debug: true, watch: true}))
);
const newWatchFiles = getWatchFiles();
watcher.unwatch(
watchFiles.filter((oldFile) => !newWatchFiles.includes(oldFile))
);
watcher.add(
newWatchFiles.filter((newFile) => watchFiles.includes(newFile))
);
const isUIOnly = entries.every((entry) => entry.reloadType === reload.UI);
reload({
type: isUIOnly ? reload.UI : reload.FULL,
});
},
);