@seanhogg/builderforce-webcontainers-core 2026.10.7 → 2026.10.9
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/archive.d.ts +2 -0
- package/dist/archive.d.ts.map +1 -1
- package/dist/archive.js +6 -4
- package/dist/archive.js.map +1 -1
- 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 +4 -8
- package/dist/html.d.ts.map +1 -1
- package/dist/html.js +15 -19
- package/dist/html.js.map +1 -1
- package/dist/index.d.ts +20 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -1
- package/dist/index.js.map +1 -1
- package/dist/installer/tree.d.ts.map +1 -1
- package/dist/installer/tree.js +22 -4
- package/dist/installer/tree.js.map +1 -1
- package/dist/installer/wasmBuilds.d.ts +27 -0
- package/dist/installer/wasmBuilds.d.ts.map +1 -0
- package/dist/installer/wasmBuilds.js +32 -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 +15 -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/client.d.ts +3 -1
- package/dist/node/http/client.d.ts.map +1 -1
- package/dist/node/http/client.js +74 -5
- package/dist/node/http/client.js.map +1 -1
- package/dist/node/http/index.d.ts.map +1 -1
- package/dist/node/http/index.js +6 -4
- package/dist/node/http/index.js.map +1 -1
- package/dist/node/http/messages.d.ts +87 -0
- package/dist/node/http/messages.d.ts.map +1 -0
- package/dist/node/http/messages.js +146 -0
- package/dist/node/http/messages.js.map +1 -0
- package/dist/node/http/server.d.ts +13 -98
- package/dist/node/http/server.d.ts.map +1 -1
- package/dist/node/http/server.js +41 -206
- package/dist/node/http/server.js.map +1 -1
- package/dist/node/http/serverResponse.d.ts +45 -0
- package/dist/node/http/serverResponse.d.ts.map +1 -0
- package/dist/node/http/serverResponse.js +96 -0
- package/dist/node/http/serverResponse.js.map +1 -0
- package/dist/node/http/upgradeSocket.d.ts +34 -0
- package/dist/node/http/upgradeSocket.d.ts.map +1 -0
- package/dist/node/http/upgradeSocket.js +73 -0
- package/dist/node/http/upgradeSocket.js.map +1 -0
- 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 -5
- 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 +25 -17
- 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/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/previewBridge.d.ts +42 -0
- package/dist/previewBridge.d.ts.map +1 -0
- package/dist/previewBridge.js +192 -0
- package/dist/previewBridge.js.map +1 -0
- package/dist/shell/ast.d.ts +101 -0
- package/dist/shell/ast.d.ts.map +1 -0
- package/dist/shell/ast.js +7 -0
- package/dist/shell/ast.js.map +1 -0
- package/dist/shell/builtins.d.ts +14 -0
- package/dist/shell/builtins.d.ts.map +1 -1
- package/dist/shell/builtins.js +87 -7
- package/dist/shell/builtins.js.map +1 -1
- package/dist/shell/compound.d.ts +30 -0
- package/dist/shell/compound.d.ts.map +1 -0
- package/dist/shell/compound.js +73 -0
- package/dist/shell/compound.js.map +1 -0
- 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 +27 -6
- package/dist/shell/exec.d.ts.map +1 -1
- package/dist/shell/exec.js +133 -46
- package/dist/shell/exec.js.map +1 -1
- package/dist/shell/expand.d.ts +5 -1
- package/dist/shell/expand.d.ts.map +1 -1
- package/dist/shell/expand.js +7 -5
- package/dist/shell/expand.js.map +1 -1
- package/dist/shell/lineReader.d.ts +9 -0
- package/dist/shell/lineReader.d.ts.map +1 -0
- package/dist/shell/lineReader.js +42 -0
- package/dist/shell/lineReader.js.map +1 -0
- package/dist/shell/npm.js +2 -2
- package/dist/shell/npm.js.map +1 -1
- package/dist/shell/packageManagers.d.ts +20 -0
- package/dist/shell/packageManagers.d.ts.map +1 -0
- package/dist/shell/packageManagers.js +74 -0
- package/dist/shell/packageManagers.js.map +1 -0
- package/dist/shell/parse.d.ts +16 -66
- package/dist/shell/parse.d.ts.map +1 -1
- package/dist/shell/parse.js +244 -243
- package/dist/shell/parse.js.map +1 -1
- package/dist/shell/program.d.ts.map +1 -1
- package/dist/shell/program.js +7 -6
- package/dist/shell/program.js.map +1 -1
- package/dist/shell/testCommand.d.ts +9 -0
- package/dist/shell/testCommand.d.ts.map +1 -0
- package/dist/shell/testCommand.js +105 -0
- package/dist/shell/testCommand.js.map +1 -0
- package/dist/shell/tokenize.d.ts +25 -0
- package/dist/shell/tokenize.d.ts.map +1 -0
- package/dist/shell/tokenize.js +279 -0
- package/dist/shell/tokenize.js.map +1 -0
- 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/cookieJar.d.ts +59 -0
- package/dist/system/cookieJar.d.ts.map +1 -0
- package/dist/system/cookieJar.js +187 -0
- package/dist/system/cookieJar.js.map +1 -0
- package/dist/system/httpStream.d.ts +40 -0
- package/dist/system/httpStream.d.ts.map +1 -0
- package/dist/system/httpStream.js +156 -0
- package/dist/system/httpStream.js.map +1 -0
- package/dist/system/index.d.ts +11 -1
- package/dist/system/index.d.ts.map +1 -1
- package/dist/system/index.js +5 -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 +10 -2
- package/dist/system/kernel.d.ts.map +1 -1
- package/dist/system/kernel.js +18 -6
- package/dist/system/kernel.js.map +1 -1
- package/dist/system/ports.d.ts +9 -4
- package/dist/system/ports.d.ts.map +1 -1
- package/dist/system/ports.js +21 -2
- package/dist/system/ports.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/programs.d.ts +1 -1
- package/dist/system/programs.d.ts.map +1 -1
- package/dist/system/programs.js +2 -0
- package/dist/system/programs.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 +65 -13
- package/dist/system/types.d.ts.map +1 -1
- package/dist/system/webSocketClient.d.ts +37 -0
- package/dist/system/webSocketClient.d.ts.map +1 -0
- package/dist/system/webSocketClient.js +152 -0
- package/dist/system/webSocketClient.js.map +1 -0
- package/dist/system/webSocketFrames.d.ts +39 -0
- package/dist/system/webSocketFrames.d.ts.map +1 -0
- package/dist/system/webSocketFrames.js +117 -0
- package/dist/system/webSocketFrames.js.map +1 -0
- 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/archive.ts +8 -4
- 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 +17 -21
- package/src/index.ts +20 -1
- package/src/installer/tree.ts +22 -4
- package/src/installer/wasmBuilds.ts +45 -0
- package/src/node/builtins.ts +21 -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/client.ts +69 -6
- package/src/node/http/index.ts +6 -4
- package/src/node/http/messages.ts +150 -0
- package/src/node/http/server.ts +44 -206
- package/src/node/http/serverResponse.ts +105 -0
- package/src/node/http/upgradeSocket.ts +79 -0
- package/src/node/ipc.ts +91 -0
- package/src/node/loader.ts +78 -14
- package/src/node/misc.ts +19 -5
- 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 +29 -17
- package/src/node/stream/readable.ts +2 -1
- package/src/node/urlqs.ts +10 -2
- package/src/node/workerThreads.ts +198 -0
- package/src/node/zlib.ts +24 -2
- package/src/previewBridge.ts +194 -0
- package/src/shell/ast.ts +85 -0
- package/src/shell/builtins.ts +97 -7
- package/src/shell/compound.ts +89 -0
- package/src/shell/context.ts +2 -2
- package/src/shell/devHandoff.ts +16 -9
- package/src/shell/exec.ts +134 -45
- package/src/shell/expand.ts +8 -6
- package/src/shell/lineReader.ts +46 -0
- package/src/shell/npm.ts +2 -2
- package/src/shell/packageManagers.ts +75 -0
- package/src/shell/parse.ts +233 -236
- package/src/shell/program.ts +7 -6
- package/src/shell/testCommand.ts +105 -0
- package/src/shell/tokenize.ts +257 -0
- 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/cookieJar.ts +187 -0
- package/src/system/httpStream.ts +168 -0
- package/src/system/index.ts +28 -1
- package/src/system/ipc.ts +89 -0
- package/src/system/kernel.ts +22 -7
- package/src/system/ports.ts +23 -4
- package/src/system/process.ts +57 -30
- package/src/system/programs.ts +3 -1
- package/src/system/terminal.ts +34 -12
- package/src/system/types.ts +71 -13
- package/src/system/webSocketClient.ts +177 -0
- package/src/system/webSocketFrames.ts +118 -0
- package/src/vueCompiler.ts +11 -8
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
|
}
|
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
|
+
}
|