-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuild.ts
More file actions
90 lines (84 loc) · 2.93 KB
/
Copy pathbuild.ts
File metadata and controls
90 lines (84 loc) · 2.93 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
import type { PluginBuild, OnLoadArgs } from 'esbuild';
import { build } from 'esbuild';
import fs from 'node:fs';
import path from 'node:path';
const commonOptions = {
bundle: true,
platform: 'node' as const,
target: 'node20',
format: 'cjs' as const,
outExtension: { '.js': '.cjs' },
banner: { js: '#!/usr/bin/env node' },
};
// jsdom reads default-stylesheet.css via fs.readFileSync at runtime with __dirname-relative paths
// and uses require.resolve for xhr-sync-worker.js loaded via new Worker().
// Inline both so the CJS bundle has zero external fs dependencies.
const jsdomCss = fs.readFileSync(
path.resolve('node_modules/jsdom/lib/jsdom/browser/default-stylesheet.css'),
'utf8',
);
const jsdomWorkerSrc = fs.readFileSync(
path.resolve('node_modules/jsdom/lib/jsdom/living/xhr/xhr-sync-worker.js'),
'utf8',
);
const jsdomInlinePlugin = {
name: 'inline-jsdom-fs-deps',
setup(build: PluginBuild) {
build.onLoad(
{
filter: /jsdom[\\/]lib[\\/]jsdom[\\/]living[\\/]helpers[\\/]style-rules\.js$/,
},
(args: OnLoadArgs) => {
let code = fs.readFileSync(args.path, 'utf8');
code = code.replace(
`const defaultStyleSheet = fs.readFileSync(\n path.resolve(__dirname, "../../browser/default-stylesheet.css"),\n { encoding: "utf-8" }\n);`,
`const defaultStyleSheet = ${JSON.stringify(jsdomCss)};`,
);
return { contents: code, loader: 'js' };
},
);
build.onLoad(
{
filter: /jsdom[\\/]lib[\\/]jsdom[\\/]living[\\/]xhr[\\/]XMLHttpRequest-impl\.js$/,
},
(args: OnLoadArgs) => {
let code = fs.readFileSync(args.path, 'utf8');
code = code.replace(
`const syncWorkerFile = require.resolve("./xhr-sync-worker.js");`,
`const syncWorkerSrc = ${JSON.stringify(jsdomWorkerSrc)};`,
);
code = code.replace(
`new Worker(syncWorkerFile)`,
`new Worker(syncWorkerSrc, { eval: true })`,
);
return { contents: code, loader: 'js' };
},
);
// @acemir/cssom CSSStyleRule sets parentRule via plain assignment (sloppy mode).
// Bundled strict mode throws because cssstyle defines parentRule as getter-only.
// Use _parentRule (cssstyle's internal backing field for the getter) instead.
build.onLoad(
{
filter: /@acemir[\\/]cssom[\\/]lib[\\/]CSSStyleRule\.js$/,
},
(args: OnLoadArgs) => {
let code = fs.readFileSync(args.path, 'utf8');
code = code.replace(`this.__style.parentRule = this;`, `this.__style._parentRule = this;`);
return { contents: code, loader: 'js' };
},
);
},
};
await Promise.all([
build({
...commonOptions,
entryPoints: ['src/websearch.ts'],
outfile: 'skills/websearch/scripts/websearch.cjs',
}),
build({
...commonOptions,
entryPoints: ['src/webfetch.ts'],
outfile: 'skills/webfetch/scripts/webfetch.cjs',
plugins: [jsdomInlinePlugin],
}),
]);