@statewalker/webrun-bundler 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +178 -2
- package/dist/index.d.mts +225 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +571 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +63 -3
- package/src/bundle.test.ts +388 -0
- package/src/bundle.ts +245 -0
- package/src/index.ts +17 -0
- package/src/input-maps.ts +79 -0
- package/src/packages.ts +126 -0
- package/src/paths.ts +62 -0
- package/src/resolve.ts +116 -0
- package/src/types.ts +154 -0
package/src/bundle.ts
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import { readText } from "@statewalker/webrun-files";
|
|
2
|
+
import type { Loader, Plugin } from "esbuild-wasm";
|
|
3
|
+
import { inlineInputMap } from "./input-maps.js";
|
|
4
|
+
import { nodeModulesPackages } from "./packages.js";
|
|
5
|
+
import { idOf, joinWithin, pathOf, readManifest } from "./paths.js";
|
|
6
|
+
import { resolveSpecifier } from "./resolve.js";
|
|
7
|
+
import type {
|
|
8
|
+
BundleOptions,
|
|
9
|
+
BundleResult,
|
|
10
|
+
ExternalPattern,
|
|
11
|
+
Mount,
|
|
12
|
+
SourceMap,
|
|
13
|
+
TranspileOptions,
|
|
14
|
+
TranspileResult,
|
|
15
|
+
} from "./types.js";
|
|
16
|
+
|
|
17
|
+
/** esbuild's namespace for the files the bundler serves; its source-map names drop it. */
|
|
18
|
+
const NS = "webrun";
|
|
19
|
+
/** esbuild's namespace for the host's modules (`modules`): `virtual:<specifier>` in maps. */
|
|
20
|
+
const VNS = "webrun-virtual";
|
|
21
|
+
/** Marks an `inject` entry, so that it resolves like an import, not like the entry point. */
|
|
22
|
+
const INJECT = "webrun-inject:";
|
|
23
|
+
|
|
24
|
+
/** esbuild `supported` flags that lower `await` to generators. */
|
|
25
|
+
const LOWER_AWAIT = { "async-await": false, "async-generator": false, "for-await": false };
|
|
26
|
+
|
|
27
|
+
const LOADERS: Record<string, Loader> = {
|
|
28
|
+
ts: "ts",
|
|
29
|
+
mts: "ts",
|
|
30
|
+
cts: "ts",
|
|
31
|
+
tsx: "tsx",
|
|
32
|
+
jsx: "jsx",
|
|
33
|
+
js: "js",
|
|
34
|
+
mjs: "js",
|
|
35
|
+
cjs: "js",
|
|
36
|
+
json: "json",
|
|
37
|
+
css: "css",
|
|
38
|
+
txt: "text",
|
|
39
|
+
wasm: "binary",
|
|
40
|
+
};
|
|
41
|
+
export const loaderFor = (path: string): Loader =>
|
|
42
|
+
LOADERS[/\.([a-z0-9]+)$/i.exec(path)?.[1]?.toLowerCase() ?? ""] ?? "js";
|
|
43
|
+
|
|
44
|
+
const isExternal = (spec: string, patterns: ExternalPattern[]) =>
|
|
45
|
+
/^(https?:|data:)/.test(spec) ||
|
|
46
|
+
patterns.some((p) =>
|
|
47
|
+
typeof p === "string"
|
|
48
|
+
? p.endsWith("*")
|
|
49
|
+
? spec.startsWith(p.slice(0, -1))
|
|
50
|
+
: spec === p
|
|
51
|
+
: p.test(spec),
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
/** A source name of esbuild's map → the file id it stands for. */
|
|
55
|
+
const sourceId = (s: string) =>
|
|
56
|
+
s
|
|
57
|
+
.replace(/^(\.\.\/)*/, "")
|
|
58
|
+
.replace(new RegExp(`^${VNS}:`), "virtual:")
|
|
59
|
+
.replace(new RegExp(`^${NS}:`), "");
|
|
60
|
+
|
|
61
|
+
const toBase64 = (text: string) => {
|
|
62
|
+
const bytes = new TextEncoder().encode(text);
|
|
63
|
+
let bin = "";
|
|
64
|
+
for (let i = 0; i < bytes.length; i += 0x8000)
|
|
65
|
+
bin += String.fromCharCode(...bytes.subarray(i, i + 0x8000));
|
|
66
|
+
return btoa(bin);
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/** `code` with its trailing map link replaced: by `map` inlined, or by a link to `name`. */
|
|
70
|
+
function linkMap(code: string, map: SourceMap, how: "external" | "inline", name: string) {
|
|
71
|
+
const body = code.replace(/\n?\/\/# sourceMappingURL=\S+\s*$/, "");
|
|
72
|
+
const url =
|
|
73
|
+
how === "inline"
|
|
74
|
+
? `data:application/json;base64,${toBase64(JSON.stringify(map))}`
|
|
75
|
+
: `${name.split("/").pop()}.map`;
|
|
76
|
+
return `${body}\n//# sourceMappingURL=${url}\n`;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Bundles `entry` of a project into one module with esbuild (esbuild-wasm in browsers).
|
|
81
|
+
* Every file is read through FilesApi (the project's, or a package's from `packages`):
|
|
82
|
+
* esbuild resolves and loads nothing itself. esbuild transpiles TypeScript and JSX,
|
|
83
|
+
* converts CommonJS, lowers `await` when asked, and bundles, all in one pass, and its
|
|
84
|
+
* source map goes back through each step to the files it read; where a package ships
|
|
85
|
+
* its own map (TypeScript compiled to `.js`), to the package's original source. The
|
|
86
|
+
* map's sources are file ids: `~/src/app.ts`, `npm:pkg@1.2.3/src/index.ts`.
|
|
87
|
+
*/
|
|
88
|
+
export async function bundle(opts: BundleOptions): Promise<BundleResult> {
|
|
89
|
+
const root = opts.root ?? "/";
|
|
90
|
+
const project: Mount = {
|
|
91
|
+
prefix: "~",
|
|
92
|
+
files: opts.files,
|
|
93
|
+
root,
|
|
94
|
+
manifest: await readManifest(opts.files, root),
|
|
95
|
+
};
|
|
96
|
+
const packages = opts.packages ?? nodeModulesPackages();
|
|
97
|
+
const external = opts.external ?? [];
|
|
98
|
+
const browser = (opts.platform ?? "browser") === "browser";
|
|
99
|
+
const conditions = opts.conditions ?? ["browser"];
|
|
100
|
+
const outfile = opts.outfile ?? "bundle.js";
|
|
101
|
+
const mounts = new Map<string, Mount>([[project.prefix, project]]);
|
|
102
|
+
const supplied = opts.modules;
|
|
103
|
+
const virtualSource = (spec: string) =>
|
|
104
|
+
typeof supplied === "function" ? supplied(spec) : supplied?.[spec];
|
|
105
|
+
|
|
106
|
+
/** The mount and path of a file id; the longest prefix wins (`npm:@a/b@1.0.0/…`). */
|
|
107
|
+
const locate = (id: string) => {
|
|
108
|
+
let best: Mount | undefined;
|
|
109
|
+
for (const m of mounts.values())
|
|
110
|
+
if (id.startsWith(`${m.prefix}/`) && (!best || m.prefix.length > best.prefix.length))
|
|
111
|
+
best = m;
|
|
112
|
+
const path = best && pathOf(best, id);
|
|
113
|
+
if (!best || !path) throw new Error(`webrun-bundler: no file ${id}`);
|
|
114
|
+
return { mount: best, path };
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const plugin: Plugin = {
|
|
118
|
+
name: "webrun-files",
|
|
119
|
+
setup(b) {
|
|
120
|
+
b.onResolve({ filter: /.*/ }, async (args) => {
|
|
121
|
+
const injected = args.path.startsWith(INJECT);
|
|
122
|
+
const spec = injected ? args.path.slice(INJECT.length) : args.path;
|
|
123
|
+
if (args.kind === "entry-point" && !injected) {
|
|
124
|
+
const path = joinWithin(root, root, args.path.replace(/^\/+/, ""));
|
|
125
|
+
if (!path) return { errors: [{ text: `entry ${args.path} is outside the project` }] };
|
|
126
|
+
return { path: idOf(project, path), namespace: NS };
|
|
127
|
+
}
|
|
128
|
+
if (virtualSource(spec) !== undefined) return { path: spec, namespace: VNS };
|
|
129
|
+
if (isExternal(spec, external)) return { path: spec, external: true };
|
|
130
|
+
// The host's modules and `inject` entries import from the project's root.
|
|
131
|
+
const importer =
|
|
132
|
+
injected || args.namespace === VNS
|
|
133
|
+
? { mount: project, path: `${root === "/" ? "" : root}/` }
|
|
134
|
+
: locate(args.importer);
|
|
135
|
+
const require = args.kind === "require-call" || args.kind === "require-resolve";
|
|
136
|
+
const failed = (why?: string) => ({
|
|
137
|
+
errors: [
|
|
138
|
+
{
|
|
139
|
+
text: `Could not resolve "${spec}" (imported by ${args.importer || "inject"})${why ? `: ${why}` : ""}`,
|
|
140
|
+
},
|
|
141
|
+
],
|
|
142
|
+
});
|
|
143
|
+
let r: Awaited<ReturnType<typeof resolveSpecifier>>;
|
|
144
|
+
try {
|
|
145
|
+
r = await resolveSpecifier(spec, importer, packages, {
|
|
146
|
+
conditions,
|
|
147
|
+
require,
|
|
148
|
+
browser,
|
|
149
|
+
});
|
|
150
|
+
} catch (e) {
|
|
151
|
+
return failed((e as Error)?.message ?? String(e));
|
|
152
|
+
}
|
|
153
|
+
if (!r) return failed();
|
|
154
|
+
mounts.set(r.mount.prefix, r.mount);
|
|
155
|
+
return { path: idOf(r.mount, r.path), namespace: NS };
|
|
156
|
+
});
|
|
157
|
+
b.onLoad({ filter: /.*/, namespace: VNS }, (args) => ({
|
|
158
|
+
contents: virtualSource(args.path) ?? "",
|
|
159
|
+
loader: "js",
|
|
160
|
+
}));
|
|
161
|
+
b.onLoad({ filter: /.*/, namespace: NS }, async (args) => {
|
|
162
|
+
const { mount, path } = locate(args.path);
|
|
163
|
+
const loader = loaderFor(path);
|
|
164
|
+
if (loader === "binary") {
|
|
165
|
+
const chunks: Uint8Array[] = [];
|
|
166
|
+
for await (const c of mount.files.read(path)) chunks.push(c);
|
|
167
|
+
const bytes = new Uint8Array(chunks.reduce((n, c) => n + c.length, 0));
|
|
168
|
+
let at = 0;
|
|
169
|
+
for (const c of chunks) {
|
|
170
|
+
bytes.set(c, at);
|
|
171
|
+
at += c.length;
|
|
172
|
+
}
|
|
173
|
+
return { contents: bytes, loader };
|
|
174
|
+
}
|
|
175
|
+
const text = await readText(mount.files, path);
|
|
176
|
+
const js = loader === "js" || loader === "ts" || loader === "jsx" || loader === "tsx";
|
|
177
|
+
return { contents: js ? await inlineInputMap(mount, path, text) : text, loader };
|
|
178
|
+
});
|
|
179
|
+
},
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
const { plugins: extraPlugins = [], ...rest } = opts.esbuildOptions ?? {};
|
|
183
|
+
const result = await opts.esbuild.build({
|
|
184
|
+
entryPoints: [opts.entry],
|
|
185
|
+
bundle: true,
|
|
186
|
+
write: false,
|
|
187
|
+
format: opts.format ?? "esm",
|
|
188
|
+
platform: opts.platform ?? "browser",
|
|
189
|
+
target: opts.target ?? "es2022",
|
|
190
|
+
outfile,
|
|
191
|
+
absWorkingDir: "/",
|
|
192
|
+
metafile: true,
|
|
193
|
+
logLevel: "silent",
|
|
194
|
+
minify: opts.minify,
|
|
195
|
+
define: opts.define,
|
|
196
|
+
...(opts.lowerAwait ? { supported: LOWER_AWAIT } : {}),
|
|
197
|
+
...rest,
|
|
198
|
+
...(opts.inject?.length ? { inject: opts.inject.map((s) => INJECT + s) } : {}),
|
|
199
|
+
sourcemap: "external",
|
|
200
|
+
sourcesContent: true,
|
|
201
|
+
plugins: [...extraPlugins, plugin],
|
|
202
|
+
});
|
|
203
|
+
const out = result.outputFiles ?? [];
|
|
204
|
+
const jsFile = out.find((f) => !f.path.endsWith(".map"));
|
|
205
|
+
const mapFile = out.find((f) => f.path.endsWith(".map"));
|
|
206
|
+
if (!jsFile || !mapFile) throw new Error("webrun-bundler: esbuild produced no output");
|
|
207
|
+
const raw = JSON.parse(mapFile.text) as SourceMap;
|
|
208
|
+
const map: SourceMap = {
|
|
209
|
+
...raw,
|
|
210
|
+
file: outfile.split("/").pop(),
|
|
211
|
+
sources: raw.sources.map(sourceId),
|
|
212
|
+
};
|
|
213
|
+
delete map.sourceRoot;
|
|
214
|
+
return {
|
|
215
|
+
code: linkMap(jsFile.text, map, opts.sourcemap ?? "external", outfile),
|
|
216
|
+
map,
|
|
217
|
+
metafile: result.metafile as BundleResult["metafile"],
|
|
218
|
+
warnings: result.warnings,
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Transpiles one file (TypeScript, JSX, modern syntax down to `target`; `await` lowered
|
|
224
|
+
* when asked) with a source map back to it. A map the code carries (`sourceMappingURL`,
|
|
225
|
+
* inline) is composed in, so the result still traces to the original.
|
|
226
|
+
*/
|
|
227
|
+
export async function transpile(code: string, opts: TranspileOptions): Promise<TranspileResult> {
|
|
228
|
+
const r = await opts.esbuild.transform(code, {
|
|
229
|
+
loader: loaderFor(opts.path),
|
|
230
|
+
sourcefile: opts.path,
|
|
231
|
+
sourcemap: "external",
|
|
232
|
+
sourcesContent: true,
|
|
233
|
+
format: opts.format,
|
|
234
|
+
target: opts.target ?? "es2022",
|
|
235
|
+
minify: opts.minify,
|
|
236
|
+
define: opts.define,
|
|
237
|
+
...(opts.lowerAwait ? { supported: LOWER_AWAIT } : {}),
|
|
238
|
+
});
|
|
239
|
+
const map = JSON.parse(r.map) as SourceMap;
|
|
240
|
+
return {
|
|
241
|
+
code: linkMap(r.code, map, opts.sourcemap ?? "external", opts.path.replace(/\.[^./]+$/, ".js")),
|
|
242
|
+
map,
|
|
243
|
+
warnings: r.warnings,
|
|
244
|
+
};
|
|
245
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export { bundle, loaderFor, transpile } from "./bundle.js";
|
|
2
|
+
export { inlineInputMap } from "./input-maps.js";
|
|
3
|
+
export { chainPackages, nodeModulesPackages, npmPackages } from "./packages.js";
|
|
4
|
+
export { resolveSpecifier } from "./resolve.js";
|
|
5
|
+
export type {
|
|
6
|
+
BundleOptions,
|
|
7
|
+
BundleResult,
|
|
8
|
+
Esbuild,
|
|
9
|
+
ExternalPattern,
|
|
10
|
+
Importer,
|
|
11
|
+
Mount,
|
|
12
|
+
PackageManifest,
|
|
13
|
+
PackageStore,
|
|
14
|
+
SourceMap,
|
|
15
|
+
TranspileOptions,
|
|
16
|
+
TranspileResult,
|
|
17
|
+
} from "./types.js";
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { tryReadText } from "@statewalker/webrun-files";
|
|
2
|
+
import { dirOf, idOf, joinWithin } from "./paths.js";
|
|
3
|
+
import type { Mount, SourceMap } from "./types.js";
|
|
4
|
+
|
|
5
|
+
// The last `//# sourceMappingURL=…` (or `//@`) comment of a JS file.
|
|
6
|
+
const MAP_COMMENT = /\/\/[#@]\s*sourceMappingURL=(\S+)\s*$/;
|
|
7
|
+
|
|
8
|
+
const toBase64 = (text: string) => {
|
|
9
|
+
const bytes = new TextEncoder().encode(text);
|
|
10
|
+
let bin = "";
|
|
11
|
+
for (let i = 0; i < bytes.length; i += 0x8000)
|
|
12
|
+
bin += String.fromCharCode(...bytes.subarray(i, i + 0x8000));
|
|
13
|
+
return btoa(bin);
|
|
14
|
+
};
|
|
15
|
+
const fromBase64 = (b64: string) =>
|
|
16
|
+
new TextDecoder().decode(Uint8Array.from(atob(b64), (c) => c.charCodeAt(0)));
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A file's own source map, made usable by esbuild, which reads only maps inlined in
|
|
20
|
+
* the file it loads (it cannot follow a linked `.map` through a FilesApi):
|
|
21
|
+
*
|
|
22
|
+
* - a linked map (`//# sourceMappingURL=index.js.map`) is read from the same tree and
|
|
23
|
+
* inlined; an inline `data:` map is kept;
|
|
24
|
+
* - its sources become ids of the same tree (`npm:pkg@1.0.0/src/index.ts`), so the
|
|
25
|
+
* bundle's map names the package's original files, not the published JavaScript;
|
|
26
|
+
* - missing `sourcesContent` is filled from the tree when the original file ships
|
|
27
|
+
* with the package.
|
|
28
|
+
*
|
|
29
|
+
* esbuild then composes it with its own: a bundled line traces to the original source.
|
|
30
|
+
* A map that is missing or unreadable is dropped (the file maps to itself).
|
|
31
|
+
*/
|
|
32
|
+
export async function inlineInputMap(mount: Mount, path: string, code: string): Promise<string> {
|
|
33
|
+
const lines = code.trimEnd().split("\n");
|
|
34
|
+
const last = lines[lines.length - 1] ?? "";
|
|
35
|
+
const match = MAP_COMMENT.exec(last);
|
|
36
|
+
if (!match) return code;
|
|
37
|
+
const url = match[1] as string;
|
|
38
|
+
const body = lines.slice(0, -1).join("\n");
|
|
39
|
+
let text: string | undefined;
|
|
40
|
+
let mapDir = dirOf(path);
|
|
41
|
+
if (url.startsWith("data:")) {
|
|
42
|
+
const comma = url.indexOf(",");
|
|
43
|
+
const meta = url.slice(5, comma);
|
|
44
|
+
const payload = url.slice(comma + 1);
|
|
45
|
+
try {
|
|
46
|
+
text = meta.endsWith(";base64") ? fromBase64(payload) : decodeURIComponent(payload);
|
|
47
|
+
} catch {
|
|
48
|
+
text = undefined;
|
|
49
|
+
}
|
|
50
|
+
} else if (!/^[a-z][a-z0-9+.-]*:/i.test(url)) {
|
|
51
|
+
const mapPath = joinWithin(mount.root, dirOf(path), decodeURIComponent(url));
|
|
52
|
+
if (mapPath) {
|
|
53
|
+
text = await tryReadText(mount.files, mapPath);
|
|
54
|
+
mapDir = dirOf(mapPath);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
let map: SourceMap;
|
|
58
|
+
try {
|
|
59
|
+
map = JSON.parse(text ?? "") as SourceMap;
|
|
60
|
+
} catch {
|
|
61
|
+
return body; // no usable map: the file is its own source
|
|
62
|
+
}
|
|
63
|
+
const sourceDir = map.sourceRoot
|
|
64
|
+
? (joinWithin("/", mapDir, map.sourceRoot.replace(/\/$/, "")) ?? mapDir)
|
|
65
|
+
: mapDir;
|
|
66
|
+
const contents: (string | null)[] = map.sourcesContent ? [...map.sourcesContent] : [];
|
|
67
|
+
const sources: string[] = [];
|
|
68
|
+
for (let i = 0; i < map.sources.length; i++) {
|
|
69
|
+
const src = map.sources[i] as string;
|
|
70
|
+
const local = /^[a-z][a-z0-9+.-]*:/i.test(src)
|
|
71
|
+
? undefined
|
|
72
|
+
: joinWithin(mount.root, sourceDir, src);
|
|
73
|
+
sources.push(local ? idOf(mount, local) : src);
|
|
74
|
+
if (contents[i] == null && local) contents[i] = (await tryReadText(mount.files, local)) ?? null;
|
|
75
|
+
}
|
|
76
|
+
const fixed: SourceMap = { ...map, sources, sourcesContent: contents };
|
|
77
|
+
delete fixed.sourceRoot;
|
|
78
|
+
return `${body}\n//# sourceMappingURL=data:application/json;base64,${toBase64(JSON.stringify(fixed))}\n`;
|
|
79
|
+
}
|
package/src/packages.ts
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import type { Source } from "@statewalker/webrun-modules";
|
|
2
|
+
import { dirOf, readManifest } from "./paths.js";
|
|
3
|
+
import type { Importer, Mount, PackageStore } from "./types.js";
|
|
4
|
+
|
|
5
|
+
const mountPrefix = (name: string, version: string | undefined) =>
|
|
6
|
+
`npm:${name}@${version ?? "0.0.0"}`;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Packages from `node_modules` folders, found as Node finds them: in the importing
|
|
10
|
+
* file's directory and each parent, inside the importer's own FilesApi. A package
|
|
11
|
+
* installed in the project (or in another package's `node_modules`) is read where it is.
|
|
12
|
+
*
|
|
13
|
+
* pnpm layouts: a package's own dependencies sit next to its real directory in
|
|
14
|
+
* `node_modules/.pnpm`, which Node reaches through the symlink's real path and a FilesApi
|
|
15
|
+
* cannot see. When the walk finds nothing, each ancestor's `node_modules/.pnpm/node_modules`
|
|
16
|
+
* (pnpm's hoisted links to every installed package) is tried too.
|
|
17
|
+
*/
|
|
18
|
+
export function nodeModulesPackages(): PackageStore {
|
|
19
|
+
const mounts = new Map<string, Mount>();
|
|
20
|
+
const at = async (files: Mount["files"], root: string, name: string) => {
|
|
21
|
+
const known = mounts.get(root);
|
|
22
|
+
if (known && known.files === files) return known;
|
|
23
|
+
const manifest = await readManifest(files, root);
|
|
24
|
+
if (!manifest) return undefined;
|
|
25
|
+
const mount: Mount = { prefix: mountPrefix(name, manifest.version), files, root, manifest };
|
|
26
|
+
mounts.set(root, mount);
|
|
27
|
+
return mount;
|
|
28
|
+
};
|
|
29
|
+
const ancestors = (path: string) => {
|
|
30
|
+
const dirs: string[] = [];
|
|
31
|
+
for (let dir = dirOf(path); ; dir = dirOf(dir)) {
|
|
32
|
+
dirs.push(dir === "/" ? "" : dir);
|
|
33
|
+
if (dir === "/") return dirs;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
return {
|
|
37
|
+
async get(name, importer) {
|
|
38
|
+
const files = importer.mount.files;
|
|
39
|
+
const dirs = ancestors(importer.path);
|
|
40
|
+
for (const dir of dirs) {
|
|
41
|
+
const m = await at(files, `${dir}/node_modules/${name}`, name);
|
|
42
|
+
if (m) return m;
|
|
43
|
+
}
|
|
44
|
+
for (const dir of dirs) {
|
|
45
|
+
const m = await at(files, `${dir}/node_modules/.pnpm/node_modules/${name}`, name);
|
|
46
|
+
if (m) return m;
|
|
47
|
+
}
|
|
48
|
+
return undefined;
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const DEPENDENCY_FIELDS = [
|
|
54
|
+
"dependencies",
|
|
55
|
+
"peerDependencies",
|
|
56
|
+
"optionalDependencies",
|
|
57
|
+
"devDependencies",
|
|
58
|
+
] as const;
|
|
59
|
+
|
|
60
|
+
/** The version range `importer`'s `package.json` gives `name`, if any. */
|
|
61
|
+
function rangeFor(name: string, importer: Importer): string | undefined {
|
|
62
|
+
const m = importer.mount.manifest;
|
|
63
|
+
for (const field of DEPENDENCY_FIELDS) {
|
|
64
|
+
const range = m?.[field]?.[name];
|
|
65
|
+
if (range) return range;
|
|
66
|
+
}
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Packages fetched on demand (default: the npm registry, through webrun-modules'
|
|
72
|
+
* `npmRegistrySource`) and kept in memory: each one is its own FilesApi tree. The
|
|
73
|
+
* version is the range the importer's `package.json` declares (the project's for the
|
|
74
|
+
* project's files), else the latest; one range loads once.
|
|
75
|
+
*/
|
|
76
|
+
export function npmPackages(opts: { source?: Source } = {}): PackageStore {
|
|
77
|
+
// Loaded only when used: a bundle of the bundler that brings its own source stays small.
|
|
78
|
+
let registry: Promise<Source> | undefined;
|
|
79
|
+
const source: Source = opts.source ?? {
|
|
80
|
+
matches: (ref) => "pkg" in ref,
|
|
81
|
+
load: async (ref) => {
|
|
82
|
+
registry ??= import("@statewalker/webrun-modules").then((m) => m.npmRegistrySource());
|
|
83
|
+
return (await registry).load(ref);
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
const byRange = new Map<string, Promise<Mount>>();
|
|
87
|
+
const byVersion = new Map<string, Mount>();
|
|
88
|
+
return {
|
|
89
|
+
get(name, importer) {
|
|
90
|
+
const range = rangeFor(name, importer);
|
|
91
|
+
const key = `${name}@${range ?? "latest"}`;
|
|
92
|
+
let mount = byRange.get(key);
|
|
93
|
+
if (!mount) {
|
|
94
|
+
mount = source.load({ pkg: name, version: range }).then((p) => {
|
|
95
|
+
const prefix = mountPrefix(p.name, p.version);
|
|
96
|
+
const seen = byVersion.get(prefix);
|
|
97
|
+
if (seen) return seen; // two ranges, one version: one copy of the package
|
|
98
|
+
const m: Mount = { prefix, files: p.files, root: "/", manifest: p.manifest };
|
|
99
|
+
byVersion.set(prefix, m);
|
|
100
|
+
return m;
|
|
101
|
+
});
|
|
102
|
+
byRange.set(key, mount);
|
|
103
|
+
}
|
|
104
|
+
return mount;
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** The first store that has the package wins (`node_modules` first, then the registry). */
|
|
110
|
+
export function chainPackages(...stores: PackageStore[]): PackageStore {
|
|
111
|
+
return {
|
|
112
|
+
async get(name, importer) {
|
|
113
|
+
let failure: unknown;
|
|
114
|
+
for (const s of stores) {
|
|
115
|
+
try {
|
|
116
|
+
const m = await s.get(name, importer);
|
|
117
|
+
if (m) return m;
|
|
118
|
+
} catch (e) {
|
|
119
|
+
failure ??= e; // the next store may still have it
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
if (failure) throw failure;
|
|
123
|
+
return undefined;
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
}
|
package/src/paths.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { normalizePath, tryReadText } from "@statewalker/webrun-files";
|
|
2
|
+
import type { Mount, PackageManifest } from "./types.js";
|
|
3
|
+
|
|
4
|
+
/** `path` joined to `dir`, with `.` and `..` resolved; `undefined` when it leaves `root`. */
|
|
5
|
+
export function joinWithin(root: string, dir: string, path: string): string | undefined {
|
|
6
|
+
const parts = (path.startsWith("/") ? path : `${dir}/${path}`).split("/");
|
|
7
|
+
const out: string[] = [];
|
|
8
|
+
for (const p of parts) {
|
|
9
|
+
if (!p || p === ".") continue;
|
|
10
|
+
if (p === "..") {
|
|
11
|
+
if (!out.length) return undefined;
|
|
12
|
+
out.pop();
|
|
13
|
+
} else out.push(p);
|
|
14
|
+
}
|
|
15
|
+
const joined = `/${out.join("/")}`;
|
|
16
|
+
const r = normalizePath(root);
|
|
17
|
+
return r === "/" || joined === r || joined.startsWith(`${r}/`) ? joined : undefined;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const dirOf = (path: string) => path.slice(0, path.lastIndexOf("/")) || "/";
|
|
21
|
+
|
|
22
|
+
/** The id of a mount's file: `<prefix>/<path below root>`. */
|
|
23
|
+
export function idOf(mount: Mount, path: string): string {
|
|
24
|
+
const r = normalizePath(mount.root);
|
|
25
|
+
const rel = r === "/" ? path : path.slice(r.length);
|
|
26
|
+
return `${mount.prefix}${rel || "/"}`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** A mount's file from its id; `undefined` when the id is not the mount's. */
|
|
30
|
+
export function pathOf(mount: Mount, id: string): string | undefined {
|
|
31
|
+
if (!id.startsWith(`${mount.prefix}/`)) return undefined;
|
|
32
|
+
const r = normalizePath(mount.root);
|
|
33
|
+
const rel = id.slice(mount.prefix.length);
|
|
34
|
+
return r === "/" ? rel : `${r}${rel}`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export async function isFile(mount: Mount, path: string): Promise<boolean> {
|
|
38
|
+
const s = await mount.files.stats(path);
|
|
39
|
+
return s?.kind === "file";
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export async function readManifest(
|
|
43
|
+
files: Mount["files"],
|
|
44
|
+
dir: string,
|
|
45
|
+
): Promise<PackageManifest | undefined> {
|
|
46
|
+
const text = await tryReadText(files, `${dir === "/" ? "" : dir}/package.json`);
|
|
47
|
+
if (text === undefined) return undefined;
|
|
48
|
+
try {
|
|
49
|
+
return JSON.parse(text) as PackageManifest;
|
|
50
|
+
} catch {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** `react` → `{ name: "react" }`; `@a/b/c.js` → `{ name: "@a/b", subpath: "c.js" }`. */
|
|
56
|
+
export function parsePackageSpecifier(spec: string): { name: string; subpath?: string } {
|
|
57
|
+
const parts = spec.split("/");
|
|
58
|
+
const n = spec.startsWith("@") ? 2 : 1;
|
|
59
|
+
const name = parts.slice(0, n).join("/");
|
|
60
|
+
const subpath = parts.slice(n).join("/");
|
|
61
|
+
return subpath ? { name, subpath } : { name };
|
|
62
|
+
}
|
package/src/resolve.ts
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { imports as resolveImports, exports as resolvePackageExports } from "resolve.exports";
|
|
2
|
+
import { dirOf, isFile, joinWithin, parsePackageSpecifier } from "./paths.js";
|
|
3
|
+
import type { Importer, Mount, PackageManifest, PackageStore } from "./types.js";
|
|
4
|
+
|
|
5
|
+
/** Extensions tried, in order, for a path written without one (or with `.js` for a `.ts` file). */
|
|
6
|
+
const EXTENSIONS = [".ts", ".tsx", ".mts", ".cts", ".js", ".jsx", ".mjs", ".cjs", ".json"];
|
|
7
|
+
|
|
8
|
+
export interface Resolved {
|
|
9
|
+
mount: Mount;
|
|
10
|
+
path: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** `path` as written, else with an extension, else its `index` file. */
|
|
14
|
+
async function resolveFile(mount: Mount, path: string): Promise<string | undefined> {
|
|
15
|
+
if (await isFile(mount, path)) return path;
|
|
16
|
+
// TypeScript sources import `./x.js` for `./x.ts`.
|
|
17
|
+
const js = /\.(m|c)?jsx?$/.exec(path);
|
|
18
|
+
if (js) {
|
|
19
|
+
const base = path.slice(0, -js[0].length);
|
|
20
|
+
for (const ext of [`.${js[1] ?? ""}ts`, ".tsx"])
|
|
21
|
+
if (await isFile(mount, base + ext)) return base + ext;
|
|
22
|
+
}
|
|
23
|
+
for (const ext of EXTENSIONS) if (await isFile(mount, path + ext)) return path + ext;
|
|
24
|
+
const dir = path.endsWith("/") ? path.slice(0, -1) : path;
|
|
25
|
+
for (const ext of EXTENSIONS)
|
|
26
|
+
if (await isFile(mount, `${dir}/index${ext}`)) return `${dir}/index${ext}`;
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
type Conditions = { conditions: string[]; require: boolean; browser: boolean };
|
|
31
|
+
|
|
32
|
+
function exportsTarget(
|
|
33
|
+
manifest: PackageManifest,
|
|
34
|
+
entry: string,
|
|
35
|
+
c: Conditions,
|
|
36
|
+
): string | undefined {
|
|
37
|
+
try {
|
|
38
|
+
const out = resolvePackageExports(manifest, entry, {
|
|
39
|
+
conditions: c.conditions,
|
|
40
|
+
require: c.require,
|
|
41
|
+
browser: c.browser,
|
|
42
|
+
}) as string[] | undefined;
|
|
43
|
+
return out?.[0];
|
|
44
|
+
} catch {
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** A package's file for `subpath`: `exports` first, then `module`/`main` (and `browser` when it is a string). */
|
|
50
|
+
async function resolvePackageEntry(
|
|
51
|
+
mount: Mount,
|
|
52
|
+
subpath: string | undefined,
|
|
53
|
+
c: Conditions,
|
|
54
|
+
): Promise<string | undefined> {
|
|
55
|
+
const manifest = mount.manifest ?? {};
|
|
56
|
+
const entry = subpath ? `./${subpath}` : ".";
|
|
57
|
+
if (manifest.exports !== undefined) {
|
|
58
|
+
const target = exportsTarget(manifest, entry, c);
|
|
59
|
+
if (!target) return undefined; // not exported: Node refuses it as well
|
|
60
|
+
return joinWithin(mount.root, mount.root, target);
|
|
61
|
+
}
|
|
62
|
+
if (subpath) return resolveFile(mount, joinWithin(mount.root, mount.root, subpath) ?? "");
|
|
63
|
+
const fields = [
|
|
64
|
+
...(c.browser && typeof manifest.browser === "string" ? [manifest.browser] : []),
|
|
65
|
+
...(c.require ? [] : [manifest.module]),
|
|
66
|
+
manifest.main,
|
|
67
|
+
"index.js",
|
|
68
|
+
].filter((f): f is string => typeof f === "string");
|
|
69
|
+
for (const f of fields) {
|
|
70
|
+
const p = joinWithin(mount.root, mount.root, f);
|
|
71
|
+
const file = p && (await resolveFile(mount, p));
|
|
72
|
+
if (file) return file;
|
|
73
|
+
}
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Resolves `spec` as written in `importer`: a path of the same tree (`./x`, `../y.js`,
|
|
79
|
+
* `/z`), a `#imports` alias of the importer's package, or a package found by `packages`
|
|
80
|
+
* (with its `exports`, `module` and `main`).
|
|
81
|
+
*/
|
|
82
|
+
export async function resolveSpecifier(
|
|
83
|
+
spec: string,
|
|
84
|
+
importer: Importer,
|
|
85
|
+
packages: PackageStore,
|
|
86
|
+
c: Conditions,
|
|
87
|
+
): Promise<Resolved | undefined> {
|
|
88
|
+
const { mount } = importer;
|
|
89
|
+
if (spec.startsWith("./") || spec.startsWith("../") || spec.startsWith("/") || spec === ".") {
|
|
90
|
+
const base = spec.startsWith("/") ? mount.root : dirOf(importer.path);
|
|
91
|
+
const target = joinWithin(mount.root, base, spec.startsWith("/") ? spec.slice(1) : spec);
|
|
92
|
+
const path = target && (await resolveFile(mount, target));
|
|
93
|
+
return path ? { mount, path } : undefined;
|
|
94
|
+
}
|
|
95
|
+
if (spec.startsWith("#")) {
|
|
96
|
+
try {
|
|
97
|
+
const out = resolveImports(mount.manifest ?? {}, spec, {
|
|
98
|
+
conditions: c.conditions,
|
|
99
|
+
require: c.require,
|
|
100
|
+
browser: c.browser,
|
|
101
|
+
}) as string[] | undefined;
|
|
102
|
+
const target = out?.[0];
|
|
103
|
+
if (!target) return undefined;
|
|
104
|
+
if (!target.startsWith(".")) return resolveSpecifier(target, importer, packages, c);
|
|
105
|
+
const path = joinWithin(mount.root, mount.root, target);
|
|
106
|
+
return path && (await isFile(mount, path)) ? { mount, path } : undefined;
|
|
107
|
+
} catch {
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
const { name, subpath } = parsePackageSpecifier(spec);
|
|
112
|
+
const pkg = await packages.get(name, importer);
|
|
113
|
+
if (!pkg) return undefined;
|
|
114
|
+
const path = await resolvePackageEntry(pkg, subpath, c);
|
|
115
|
+
return path && (await isFile(pkg, path)) ? { mount: pkg, path } : undefined;
|
|
116
|
+
}
|