-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbuild.mjs
More file actions
86 lines (77 loc) · 2.76 KB
/
Copy pathbuild.mjs
File metadata and controls
86 lines (77 loc) · 2.76 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
import esbuild from 'esbuild';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const PUBLIC = path.join(__dirname, 'public');
const DIST = path.join(__dirname, 'dist');
function copyDir(src, dest) {
fs.mkdirSync(dest, { recursive: true });
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
const s = path.join(src, entry.name);
const d = path.join(dest, entry.name);
if (entry.isDirectory()) copyDir(s, d);
else fs.copyFileSync(s, d);
}
}
// Third-party vendored scripts (e.g. js/vendor/jszip.min.js) are plain global
// scripts, not ESM modules. Running them through the esbuild ESM transpile
// below wraps their CJS-style code in a require_*() shim and appends an
// `export default ...` statement — a syntax error in a classic <script> tag
// that silently kills the whole file, so the global it defines never exists.
const NO_TRANSPILE_DIRS = ['vendor'];
function collectJs(dir) {
const files = [];
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
const p = path.join(dir, entry.name);
if (entry.isDirectory()) {
if (NO_TRANSPILE_DIRS.includes(entry.name)) continue;
files.push(...collectJs(p));
}
else if (entry.name.endsWith('.js')) files.push(p);
}
return files;
}
// Step 1: copy public/ → dist/
console.log('[build] Copying public/ → dist/ ...');
if (fs.existsSync(DIST)) fs.rmSync(DIST, { recursive: true });
copyDir(PUBLIC, DIST);
// Step 2: transpile dist/js/**/*.js in-place
// bundle:false keeps each file's relative import paths intact
console.log('[build] Transpiling JS (target: chrome69) ...');
const jsFiles = collectJs(path.join(DIST, 'js'));
await esbuild.build({
entryPoints: jsFiles,
outdir: path.join(DIST, 'js'),
allowOverwrite: true,
bundle: false,
target: 'chrome69',
format: 'esm',
logLevel: 'warning',
});
// Step 3: bundle reader.js with all its flow/ imports into one self-contained ESM file.
// Reduces HTTP requests and avoids issues with many concurrent SW respondWith() calls
// during module loading on old WebViews.
console.log('[build] Bundling reader.js ...');
await esbuild.build({
entryPoints: [path.join(PUBLIC, 'js', 'reader.js')],
outfile: path.join(DIST, 'js', 'reader.js'),
allowOverwrite: true,
bundle: true,
format: 'esm',
target: 'chrome69',
logLevel: 'warning',
});
// Step 4: transpile sw.js (classic script — no import/export, just syntax lowering)
const swSrc = path.join(DIST, 'sw.js');
if (fs.existsSync(swSrc)) {
await esbuild.build({
entryPoints: [swSrc],
outfile: swSrc,
allowOverwrite: true,
bundle: false,
target: 'chrome69',
logLevel: 'warning',
});
}
console.log('[build] Done → dist/');