-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
130 lines (125 loc) · 4.64 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
/**
* Build with esbuild for all platforms
*/
const fsPromises = require('fs').promises;
const fs = require('fs');
const path = require('path');
const {
Worker,
isMainThread,
workerData,
} = require('worker_threads');
const { build } = require('esbuild');
const version = '2.0.0';
const mode = process.env.NODE_ENV || 'production';
const outdir = path.join(__dirname, 'dist');
const GOOGLE_CLIENT_ID = '531665009269-96fvecl3pj4717mj2e6if6oaph7eu8ar.apps.googleusercontent.com';
const packageJson = JSON.parse(fs.readFileSync('package.json', { encoding: 'utf-8' }));
const commonOptions = {
bundle: true,
minify: mode === 'production',
sourcemap: mode === 'development',
define: {
'process.env.REPOSITORY_URL': JSON.stringify(packageJson.repository.url),
'process.env.VERSION': JSON.stringify(version),
'process.env.GOOGLE_CLIENT_ID': JSON.stringify(GOOGLE_CLIENT_ID),
},
watch: mode === 'development' && {
onRebuild: (_error, _result) => {
console.log('----------------------');
},
},
banner: {
js: `/* Search Blocker v${version} */`,
},
};
const tasks = {
/**
* TamperMonkey User Script
*/
buildTamperMonkey: async () => {
let banner = await fsPromises.readFile('platforms/tampermonkey/header.js', { encoding: 'utf-8' });
banner = banner.replace(/process.env.VERSION/g, version);
await build({
...commonOptions,
entryPoints: ['src/index.tsx'],
outfile: path.join(outdir, 'search-blocker.user.js'),
banner: { js: banner },
define: {
...commonOptions.define,
'process.env.PLATFORM': JSON.stringify('tampermonkey'),
},
});
},
/**
* Chrome Extension
*/
buildChromeExtension: async () => {
await build({
...commonOptions,
entryPoints: ['src/index.tsx'],
outfile: path.join(outdir, 'chrome/search-blocker/search-blocker.js'),
define: {
...commonOptions.define,
'process.env.PLATFORM': JSON.stringify('chrome'),
},
});
/* build manifest */
const manifest = JSON.parse(await fsPromises.readFile('platforms/chrome/manifest.json'));
const matches = JSON.parse(fs.readFileSync('platforms/chrome/matches.json'));
manifest.version = version;
manifest.name = packageJson.name;
manifest.description = packageJson.description;
for (const contentScript of manifest.content_scripts) {
contentScript.matches = matches;
}
await fsPromises.writeFile(path.join(outdir, 'chrome/search-blocker/manifest.json'),
JSON.stringify(manifest), { encoding: 'utf-8' });
await fsPromises.copyFile('images/icon16.png', path.join(outdir, 'chrome/search-blocker/icon16.png'));
await fsPromises.copyFile('images/icon32.png', path.join(outdir, 'chrome/search-blocker/icon32.png'));
await fsPromises.copyFile('images/icon96.png', path.join(outdir, 'chrome/search-blocker/icon96.png'));
await fsPromises.copyFile('images/icon128.png', path.join(outdir, 'chrome/search-blocker/icon128.png'));
},
buildChromeExtensionContentScript: async () => {
await build({
...commonOptions,
entryPoints: ['platforms/chrome/content.ts'],
outfile: path.join(outdir, 'chrome/search-blocker/content.js'),
});
},
/**
* Firefox Extension
*/
buildFirefoxExtension: async () => {
await build({
...commonOptions,
entryPoints: ['src/index.tsx'],
outfile: path.join(outdir, 'firefox/search-blocker.js'),
define: {
...commonOptions.define,
'process.env.PLATFORM': JSON.stringify('firefox'),
},
});
const manifest = JSON.parse(await fsPromises.readFile('platforms/firefox/manifest.json'));
manifest.version = version;
await fsPromises.writeFile(path.join(outdir, 'firefox/manifest.json'),
JSON.stringify(manifest), { encoding: 'utf-8' });
},
buildFirefoxExtensionContentScript: async () => {
await build({
...commonOptions,
entryPoints: ['platforms/firefox/content.ts'],
outfile: path.join(outdir, 'firefox/content.js'),
});
},
};
/* use threads */
if (isMainThread) {
fs.rmSync(outdir, { recursive: true, force: true });
for (const taskName of Object.keys(tasks)) {
new Worker(__filename, { workerData: taskName });
}
} else {
const taskName = workerData;
tasks[taskName]();
}