@seanhogg/builderforce-webcontainers-core 2026.10.8 → 2026.10.10
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/dist/build.d.ts +6 -0
- package/dist/build.d.ts.map +1 -1
- package/dist/build.js +9 -1
- package/dist/build.js.map +1 -1
- package/dist/buildHost.d.ts +3 -0
- package/dist/buildHost.d.ts.map +1 -1
- package/dist/buildHost.js +25 -12
- package/dist/buildHost.js.map +1 -1
- package/dist/compileComponent.d.ts.map +1 -1
- package/dist/compileComponent.js +4 -1
- package/dist/compileComponent.js.map +1 -1
- package/dist/compileScript.d.ts +12 -4
- package/dist/compileScript.d.ts.map +1 -1
- package/dist/compileScript.js +26 -13
- package/dist/compileScript.js.map +1 -1
- package/dist/components.d.ts +5 -0
- package/dist/components.d.ts.map +1 -1
- package/dist/components.js.map +1 -1
- package/dist/detectProject.d.ts +2 -0
- package/dist/detectProject.d.ts.map +1 -1
- package/dist/detectProject.js +13 -7
- package/dist/detectProject.js.map +1 -1
- package/dist/devServer.d.ts +38 -4
- package/dist/devServer.d.ts.map +1 -1
- package/dist/devServer.js +140 -24
- package/dist/devServer.js.map +1 -1
- package/dist/devStyles.d.ts +27 -0
- package/dist/devStyles.d.ts.map +1 -0
- package/dist/devStyles.js +52 -0
- package/dist/devStyles.js.map +1 -0
- package/dist/hot/client.d.ts +33 -0
- package/dist/hot/client.d.ts.map +1 -0
- package/dist/hot/client.js +46 -0
- package/dist/hot/client.js.map +1 -0
- package/dist/hot/moduleGraph.d.ts +39 -0
- package/dist/hot/moduleGraph.d.ts.map +1 -0
- package/dist/hot/moduleGraph.js +69 -0
- package/dist/hot/moduleGraph.js.map +1 -0
- package/dist/hot/planUpdate.d.ts +33 -0
- package/dist/hot/planUpdate.d.ts.map +1 -0
- package/dist/hot/planUpdate.js +43 -0
- package/dist/hot/planUpdate.js.map +1 -0
- package/dist/hot/reactRefresh.d.ts +45 -0
- package/dist/hot/reactRefresh.d.ts.map +1 -0
- package/dist/hot/reactRefresh.js +86 -0
- package/dist/hot/reactRefresh.js.map +1 -0
- package/dist/html.d.ts +2 -8
- package/dist/html.d.ts.map +1 -1
- package/dist/html.js +11 -17
- package/dist/html.js.map +1 -1
- package/dist/index.d.ts +19 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -1
- package/dist/index.js.map +1 -1
- package/dist/installer/tree.d.ts.map +1 -1
- package/dist/installer/tree.js +30 -5
- package/dist/installer/tree.js.map +1 -1
- package/dist/installer/wasmBuilds.d.ts +34 -0
- package/dist/installer/wasmBuilds.d.ts.map +1 -0
- package/dist/installer/wasmBuilds.js +41 -0
- package/dist/installer/wasmBuilds.js.map +1 -0
- package/dist/node/builtins.d.ts +6 -1
- package/dist/node/builtins.d.ts.map +1 -1
- package/dist/node/builtins.js +17 -10
- package/dist/node/builtins.js.map +1 -1
- package/dist/node/childProcess.d.ts +3 -3
- package/dist/node/childProcess.d.ts.map +1 -1
- package/dist/node/childProcess.js +68 -16
- package/dist/node/childProcess.js.map +1 -1
- package/dist/node/crypto.d.ts.map +1 -1
- package/dist/node/crypto.js +5 -4
- package/dist/node/crypto.js.map +1 -1
- package/dist/node/esm.d.ts +7 -0
- package/dist/node/esm.d.ts.map +1 -1
- package/dist/node/esm.js +28 -2
- package/dist/node/esm.js.map +1 -1
- package/dist/node/fs/index.d.ts +3 -0
- package/dist/node/fs/index.d.ts.map +1 -1
- package/dist/node/fs/index.js +22 -106
- package/dist/node/fs/index.js.map +1 -1
- package/dist/node/fs/stdinFd.d.ts +10 -0
- package/dist/node/fs/stdinFd.d.ts.map +1 -0
- package/dist/node/fs/stdinFd.js +40 -0
- package/dist/node/fs/stdinFd.js.map +1 -0
- package/dist/node/fs/streams.d.ts +24 -0
- package/dist/node/fs/streams.d.ts.map +1 -0
- package/dist/node/fs/streams.js +144 -0
- package/dist/node/fs/streams.js.map +1 -0
- package/dist/node/fs/sync.d.ts +2 -0
- package/dist/node/fs/sync.d.ts.map +1 -1
- package/dist/node/fs/sync.js +17 -0
- package/dist/node/fs/sync.js.map +1 -1
- package/dist/node/globalObject.d.ts +25 -0
- package/dist/node/globalObject.d.ts.map +1 -0
- package/dist/node/globalObject.js +83 -0
- package/dist/node/globalObject.js.map +1 -0
- package/dist/node/http/index.d.ts.map +1 -1
- package/dist/node/http/index.js +4 -3
- package/dist/node/http/index.js.map +1 -1
- package/dist/node/http/server.d.ts.map +1 -1
- package/dist/node/http/server.js +8 -5
- package/dist/node/http/server.js.map +1 -1
- package/dist/node/http/serverResponse.d.ts +3 -0
- package/dist/node/http/serverResponse.d.ts.map +1 -1
- package/dist/node/http/serverResponse.js +7 -0
- package/dist/node/http/serverResponse.js.map +1 -1
- package/dist/node/ipc.d.ts +26 -0
- package/dist/node/ipc.d.ts.map +1 -0
- package/dist/node/ipc.js +76 -0
- package/dist/node/ipc.js.map +1 -0
- package/dist/node/loader.d.ts +2 -0
- package/dist/node/loader.d.ts.map +1 -1
- package/dist/node/loader.js +79 -14
- package/dist/node/loader.js.map +1 -1
- package/dist/node/misc.d.ts.map +1 -1
- package/dist/node/misc.js +20 -6
- package/dist/node/misc.js.map +1 -1
- package/dist/node/process.d.ts +2 -5
- package/dist/node/process.d.ts.map +1 -1
- package/dist/node/process.js +16 -11
- package/dist/node/process.js.map +1 -1
- package/dist/node/processExit.d.ts +13 -0
- package/dist/node/processExit.d.ts.map +1 -0
- package/dist/node/processExit.js +25 -0
- package/dist/node/processExit.js.map +1 -0
- package/dist/node/program.d.ts.map +1 -1
- package/dist/node/program.js +6 -5
- package/dist/node/program.js.map +1 -1
- package/dist/node/runtime.d.ts +6 -1
- package/dist/node/runtime.d.ts.map +1 -1
- package/dist/node/runtime.js +23 -15
- package/dist/node/runtime.js.map +1 -1
- package/dist/node/stream/readable.d.ts +2 -1
- package/dist/node/stream/readable.d.ts.map +1 -1
- package/dist/node/stream/readable.js +2 -1
- package/dist/node/stream/readable.js.map +1 -1
- package/dist/node/urlqs.d.ts +1 -1
- package/dist/node/urlqs.d.ts.map +1 -1
- package/dist/node/urlqs.js +11 -2
- package/dist/node/urlqs.js.map +1 -1
- package/dist/node/wasi/abi.d.ts +82 -0
- package/dist/node/wasi/abi.d.ts.map +1 -0
- package/dist/node/wasi/abi.js +144 -0
- package/dist/node/wasi/abi.js.map +1 -0
- package/dist/node/wasi/files.d.ts +67 -0
- package/dist/node/wasi/files.d.ts.map +1 -0
- package/dist/node/wasi/files.js +319 -0
- package/dist/node/wasi/files.js.map +1 -0
- package/dist/node/wasi/index.d.ts +17 -0
- package/dist/node/wasi/index.d.ts.map +1 -0
- package/dist/node/wasi/index.js +129 -0
- package/dist/node/wasi/index.js.map +1 -0
- package/dist/node/workerThreads.d.ts +31 -0
- package/dist/node/workerThreads.d.ts.map +1 -0
- package/dist/node/workerThreads.js +165 -0
- package/dist/node/workerThreads.js.map +1 -0
- package/dist/node/zlib.d.ts.map +1 -1
- package/dist/node/zlib.js +23 -2
- package/dist/node/zlib.js.map +1 -1
- package/dist/shell/context.d.ts.map +1 -1
- package/dist/shell/context.js +2 -2
- package/dist/shell/context.js.map +1 -1
- package/dist/shell/devHandoff.d.ts +10 -4
- package/dist/shell/devHandoff.d.ts.map +1 -1
- package/dist/shell/devHandoff.js +7 -5
- package/dist/shell/devHandoff.js.map +1 -1
- package/dist/shell/exec.d.ts.map +1 -1
- package/dist/shell/exec.js +6 -5
- package/dist/shell/exec.js.map +1 -1
- package/dist/shell/npm.js +2 -2
- package/dist/shell/npm.js.map +1 -1
- package/dist/shell/program.js +5 -5
- package/dist/shell/program.js.map +1 -1
- package/dist/styleModules.d.ts +2 -5
- package/dist/styleModules.d.ts.map +1 -1
- package/dist/styleModules.js +23 -14
- package/dist/styleModules.js.map +1 -1
- package/dist/styles/compile.d.ts +18 -0
- package/dist/styles/compile.d.ts.map +1 -0
- package/dist/styles/compile.js +34 -0
- package/dist/styles/compile.js.map +1 -0
- package/dist/styles/compilers.d.ts +75 -0
- package/dist/styles/compilers.d.ts.map +1 -0
- package/dist/styles/compilers.js +77 -0
- package/dist/styles/compilers.js.map +1 -0
- package/dist/styles/cssModules.d.ts +40 -0
- package/dist/styles/cssModules.d.ts.map +1 -0
- package/dist/styles/cssModules.js +239 -0
- package/dist/styles/cssModules.js.map +1 -0
- package/dist/styles/imports.d.ts +37 -0
- package/dist/styles/imports.d.ts.map +1 -0
- package/dist/styles/imports.js +88 -0
- package/dist/styles/imports.js.map +1 -0
- package/dist/styles/languages.d.ts +13 -0
- package/dist/styles/languages.d.ts.map +1 -0
- package/dist/styles/languages.js +20 -0
- package/dist/styles/languages.js.map +1 -0
- package/dist/styles/packageStyles.d.ts +19 -0
- package/dist/styles/packageStyles.d.ts.map +1 -0
- package/dist/styles/packageStyles.js +32 -0
- package/dist/styles/packageStyles.js.map +1 -0
- package/dist/svelteCompiler.d.ts.map +1 -1
- package/dist/svelteCompiler.js +19 -2
- package/dist/svelteCompiler.js.map +1 -1
- package/dist/system/commands.d.ts +2 -0
- package/dist/system/commands.d.ts.map +1 -1
- package/dist/system/commands.js +18 -0
- package/dist/system/commands.js.map +1 -1
- package/dist/system/index.d.ts +3 -1
- package/dist/system/index.d.ts.map +1 -1
- package/dist/system/index.js +1 -0
- package/dist/system/index.js.map +1 -1
- package/dist/system/ipc.d.ts +33 -0
- package/dist/system/ipc.d.ts.map +1 -0
- package/dist/system/ipc.js +65 -0
- package/dist/system/ipc.js.map +1 -0
- package/dist/system/kernel.d.ts.map +1 -1
- package/dist/system/kernel.js +6 -3
- package/dist/system/kernel.js.map +1 -1
- package/dist/system/process.d.ts +3 -0
- package/dist/system/process.d.ts.map +1 -1
- package/dist/system/process.js +50 -26
- package/dist/system/process.js.map +1 -1
- package/dist/system/terminal.d.ts +10 -4
- package/dist/system/terminal.d.ts.map +1 -1
- package/dist/system/terminal.js +21 -13
- package/dist/system/terminal.js.map +1 -1
- package/dist/system/types.d.ts +29 -7
- package/dist/system/types.d.ts.map +1 -1
- package/dist/vueCompiler.d.ts.map +1 -1
- package/dist/vueCompiler.js +11 -8
- package/dist/vueCompiler.js.map +1 -1
- package/package.json +4 -1
- package/src/build.ts +13 -1
- package/src/buildHost.ts +23 -10
- package/src/compileComponent.ts +4 -1
- package/src/compileScript.ts +30 -11
- package/src/components.ts +5 -0
- package/src/detectProject.ts +13 -6
- package/src/devServer.ts +148 -23
- package/src/devStyles.ts +70 -0
- package/src/hot/client.ts +70 -0
- package/src/hot/moduleGraph.ts +90 -0
- package/src/hot/planUpdate.ts +66 -0
- package/src/hot/reactRefresh.ts +93 -0
- package/src/html.ts +13 -19
- package/src/index.ts +19 -1
- package/src/installer/tree.ts +32 -5
- package/src/installer/wasmBuilds.ts +60 -0
- package/src/node/builtins.ts +23 -12
- package/src/node/childProcess.ts +73 -20
- package/src/node/crypto.ts +5 -4
- package/src/node/esm.ts +26 -2
- package/src/node/fs/index.ts +26 -103
- package/src/node/fs/stdinFd.ts +44 -0
- package/src/node/fs/streams.ts +177 -0
- package/src/node/fs/sync.ts +15 -0
- package/src/node/globalObject.ts +82 -0
- package/src/node/http/index.ts +4 -3
- package/src/node/http/server.ts +8 -5
- package/src/node/http/serverResponse.ts +7 -0
- package/src/node/ipc.ts +91 -0
- package/src/node/loader.ts +78 -14
- package/src/node/misc.ts +19 -6
- package/src/node/process.ts +15 -8
- package/src/node/processExit.ts +21 -0
- package/src/node/program.ts +12 -9
- package/src/node/runtime.ts +27 -15
- package/src/node/stream/readable.ts +2 -1
- package/src/node/urlqs.ts +10 -2
- package/src/node/wasi/abi.ts +151 -0
- package/src/node/wasi/files.ts +350 -0
- package/src/node/wasi/index.ts +149 -0
- package/src/node/workerThreads.ts +198 -0
- package/src/node/zlib.ts +24 -2
- package/src/shell/context.ts +2 -2
- package/src/shell/devHandoff.ts +16 -9
- package/src/shell/exec.ts +6 -5
- package/src/shell/npm.ts +2 -2
- package/src/shell/program.ts +5 -5
- package/src/styleModules.ts +30 -14
- package/src/styles/compile.ts +43 -0
- package/src/styles/compilers.ts +143 -0
- package/src/styles/cssModules.ts +254 -0
- package/src/styles/imports.ts +125 -0
- package/src/styles/languages.ts +29 -0
- package/src/styles/packageStyles.ts +35 -0
- package/src/svelteCompiler.ts +20 -3
- package/src/system/commands.ts +17 -0
- package/src/system/index.ts +4 -0
- package/src/system/ipc.ts +89 -0
- package/src/system/kernel.ts +6 -3
- package/src/system/process.ts +57 -30
- package/src/system/terminal.ts +34 -12
- package/src/system/types.ts +31 -7
- package/src/vueCompiler.ts +11 -8
package/src/devStyles.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { CompileContext } from './compileScript.js';
|
|
2
|
+
import { mapSpecifier } from './compileScript.js';
|
|
3
|
+
import { dirname } from './paths.js';
|
|
4
|
+
import { resolvePackage } from './resolve.js';
|
|
5
|
+
import { cssToModule, injectStyleStatement, rewriteCssImports, rewriteCssUrls } from './styleModules.js';
|
|
6
|
+
import { compileStylesheet, type StylesheetContext } from './styles/compile.js';
|
|
7
|
+
import { cssModuleToModule, scopeCssModule } from './styles/cssModules.js';
|
|
8
|
+
import { lessCandidates, sassCandidates } from './styles/compilers.js';
|
|
9
|
+
import { isCssModule, styleLanguageOf } from './styles/languages.js';
|
|
10
|
+
import { linkStylesheetModule } from './styles/packageStyles.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* How the dev server serves stylesheets: compiled (Sass/Less), with `url()`s and
|
|
14
|
+
* `@import`s pointed where the preview can load them, as CSS (a `<link>`) or as
|
|
15
|
+
* the JS module an import from code becomes — a <style> tag, plus the scoped
|
|
16
|
+
* class map for a CSS Module.
|
|
17
|
+
*/
|
|
18
|
+
export interface DevStyleContext extends CompileContext {
|
|
19
|
+
styles: StylesheetContext;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface PreviewStyle {
|
|
23
|
+
body: string;
|
|
24
|
+
/** VFS files read, the stylesheet's partials included — the hot-update graph's edges. */
|
|
25
|
+
loaded: string[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const isUrl = (id: string) => /^https?:/i.test(id);
|
|
29
|
+
|
|
30
|
+
async function previewCssOf(ctx: DevStyleContext, path: string, source?: string): Promise<PreviewStyle> {
|
|
31
|
+
const compiled = await compileStylesheet(ctx.styles, path, source);
|
|
32
|
+
const packageUrl = (specifier: string) => {
|
|
33
|
+
const { name, subpath } = resolvePackage(ctx.config.packageAliases, specifier);
|
|
34
|
+
return ctx.cdn.urlFor(name, subpath);
|
|
35
|
+
};
|
|
36
|
+
const css = rewriteCssImports(ctx.fs, path, rewriteCssUrls(ctx.fs, path, compiled.css, ctx.base), ctx.base, packageUrl);
|
|
37
|
+
return { body: css, loaded: compiled.loaded.filter((id) => !isUrl(id)) };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** A stylesheet requested as CSS (a `<link>`, a CSS `@import`). */
|
|
41
|
+
export function previewCss(ctx: DevStyleContext, path: string): Promise<PreviewStyle> {
|
|
42
|
+
return previewCssOf(ctx, path);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** A stylesheet imported from JS: a module injecting it, exporting the class map for a CSS Module. */
|
|
46
|
+
export async function stylesheetModule(ctx: DevStyleContext, path: string): Promise<PreviewStyle> {
|
|
47
|
+
const { body: css, loaded } = await previewCssOf(ctx, path);
|
|
48
|
+
if (!isCssModule(path)) return { body: cssToModule(path, css), loaded };
|
|
49
|
+
const scoped = scopeCssModule(css, path);
|
|
50
|
+
const body = cssModuleToModule(scoped, path, (expression) => injectStyleStatement(path, expression), (from) => mapSpecifier(ctx, dirname(path), from));
|
|
51
|
+
return { body, loaded };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* `import 'bootstrap/dist/css/bootstrap.css'`: a plain package stylesheet is
|
|
56
|
+
* linked from the package CDN; a Sass/Less one (`bootstrap/scss/bootstrap.scss`)
|
|
57
|
+
* is found the way its own imports are (installed, else the CDN) and compiled.
|
|
58
|
+
*/
|
|
59
|
+
export async function packageStylesheetModule(ctx: DevStyleContext, specifier: string): Promise<PreviewStyle> {
|
|
60
|
+
const { name, subpath } = resolvePackage(ctx.config.packageAliases, specifier);
|
|
61
|
+
const language = styleLanguageOf(subpath);
|
|
62
|
+
if (language === 'css') return { body: linkStylesheetModule(specifier, ctx.cdn.urlFor(name, subpath)), loaded: [] };
|
|
63
|
+
const id = await ctx.styles.imports.resolve(`~${name}${subpath}`, undefined, language === 'less' ? lessCandidates : sassCandidates);
|
|
64
|
+
if (!id) throw new Error(`Cannot find the stylesheet ${specifier}`);
|
|
65
|
+
const file = await ctx.styles.imports.load(id);
|
|
66
|
+
const compiled = await compileStylesheet(ctx.styles, id, file.contents);
|
|
67
|
+
// In a <style> tag a relative `url()` would resolve against the page; anchor it to the file.
|
|
68
|
+
const css = isUrl(id) ? compiled.css.replace(/url\(\s*(['"]?)(?!data:|https?:|\/|#)([^'")]+)\1\s*\)/g, (_m, quote: string, ref: string) => `url(${quote}${new URL(ref, id).href}${quote})`) : rewriteCssUrls(ctx.fs, id, compiled.css, ctx.base);
|
|
69
|
+
return { body: cssToModule(`/${specifier}`, css), loaded: compiled.loaded.filter((loaded) => !isUrl(loaded)) };
|
|
70
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { REFRESH_GLOBAL } from './reactRefresh.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What the host tells open previews after an edit. Old pages reload on any
|
|
5
|
+
* message, and new pages treat anything that is not an `update` as a reload,
|
|
6
|
+
* so a host and a preview from different releases still agree.
|
|
7
|
+
*/
|
|
8
|
+
export type PreviewUpdate = { type: 'reload' } | HotUpdate;
|
|
9
|
+
|
|
10
|
+
export interface HotUpdate {
|
|
11
|
+
type: 'update';
|
|
12
|
+
/** Stylesheets to swap without reloading. */
|
|
13
|
+
styles: HotStyle[];
|
|
14
|
+
/** Module URLs (fresh stamps) to re-run, then let React Refresh re-render. */
|
|
15
|
+
modules: string[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface HotStyle {
|
|
19
|
+
/** The stylesheet's URL path, as a `<link href>` names it. */
|
|
20
|
+
path: string;
|
|
21
|
+
/** Its JS module URL with a fresh stamp, for a stylesheet imported from JS. */
|
|
22
|
+
url: string;
|
|
23
|
+
stamp: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** The BroadcastChannel previews of `base` listen on — keyed, so two runtimes never cross. */
|
|
27
|
+
export function updateChannelName(base: string): string {
|
|
28
|
+
return `bfwc:reload:${base}`;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Runs in every preview page. A stylesheet `<link>` gets a cache-busted `href`;
|
|
33
|
+
* one imported from JS is re-imported, which replaces its <style> tag. Changed
|
|
34
|
+
* modules are re-imported in order, then React Refresh re-renders what they
|
|
35
|
+
* define. An update that fails to apply (a syntax error mid-edit) is reported
|
|
36
|
+
* as an uncaught error and leaves the page as it was; the next edit applies on top.
|
|
37
|
+
*/
|
|
38
|
+
export function hotUpdateScript(base: string): string {
|
|
39
|
+
return `<script>(function(){
|
|
40
|
+
if (typeof BroadcastChannel === 'undefined') return;
|
|
41
|
+
var pending = Promise.resolve();
|
|
42
|
+
function swapStyle(style) {
|
|
43
|
+
var swapped = false;
|
|
44
|
+
document.querySelectorAll('link[rel="stylesheet"]').forEach(function (link) {
|
|
45
|
+
var url = new URL(link.href, location.href);
|
|
46
|
+
if (url.pathname !== style.path) return;
|
|
47
|
+
url.searchParams.set('t', String(style.stamp));
|
|
48
|
+
link.href = url.href;
|
|
49
|
+
swapped = true;
|
|
50
|
+
});
|
|
51
|
+
return swapped ? undefined : import(style.url);
|
|
52
|
+
}
|
|
53
|
+
function apply(update) {
|
|
54
|
+
return Promise.all(update.styles.map(swapStyle)).then(function () {
|
|
55
|
+
if (!update.modules.length) return;
|
|
56
|
+
var refresh = window[${JSON.stringify(REFRESH_GLOBAL)}];
|
|
57
|
+
if (!refresh) return location.reload();
|
|
58
|
+
return update.modules.reduce(function (chain, url) { return chain.then(function () { return import(url); }); }, Promise.resolve())
|
|
59
|
+
.then(function () { refresh.performReactRefresh(); });
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
new BroadcastChannel(${JSON.stringify(updateChannelName(base))}).onmessage = function (event) {
|
|
63
|
+
var update = event.data;
|
|
64
|
+
if (!update || update.type !== 'update') return location.reload();
|
|
65
|
+
pending = pending.then(function () { return apply(update); }).catch(function (error) {
|
|
66
|
+
setTimeout(function () { throw error; });
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
})();</script>`;
|
|
70
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the open preview has loaded, and what depends on what — the graph a hot
|
|
3
|
+
* update walks. Nodes are VFS paths. An edge `a → b` means serving `a` read `b`:
|
|
4
|
+
* a module's local imports, or the partials a Sass/Less stylesheet pulled in.
|
|
5
|
+
*
|
|
6
|
+
* Each module may carry a STAMP: bumped when a hot update re-runs it, and
|
|
7
|
+
* written into the import URLs that point at it (`?t=3`), so the browser — whose
|
|
8
|
+
* module map is keyed by URL — fetches a fresh copy instead of the one it holds.
|
|
9
|
+
*/
|
|
10
|
+
export interface ModuleFacts {
|
|
11
|
+
/** Every export is a React component, so React Refresh can swap it in place. */
|
|
12
|
+
refreshBoundary: boolean;
|
|
13
|
+
/** The hooks the module calls, in order; a change means component state cannot survive. */
|
|
14
|
+
hooks: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export class ModuleGraph {
|
|
18
|
+
private readonly importsOf = new Map<string, Set<string>>();
|
|
19
|
+
private readonly importersOf = new Map<string, Set<string>>();
|
|
20
|
+
private readonly stamps = new Map<string, number>();
|
|
21
|
+
private readonly facts = new Map<string, ModuleFacts>();
|
|
22
|
+
private readonly served = new Set<string>();
|
|
23
|
+
private clock = 0;
|
|
24
|
+
|
|
25
|
+
/** Replace what `module` reads; called each time it is compiled. */
|
|
26
|
+
setDependencies(module: string, dependencies: Iterable<string>): void {
|
|
27
|
+
for (const old of this.importsOf.get(module) ?? []) this.importersOf.get(old)?.delete(module);
|
|
28
|
+
const next = new Set([...dependencies].filter((dependency) => dependency !== module));
|
|
29
|
+
this.importsOf.set(module, next);
|
|
30
|
+
for (const dependency of next) {
|
|
31
|
+
let importers = this.importersOf.get(dependency);
|
|
32
|
+
if (!importers) this.importersOf.set(dependency, (importers = new Set()));
|
|
33
|
+
importers.add(module);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
importers(path: string): ReadonlySet<string> {
|
|
38
|
+
return this.importersOf.get(path) ?? new Set();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Everything that reads `path`, directly or through others (`path` itself excluded). */
|
|
42
|
+
dependents(path: string): Set<string> {
|
|
43
|
+
const found = new Set<string>();
|
|
44
|
+
const queue = [...this.importers(path)];
|
|
45
|
+
while (queue.length) {
|
|
46
|
+
const next = queue.pop()!;
|
|
47
|
+
if (found.has(next) || next === path) continue;
|
|
48
|
+
found.add(next);
|
|
49
|
+
queue.push(...this.importers(next));
|
|
50
|
+
}
|
|
51
|
+
return found;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
markServed(path: string): void {
|
|
55
|
+
this.served.add(path);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The preview requested `path` itself (a module, a stylesheet, an image). */
|
|
59
|
+
isServed(path: string): boolean {
|
|
60
|
+
return this.served.has(path);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
setFacts(path: string, facts: ModuleFacts): void {
|
|
64
|
+
this.facts.set(path, facts);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
factsOf(path: string): ModuleFacts | undefined {
|
|
68
|
+
return this.facts.get(path);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
stampOf(path: string): number | undefined {
|
|
72
|
+
return this.stamps.get(path);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Give every path in `paths` one new stamp, and return it. */
|
|
76
|
+
bump(paths: Iterable<string>): number {
|
|
77
|
+
const stamp = ++this.clock;
|
|
78
|
+
for (const path of paths) this.stamps.set(path, stamp);
|
|
79
|
+
return stamp;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Forget everything: the page is reloading and will request it all again. Stamps keep rising. */
|
|
83
|
+
clear(): void {
|
|
84
|
+
this.importsOf.clear();
|
|
85
|
+
this.importersOf.clear();
|
|
86
|
+
this.facts.clear();
|
|
87
|
+
this.served.clear();
|
|
88
|
+
this.stamps.clear();
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { ModuleGraph } from './moduleGraph.js';
|
|
2
|
+
import { isCssModule, isStylesheet } from '../styles/languages.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Decide what an edit means for the open preview, from the module graph:
|
|
6
|
+
*
|
|
7
|
+
* • a plain stylesheet (or a partial one pulls in) is swapped in place;
|
|
8
|
+
* • a module is re-run at the nearest refresh BOUNDARIES above it — the
|
|
9
|
+
* changed module itself when it only exports components, else the modules
|
|
10
|
+
* importing it, and so on — when React Refresh is on;
|
|
11
|
+
* • reaching a module nothing imports (the entry) without a boundary, or a
|
|
12
|
+
* component whose hooks changed, means a full reload;
|
|
13
|
+
* • a file the page never loaded changes nothing.
|
|
14
|
+
*
|
|
15
|
+
* `touched` is every module the update re-runs or passes through: each needs a
|
|
16
|
+
* fresh stamp, so the re-run imports fresh copies all the way down.
|
|
17
|
+
*/
|
|
18
|
+
export type UpdatePlan = { reload: true } | { reload: false; styles: string[]; boundaries: string[]; touched: string[] };
|
|
19
|
+
|
|
20
|
+
export interface PlanInput {
|
|
21
|
+
changed: readonly string[];
|
|
22
|
+
graph: ModuleGraph;
|
|
23
|
+
/** React Refresh is available in this preview. */
|
|
24
|
+
refresh: boolean;
|
|
25
|
+
/** The module's hook calls differ from the ones the preview is running. */
|
|
26
|
+
hooksChanged(path: string): boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** A stylesheet whose edit is only a style swap: not a CSS Module, whose class map is JS. */
|
|
30
|
+
const isPlainStylesheet = (path: string) => isStylesheet(path) && !isCssModule(path);
|
|
31
|
+
|
|
32
|
+
export function planUpdate({ changed, graph, refresh, hooksChanged }: PlanInput): UpdatePlan {
|
|
33
|
+
const styles = new Set<string>();
|
|
34
|
+
const boundaries = new Set<string>();
|
|
35
|
+
const touched = new Set<string>();
|
|
36
|
+
const seen = new Set<string>();
|
|
37
|
+
|
|
38
|
+
for (const start of changed) {
|
|
39
|
+
if (!graph.isServed(start) && graph.importers(start).size === 0) continue;
|
|
40
|
+
const queue = [start];
|
|
41
|
+
while (queue.length) {
|
|
42
|
+
const path = queue.shift()!;
|
|
43
|
+
if (seen.has(path)) continue;
|
|
44
|
+
seen.add(path);
|
|
45
|
+
touched.add(path);
|
|
46
|
+
|
|
47
|
+
if (isPlainStylesheet(path)) {
|
|
48
|
+
if (graph.isServed(path)) styles.add(path);
|
|
49
|
+
// A partial's edit reaches the stylesheets that pulled it in — never the JS
|
|
50
|
+
// importing a stylesheet, which a swap leaves untouched.
|
|
51
|
+
for (const importer of graph.importers(path)) if (isStylesheet(importer)) queue.push(importer);
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (refresh && hooksChanged(path)) return { reload: true };
|
|
56
|
+
if (refresh && graph.factsOf(path)?.refreshBoundary) {
|
|
57
|
+
boundaries.add(path);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
const importers = graph.importers(path);
|
|
61
|
+
if (importers.size === 0) return { reload: true };
|
|
62
|
+
queue.push(...importers);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return { reload: false, styles: [...styles], boundaries: [...boundaries], touched: [...touched] };
|
|
66
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { init, parse } from 'es-module-lexer';
|
|
2
|
+
import { contentHash } from '../hash.js';
|
|
3
|
+
import type { ProjectConfig } from '../projectConfig.js';
|
|
4
|
+
import type { ModuleFacts } from './moduleGraph.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* React Fast Refresh without Babel. The official runtime (`react-refresh/runtime`)
|
|
8
|
+
* does the hard part — swapping a component's implementation while React keeps
|
|
9
|
+
* its state — and only needs two things from the compiler:
|
|
10
|
+
*
|
|
11
|
+
* • each component REGISTERED under a stable id when its module runs, which
|
|
12
|
+
* the dev server appends to compiled modules (`refreshFooter`);
|
|
13
|
+
* • to know when a component's hooks changed, because state cannot carry
|
|
14
|
+
* across a different hook sequence. Babel's plugin signs each component; here
|
|
15
|
+
* the module's hook calls are signed as a whole (`hookSignature`), and a
|
|
16
|
+
* change makes the update a full reload rather than a refresh.
|
|
17
|
+
*
|
|
18
|
+
* A module is a refresh BOUNDARY when every export is a registered component:
|
|
19
|
+
* re-running it is all an update needs. Edits to anything else propagate to the
|
|
20
|
+
* modules importing it until they reach boundaries, or the entry (a reload).
|
|
21
|
+
*/
|
|
22
|
+
export const REFRESH_GLOBAL = '__bfwc_refresh';
|
|
23
|
+
|
|
24
|
+
/** React with a DOM renderer and the automatic or classic React JSX runtime. */
|
|
25
|
+
export function usesReactRefresh(config: ProjectConfig): boolean {
|
|
26
|
+
return 'react-dom' in config.dependencies && config.jsx.importSource === 'react';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Inline module script, placed before the app's: installs the runtime into React's
|
|
31
|
+
* DevTools hook, which react-dom reads once, when it first loads — so it must
|
|
32
|
+
* run first. Module scripts run in document order, which guarantees that.
|
|
33
|
+
*/
|
|
34
|
+
export function refreshPreambleScript(runtimeUrl: string): string {
|
|
35
|
+
return [
|
|
36
|
+
`<script type="module">`,
|
|
37
|
+
`import * as runtime from ${JSON.stringify(runtimeUrl)};`,
|
|
38
|
+
`const refresh = runtime.default || runtime;`,
|
|
39
|
+
`refresh.injectIntoGlobalHook(window);`,
|
|
40
|
+
`window.$RefreshReg$ = () => {};`,
|
|
41
|
+
`window.$RefreshSig$ = () => (type) => type;`,
|
|
42
|
+
`window.${REFRESH_GLOBAL} = refresh;`,
|
|
43
|
+
`</script>`,
|
|
44
|
+
].join('\n');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Top-level bindings in compiled code that look like components: PascalCase
|
|
49
|
+
* function and class declarations, and PascalCase consts bound to a function,
|
|
50
|
+
* arrow, `memo(…)` or `forwardRef(…)`. Compiled output puts top-level statements
|
|
51
|
+
* at column 0, which keeps this to line-anchored patterns.
|
|
52
|
+
*/
|
|
53
|
+
export function componentNames(code: string): string[] {
|
|
54
|
+
const names = new Set<string>();
|
|
55
|
+
const declaration = /^(?:export\s+(?:default\s+)?)?(?:async\s+)?(?:function\*?|class)\s+([A-Z][\w$]*)/gm;
|
|
56
|
+
const binding = /^(?:export\s+)?(?:const|let|var)\s+([A-Z][\w$]*)\s*(?::[^=]+)?=\s*(?:async\s+)?(?:function\b|\(|[\w$]+\s*=>|(?:[\w$]+\.)?(?:memo|forwardRef)\s*\()/gm;
|
|
57
|
+
for (const match of code.matchAll(declaration)) names.add(match[1]!);
|
|
58
|
+
for (const match of code.matchAll(binding)) names.add(match[1]!);
|
|
59
|
+
return [...names];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Appended to a module: register each component under `<path> <name>`. */
|
|
63
|
+
export function refreshFooter(path: string, names: readonly string[]): string {
|
|
64
|
+
if (!names.length) return '';
|
|
65
|
+
const lines = names.map((name) => ` refresh.register(${name}, ${JSON.stringify(`${path} ${name}`)});`);
|
|
66
|
+
return `\n{ const refresh = window.${REFRESH_GLOBAL};\nif (refresh) {\n${lines.join('\n')}\n} }\n`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** The module's hook calls in order, hashed. */
|
|
70
|
+
export function hookSignature(source: string): string {
|
|
71
|
+
return contentHash((source.match(/\buse[A-Z][\w$]*\s*\(/g) ?? []).map((call) => call.replace(/\s*\($/, '')).join(','));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* What the dev server learns about a compiled module: whether it is a refresh
|
|
76
|
+
* boundary (it exports something, and every export is one of `components`),
|
|
77
|
+
* and its hook signature (from `source`, the code as written).
|
|
78
|
+
*/
|
|
79
|
+
export async function refreshFacts(code: string, source: string, components: readonly string[]): Promise<ModuleFacts> {
|
|
80
|
+
await init;
|
|
81
|
+
const [, exports] = parse(code);
|
|
82
|
+
const known = new Set(components);
|
|
83
|
+
// The lexer names the local behind most exports; spell out the default forms it may not.
|
|
84
|
+
const defaultExpression =
|
|
85
|
+
/^export\s+default\s+(?:async\s+)?(?:function\*?|class)\s+([A-Z][\w$]*)/m.exec(code)?.[1] ?? /^export\s+default\s+([A-Z][\w$]*)\s*;?\s*$/m.exec(code)?.[1];
|
|
86
|
+
const refreshBoundary =
|
|
87
|
+
exports.length > 0 &&
|
|
88
|
+
exports.every((entry) => {
|
|
89
|
+
const local = entry.ln ?? (entry.n === 'default' ? defaultExpression : entry.n);
|
|
90
|
+
return local !== undefined && known.has(local);
|
|
91
|
+
});
|
|
92
|
+
return { refreshBoundary, hooks: hookSignature(source) };
|
|
93
|
+
}
|
package/src/html.ts
CHANGED
|
@@ -7,11 +7,14 @@
|
|
|
7
7
|
* • the error bridge is prepended, so runtime errors and console output reach the host;
|
|
8
8
|
* • the WebSocket bridge follows, so `new WebSocket('ws://localhost:3000')` reaches a virtual server;
|
|
9
9
|
* • the host's preview scripts run next, before the app;
|
|
10
|
-
* • the
|
|
10
|
+
* • the hot-update listener is prepended, so an edit swaps styles, re-renders
|
|
11
|
+
* components or reloads — and, for React, the Fast Refresh runtime before the app;
|
|
11
12
|
* • the attribution badge is appended unless the host turned it off.
|
|
12
13
|
*/
|
|
13
14
|
import { injectAttribution } from './attribution.js';
|
|
14
15
|
import { previewSocketScript } from './previewBridge.js';
|
|
16
|
+
import { hotUpdateScript } from './hot/client.js';
|
|
17
|
+
import { refreshPreambleScript } from './hot/reactRefresh.js';
|
|
15
18
|
|
|
16
19
|
export const ERROR_MESSAGE_TYPE = 'bfwc:error';
|
|
17
20
|
export const CONSOLE_MESSAGE_TYPE = 'bfwc:console';
|
|
@@ -52,23 +55,6 @@ export function errorBridgeScript(): string {
|
|
|
52
55
|
})();</script>`;
|
|
53
56
|
}
|
|
54
57
|
|
|
55
|
-
/**
|
|
56
|
-
* The channel a preview listens on for "files changed, reload". Keyed by the
|
|
57
|
-
* preview base, so two runtimes on one origin never reload each other's frames.
|
|
58
|
-
* A BroadcastChannel rather than `postMessage` because the host does not hold a
|
|
59
|
-
* reference to the frame — any number of frames (or tabs) may show one preview.
|
|
60
|
-
*/
|
|
61
|
-
export function reloadChannelName(base: string): string {
|
|
62
|
-
return `bfwc:reload:${base}`;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export function liveReloadScript(base: string): string {
|
|
66
|
-
return `<script>(function(){
|
|
67
|
-
if (typeof BroadcastChannel === 'undefined') return;
|
|
68
|
-
new BroadcastChannel(${JSON.stringify(reloadChannelName(base))}).onmessage = function () { location.reload(); };
|
|
69
|
-
})();</script>`;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
58
|
const ABSOLUTE_ATTR = /(\s(?:src|href)\s*=\s*)(["'])\/(?!\/)([^"']*)\2/gi;
|
|
73
59
|
const MODULE_SCRIPT = /<script\b[^>]*type\s*=\s*["']module["'][^>]*>/i;
|
|
74
60
|
|
|
@@ -81,6 +67,8 @@ export interface HtmlOptions {
|
|
|
81
67
|
attribution?: boolean;
|
|
82
68
|
/** Host scripts (JavaScript source) run in every preview page before the app. */
|
|
83
69
|
scripts?: readonly string[];
|
|
70
|
+
/** URL of `react-refresh/runtime`; installed before the app so components re-render in place on edits. */
|
|
71
|
+
refreshRuntime?: string;
|
|
84
72
|
}
|
|
85
73
|
|
|
86
74
|
/** A host script as a tag. `</script>` inside the body would end the tag early, so it is escaped. */
|
|
@@ -99,7 +87,13 @@ export function transformHtml(html: string, options: HtmlOptions): string {
|
|
|
99
87
|
}
|
|
100
88
|
if (options.attribution !== false) out = injectAttribution(out);
|
|
101
89
|
|
|
102
|
-
const bridge = [
|
|
90
|
+
const bridge = [
|
|
91
|
+
errorBridgeScript(),
|
|
92
|
+
previewSocketScript(),
|
|
93
|
+
hotUpdateScript(base),
|
|
94
|
+
...(options.scripts ?? []).map(scriptTag),
|
|
95
|
+
...(options.refreshRuntime ? [refreshPreambleScript(options.refreshRuntime)] : []),
|
|
96
|
+
].join('\n');
|
|
103
97
|
if (/<head[^>]*>/i.test(out)) return out.replace(/<head[^>]*>/i, (head) => `${head}\n${bridge}`);
|
|
104
98
|
return bridge + out;
|
|
105
99
|
}
|
package/src/index.ts
CHANGED
|
@@ -44,7 +44,25 @@ export { rewriteImports } from './rewriteImports.js';
|
|
|
44
44
|
export { resolveLocal, resolveAlias, isBareSpecifier, splitPackageSpecifier, resolvePackage, RESOLVE_EXTENSIONS } from './resolve.js';
|
|
45
45
|
export { compileScript, compileModule, isScriptPath, mapSpecifier, moduleUrl, buildDefine } from './compileScript.js';
|
|
46
46
|
export { compileComponent } from './compileComponent.js';
|
|
47
|
-
export { transformHtml, scriptTag, errorBridgeScript,
|
|
47
|
+
export { transformHtml, scriptTag, errorBridgeScript, ERROR_MESSAGE_TYPE, CONSOLE_MESSAGE_TYPE, CONSOLE_MESSAGE_LIMIT } from './html.js';
|
|
48
|
+
export { hotUpdateScript, updateChannelName } from './hot/client.js';
|
|
49
|
+
export type { PreviewUpdate, HotUpdate, HotStyle } from './hot/client.js';
|
|
50
|
+
export { ModuleGraph } from './hot/moduleGraph.js';
|
|
51
|
+
export type { ModuleFacts } from './hot/moduleGraph.js';
|
|
52
|
+
export { planUpdate } from './hot/planUpdate.js';
|
|
53
|
+
export type { UpdatePlan, PlanInput } from './hot/planUpdate.js';
|
|
54
|
+
export { usesReactRefresh, refreshPreambleScript, componentNames, refreshFooter, hookSignature, refreshFacts, REFRESH_GLOBAL } from './hot/reactRefresh.js';
|
|
55
|
+
export { styleLanguageOf, isStylesheet, isCssModule, styleLanguageOfLang } from './styles/languages.js';
|
|
56
|
+
export type { StyleLanguage } from './styles/languages.js';
|
|
57
|
+
export { createSassCompiler, createLessCompiler, staticStyleCompilers, noStyleCompilers, sassCandidates, lessCandidates } from './styles/compilers.js';
|
|
58
|
+
export type { StyleCompiler, StyleCompilers, StyleCompileRequest, CompiledStyle, PreprocessedLanguage, SassLike, LessLike } from './styles/compilers.js';
|
|
59
|
+
export { createStyleImportHost, jsDelivrFileUrl } from './styles/imports.js';
|
|
60
|
+
export type { StyleImportHost, StyleImportHostOptions, StyleSource } from './styles/imports.js';
|
|
61
|
+
export { compileStylesheet, preprocessStyleBlock } from './styles/compile.js';
|
|
62
|
+
export type { StylesheetContext } from './styles/compile.js';
|
|
63
|
+
export { scopeCssModule, cssModuleToModule } from './styles/cssModules.js';
|
|
64
|
+
export type { ScopedCssModule, ComposedClass } from './styles/cssModules.js';
|
|
65
|
+
export { PACKAGE_STYLE_DIR, packageStylePath, packageStyleSpecifier, linkStylesheetModule } from './styles/packageStyles.js';
|
|
48
66
|
export { previewSocketScript, previewCookieScript, bridgeInsertionPoint, SOCKET_MESSAGE_TYPE, COOKIE_MESSAGE_TYPE, SOCKET_CONNECT_TIMEOUT_MS } from './previewBridge.js';
|
|
49
67
|
export { mimeFor } from './mime.js';
|
|
50
68
|
export { attributionScript, injectAttribution, ATTRIBUTION_URL, ATTRIBUTION_LABEL } from './attribution.js';
|
package/src/installer/tree.ts
CHANGED
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
import type { PackageManifest } from './registry.js';
|
|
14
14
|
import { parseDependency, type PackageJson, type ParsedSpec } from './spec.js';
|
|
15
15
|
import { satisfies, validRange } from './semver.js';
|
|
16
|
+
import { napiWasmSiblings, wasmBuildOf } from './wasmBuilds.js';
|
|
16
17
|
|
|
17
18
|
export type EdgeType = 'prod' | 'dev' | 'optional' | 'peer';
|
|
18
19
|
|
|
@@ -31,6 +32,10 @@ interface Dependency {
|
|
|
31
32
|
name: string;
|
|
32
33
|
range: string;
|
|
33
34
|
type: EdgeType;
|
|
35
|
+
/** Added by this installer (a napi-rs wasm binding), not declared: missing is silent. */
|
|
36
|
+
implied?: boolean;
|
|
37
|
+
/** Installed only when this preferred package did not resolve (the threaded wasm binding stands in for the single-threaded one). */
|
|
38
|
+
fallbackFor?: string;
|
|
34
39
|
}
|
|
35
40
|
|
|
36
41
|
interface Edge extends Dependency {
|
|
@@ -90,12 +95,16 @@ function bundledNames(m: { bundleDependencies?: string[] | boolean; bundledDepen
|
|
|
90
95
|
|
|
91
96
|
function dependenciesOf(m: PackageJson | PackageManifest, isRoot: boolean, production: boolean): Dependency[] {
|
|
92
97
|
const out = new Map<string, Dependency>();
|
|
93
|
-
const add = (record: Record<string, string> | undefined, type: EdgeType) => {
|
|
94
|
-
for (const [name, range] of Object.entries(record ?? {})) if (!out.has(name) || type === 'optional') out.set(name, { name, range, type });
|
|
98
|
+
const add = (record: Record<string, string> | undefined, type: EdgeType, implied?: boolean) => {
|
|
99
|
+
for (const [name, range] of Object.entries(record ?? {})) if (!out.has(name) || type === 'optional') out.set(name, { name, range, type, implied });
|
|
95
100
|
};
|
|
96
101
|
if (isRoot && !production) add((m as PackageJson).devDependencies, 'dev');
|
|
97
102
|
add(m.dependencies, 'prod');
|
|
98
103
|
add(m.optionalDependencies, 'optional');
|
|
104
|
+
for (const sibling of napiWasmSiblings(m.optionalDependencies)) {
|
|
105
|
+
const listed = out.get(sibling.name);
|
|
106
|
+
out.set(sibling.name, listed ? { ...listed, fallbackFor: sibling.fallbackFor } : { name: sibling.name, range: sibling.range, type: 'optional', implied: true, fallbackFor: sibling.fallbackFor });
|
|
107
|
+
}
|
|
99
108
|
if (!isRoot) {
|
|
100
109
|
const meta = (m as PackageManifest).peerDependenciesMeta ?? {};
|
|
101
110
|
for (const [name, range] of Object.entries(m.peerDependencies ?? {})) {
|
|
@@ -103,7 +112,21 @@ function dependenciesOf(m: PackageJson | PackageManifest, isRoot: boolean, produ
|
|
|
103
112
|
}
|
|
104
113
|
}
|
|
105
114
|
const bundled = bundledNames(m as PackageManifest);
|
|
106
|
-
|
|
115
|
+
// A fallback resolves after the flavor it stands in for, so it can see whether that one landed.
|
|
116
|
+
return [...out.values()].filter((d) => !bundled.has(d.name)).sort((a, b) => Number(!!a.fallbackFor) - Number(!!b.fallbackFor) || (a.name < b.name ? -1 : 1));
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** A native-binary package's wasm twin when one fits the range (`wasmBuilds.ts`), else the package itself. */
|
|
120
|
+
async function pickPreferringWasm(options: ResolveTreeOptions, spec: ParsedSpec): Promise<PackageManifest> {
|
|
121
|
+
const twin = wasmBuildOf(spec);
|
|
122
|
+
if (twin) {
|
|
123
|
+
try {
|
|
124
|
+
return await options.pick(twin);
|
|
125
|
+
} catch {
|
|
126
|
+
// no twin release for this range: the original package, as written
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return options.pick(spec);
|
|
107
130
|
}
|
|
108
131
|
|
|
109
132
|
/** Does `version` satisfy the edge's range? A dist-tag never "matches" without a pick. */
|
|
@@ -165,6 +188,10 @@ export async function resolveTree(options: ResolveTreeOptions): Promise<Map<stri
|
|
|
165
188
|
for (const dep of deps) {
|
|
166
189
|
const spec = specs.get(dep.name);
|
|
167
190
|
if (!spec) continue;
|
|
191
|
+
if (dep.fallbackFor) {
|
|
192
|
+
const preferred = lookup(location, dep.fallbackFor);
|
|
193
|
+
if (preferred && matches(nodes.get(preferred)!.manifest.version, specs.get(dep.fallbackFor)?.range ?? spec.range)) continue;
|
|
194
|
+
}
|
|
168
195
|
const from = dep.type === 'peer' ? parentLocation(location) : location;
|
|
169
196
|
const existing = lookup(from, dep.name);
|
|
170
197
|
const edge = { ...dep, range: spec.range, owner: location, from };
|
|
@@ -180,10 +207,10 @@ export async function resolveTree(options: ResolveTreeOptions): Promise<Map<stri
|
|
|
180
207
|
|
|
181
208
|
let manifest: PackageManifest;
|
|
182
209
|
try {
|
|
183
|
-
manifest = await options
|
|
210
|
+
manifest = await pickPreferringWasm(options, spec);
|
|
184
211
|
} catch (error) {
|
|
185
212
|
if (dep.type !== 'optional') throw error;
|
|
186
|
-
options.warn(`skipped optional ${dep.name}: ${(error as Error).message}`);
|
|
213
|
+
if (!dep.implied) options.warn(`skipped optional ${dep.name}: ${(error as Error).message}`);
|
|
187
214
|
continue;
|
|
188
215
|
}
|
|
189
216
|
if (!fitsPlatform(manifest)) {
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Native binaries → the same projects' own WebAssembly builds, in two shapes:
|
|
3
|
+
*
|
|
4
|
+
* - A twin package (`WASM_BUILDS`), installed under the original name: code that
|
|
5
|
+
* requires `esbuild` gets `esbuild-wasm` at the same version, with the same
|
|
6
|
+
* JavaScript API. Vite, Nuxt, SvelteKit, tsx and tsup all reach for these.
|
|
7
|
+
* Data, not branches: another tool with an official wasm twin is one more row.
|
|
8
|
+
* - A napi-rs WebAssembly binding (`napiWasmSiblings`), added beside the
|
|
9
|
+
* per-platform ones the package lists.
|
|
10
|
+
*/
|
|
11
|
+
import type { ParsedSpec } from './spec.js';
|
|
12
|
+
|
|
13
|
+
const WASM_BUILDS: Readonly<Record<string, string>> = {
|
|
14
|
+
esbuild: 'esbuild-wasm',
|
|
15
|
+
rollup: '@rollup/wasm-node',
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/** A napi-rs per-platform binding name: `@oxc-parser/binding-linux-x64-gnu`, `lightningcss-darwin-arm64`. */
|
|
19
|
+
const NAPI_PLATFORM = /-(?:darwin|linux|win32|android|freebsd|openharmony|sunos|aix)-[a-z0-9_-]+$/;
|
|
20
|
+
|
|
21
|
+
/** WebAssembly binding flavors, preferred first: `wasip1` is single-threaded (no shared memory, so no cross-origin isolation); `wasi` needs threads. */
|
|
22
|
+
const NAPI_WASM_FLAVORS = ['-wasm32-wasip1', '-wasm32-wasi'] as const;
|
|
23
|
+
|
|
24
|
+
export interface NapiWasmSibling {
|
|
25
|
+
name: string;
|
|
26
|
+
range: string;
|
|
27
|
+
/** Install only when this preferred flavor did not resolve. */
|
|
28
|
+
fallbackFor?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* napi-rs packages list a binding per platform as optional dependencies, none of
|
|
33
|
+
* which fits here; most also publish WebAssembly siblings they look for next but do
|
|
34
|
+
* not list (oxc-parser, rolldown, @tailwindcss/oxide). This names them, at the
|
|
35
|
+
* bindings' range, as optional dependencies: the single-threaded `-wasm32-wasip1`
|
|
36
|
+
* first, the threaded `-wasm32-wasi` only if that one is not published (a registry
|
|
37
|
+
* miss is skipped like any optional one). Nothing when the package lists the single-threaded one itself; one that lists only the threaded one gets the single-threaded one added, and its own entry becomes the fallback.
|
|
38
|
+
*/
|
|
39
|
+
export function napiWasmSiblings(optional: Readonly<Record<string, string>> | undefined): NapiWasmSibling[] {
|
|
40
|
+
const bindings = Object.entries(optional ?? {}).filter(([name]) => NAPI_PLATFORM.test(name));
|
|
41
|
+
if (bindings.length < 2) return [];
|
|
42
|
+
const [first, range] = bindings[0]!;
|
|
43
|
+
const base = first.replace(NAPI_PLATFORM, '');
|
|
44
|
+
if (bindings.some(([name]) => name.replace(NAPI_PLATFORM, '') !== base)) return [];
|
|
45
|
+
const [preferred, fallback] = NAPI_WASM_FLAVORS.map((flavor) => base + flavor) as [string, string];
|
|
46
|
+
if (optional![preferred]) return [];
|
|
47
|
+
// Listing only the threaded flavor still gets the single-threaded one added; the listed entry becomes its fallback.
|
|
48
|
+
return [{ name: preferred, range }, { name: fallback, range, fallbackFor: preferred }];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The wasm twin of `spec`, when it has one and was asked for by its own name
|
|
53
|
+
* (an explicit `npm:` alias is left alone). The caller falls back to `spec` when
|
|
54
|
+
* the twin has no matching version: Rollup 3 is plain JavaScript and has no
|
|
55
|
+
* `@rollup/wasm-node` release, and needs no substitute.
|
|
56
|
+
*/
|
|
57
|
+
export function wasmBuildOf(spec: ParsedSpec): ParsedSpec | undefined {
|
|
58
|
+
const twin = spec.registryName === spec.name ? WASM_BUILDS[spec.name] : undefined;
|
|
59
|
+
return twin ? { ...spec, registryName: twin } : undefined;
|
|
60
|
+
}
|