-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathbuild.config.ts
44 lines (39 loc) · 1.18 KB
/
build.config.ts
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
import { dirname, resolve } from "node:path";
import { writeFile, mkdir } from "node:fs/promises";
import { defineBuildConfig } from "unbuild";
import { build, type BuildOptions, transform } from "esbuild";
export default defineBuildConfig({
hooks: {
async "build:before"(ctx) {
ctx.options.alias["zeptomatch"] = await buildZeptomatch();
},
},
});
async function buildZeptomatch() {
let bundle = await build(<BuildOptions>{
format: "iife",
globalName: "__lib__",
bundle: true,
write: false,
stdin: {
resolveDir: process.cwd(),
contents: /* js */ `export { default as zeptomatch } from "zeptomatch";`,
},
}).then((r) => r.outputFiles![0].text);
bundle = (await transform(bundle, { minify: true })).code!;
bundle = /* js */ `
let _lazyMatch = () => { ${bundle}; return __lib__.default || __lib__; };
let _match;
export default (path, pattern) => {
if (!_match) {
_match = _lazyMatch();
_lazyMatch = null;
}
return _match(path, pattern);
};
`;
const outFile = resolve("tmp/node_modules/zeptomatch/zeptomatch.min.mjs");
await mkdir(dirname(outFile), { recursive: true });
await writeFile(outFile, bundle);
return outFile;
}