-
Notifications
You must be signed in to change notification settings - Fork 344
Expand file tree
/
Copy pathesbuild.js
More file actions
66 lines (63 loc) · 2.22 KB
/
Copy pathesbuild.js
File metadata and controls
66 lines (63 loc) · 2.22 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
const { build } = require('esbuild');
const { cp } = require('fs/promises');
const fs = require('fs/promises');
const path = require('path');
const objectHasOwnPolyfill = require.resolve('core-js/actual/object/has-own');
const replaceOpenApiIsNode = {
name: 'replace-open-api-is-node',
setup(build) {
build.onLoad(
{
filter: /open-api\.js$/,
},
async (args) => {
let contents = await fs.readFile(args.path, 'utf8');
if (args.path.includes(path.join('src', 'core', 'Sub-Store', 'backend', 'src', 'vendor'))) {
contents = contents.replace(/const\s+isNode\s*=\s*eval\(`typeof process !== "undefined"`\)\s*;/, 'const isNode = false;');
}
return {
contents,
loader: 'js',
};
},
);
},
};
!(async () => {
const artifacts = [{ src: 'src/worker.js', dest: 'dist/_worker.js' }];
for (const artifact of artifacts) {
await build({
entryPoints: [artifact.src],
bundle: true,
minify: true,
sourcemap: false,
platform: 'browser',
format: 'esm',
outfile: artifact.dest,
inject: [objectHasOwnPolyfill],
plugins: [replaceOpenApiIsNode],
});
console.log(`✔️ 打包完成: ${artifact.src} → ${artifact.dest}`);
}
const verfacts = [{ src: 'src/vercel.js', dest: 'src/server.js' }];
for (const artifact of verfacts) {
await build({
entryPoints: [artifact.src],
bundle: true,
minify: true,
sourcemap: false,
platform: 'node',
format: 'cjs',
outfile: artifact.dest,
inject: [objectHasOwnPolyfill],
plugins: [replaceOpenApiIsNode],
});
console.log(`✔️ 打包完成: ${artifact.src} → ${artifact.dest}`);
}
const copyTasks = [
['./template', './dist/template'],
['./favicon.png', './dist/favicon.png'],
['./icon', './dist/icon'],
];
await Promise.all(copyTasks.map(([src, dest]) => cp(src, dest, { recursive: true })));
})();