-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.mjs
More file actions
94 lines (84 loc) · 2.84 KB
/
build.mjs
File metadata and controls
94 lines (84 loc) · 2.84 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
import { execFileSync } from 'node:child_process';
import { cpSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { createRequire } from 'node:module';
import { stampVersion } from './src/stamp-version.ts';
const target = process.argv.includes('--target=firefox') ? 'firefox' : 'chrome';
const cwd = import.meta.dirname;
const run = (cmd, args) => execFileSync(cmd, args, { stdio: 'inherit', cwd });
const npx = (args) => run('npx', args);
console.log(`Building for ${target}...`);
mkdirSync('dist/panel', { recursive: true });
mkdirSync('dist/background', { recursive: true });
mkdirSync('dist/content', { recursive: true });
// TypeScript entry points via esbuild
const esbuildEntries = [
{
input: 'src/devtools/devtools.ts',
output: 'dist/devtools.js',
format: 'esm',
},
{
input: 'src/panel/panel.ts',
output: 'dist/panel/panel.js',
format: 'esm',
},
{
input: 'src/background/service-worker.ts',
output: 'dist/background/service-worker.js',
format: 'esm',
footer: 'export {}',
},
{
input: 'src/content/content-script.ts',
output: 'dist/content/content-script.js',
format: 'iife',
},
{
input: 'src/content/relay.ts',
output: 'dist/content/relay.js',
format: 'iife',
},
];
for (const entry of esbuildEntries) {
const args = [
'esbuild',
entry.input,
'--bundle',
'--minify',
`--outfile=${entry.output}`,
`--format=${entry.format}`,
'--platform=browser',
];
if (entry.footer) args.push(`--footer:js=${entry.footer}`);
npx(args);
}
// Copy Elm + CSS from devtools-ui
const require = createRequire(import.meta.url);
const uiPkg = require.resolve('@wolfcola/devtools-ui/package.json');
const uiDir = uiPkg.replace('/package.json', '');
cpSync(`${uiDir}/dist/elm.js`, 'dist/panel/elm.js');
cpSync(`${uiDir}/dist/panel.css`, 'dist/panel/panel.css');
cpSync(`${uiDir}/dist/panel.html`, 'dist/panel/panel.html');
// Manifest
const manifest = JSON.parse(readFileSync('manifest.json', 'utf8'));
// Stamp version: append build number as 4th segment for Chrome Web Store
const buildNumber = parseInt(process.env.BUILD_NUMBER || '0', 10);
const isSnapshot = process.env.SNAPSHOT === 'true';
const stamped = stampVersion(manifest.version, buildNumber, isSnapshot);
manifest.version = stamped.version;
if (stamped.version_name) {
manifest.version_name = stamped.version_name;
}
if (target === 'firefox') {
manifest.background = { scripts: ['background/service-worker.js'], type: 'module' };
manifest.browser_specific_settings = {
gecko: {
id: 'oidc-devtool@wolfcola',
data_collection_permissions: { required: ['none'] },
},
};
}
writeFileSync('dist/manifest.json', JSON.stringify(manifest, null, 2));
cpSync('icons', 'dist/icons', { recursive: true });
cpSync('src/devtools/devtools.html', 'dist/devtools.html');
console.log('Build complete.');