@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/dist/vueCompiler.js
CHANGED
|
@@ -6,15 +6,18 @@ function fail(path, problems) {
|
|
|
6
6
|
}
|
|
7
7
|
export function createVueCompiler(sfc) {
|
|
8
8
|
return {
|
|
9
|
-
compile(source, { path, dev }) {
|
|
9
|
+
async compile(source, { path, dev, preprocessStyle }) {
|
|
10
10
|
const { descriptor, errors } = sfc.parse(source, { filename: path, sourceMap: false });
|
|
11
11
|
if (errors.length)
|
|
12
12
|
fail(path, errors);
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
// Sass/Less blocks become CSS first; scoping then works on plain CSS.
|
|
14
|
+
const styleSources = await Promise.all(descriptor.styles.map((style) => {
|
|
15
|
+
if (!style.lang || style.lang === 'css')
|
|
16
|
+
return style.content;
|
|
17
|
+
if (!preprocessStyle)
|
|
18
|
+
throw new Error(`${path}: <style lang="${style.lang}"> needs a CSS preprocessor, and none is configured.`);
|
|
19
|
+
return preprocessStyle(style.lang, style.content);
|
|
20
|
+
}));
|
|
18
21
|
// Stable per file, so the scope attribute does not change between reloads.
|
|
19
22
|
const id = contentHash(path);
|
|
20
23
|
const scoped = descriptor.styles.some((style) => style.scoped);
|
|
@@ -53,8 +56,8 @@ export function createVueCompiler(sfc) {
|
|
|
53
56
|
parts.push(`_sfc_main.__file = ${JSON.stringify(path)};`);
|
|
54
57
|
parts.push('export default _sfc_main;');
|
|
55
58
|
const css = descriptor.styles
|
|
56
|
-
.map((style) => {
|
|
57
|
-
const out = sfc.compileStyle({ source:
|
|
59
|
+
.map((style, index) => {
|
|
60
|
+
const out = sfc.compileStyle({ source: styleSources[index], filename: path, id: `data-v-${id}`, scoped: !!style.scoped, isProd: !dev });
|
|
58
61
|
if (out.errors.length)
|
|
59
62
|
fail(path, out.errors);
|
|
60
63
|
return out.code;
|
package/dist/vueCompiler.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vueCompiler.js","sourceRoot":"","sources":["../src/vueCompiler.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAqDxC,MAAM,cAAc,GAA2B,EAAE,EAAE,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,EAAE,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC;AAE9F,SAAS,IAAI,CAAC,IAAY,EAAE,QAAmB;IAC7C,MAAM,KAAK,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;IAC1B,MAAM,IAAI,KAAK,CAAC,GAAG,IAAI,KAAK,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,EAAE,OAAO,IAAI,mBAAmB,EAAE,CAAC,CAAC;AAC3G,CAAC;AAED,MAAM,UAAU,iBAAiB,CAAC,GAAuB;IACvD,OAAO;QACL,OAAO,CAAC,MAAM,EAAE,EAAE,IAAI,EAAE,GAAG,EAAE;
|
|
1
|
+
{"version":3,"file":"vueCompiler.js","sourceRoot":"","sources":["../src/vueCompiler.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAqDxC,MAAM,cAAc,GAA2B,EAAE,EAAE,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,EAAE,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC;AAE9F,SAAS,IAAI,CAAC,IAAY,EAAE,QAAmB;IAC7C,MAAM,KAAK,GAAG,QA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,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@seanhogg/builderforce-webcontainers-core",
|
|
3
|
-
"version": "2026.10.
|
|
3
|
+
"version": "2026.10.10",
|
|
4
4
|
"description": "BuilderForce WebContainers — core. A runtime-agnostic in-browser dev server: virtual file system, module resolution, import rewriting to a package CDN, CSS/JSON/asset modules and project detection. No COOP/COEP required.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.js",
|
|
@@ -71,6 +71,9 @@
|
|
|
71
71
|
"@types/node": "^20",
|
|
72
72
|
"@vue/compiler-sfc": "^3.5.0",
|
|
73
73
|
"esbuild": "^0.24.0",
|
|
74
|
+
"less": "^4.2.0",
|
|
75
|
+
"@types/less": "^3.0.6",
|
|
76
|
+
"sass": "^1.80.0",
|
|
74
77
|
"svelte": "^5.0.0",
|
|
75
78
|
"typescript": "^5.5.0",
|
|
76
79
|
"vitest": "^2.1.0"
|
package/src/build.ts
CHANGED
|
@@ -8,6 +8,8 @@ import { readProjectConfig } from './projectConfig.js';
|
|
|
8
8
|
import { buildDefine } from './compileScript.js';
|
|
9
9
|
import { createBuildHost } from './buildHost.js';
|
|
10
10
|
import { findHtmlEntries, rewriteBuiltHtml } from './buildHtml.js';
|
|
11
|
+
import { noStyleCompilers, type StyleCompilers } from './styles/compilers.js';
|
|
12
|
+
import { createStyleImportHost, type StyleImportHostOptions } from './styles/imports.js';
|
|
11
13
|
|
|
12
14
|
/**
|
|
13
15
|
* `npm run build`, in the browser: the project in, a deployable static site out.
|
|
@@ -25,6 +27,10 @@ export interface BuildOptions {
|
|
|
25
27
|
cdn?: PackageCdnFactory;
|
|
26
28
|
/** `.vue` / `.svelte` compilers, needed when the project has components. */
|
|
27
29
|
components?: ComponentCompilers;
|
|
30
|
+
/** Sass / Less compilers, needed when the project has `.scss`, `.sass` or `.less` files. */
|
|
31
|
+
styles?: StyleCompilers;
|
|
32
|
+
/** Where Sass/Less package imports are fetched from when not installed (default jsDelivr). */
|
|
33
|
+
styleImports?: Pick<StyleImportHostOptions, 'fetchText' | 'packageFileUrl'>;
|
|
28
34
|
/**
|
|
29
35
|
* Where the site will be served from. `./` (default) works from any directory;
|
|
30
36
|
* an absolute base (`/app/`, `https://cdn.example.com/site/`) is used verbatim.
|
|
@@ -78,7 +84,13 @@ export async function buildProject(options: BuildOptions): Promise<BuildResult>
|
|
|
78
84
|
const inject = profile.entry && !entries.some((entry) => entry.tag === 'script') ? profile.entry : undefined;
|
|
79
85
|
const entryPoints = [...new Set([...entries.map((entry) => entry.path), ...(inject ? [inject] : [])])];
|
|
80
86
|
|
|
81
|
-
const
|
|
87
|
+
const styles = {
|
|
88
|
+
fs,
|
|
89
|
+
dependencies: config.dependencies,
|
|
90
|
+
styles: options.styles ?? noStyleCompilers,
|
|
91
|
+
imports: createStyleImportHost({ fs, dependencies: config.dependencies, ...options.styleImports }),
|
|
92
|
+
};
|
|
93
|
+
const host = createBuildHost({ fs, config, cdn, components: options.components ?? noComponentCompilers, styles, publicDir: profile.publicDir, base });
|
|
82
94
|
const bundled: BundleOutput = entryPoints.length
|
|
83
95
|
? await options.bundler.bundle({
|
|
84
96
|
entryPoints,
|
package/src/buildHost.ts
CHANGED
|
@@ -8,6 +8,10 @@ import { loaderFor } from './transformer.js';
|
|
|
8
8
|
import { basename, dirname, extname, join } from './paths.js';
|
|
9
9
|
import { isBareSpecifier, resolveAlias, resolveLocal, resolvePackage } from './resolve.js';
|
|
10
10
|
import { contentHash } from './hash.js';
|
|
11
|
+
import { compileStylesheet, preprocessStyleBlock, type StylesheetContext } from './styles/compile.js';
|
|
12
|
+
import { lessCandidates, sassCandidates } from './styles/compilers.js';
|
|
13
|
+
import { isCssModule, isStylesheet, styleLanguageOf } from './styles/languages.js';
|
|
14
|
+
import { PACKAGE_STYLE_DIR, packageStylePath, packageStyleSpecifier } from './styles/packageStyles.js';
|
|
11
15
|
|
|
12
16
|
/**
|
|
13
17
|
* The project side of a production build: the same resolution rules the dev
|
|
@@ -25,6 +29,8 @@ export interface BuildHostOptions {
|
|
|
25
29
|
config: ProjectConfig;
|
|
26
30
|
cdn: PackageCdn;
|
|
27
31
|
components: ComponentCompilers;
|
|
32
|
+
/** Sass / Less, and where their imports are found. */
|
|
33
|
+
styles: StylesheetContext;
|
|
28
34
|
publicDir: string;
|
|
29
35
|
/** The URL the site is served under: `./` (relative) or absolute, ending in `/`. */
|
|
30
36
|
base: string;
|
|
@@ -37,8 +43,6 @@ export interface BuildHost extends BundleHost {
|
|
|
37
43
|
|
|
38
44
|
/** The query a compiled component's own CSS is imported under. */
|
|
39
45
|
const COMPONENT_CSS = 'bfwc-component-css';
|
|
40
|
-
/** The query (and virtual directory) of a package stylesheet imported from JS. */
|
|
41
|
-
const PACKAGE_CSS = 'bfwc-package-css';
|
|
42
46
|
const encoder = new TextEncoder();
|
|
43
47
|
|
|
44
48
|
function splitAnyQuery(specifier: string): { path: string; query?: string } {
|
|
@@ -94,6 +98,17 @@ export function createBuildHost(options: BuildHostOptions): BuildHost {
|
|
|
94
98
|
return undefined;
|
|
95
99
|
}
|
|
96
100
|
|
|
101
|
+
/** A plain package stylesheet stays on the CDN as an `@import`; a Sass/Less one is compiled in. */
|
|
102
|
+
async function loadPackageStyle(specifier: string): Promise<BundleLoad> {
|
|
103
|
+
const { name, subpath } = resolvePackage(config.packageAliases, specifier);
|
|
104
|
+
const language = styleLanguageOf(subpath);
|
|
105
|
+
if (language === 'css') return { contents: `@import ${JSON.stringify(cdn.urlFor(name, subpath))};`, loader: 'css' };
|
|
106
|
+
const id = await options.styles.imports.resolve(`~${name}${subpath}`, undefined, language === 'less' ? lessCandidates : sassCandidates);
|
|
107
|
+
if (!id) throw new Error(`Cannot find the stylesheet ${specifier}`);
|
|
108
|
+
const file = await options.styles.imports.load(id);
|
|
109
|
+
return { contents: (await compileStylesheet(options.styles, id, file.contents)).css, loader: 'css' };
|
|
110
|
+
}
|
|
111
|
+
|
|
97
112
|
return {
|
|
98
113
|
assets,
|
|
99
114
|
|
|
@@ -112,8 +127,8 @@ export function createBuildHost(options: BuildHostOptions): BuildHost {
|
|
|
112
127
|
if (!file) {
|
|
113
128
|
const { name, subpath } = resolvePackage(config.packageAliases, path);
|
|
114
129
|
// A package stylesheet imported from JS (`import 'bootstrap/dist/css/bootstrap.css'`)
|
|
115
|
-
// can't stay a JS import of a CSS URL; it joins the CSS bundle
|
|
116
|
-
if (!css &&
|
|
130
|
+
// can't stay a JS import of a CSS URL; it joins the CSS bundle (see `load`).
|
|
131
|
+
if (!css && isStylesheet(subpath)) return { path: packageStylePath(path), query: PACKAGE_STYLE_DIR };
|
|
117
132
|
return { external: cdn.urlFor(name, subpath) };
|
|
118
133
|
}
|
|
119
134
|
} else {
|
|
@@ -127,22 +142,20 @@ export function createBuildHost(options: BuildHostOptions): BuildHost {
|
|
|
127
142
|
async load(path, query): Promise<BundleLoad> {
|
|
128
143
|
if (query === 'raw') return { contents: fs.readText(path) ?? '', loader: 'text' };
|
|
129
144
|
if (query === COMPONENT_CSS) return { contents: componentCss.get(path) ?? '', loader: 'css' };
|
|
130
|
-
if (query ===
|
|
131
|
-
const { name, subpath } = resolvePackage(config.packageAliases, path.slice(PACKAGE_CSS.length + 2));
|
|
132
|
-
return { contents: `@import ${JSON.stringify(cdn.urlFor(name, subpath))};`, loader: 'css' };
|
|
133
|
-
}
|
|
145
|
+
if (query === PACKAGE_STYLE_DIR) return loadPackageStyle(packageStyleSpecifier(path)!);
|
|
134
146
|
|
|
135
147
|
const ext = extname(path);
|
|
136
148
|
if (query === 'url') return { contents: assetModule(path), loader: 'js' };
|
|
137
149
|
const loader = loaderFor(ext);
|
|
138
150
|
if (loader) return { contents: fs.readFile(path) ?? '', loader };
|
|
139
|
-
if (
|
|
151
|
+
if (isStylesheet(path)) return { contents: (await compileStylesheet(options.styles, path)).css, loader: isCssModule(path) ? 'local-css' : 'css' };
|
|
140
152
|
if (ext === '.json') return { contents: fs.readFile(path) ?? '', loader: 'json' };
|
|
141
153
|
|
|
142
154
|
const extension = componentExtension(ext);
|
|
143
155
|
if (extension) {
|
|
144
156
|
const compiler = await options.components(extension, config.dependencies);
|
|
145
|
-
const
|
|
157
|
+
const preprocessStyle = (lang: string, source: string) => preprocessStyleBlock(options.styles, lang, source, path);
|
|
158
|
+
const compiled = await compiler.compile(fs.readText(path) ?? '', { path, dev: false, preprocessStyle });
|
|
146
159
|
if (!compiled.css) return { contents: compiled.code, loader: compiled.loader };
|
|
147
160
|
componentCss.set(path, compiled.css);
|
|
148
161
|
// Importing its own CSS puts the component's styles in the entry's CSS bundle.
|
package/src/compileComponent.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { compileModule } from './compileScript.js';
|
|
|
3
3
|
import { componentExtension } from './components.js';
|
|
4
4
|
import { extname } from './paths.js';
|
|
5
5
|
import { injectStyleStatement, rewriteCssUrls } from './styleModules.js';
|
|
6
|
+
import { preprocessStyleBlock } from './styles/compile.js';
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* A `.vue` / `.svelte` file → one ES module for the dev server: the compiled
|
|
@@ -13,7 +14,9 @@ export async function compileComponent(ctx: CompileContext, path: string): Promi
|
|
|
13
14
|
const extension = componentExtension(extname(path));
|
|
14
15
|
if (!extension) throw new Error(`Not a component: ${path}`);
|
|
15
16
|
const compiler = await ctx.components(extension, ctx.config.dependencies);
|
|
16
|
-
const
|
|
17
|
+
const { styles } = ctx;
|
|
18
|
+
const preprocessStyle = styles ? (lang: string, source: string) => preprocessStyleBlock(styles, lang, source, path) : undefined;
|
|
19
|
+
const compiled = await compiler.compile(ctx.fs.readText(path) ?? '', { path, dev: true, preprocessStyle });
|
|
17
20
|
const js = await compileModule(ctx, path, compiled.code, compiled.loader);
|
|
18
21
|
if (!compiled.css) return js;
|
|
19
22
|
const css = rewriteCssUrls(ctx.fs, path, compiled.css, ctx.base);
|
package/src/compileScript.ts
CHANGED
|
@@ -8,6 +8,9 @@ import { loaderFor } from './transformer.js';
|
|
|
8
8
|
import { dirname, extname } from './paths.js';
|
|
9
9
|
import { isBareSpecifier, resolveAlias, resolveLocal, resolvePackage } from './resolve.js';
|
|
10
10
|
import { rewriteImports } from './rewriteImports.js';
|
|
11
|
+
import { isStylesheet } from './styles/languages.js';
|
|
12
|
+
import { packageStylePath } from './styles/packageStyles.js';
|
|
13
|
+
import type { StylesheetContext } from './styles/compile.js';
|
|
11
14
|
|
|
12
15
|
export interface CompileContext {
|
|
13
16
|
fs: VirtualFileSystem;
|
|
@@ -18,6 +21,12 @@ export interface CompileContext {
|
|
|
18
21
|
base: string;
|
|
19
22
|
/** `.vue` / `.svelte` compilers. */
|
|
20
23
|
components: ComponentCompilers;
|
|
24
|
+
/** Sass/Less, for `<style lang>` blocks in components. */
|
|
25
|
+
styles?: StylesheetContext;
|
|
26
|
+
/** Told about every local file a module imports — the hot-update graph's edges. */
|
|
27
|
+
track?: (path: string) => void;
|
|
28
|
+
/** A file's hot-update stamp, written into the URLs importing it. */
|
|
29
|
+
stampOf?: (path: string) => number | undefined;
|
|
21
30
|
}
|
|
22
31
|
|
|
23
32
|
/** Vite-style import queries this runtime honours. */
|
|
@@ -30,17 +39,25 @@ export function isScriptPath(path: string): boolean {
|
|
|
30
39
|
}
|
|
31
40
|
|
|
32
41
|
/**
|
|
33
|
-
* The URL a resolved VFS file is imported by. Scripts are served as-is;
|
|
34
|
-
* JSON need the `?import` JS wrapper; anything else imported
|
|
35
|
-
* fonts) exports its URL, as in Vite.
|
|
42
|
+
* The URL a resolved VFS file is imported by. Scripts are served as-is;
|
|
43
|
+
* stylesheets and JSON need the `?import` JS wrapper; anything else imported
|
|
44
|
+
* from JS (images, fonts) exports its URL, as in Vite. A hot-update `stamp`
|
|
45
|
+
* rides along as `t`, so a re-run module imports a fresh copy.
|
|
36
46
|
*/
|
|
37
|
-
export function moduleUrl(base: string, path: string, query?: ImportQuery): string {
|
|
47
|
+
export function moduleUrl(base: string, path: string, query?: ImportQuery, stamp?: number): string {
|
|
38
48
|
const url = base + path.slice(1);
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
if (
|
|
42
|
-
if (
|
|
43
|
-
|
|
49
|
+
const params: string[] = [];
|
|
50
|
+
if (query === 'raw' || query === 'url') params.push(query);
|
|
51
|
+
else if (isStylesheet(path) || extname(path) === '.json') params.push('import');
|
|
52
|
+
else if (!isScriptPath(path)) params.push('url');
|
|
53
|
+
if (stamp !== undefined) params.push(`t=${stamp}`);
|
|
54
|
+
return params.length ? `${url}?${params.join('&')}` : url;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** A resolved local import: recorded for hot updates, and stamped. */
|
|
58
|
+
function localUrl(ctx: CompileContext, path: string, query: ImportQuery | undefined): string {
|
|
59
|
+
ctx.track?.(path);
|
|
60
|
+
return moduleUrl(ctx.base, path, query, ctx.stampOf?.(path));
|
|
44
61
|
}
|
|
45
62
|
|
|
46
63
|
/** Split `./notes.md?raw` into the path and the query this runtime honours (if any). */
|
|
@@ -62,12 +79,14 @@ export function mapSpecifier(ctx: CompileContext, fromDir: string, specifier: st
|
|
|
62
79
|
|
|
63
80
|
if (isBareSpecifier(path)) {
|
|
64
81
|
const aliased = resolveAlias(ctx.fs, ctx.config.aliases, path);
|
|
65
|
-
if (aliased) return
|
|
82
|
+
if (aliased) return localUrl(ctx, aliased, query);
|
|
66
83
|
const { name, subpath } = resolvePackage(ctx.config.packageAliases, path);
|
|
84
|
+
// A package stylesheet is served by the dev server, which links or compiles it.
|
|
85
|
+
if (isStylesheet(subpath) && !query) return ctx.base + packageStylePath(path).slice(1);
|
|
67
86
|
return ctx.cdn.urlFor(name, subpath);
|
|
68
87
|
}
|
|
69
88
|
const resolved = resolveLocal(ctx.fs, fromDir, path);
|
|
70
|
-
return resolved ?
|
|
89
|
+
return resolved ? localUrl(ctx, resolved, query) : undefined;
|
|
71
90
|
}
|
|
72
91
|
|
|
73
92
|
/**
|
package/src/components.ts
CHANGED
|
@@ -24,6 +24,11 @@ export interface ComponentRequest {
|
|
|
24
24
|
path: string;
|
|
25
25
|
/** Development output (dev warnings, readable names) vs production. */
|
|
26
26
|
dev: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Compiles a `<style lang="scss|sass|less">` block to CSS (its imports resolve
|
|
29
|
+
* beside the component). Without it, such a block is an error.
|
|
30
|
+
*/
|
|
31
|
+
preprocessStyle?: (lang: string, source: string) => Promise<string>;
|
|
27
32
|
}
|
|
28
33
|
|
|
29
34
|
export interface CompiledComponent {
|
package/src/detectProject.ts
CHANGED
|
@@ -21,10 +21,11 @@ export interface ProjectProfile {
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
|
-
* Frameworks that
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
24
|
+
* Frameworks that run their own dev server, which the instant preview does not
|
|
25
|
+
* replace: they run as Node processes here (`npm run dev`, Next.js on its wasm
|
|
26
|
+
* SWC, SvelteKit/Nuxt on real Vite). Checked before the plain-framework rules
|
|
27
|
+
* below, so SvelteKit and Nuxt (which depend on svelte and vue) are not mistaken
|
|
28
|
+
* for a Vite + Svelte/Vue app.
|
|
28
29
|
*/
|
|
29
30
|
const NEEDS_NODE: ReadonlyArray<{ dependency: string; reason: string }> = [
|
|
30
31
|
{ dependency: 'next', reason: 'Next.js needs its Node server.' },
|
|
@@ -59,10 +60,16 @@ function findEntry(fs: VirtualFileSystem): string | undefined {
|
|
|
59
60
|
return undefined;
|
|
60
61
|
}
|
|
61
62
|
|
|
63
|
+
/** Why the project's framework runs its own dev server (Next.js, Nuxt, SvelteKit…), or undefined. */
|
|
64
|
+
export function ownServerReason(fs: VirtualFileSystem): string | undefined {
|
|
65
|
+
const deps = readDependencyNames(fs);
|
|
66
|
+
return NEEDS_NODE.find((row) => deps.has(row.dependency))?.reason;
|
|
67
|
+
}
|
|
68
|
+
|
|
62
69
|
export function detectProject(fs: VirtualFileSystem): ProjectProfile {
|
|
70
|
+
const blocker = ownServerReason(fs);
|
|
71
|
+
if (blocker) return { supported: false, publicDir: '/public', reason: blocker };
|
|
63
72
|
const deps = readDependencyNames(fs);
|
|
64
|
-
const blocker = NEEDS_NODE.find((row) => deps.has(row.dependency));
|
|
65
|
-
if (blocker) return { supported: false, publicDir: '/public', reason: blocker.reason };
|
|
66
73
|
|
|
67
74
|
const framework = detectFramework(deps);
|
|
68
75
|
if (fs.isFile('/index.html')) {
|
package/src/devServer.ts
CHANGED
|
@@ -5,13 +5,22 @@ import { esmShCdnFactory } from './packageCdn.js';
|
|
|
5
5
|
import { noComponentCompilers, type ComponentCompilers } from './components.js';
|
|
6
6
|
import { isConfigFile, readProjectConfig, type ProjectConfig } from './projectConfig.js';
|
|
7
7
|
import { detectProject, type ProjectProfile } from './detectProject.js';
|
|
8
|
-
import { compileScript, isScriptPath,
|
|
8
|
+
import { compileScript, isScriptPath, moduleUrl, type ImportQuery } from './compileScript.js';
|
|
9
9
|
import { compileComponent } from './compileComponent.js';
|
|
10
10
|
import { transformHtml } from './html.js';
|
|
11
|
-
import {
|
|
11
|
+
import { jsonToModule, rawToModule, urlToModule } from './styleModules.js';
|
|
12
|
+
import { packageStylesheetModule, previewCss, stylesheetModule, type DevStyleContext } from './devStyles.js';
|
|
12
13
|
import { loaderFor } from './transformer.js';
|
|
13
14
|
import { extname, join, normalizePath } from './paths.js';
|
|
14
15
|
import { JS_MIME, mimeFor } from './mime.js';
|
|
16
|
+
import { noStyleCompilers, type StyleCompilers } from './styles/compilers.js';
|
|
17
|
+
import { createStyleImportHost, type StyleImportHost, type StyleImportHostOptions } from './styles/imports.js';
|
|
18
|
+
import { isStylesheet } from './styles/languages.js';
|
|
19
|
+
import { packageStyleSpecifier } from './styles/packageStyles.js';
|
|
20
|
+
import { ModuleGraph } from './hot/moduleGraph.js';
|
|
21
|
+
import { planUpdate } from './hot/planUpdate.js';
|
|
22
|
+
import { componentNames, hookSignature, refreshFacts, refreshFooter, usesReactRefresh } from './hot/reactRefresh.js';
|
|
23
|
+
import type { PreviewUpdate } from './hot/client.js';
|
|
15
24
|
|
|
16
25
|
export interface ServedFile {
|
|
17
26
|
status: number;
|
|
@@ -28,13 +37,23 @@ export interface DevServerOptions {
|
|
|
28
37
|
cdn?: PackageCdnFactory;
|
|
29
38
|
/** `.vue` / `.svelte` compilers. Without them, requesting a component is an error. */
|
|
30
39
|
components?: ComponentCompilers;
|
|
40
|
+
/** Sass / Less compilers. Without them, requesting a `.scss` or `.less` file is an error. */
|
|
41
|
+
styles?: StyleCompilers;
|
|
42
|
+
/** Where Sass/Less package imports are fetched from when not installed (default jsDelivr). */
|
|
43
|
+
styleImports?: Pick<StyleImportHostOptions, 'fetchText' | 'packageFileUrl'>;
|
|
31
44
|
/** The "Built with Builderforce.ai" badge in served pages. Default true. */
|
|
32
45
|
attribution?: boolean;
|
|
33
46
|
/** Host scripts (JavaScript source) run in every preview page before the app. */
|
|
34
47
|
previewScripts?: readonly string[];
|
|
48
|
+
/**
|
|
49
|
+
* Hot updates: stylesheets swap and React components re-render in place on an
|
|
50
|
+
* edit, keeping state (see {@link DevServer.planUpdate}). Default true.
|
|
51
|
+
*/
|
|
52
|
+
hot?: boolean;
|
|
35
53
|
}
|
|
36
54
|
|
|
37
55
|
const NO_STORE = { 'cache-control': 'no-store' };
|
|
56
|
+
const CSS_MIME = mimeFor('.css');
|
|
38
57
|
|
|
39
58
|
/**
|
|
40
59
|
* Documents opt into COEP so the preview can be framed by a cross-origin-isolated
|
|
@@ -67,33 +86,45 @@ function parseQuery(search: string): ImportQuery | undefined {
|
|
|
67
86
|
return undefined;
|
|
68
87
|
}
|
|
69
88
|
|
|
89
|
+
/** Adding or removing a file, or touching config or HTML, can change how OTHER files resolve. */
|
|
90
|
+
function isStructuralChange(change: FsChange): boolean {
|
|
91
|
+
return change.type !== 'write' || change.created === true || isConfigFile(change.path) || extname(change.path) === '.html';
|
|
92
|
+
}
|
|
93
|
+
|
|
70
94
|
/**
|
|
71
95
|
* The dev server: request path in, response out. It does not listen on anything —
|
|
72
96
|
* the host decides how requests arrive (a service worker in the browser, a plain
|
|
73
97
|
* function call in tests), which is what keeps it runtime-agnostic.
|
|
74
98
|
*
|
|
75
99
|
* Responses are cached per (file, query) and validated against the file's VFS
|
|
76
|
-
* version.
|
|
77
|
-
* file, or touching a config file, clears everything
|
|
78
|
-
*
|
|
100
|
+
* version. Editing a file evicts it and everything that read it; creating or
|
|
101
|
+
* deleting a file, or touching a config file, clears everything.
|
|
102
|
+
*
|
|
103
|
+
* While serving, it records what the preview loaded and what imports what (the
|
|
104
|
+
* {@link ModuleGraph}); {@link planUpdate} turns an edit into a hot update from it.
|
|
79
105
|
*/
|
|
80
106
|
export class DevServer {
|
|
81
107
|
private config?: ProjectConfig;
|
|
82
108
|
private cdn?: Promise<PackageCdn>;
|
|
109
|
+
private styleImports?: StyleImportHost;
|
|
83
110
|
private profileCache?: ProjectProfile;
|
|
84
111
|
private readonly cache = new Map<string, { version: number; file: ServedFile }>();
|
|
112
|
+
private readonly graph = new ModuleGraph();
|
|
85
113
|
private readonly unwatch: () => void;
|
|
114
|
+
private readonly hot: boolean;
|
|
86
115
|
private previewScripts: readonly string[];
|
|
87
116
|
|
|
88
117
|
constructor(private readonly options: DevServerOptions) {
|
|
89
118
|
if (!options.base.endsWith('/')) throw new Error('DevServer base must end with "/"');
|
|
90
119
|
this.previewScripts = options.previewScripts ?? [];
|
|
120
|
+
this.hot = options.hot !== false;
|
|
91
121
|
this.unwatch = options.fs.watch((change) => this.onChange(change));
|
|
92
122
|
}
|
|
93
123
|
|
|
94
124
|
dispose(): void {
|
|
95
125
|
this.unwatch();
|
|
96
126
|
this.cache.clear();
|
|
127
|
+
this.graph.clear();
|
|
97
128
|
}
|
|
98
129
|
|
|
99
130
|
/** Replace the host scripts injected into preview pages; takes effect on the next page load. */
|
|
@@ -114,6 +145,9 @@ export class DevServer {
|
|
|
114
145
|
|
|
115
146
|
if (path === '/' || path === '/index.html' || path === profile.htmlPath) return this.serveDocument(profile);
|
|
116
147
|
|
|
148
|
+
const specifier = packageStyleSpecifier(path);
|
|
149
|
+
if (specifier !== undefined) return this.cached(path, `${path}?`, 0, () => this.producePackageStyle(specifier));
|
|
150
|
+
|
|
117
151
|
const publicPath = join(profile.publicDir, path);
|
|
118
152
|
const file = fs.isFile(path) ? path : fs.isFile(publicPath) ? publicPath : undefined;
|
|
119
153
|
if (!file) {
|
|
@@ -121,30 +155,70 @@ export class DevServer {
|
|
|
121
155
|
if (!extname(path) && !query) return this.serveDocument(profile);
|
|
122
156
|
return query || isScriptPath(path) ? errorModule(`Cannot find module ${path}`) : reply(404, 'text/plain; charset=utf-8', `Not found: ${path}`);
|
|
123
157
|
}
|
|
158
|
+
return this.cached(file, `${file}?${query ?? ''}`, fs.version(file) ?? 0, () => this.produce(file, query));
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* What open previews should do about `changes` (one burst of edits): swap
|
|
163
|
+
* stylesheets and re-render React components in place, or reload. Undefined
|
|
164
|
+
* when nothing the preview loaded changed. Bumps the stamps of re-run modules,
|
|
165
|
+
* so call it once per burst and send the result.
|
|
166
|
+
*/
|
|
167
|
+
planUpdate(changes: readonly FsChange[]): PreviewUpdate | undefined {
|
|
168
|
+
if (!changes.length) return undefined;
|
|
169
|
+
if (!this.hot || changes.some(isStructuralChange)) return { type: 'reload' };
|
|
170
|
+
const plan = planUpdate({
|
|
171
|
+
changed: [...new Set(changes.map((change) => change.path))],
|
|
172
|
+
graph: this.graph,
|
|
173
|
+
refresh: this.refreshEnabled(),
|
|
174
|
+
hooksChanged: (path) => this.hooksChanged(path),
|
|
175
|
+
});
|
|
176
|
+
if (plan.reload) return { type: 'reload' };
|
|
177
|
+
if (!plan.styles.length && !plan.boundaries.length) return undefined;
|
|
178
|
+
|
|
179
|
+
const stamp = this.graph.bump(plan.touched);
|
|
180
|
+
// Their importers' compiled output names the old stamps.
|
|
181
|
+
for (const path of plan.touched) {
|
|
182
|
+
this.evict(path);
|
|
183
|
+
for (const importer of this.graph.importers(path)) this.evict(importer);
|
|
184
|
+
}
|
|
185
|
+
const { base } = this.options;
|
|
186
|
+
return {
|
|
187
|
+
type: 'update',
|
|
188
|
+
styles: plan.styles.map((path) => ({ path: base + path.slice(1), url: moduleUrl(base, path, undefined, stamp), stamp })),
|
|
189
|
+
modules: plan.boundaries.map((path) => moduleUrl(base, path, undefined, stamp)),
|
|
190
|
+
};
|
|
191
|
+
}
|
|
124
192
|
|
|
125
|
-
|
|
126
|
-
|
|
193
|
+
private async cached(file: string, key: string, version: number, produce: () => Promise<ServedFile>): Promise<ServedFile> {
|
|
194
|
+
this.graph.markServed(file);
|
|
127
195
|
const hit = this.cache.get(key);
|
|
128
196
|
if (hit && hit.version === version) return hit.file;
|
|
129
|
-
|
|
130
|
-
const served = await this.produce(file, query);
|
|
197
|
+
const served = await produce();
|
|
131
198
|
this.cache.set(key, { version, file: served });
|
|
132
199
|
return served;
|
|
133
200
|
}
|
|
134
201
|
|
|
202
|
+
private evict(file: string): void {
|
|
203
|
+
for (const key of [...this.cache.keys()]) if (key.startsWith(file + '?')) this.cache.delete(key);
|
|
204
|
+
}
|
|
205
|
+
|
|
135
206
|
private async produce(file: string, query: ImportQuery | undefined): Promise<ServedFile> {
|
|
136
207
|
const { fs, base } = this.options;
|
|
137
208
|
const ext = extname(file);
|
|
209
|
+
const dependencies = new Set<string>();
|
|
138
210
|
try {
|
|
139
211
|
if (query === 'raw') return reply(200, JS_MIME, rawToModule(fs.readText(file) ?? ''));
|
|
140
212
|
if (query === 'url') return reply(200, JS_MIME, urlToModule(base + file.slice(1)));
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
213
|
+
const ctx = await this.context(dependencies);
|
|
214
|
+
if (isStylesheet(file)) {
|
|
215
|
+
const style = query === 'import' ? await stylesheetModule(ctx, file) : await previewCss(ctx, file);
|
|
216
|
+
for (const loaded of style.loaded) dependencies.add(loaded);
|
|
217
|
+
return reply(200, query === 'import' ? JS_MIME : CSS_MIME, style.body);
|
|
144
218
|
}
|
|
145
219
|
if (ext === '.json' && query === 'import') return reply(200, JS_MIME, jsonToModule(fs.readText(file) ?? ''));
|
|
146
|
-
if (loaderFor(ext)) return reply(200, JS_MIME, await
|
|
147
|
-
if (isScriptPath(file)) return reply(200, JS_MIME, await compileComponent(
|
|
220
|
+
if (loaderFor(ext)) return reply(200, JS_MIME, await this.compileWithRefresh(ctx, file));
|
|
221
|
+
if (isScriptPath(file)) return reply(200, JS_MIME, await compileComponent(ctx, file));
|
|
148
222
|
if (ext === '.html' || ext === '.htm') {
|
|
149
223
|
return reply(200, mimeFor(ext), transformHtml(fs.readText(file) ?? '', { base, attribution: this.options.attribution, scripts: this.previewScripts }));
|
|
150
224
|
}
|
|
@@ -152,34 +226,85 @@ export class DevServer {
|
|
|
152
226
|
} catch (error) {
|
|
153
227
|
const message = error instanceof Error ? error.message : String(error);
|
|
154
228
|
return query || isScriptPath(file) ? errorModule(`${file}: ${message}`) : reply(500, 'text/plain; charset=utf-8', message);
|
|
229
|
+
} finally {
|
|
230
|
+
this.graph.setDependencies(file, dependencies);
|
|
155
231
|
}
|
|
156
232
|
}
|
|
157
233
|
|
|
158
|
-
private
|
|
234
|
+
private async producePackageStyle(specifier: string): Promise<ServedFile> {
|
|
235
|
+
try {
|
|
236
|
+
return reply(200, JS_MIME, (await packageStylesheetModule(await this.context(new Set()), specifier)).body);
|
|
237
|
+
} catch (error) {
|
|
238
|
+
return errorModule(`${specifier}: ${error instanceof Error ? error.message : String(error)}`);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** A script, with React Refresh registration appended when the preview runs it. */
|
|
243
|
+
private async compileWithRefresh(ctx: DevStyleContext, file: string): Promise<string> {
|
|
244
|
+
const code = await compileScript(ctx, file);
|
|
245
|
+
if (!this.refreshEnabled()) return code;
|
|
246
|
+
const names = componentNames(code);
|
|
247
|
+
this.graph.setFacts(file, await refreshFacts(code, this.options.fs.readText(file) ?? '', names));
|
|
248
|
+
return code + refreshFooter(file, names);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
private hooksChanged(path: string): boolean {
|
|
252
|
+
const facts = this.graph.factsOf(path);
|
|
253
|
+
return !!facts && !!loaderFor(extname(path)) && hookSignature(this.options.fs.readText(path) ?? '') !== facts.hooks;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
private projectConfig(): ProjectConfig {
|
|
257
|
+
return (this.config ??= readProjectConfig(this.options.fs));
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
private refreshEnabled(): boolean {
|
|
261
|
+
return this.hot && usesReactRefresh(this.projectConfig());
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
private async serveDocument(profile: ProjectProfile): Promise<ServedFile> {
|
|
159
265
|
if (!profile.supported || !profile.htmlPath) {
|
|
160
266
|
return reply(501, 'text/html; charset=utf-8', `<!doctype html><meta charset="utf-8"><p>${profile.reason ?? 'Unsupported project.'}</p>`);
|
|
161
267
|
}
|
|
162
268
|
const html = this.options.fs.readText(profile.htmlPath) ?? '';
|
|
163
269
|
const { base, attribution } = this.options;
|
|
164
|
-
|
|
270
|
+
const refreshRuntime = this.refreshEnabled() ? (await this.packageCdn()).urlFor('react-refresh', '/runtime') : undefined;
|
|
271
|
+
return reply(200, 'text/html; charset=utf-8', transformHtml(html, { base, entry: profile.entry, attribution, scripts: this.previewScripts, refreshRuntime }));
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
private packageCdn(): Promise<PackageCdn> {
|
|
275
|
+
const config = this.projectConfig();
|
|
276
|
+
return (this.cdn ??= Promise.resolve((this.options.cdn ?? esmShCdnFactory)(config.dependencies, { dev: true })));
|
|
165
277
|
}
|
|
166
278
|
|
|
167
|
-
|
|
279
|
+
/** The compile context for one response; `dependencies` collects the local files it reads. */
|
|
280
|
+
private async context(dependencies: Set<string>): Promise<DevStyleContext> {
|
|
168
281
|
const { fs, transformer, base } = this.options;
|
|
169
|
-
|
|
170
|
-
this.
|
|
171
|
-
return {
|
|
282
|
+
const config = this.projectConfig();
|
|
283
|
+
this.styleImports ??= createStyleImportHost({ fs, dependencies: config.dependencies, ...this.options.styleImports });
|
|
284
|
+
return {
|
|
285
|
+
fs,
|
|
286
|
+
config,
|
|
287
|
+
cdn: await this.packageCdn(),
|
|
288
|
+
transformer,
|
|
289
|
+
base,
|
|
290
|
+
components: this.options.components ?? noComponentCompilers,
|
|
291
|
+
styles: { fs, dependencies: config.dependencies, styles: this.options.styles ?? noStyleCompilers, imports: this.styleImports },
|
|
292
|
+
track: (path) => dependencies.add(path),
|
|
293
|
+
stampOf: (path) => this.graph.stampOf(path),
|
|
294
|
+
};
|
|
172
295
|
}
|
|
173
296
|
|
|
174
297
|
private onChange(change: FsChange): void {
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
for (const
|
|
298
|
+
if (!isStructuralChange(change)) {
|
|
299
|
+
this.evict(change.path);
|
|
300
|
+
for (const dependent of this.graph.dependents(change.path)) this.evict(dependent);
|
|
178
301
|
return;
|
|
179
302
|
}
|
|
180
303
|
this.cache.clear();
|
|
304
|
+
this.graph.clear();
|
|
181
305
|
this.config = undefined;
|
|
182
306
|
this.cdn = undefined;
|
|
307
|
+
this.styleImports = undefined;
|
|
183
308
|
this.profileCache = undefined;
|
|
184
309
|
}
|
|
185
310
|
}
|