@mui/internal-docs-infra 0.12.1-canary.2 → 0.12.1-canary.21
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/ChunkProvider/PreloadProvider.mjs +1 -2
- package/ChunkProvider/usePreload.d.mts +1 -1
- package/ChunkProvider/usePreload.mjs +0 -1
- package/CodeControllerContext/CodeControllerContext.d.mts +45 -10
- package/CodeControllerContext/CodeControllerContext.mjs +15 -1
- package/CodeHighlighter/CodeHighlighterClient.d.mts +3 -2
- package/CodeHighlighter/CodeHighlighterClient.mjs +113 -18
- package/CodeHighlighter/CodeHighlighterContext.d.mts +9 -3
- package/CodeHighlighter/buildStringFallback.d.mts +1 -1
- package/CodeHighlighter/codeToFallbackProps.d.mts +1 -1
- package/CodeHighlighter/createClientProps.mjs +3 -0
- package/CodeHighlighter/fallbackCompression.d.mts +1 -1
- package/CodeHighlighter/parseControlledCode.d.mts +13 -4
- package/CodeHighlighter/parseControlledCode.mjs +41 -49
- package/CodeHighlighter/types.d.mts +21 -0
- package/CodeHighlighter/useGrammarsReady.mjs +44 -9
- package/CodeProvider/CodeProvider.d.mts +1 -1
- package/CodeProvider/CodeProviderLazy.d.mts +1 -1
- package/CodeProvider/createParseSourceWorkerClient.d.mts +6 -0
- package/CodeProvider/createParseSourceWorkerClient.mjs +83 -15
- package/CodeProvider/useCodeProviderValue.mjs +50 -4
- package/abstractCreateDemo/abstractCreateDemo.d.mts +15 -1
- package/abstractCreateDemo/abstractCreateDemo.mjs +8 -0
- package/abstractCreateDemoClient/abstractCreateDemoClient.mjs +4 -0
- package/abstractCreateTypes/abstractCreateTypes.d.mts +12 -1
- package/abstractCreateTypes/abstractCreateTypes.mjs +4 -0
- package/abstractCreateTypes/typesToJsx.d.mts +8 -0
- package/abstractCreateTypes/typesToJsx.mjs +2 -1
- package/cli/loadNextConfig.d.mts +9 -0
- package/cli/loadNextConfig.mjs +11 -2
- package/cli/runValidate.mjs +15 -7
- package/cli/validateWorker.d.mts +1 -0
- package/cli/validateWorker.mjs +2 -1
- package/createDemoData/createDemoData.d.mts +1 -1
- package/package.json +34 -5
- package/pipeline/cacheUtils/constants.d.mts +5 -0
- package/pipeline/cacheUtils/constants.mjs +5 -0
- package/pipeline/cacheUtils/hashCacheContent.d.mts +5 -0
- package/pipeline/cacheUtils/hashCacheContent.mjs +9 -0
- package/pipeline/cacheUtils/index.d.mts +9 -0
- package/pipeline/cacheUtils/index.mjs +7 -0
- package/pipeline/cacheUtils/loadFileCache.d.mts +13 -0
- package/pipeline/cacheUtils/loadFileCache.mjs +20 -0
- package/pipeline/cacheUtils/loadFileCacheEntry.d.mts +13 -0
- package/pipeline/cacheUtils/loadFileCacheEntry.mjs +47 -0
- package/pipeline/cacheUtils/resolveCachePath.d.mts +16 -0
- package/pipeline/cacheUtils/resolveCachePath.mjs +24 -0
- package/pipeline/cacheUtils/saveFileCache.d.mts +17 -0
- package/pipeline/cacheUtils/saveFileCache.mjs +54 -0
- package/pipeline/cacheUtils/types.d.mts +23 -0
- package/pipeline/cacheUtils/types.mjs +1 -0
- package/pipeline/cacheUtils/withFileCache.d.mts +40 -0
- package/pipeline/cacheUtils/withFileCache.mjs +81 -0
- package/pipeline/hastUtils/hastUtils.d.mts +1 -1
- package/pipeline/hastUtils/hastUtils.mjs +0 -1
- package/pipeline/loadIsomorphicCodeSource/loadIsomorphicCodeSource.d.mts +1 -1
- package/pipeline/loadIsomorphicCodeSource/loadIsomorphicCodeSource.mjs +1 -1
- package/pipeline/loadIsomorphicCodeVariant/applyCodeTransform.mjs +0 -1
- package/pipeline/loadIsomorphicCodeVariant/diffHast.d.mts +1 -1
- package/pipeline/loadIsomorphicCodeVariant/hasAllCodeVariants.d.mts +1 -1
- package/pipeline/loadIsomorphicCodeVariant/maybeCodeInitialData.d.mts +1 -1
- package/pipeline/loadPrecomputedSitemap/loadPrecomputedSitemap.d.mts +6 -0
- package/pipeline/loadPrecomputedSitemap/loadPrecomputedSitemap.mjs +2 -1
- package/pipeline/loadPrecomputedTypes/loadPrecomputedTypes.d.mts +6 -0
- package/pipeline/loadPrecomputedTypes/loadPrecomputedTypes.mjs +5 -1
- package/pipeline/loadServerCodeMeta/resolveModulePathWithFs.d.mts +1 -1
- package/pipeline/loadServerCodeMeta/resolveModulePathWithFs.mjs +0 -1
- package/pipeline/loadServerPageIndex/enrichPageIndex.d.mts +15 -0
- package/pipeline/loadServerPageIndex/enrichPageIndex.mjs +50 -0
- package/pipeline/loadServerPageIndex/extractPrefixAndTitle.d.mts +19 -0
- package/pipeline/loadServerPageIndex/extractPrefixAndTitle.mjs +80 -0
- package/pipeline/loadServerPageIndex/index.d.mts +5 -2
- package/pipeline/loadServerPageIndex/index.mjs +4 -1
- package/pipeline/loadServerPageIndex/loadServerPageIndex.d.mts +13 -22
- package/pipeline/loadServerPageIndex/loadServerPageIndex.mjs +32 -124
- package/pipeline/loadServerPageIndex/resolvePageIndexCacheKey.d.mts +13 -0
- package/pipeline/loadServerPageIndex/resolvePageIndexCacheKey.mjs +23 -0
- package/pipeline/loadServerTypes/highlightTypes.d.mts +3 -3
- package/pipeline/loadServerTypes/highlightTypesMeta.d.mts +4 -4
- package/pipeline/loadServerTypes/loadServerTypes.d.mts +2 -2
- package/pipeline/loadServerTypes/loadServerTypes.mjs +67 -7
- package/pipeline/loadServerTypesMeta/createOptimizedProgram.d.mts +2 -1
- package/pipeline/loadServerTypesMeta/formatClass.d.mts +2 -2
- package/pipeline/loadServerTypesMeta/formatComponent.d.mts +2 -2
- package/pipeline/loadServerTypesMeta/formatFunction.d.mts +2 -2
- package/pipeline/loadServerTypesMeta/formatHook.d.mts +2 -2
- package/pipeline/loadServerTypesMeta/formatRaw.d.mts +2 -2
- package/pipeline/loadServerTypesMeta/formatType.d.mts +1 -1
- package/pipeline/loadServerTypesMeta/loadServerTypesMeta.d.mts +7 -7
- package/pipeline/loadServerTypesMeta/loadServerTypesMeta.mjs +1 -1
- package/pipeline/loadServerTypesMeta/processTypes.d.mts +2 -2
- package/pipeline/loadServerTypesMeta/worker.d.mts +1 -1
- package/pipeline/loadServerTypesText/loadServerTypesText.d.mts +16 -2
- package/pipeline/loadServerTypesText/loadServerTypesText.mjs +30 -8
- package/pipeline/loadServerTypesText/normalizeTypesSourceDataForCache.d.mts +2 -0
- package/pipeline/loadServerTypesText/normalizeTypesSourceDataForCache.mjs +144 -0
- package/pipeline/loadServerTypesText/resolveTypesCacheKey.d.mts +19 -0
- package/pipeline/loadServerTypesText/resolveTypesCacheKey.mjs +29 -0
- package/pipeline/loaderUtils/parseImportsAndComments.d.mts +5 -3
- package/pipeline/loaderUtils/parseImportsAndComments.mjs +284 -40
- package/pipeline/parseCreateFactoryCall/parseCreateFactoryCall.d.mts +2 -2
- package/pipeline/parseCreateFactoryCall/parseCreateFactoryCall.mjs +2 -3
- package/pipeline/parseSource/detectFileTypes.d.mts +16 -0
- package/pipeline/parseSource/detectFileTypes.mjs +44 -0
- package/pipeline/syncPageIndex/mergeMetadataMarkdown.d.mts +25 -1
- package/pipeline/syncPageIndex/mergeMetadataMarkdown.mjs +79 -49
- package/pipeline/syncPageIndex/metadataToMarkdown.d.mts +15 -1
- package/pipeline/syncPageIndex/metadataToMarkdown.mjs +32 -10
- package/pipeline/syncPageIndex/syncPageIndex.d.mts +12 -0
- package/pipeline/syncPageIndex/syncPageIndex.mjs +154 -40
- package/pipeline/syncTypes/generateTypesMarkdown.d.mts +2 -2
- package/pipeline/syncTypes/syncTypes.d.mts +7 -0
- package/pipeline/syncTypes/syncTypes.mjs +31 -1
- package/pipeline/transformHtmlCodeBlock/transformHtmlCodeBlock.mjs +1 -1
- package/pipeline/transformMarkdownCode/transformMarkdownCode.mjs +25 -15
- package/pipeline/transformMarkdownMetadata/transformMarkdownMetadata.mjs +3 -0
- package/pipeline/transformMarkdownMetadata/types.d.mts +8 -1
- package/useCode/EditableEngine.d.mts +1 -1
- package/useCode/Pre.d.mts +9 -2
- package/useCode/Pre.mjs +4 -3
- package/useCode/TransformEngine.d.mts +1 -1
- package/useCode/useCode.d.mts +17 -1
- package/useCode/useCode.mjs +22 -1
- package/useCode/useCopyFunctionality.d.mts +2 -2
- package/useCode/useEditable.browser.mjs +0 -1
- package/useCode/useFileNavigation.d.mts +3 -0
- package/useCode/useFileNavigation.mjs +3 -1
- package/useCode/useSourceEditing.mjs +45 -10
- package/useCode/useSourceEnhancing.mjs +41 -9
- package/useCode/useTransformManagement.d.mts +2 -2
- package/useCode/useTransformManagement.mjs +19 -3
- package/useCode/useUIState.d.mts +4 -0
- package/useCode/useUIState.mjs +8 -1
- package/useCode/useVariantSelection.d.mts +1 -1
- package/useCoordinated/useCoordinated.d.mts +1 -1
- package/useCoordinated/useCoordinatedLocalStorage.d.mts +1 -1
- package/useCoordinated/useCoordinatedLocalStorage.mjs +0 -1
- package/useCoordinated/useCoordinatedPreference.d.mts +1 -1
- package/useCoordinated/useCoordinatedPreference.mjs +0 -1
- package/useCrossTabState/index.d.mts +2 -0
- package/useCrossTabState/index.mjs +2 -0
- package/useCrossTabState/useCrossTabMirror.d.mts +26 -0
- package/useCrossTabState/useCrossTabMirror.mjs +108 -0
- package/useCrossTabState/useCrossTabState.d.mts +21 -0
- package/useCrossTabState/useCrossTabState.mjs +31 -0
- package/useDemo/exportVariant.mjs +2 -4
- package/useDemo/exportVariantAsCra.d.mts +1 -1
- package/useDemo/exportVariantAsCra.mjs +0 -1
- package/useDemo/useDemo.d.mts +4 -1
- package/useDemo/useDemo.mjs +5 -0
- package/useDemoController/BuildEngine.d.mts +2 -0
- package/useDemoController/BuildEngine.mjs +10 -0
- package/useDemoController/DemoRunner.d.mts +34 -0
- package/useDemoController/DemoRunner.mjs +83 -0
- package/useDemoController/ErrorBoundary.d.mts +34 -0
- package/useDemoController/ErrorBoundary.mjs +49 -0
- package/useDemoController/Runner.d.mts +44 -0
- package/useDemoController/Runner.mjs +71 -0
- package/useDemoController/absolutizeImports.d.mts +13 -0
- package/useDemoController/absolutizeImports.mjs +73 -0
- package/useDemoController/buildScope.d.mts +48 -0
- package/useDemoController/buildScope.mjs +270 -0
- package/useDemoController/collectSources.d.mts +67 -0
- package/useDemoController/collectSources.mjs +92 -0
- package/useDemoController/compileCssModule.d.mts +64 -0
- package/useDemoController/compileCssModule.mjs +40 -0
- package/useDemoController/compileCssWithPostcss.d.mts +17 -0
- package/useDemoController/compileCssWithPostcss.mjs +89 -0
- package/useDemoController/compileModule.d.mts +14 -0
- package/useDemoController/compileModule.mjs +17 -0
- package/useDemoController/constants.d.mts +13 -0
- package/useDemoController/constants.mjs +13 -0
- package/useDemoController/createRequire.d.mts +10 -0
- package/useDemoController/createRequire.mjs +17 -0
- package/useDemoController/createTranspileWorkerClient.d.mts +27 -0
- package/useDemoController/createTranspileWorkerClient.mjs +139 -0
- package/useDemoController/cssModules.d.mts +47 -0
- package/useDemoController/currentBrowserTarget.d.mts +16 -0
- package/useDemoController/currentBrowserTarget.mjs +36 -0
- package/useDemoController/evalCode.d.mts +12 -0
- package/useDemoController/evalCode.mjs +35 -0
- package/useDemoController/generateCssModuleSource.d.mts +11 -0
- package/useDemoController/generateCssModuleSource.mjs +55 -0
- package/useDemoController/generateElement.d.mts +21 -0
- package/useDemoController/generateElement.mjs +26 -0
- package/useDemoController/hashString.d.mts +11 -0
- package/useDemoController/hashString.mjs +20 -0
- package/useDemoController/importCode.d.mts +11 -0
- package/useDemoController/importCode.mjs +15 -0
- package/useDemoController/index.d.mts +1 -0
- package/useDemoController/index.mjs +1 -0
- package/useDemoController/instantiateElement.d.mts +24 -0
- package/useDemoController/instantiateElement.mjs +57 -0
- package/useDemoController/instantiateModule.d.mts +17 -0
- package/useDemoController/instantiateModule.mjs +24 -0
- package/useDemoController/resolveCssImports.d.mts +27 -0
- package/useDemoController/resolveCssImports.mjs +79 -0
- package/useDemoController/transformCode.d.mts +14 -0
- package/useDemoController/transformCode.mjs +42 -0
- package/useDemoController/transpileClientSingleton.d.mts +10 -0
- package/useDemoController/transpileClientSingleton.mjs +83 -0
- package/useDemoController/transpileSource.d.mts +29 -0
- package/useDemoController/transpileSource.mjs +36 -0
- package/useDemoController/transpileWorker.d.mts +14 -0
- package/useDemoController/transpileWorker.mjs +41 -0
- package/useDemoController/types.d.mts +30 -0
- package/useDemoController/types.mjs +1 -0
- package/useDemoController/useDemoController.d.mts +54 -0
- package/useDemoController/useDemoController.mjs +186 -0
- package/useDemoController/useRunner.d.mts +53 -0
- package/useDemoController/useRunner.mjs +118 -0
- package/useDemoController/useVariantBuilds.d.mts +31 -0
- package/useDemoController/useVariantBuilds.mjs +188 -0
- package/useSearch/types.d.mts +1 -1
- package/useType/TypesDataProvider.d.mts +1 -1
- package/useType/TypesDataProvider.mjs +1 -2
- package/useType/useType.d.mts +1 -1
- package/useType/useTypeProp.d.mts +1 -1
- package/withDocsInfra/withDeploymentConfig.mjs +57 -0
- package/withDocsInfra/withDocsInfra.d.mts +14 -0
- package/withDocsInfra/withDocsInfra.mjs +25 -11
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Scope } from "./types.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* Transpiles and evaluates a module's source, returning its exports. Unlike the
|
|
4
|
+
* element runner the source is not normalized, so it must declare its own
|
|
5
|
+
* `export`s. Use this to register a file's exports under a specifier in a scope's
|
|
6
|
+
* `import` registry, letting other sources `import` from it.
|
|
7
|
+
*
|
|
8
|
+
* A one-shot wrapper over {@link compileModule}; callers that re-evaluate the same
|
|
9
|
+
* source as siblings change should cache the `compileModule` runner instead.
|
|
10
|
+
*/
|
|
11
|
+
export declare function importCode(code: string, scope?: Scope): Scope;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { compileModule } from "./compileModule.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* Transpiles and evaluates a module's source, returning its exports. Unlike the
|
|
4
|
+
* element runner the source is not normalized, so it must declare its own
|
|
5
|
+
* `export`s. Use this to register a file's exports under a specifier in a scope's
|
|
6
|
+
* `import` registry, letting other sources `import` from it.
|
|
7
|
+
*
|
|
8
|
+
* A one-shot wrapper over {@link compileModule}; callers that re-evaluate the same
|
|
9
|
+
* source as siblings change should cache the `compileModule` runner instead.
|
|
10
|
+
*/
|
|
11
|
+
export function importCode(code, scope) {
|
|
12
|
+
const exports = {};
|
|
13
|
+
compileModule(code)(scope?.import ?? {}, exports);
|
|
14
|
+
return exports;
|
|
15
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./useDemoController.mjs";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./useDemoController.mjs";
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import type { Scope } from "./types.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* Registry key (deliberately not a valid import specifier, so no demo source can
|
|
5
|
+
* import it) under which the scope may hold the exports object the entry should
|
|
6
|
+
* populate. When `buildScope` registers the main source so an extra can import the
|
|
7
|
+
* entry, it points this — and the entry's `./index` keys — at one object, so the
|
|
8
|
+
* rendered entry and the importing extra share a SINGLE evaluation.
|
|
9
|
+
*/
|
|
10
|
+
export declare const ENTRY_EXPORTS_KEY = "\0entry-exports";
|
|
11
|
+
/**
|
|
12
|
+
* Evaluates ALREADY-TRANSPILED entry code and returns the React node it exports
|
|
13
|
+
* as its default. This is the main-thread half of rendering the entry: the
|
|
14
|
+
* `evalCode`/`new Function` here cannot run in a Web Worker, while the
|
|
15
|
+
* `transformCode`/`normalizeCode` that produce `transpiled` can run off-thread.
|
|
16
|
+
*
|
|
17
|
+
* A source can expose its default three ways: an explicit `export default`, a
|
|
18
|
+
* call to the injected `render(value)` callback, or a bare leading expression
|
|
19
|
+
* promoted to a default export by `normalizeCode` (applied before transpiling).
|
|
20
|
+
* The resolved default is coerced to something renderable — an element is
|
|
21
|
+
* returned as-is, a component type is instantiated with no props, and a string is
|
|
22
|
+
* returned verbatim. A falsy/non-renderable default (or empty code) yields `null`.
|
|
23
|
+
*/
|
|
24
|
+
export declare function instantiateElement(transpiled: string, scope?: Scope): React.ReactNode;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { evalCode } from "./evalCode.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* Registry key (deliberately not a valid import specifier, so no demo source can
|
|
5
|
+
* import it) under which the scope may hold the exports object the entry should
|
|
6
|
+
* populate. When `buildScope` registers the main source so an extra can import the
|
|
7
|
+
* entry, it points this — and the entry's `./index` keys — at one object, so the
|
|
8
|
+
* rendered entry and the importing extra share a SINGLE evaluation.
|
|
9
|
+
*/
|
|
10
|
+
export const ENTRY_EXPORTS_KEY = '\0entry-exports';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Evaluates ALREADY-TRANSPILED entry code and returns the React node it exports
|
|
14
|
+
* as its default. This is the main-thread half of rendering the entry: the
|
|
15
|
+
* `evalCode`/`new Function` here cannot run in a Web Worker, while the
|
|
16
|
+
* `transformCode`/`normalizeCode` that produce `transpiled` can run off-thread.
|
|
17
|
+
*
|
|
18
|
+
* A source can expose its default three ways: an explicit `export default`, a
|
|
19
|
+
* call to the injected `render(value)` callback, or a bare leading expression
|
|
20
|
+
* promoted to a default export by `normalizeCode` (applied before transpiling).
|
|
21
|
+
* The resolved default is coerced to something renderable — an element is
|
|
22
|
+
* returned as-is, a component type is instantiated with no props, and a string is
|
|
23
|
+
* returned verbatim. A falsy/non-renderable default (or empty code) yields `null`.
|
|
24
|
+
*/
|
|
25
|
+
export function instantiateElement(transpiled, scope) {
|
|
26
|
+
// Evaluate into the shared registry object when present (see ENTRY_EXPORTS_KEY),
|
|
27
|
+
// so this entry and any extra that imports it see the same exports; otherwise a
|
|
28
|
+
// fresh object.
|
|
29
|
+
const shared = scope?.import?.[ENTRY_EXPORTS_KEY];
|
|
30
|
+
const exports = shared && typeof shared === 'object' ? shared : {};
|
|
31
|
+
const render = value => {
|
|
32
|
+
exports.default = value;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// Spread `scope` FIRST so the injected `render` escape hatch and `exports` sink
|
|
36
|
+
// always win over a same-named scope entry rather than being shadowed by one.
|
|
37
|
+
evalCode(transpiled, {
|
|
38
|
+
...scope,
|
|
39
|
+
render,
|
|
40
|
+
exports
|
|
41
|
+
});
|
|
42
|
+
const exported = exports.default;
|
|
43
|
+
if (!exported) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
if (/*#__PURE__*/React.isValidElement(exported)) {
|
|
47
|
+
return exported;
|
|
48
|
+
}
|
|
49
|
+
if (typeof exported === 'function') {
|
|
50
|
+
// Verified as a function above; treat it as a component and render it.
|
|
51
|
+
return /*#__PURE__*/React.createElement(exported);
|
|
52
|
+
}
|
|
53
|
+
if (typeof exported === 'string') {
|
|
54
|
+
return exported;
|
|
55
|
+
}
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ModuleRun } from "./types.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* Compiles ALREADY-TRANSPILED CommonJS-style module code into a runner that
|
|
4
|
+
* evaluates it against a `require` registry, writing into a caller-provided
|
|
5
|
+
* `exports`. This is the main-thread half of loading a module: `new Function`
|
|
6
|
+
* (and the eval it produces) cannot run in a Web Worker, so it stays here — while
|
|
7
|
+
* the heavy `transformCode` transpile that produces `transpiled` can run
|
|
8
|
+
* off-thread.
|
|
9
|
+
*
|
|
10
|
+
* `new Function` compiles the transpiled body once; the returned closure re-runs
|
|
11
|
+
* it with the current registry, so changing one sibling doesn't re-transpile the
|
|
12
|
+
* others. It can't close over the surrounding lexical scope. The caller registers
|
|
13
|
+
* the `exports` object BEFORE running, so a circular import re-entering this
|
|
14
|
+
* module sees the in-progress exports instead of looping forever (CommonJS
|
|
15
|
+
* semantics).
|
|
16
|
+
*/
|
|
17
|
+
export declare function instantiateModule(transpiled: string): ModuleRun;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { createRequire } from "./createRequire.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* Compiles ALREADY-TRANSPILED CommonJS-style module code into a runner that
|
|
5
|
+
* evaluates it against a `require` registry, writing into a caller-provided
|
|
6
|
+
* `exports`. This is the main-thread half of loading a module: `new Function`
|
|
7
|
+
* (and the eval it produces) cannot run in a Web Worker, so it stays here — while
|
|
8
|
+
* the heavy `transformCode` transpile that produces `transpiled` can run
|
|
9
|
+
* off-thread.
|
|
10
|
+
*
|
|
11
|
+
* `new Function` compiles the transpiled body once; the returned closure re-runs
|
|
12
|
+
* it with the current registry, so changing one sibling doesn't re-transpile the
|
|
13
|
+
* others. It can't close over the surrounding lexical scope. The caller registers
|
|
14
|
+
* the `exports` object BEFORE running, so a circular import re-entering this
|
|
15
|
+
* module sees the in-progress exports instead of looping forever (CommonJS
|
|
16
|
+
* semantics).
|
|
17
|
+
*/
|
|
18
|
+
export function instantiateModule(transpiled) {
|
|
19
|
+
// eslint-disable-next-line no-new-func
|
|
20
|
+
const evaluate = new Function('React', 'require', 'exports', transpiled);
|
|
21
|
+
return (imports, exports) => {
|
|
22
|
+
evaluate(React, createRequire(imports), exports);
|
|
23
|
+
};
|
|
24
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** A compiled CSS-module's single-file form, awaiting cross-file resolution. */
|
|
2
|
+
export interface CssModuleToResolve {
|
|
3
|
+
/** The registry key this module is exported under (used verbatim in the output). */
|
|
4
|
+
key: string;
|
|
5
|
+
/** The module's path within the demo (e.g. `dir/theme.module.css`) — drives `from` resolution. */
|
|
6
|
+
fileName: string;
|
|
7
|
+
/** Single-file exports; a `composes ... from` value still holds opaque placeholder tokens. */
|
|
8
|
+
exports: Record<string, string>;
|
|
9
|
+
/** `{ './other.module.css': { '<placeholder>': 'importedName' } }` from `compileCssModule`. */
|
|
10
|
+
imports: Record<string, Record<string, string>>;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Resolves cross-file `composes ... from` references across a set of compiled
|
|
14
|
+
* CSS modules, returning each module's final exports keyed by registry key.
|
|
15
|
+
*
|
|
16
|
+
* Each module's single-file exports may carry opaque placeholder tokens standing
|
|
17
|
+
* in for a class composed from a sibling (see {@link CssModuleToResolve.imports}).
|
|
18
|
+
* This swaps every token for the sibling's resolved scoped name — recursively, so
|
|
19
|
+
* a chain (`a` composes from `b` composes from `c`) flattens fully, with a stack
|
|
20
|
+
* guard breaking any composition cycle. A placeholder whose sibling or name is
|
|
21
|
+
* missing is dropped (the rest of the value survives).
|
|
22
|
+
*
|
|
23
|
+
* This runs on every assembly rather than being cached per file: a module's
|
|
24
|
+
* resolved value depends on its SIBLINGS' exports, which change when a sibling is
|
|
25
|
+
* edited even though this module's own source — and its cache entry — did not.
|
|
26
|
+
*/
|
|
27
|
+
export declare function resolveCssImports(modules: CssModuleToResolve[]): Map<string, Record<string, string>>;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/** A compiled CSS-module's single-file form, awaiting cross-file resolution. */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Resolves a relative CSS `composes ... from` path against the importing file,
|
|
5
|
+
* using plain `/` segment math (no `path` module, so it runs in the browser).
|
|
6
|
+
* Returns a normalized file name to look the sibling up by.
|
|
7
|
+
*/
|
|
8
|
+
function resolveRelativePath(fromFileName, importPath) {
|
|
9
|
+
const lastSlash = fromFileName.lastIndexOf('/');
|
|
10
|
+
const segments = lastSlash === -1 ? [] : fromFileName.slice(0, lastSlash).split('/');
|
|
11
|
+
for (const segment of importPath.split('/')) {
|
|
12
|
+
if (segment === '' || segment === '.') {
|
|
13
|
+
continue;
|
|
14
|
+
}
|
|
15
|
+
if (segment !== '..') {
|
|
16
|
+
segments.push(segment);
|
|
17
|
+
} else if (segments.length > 0 && segments[segments.length - 1] !== '..') {
|
|
18
|
+
// Step up into a real parent directory.
|
|
19
|
+
segments.pop();
|
|
20
|
+
} else {
|
|
21
|
+
// Already at (or above) the base — keep the `..` so a path that legitimately
|
|
22
|
+
// points above the importing file's directory (e.g. a root-level module that
|
|
23
|
+
// composes from `../shared/x`) survives instead of being silently flattened.
|
|
24
|
+
segments.push('..');
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return segments.join('/');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Replaces placeholder tokens in a space-separated class value, dropping empties. */
|
|
31
|
+
function applyReplacements(value, replacements) {
|
|
32
|
+
return value.split(' ').map(token => token in replacements ? replacements[token] : token).filter(Boolean).join(' ');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Resolves cross-file `composes ... from` references across a set of compiled
|
|
37
|
+
* CSS modules, returning each module's final exports keyed by registry key.
|
|
38
|
+
*
|
|
39
|
+
* Each module's single-file exports may carry opaque placeholder tokens standing
|
|
40
|
+
* in for a class composed from a sibling (see {@link CssModuleToResolve.imports}).
|
|
41
|
+
* This swaps every token for the sibling's resolved scoped name — recursively, so
|
|
42
|
+
* a chain (`a` composes from `b` composes from `c`) flattens fully, with a stack
|
|
43
|
+
* guard breaking any composition cycle. A placeholder whose sibling or name is
|
|
44
|
+
* missing is dropped (the rest of the value survives).
|
|
45
|
+
*
|
|
46
|
+
* This runs on every assembly rather than being cached per file: a module's
|
|
47
|
+
* resolved value depends on its SIBLINGS' exports, which change when a sibling is
|
|
48
|
+
* edited even though this module's own source — and its cache entry — did not.
|
|
49
|
+
*/
|
|
50
|
+
export function resolveCssImports(modules) {
|
|
51
|
+
const byFileName = new Map(modules.map(module => [module.fileName, module]));
|
|
52
|
+
const resolvedByKey = new Map();
|
|
53
|
+
const resolve = (module, ancestors) => {
|
|
54
|
+
const cached = resolvedByKey.get(module.key);
|
|
55
|
+
if (cached) {
|
|
56
|
+
return cached;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Build the token → resolved-value map from this module's cross-file imports.
|
|
60
|
+
const replacements = {};
|
|
61
|
+
for (const [importPath, tokens] of Object.entries(module.imports)) {
|
|
62
|
+
const target = byFileName.get(resolveRelativePath(module.fileName, importPath));
|
|
63
|
+
const targetExports = target && !ancestors.has(target.key) ? resolve(target, new Set(ancestors).add(module.key)) : {};
|
|
64
|
+
for (const [token, name] of Object.entries(tokens)) {
|
|
65
|
+
replacements[token] = targetExports[name] ?? '';
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const resolvedExports = {};
|
|
69
|
+
for (const [local, value] of Object.entries(module.exports)) {
|
|
70
|
+
resolvedExports[local] = applyReplacements(value, replacements);
|
|
71
|
+
}
|
|
72
|
+
resolvedByKey.set(module.key, resolvedExports);
|
|
73
|
+
return resolvedExports;
|
|
74
|
+
};
|
|
75
|
+
for (const module of modules) {
|
|
76
|
+
resolve(module, new Set());
|
|
77
|
+
}
|
|
78
|
+
return resolvedByKey;
|
|
79
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transpiles a TypeScript/JSX source string into plain JavaScript that uses
|
|
3
|
+
* CommonJS-style `require`/`exports`, ready to run with `evalCode`.
|
|
4
|
+
*
|
|
5
|
+
* JSX is emitted with the classic runtime (`React.createElement`), so `React`
|
|
6
|
+
* must be in scope when the result is evaluated.
|
|
7
|
+
*/
|
|
8
|
+
export declare function transformCode(code: string): string;
|
|
9
|
+
/**
|
|
10
|
+
* Promotes a bare leading expression to the module's default export, so a snippet
|
|
11
|
+
* like `<App />` can be run on its own. Sources that already declare their own
|
|
12
|
+
* exports — or lead with anything else — are returned unchanged.
|
|
13
|
+
*/
|
|
14
|
+
export declare function normalizeCode(code: string): string;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { transform as sucraseTransform } from 'sucrase';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Sucrase prefixes its output with a `"use strict";` prologue. The evaluated body
|
|
5
|
+
* already runs in strict mode inside the generated function, so the prologue is
|
|
6
|
+
* stripped to keep the result minimal.
|
|
7
|
+
*/
|
|
8
|
+
const USE_STRICT_PROLOGUE = '"use strict";';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Transpiles a TypeScript/JSX source string into plain JavaScript that uses
|
|
12
|
+
* CommonJS-style `require`/`exports`, ready to run with `evalCode`.
|
|
13
|
+
*
|
|
14
|
+
* JSX is emitted with the classic runtime (`React.createElement`), so `React`
|
|
15
|
+
* must be in scope when the result is evaluated.
|
|
16
|
+
*/
|
|
17
|
+
export function transformCode(code) {
|
|
18
|
+
const {
|
|
19
|
+
code: transpiled
|
|
20
|
+
} = sucraseTransform(code, {
|
|
21
|
+
transforms: ['jsx', 'typescript', 'imports'],
|
|
22
|
+
production: true
|
|
23
|
+
});
|
|
24
|
+
return transpiled.startsWith(USE_STRICT_PROLOGUE) ? transpiled.slice(USE_STRICT_PROLOGUE.length) : transpiled;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Matches a source that leads (after optional whitespace) with a renderable
|
|
29
|
+
* expression: a JSX element, a function declaration/expression, a zero-argument
|
|
30
|
+
* arrow function, or a class. Only the first line is captured so the rest of the
|
|
31
|
+
* source is preserved untouched.
|
|
32
|
+
*/
|
|
33
|
+
const LEADING_RENDERABLE = /^(\s*)(<[^>]*>|function[\s(]|\(\)\s*=>|class\s)(.*)/;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Promotes a bare leading expression to the module's default export, so a snippet
|
|
37
|
+
* like `<App />` can be run on its own. Sources that already declare their own
|
|
38
|
+
* exports — or lead with anything else — are returned unchanged.
|
|
39
|
+
*/
|
|
40
|
+
export function normalizeCode(code) {
|
|
41
|
+
return code.replace(LEADING_RENDERABLE, '$1export default $2$3');
|
|
42
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Transpile } from "./transpileSource.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* Returns the page-shared {@link Transpile}, creating the worker (or main-thread
|
|
4
|
+
* fallback) on first call and reusing it thereafter. Call this from a browser
|
|
5
|
+
* effect — never at module top level — so the worker is never constructed during
|
|
6
|
+
* SSR.
|
|
7
|
+
*/
|
|
8
|
+
export declare function getTranspile(): Promise<Transpile>;
|
|
9
|
+
/** Test-only: drop the cached singleton (and terminate any worker it created). */
|
|
10
|
+
export declare function resetTranspileClientForTests(): void;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { createTranspileWorkerClient } from "./createTranspileWorkerClient.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* The page-shared transpile, created lazily once and reused across every demo
|
|
4
|
+
* controller. Transpilation is stateless (sucrase is pure), so — unlike the
|
|
5
|
+
* per-provider syntax-highlight worker — a single worker serves the whole page:
|
|
6
|
+
* fewer worker spin-ups, one shared module-graph, and a process-wide compile
|
|
7
|
+
* cache. It is intentionally never terminated; an idle worker is cheap.
|
|
8
|
+
*/
|
|
9
|
+
let transpilePromise = null;
|
|
10
|
+
let activeClient = null;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Lazily loads the main-thread transpile. The heavy `transpileSource` (sucrase + the
|
|
14
|
+
* import parser) rides in its own chunk, fetched only when there's no worker — so a
|
|
15
|
+
* worker-capable environment never downloads it.
|
|
16
|
+
*/
|
|
17
|
+
async function loadMainThreadTranspile() {
|
|
18
|
+
const {
|
|
19
|
+
transpileSource
|
|
20
|
+
} = await import("./transpileSource.mjs");
|
|
21
|
+
// `async` so a transpile failure rejects (matching the worker client) rather than
|
|
22
|
+
// throwing synchronously.
|
|
23
|
+
return async (source, options, signal) => {
|
|
24
|
+
if (signal?.aborted) {
|
|
25
|
+
throw signal.reason;
|
|
26
|
+
}
|
|
27
|
+
return transpileSource(source, options);
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
async function createTranspile() {
|
|
31
|
+
// The live transpile implementation — a worker, or the main-thread fallback. Held
|
|
32
|
+
// in a closure variable so a worker crash can swap it WITHOUT the caller (which
|
|
33
|
+
// caches the returned function) having to re-fetch.
|
|
34
|
+
let delegate;
|
|
35
|
+
const fallBackToMainThread = () => {
|
|
36
|
+
// The same worker URL would just fail again, so don't rebuild it — load the
|
|
37
|
+
// main-thread transpile once and route every later call through it. Until it
|
|
38
|
+
// resolves, calls await the same promise so none are dropped.
|
|
39
|
+
const mainThread = loadMainThreadTranspile();
|
|
40
|
+
delegate = (source, options, signal) => mainThread.then(run => run(source, options, signal));
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// A browser with module-worker support runs transpilation off the UI thread.
|
|
44
|
+
// `createTranspileWorkerClient()` throws synchronously where module workers are
|
|
45
|
+
// unsupported (SSR, no `Worker`, older Safari) — caught here to fall back to a
|
|
46
|
+
// main-thread transpile, still async so the caller has one code path.
|
|
47
|
+
try {
|
|
48
|
+
const client = createTranspileWorkerClient(() => {
|
|
49
|
+
// The shared worker died (a load/init failure). Its in-flight requests were
|
|
50
|
+
// already rejected by the client (they retry from the next edit); swap to the
|
|
51
|
+
// main thread so live editing keeps working instead of failing every build.
|
|
52
|
+
activeClient = null;
|
|
53
|
+
fallBackToMainThread();
|
|
54
|
+
});
|
|
55
|
+
activeClient = client;
|
|
56
|
+
delegate = client.transpile;
|
|
57
|
+
} catch {
|
|
58
|
+
fallBackToMainThread();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// A STABLE wrapper: callers cache it, and `delegate` swaps underneath on a crash.
|
|
62
|
+
return (source, options, signal) => delegate(source, options, signal);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Returns the page-shared {@link Transpile}, creating the worker (or main-thread
|
|
67
|
+
* fallback) on first call and reusing it thereafter. Call this from a browser
|
|
68
|
+
* effect — never at module top level — so the worker is never constructed during
|
|
69
|
+
* SSR.
|
|
70
|
+
*/
|
|
71
|
+
export function getTranspile() {
|
|
72
|
+
if (!transpilePromise) {
|
|
73
|
+
transpilePromise = createTranspile();
|
|
74
|
+
}
|
|
75
|
+
return transpilePromise;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Test-only: drop the cached singleton (and terminate any worker it created). */
|
|
79
|
+
export function resetTranspileClientForTests() {
|
|
80
|
+
activeClient?.terminate();
|
|
81
|
+
activeClient = null;
|
|
82
|
+
transpilePromise = null;
|
|
83
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** How to transform one source before sucrase transpiles it. */
|
|
2
|
+
export interface TranspileSourceOptions {
|
|
3
|
+
/** The file's path within the demo (e.g. `dir/util.ts`) — resolves relative imports when `nested`. */
|
|
4
|
+
fileName?: string;
|
|
5
|
+
/** Rewrite relative imports to absolute scope keys (subdirectory demos). */
|
|
6
|
+
nested?: boolean;
|
|
7
|
+
/** Promote a bare leading expression to a default export — used for the entry source. */
|
|
8
|
+
normalize?: boolean;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The full source → runnable-JS transform, run for one file: rewrite relative
|
|
12
|
+
* imports to absolute scope keys (when `nested`), promote a bare leading
|
|
13
|
+
* expression to a default export (the `normalize` entry case), then transpile
|
|
14
|
+
* TS/JSX to CommonJS via sucrase.
|
|
15
|
+
*
|
|
16
|
+
* Pure `string → string` with no DOM, React, or `new Function`, so it runs
|
|
17
|
+
* identically on the main thread or inside a Web Worker — it is the unit the
|
|
18
|
+
* transpile worker offloads off the UI thread. The `new Function`/eval that turns
|
|
19
|
+
* the result into a module stays on the main thread (see `instantiateModule`).
|
|
20
|
+
*/
|
|
21
|
+
export declare function transpileSource(source: string, options?: TranspileSourceOptions): string;
|
|
22
|
+
/**
|
|
23
|
+
* Transpiles one source to runnable JS asynchronously — on the main thread, or
|
|
24
|
+
* (preferably) in a Web Worker off the UI thread. An optional `AbortSignal` drops
|
|
25
|
+
* a stale request when a newer edit supersedes it. Mirrors {@link transpileSource}
|
|
26
|
+
* but async, so a worker-backed and a main-thread implementation are
|
|
27
|
+
* interchangeable.
|
|
28
|
+
*/
|
|
29
|
+
export type Transpile = (source: string, options?: TranspileSourceOptions, signal?: AbortSignal) => Promise<string>;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { absolutizeImports } from "./absolutizeImports.mjs";
|
|
2
|
+
import { normalizeCode, transformCode } from "./transformCode.mjs";
|
|
3
|
+
|
|
4
|
+
/** How to transform one source before sucrase transpiles it. */
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The full source → runnable-JS transform, run for one file: rewrite relative
|
|
8
|
+
* imports to absolute scope keys (when `nested`), promote a bare leading
|
|
9
|
+
* expression to a default export (the `normalize` entry case), then transpile
|
|
10
|
+
* TS/JSX to CommonJS via sucrase.
|
|
11
|
+
*
|
|
12
|
+
* Pure `string → string` with no DOM, React, or `new Function`, so it runs
|
|
13
|
+
* identically on the main thread or inside a Web Worker — it is the unit the
|
|
14
|
+
* transpile worker offloads off the UI thread. The `new Function`/eval that turns
|
|
15
|
+
* the result into a module stays on the main thread (see `instantiateModule`).
|
|
16
|
+
*/
|
|
17
|
+
export function transpileSource(source, options = {}) {
|
|
18
|
+
const {
|
|
19
|
+
fileName = '',
|
|
20
|
+
nested = false,
|
|
21
|
+
normalize = false
|
|
22
|
+
} = options;
|
|
23
|
+
let result = nested ? absolutizeImports(source, fileName) : source;
|
|
24
|
+
if (normalize) {
|
|
25
|
+
result = normalizeCode(result);
|
|
26
|
+
}
|
|
27
|
+
return transformCode(result);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Transpiles one source to runnable JS asynchronously — on the main thread, or
|
|
32
|
+
* (preferably) in a Web Worker off the UI thread. An optional `AbortSignal` drops
|
|
33
|
+
* a stale request when a newer edit supersedes it. Mirrors {@link transpileSource}
|
|
34
|
+
* but async, so a worker-backed and a main-thread implementation are
|
|
35
|
+
* interchangeable.
|
|
36
|
+
*/
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Worker entry point for off-main-thread transpilation.
|
|
3
|
+
*
|
|
4
|
+
* For each request the main thread posts `{ type: 'transpile', id, source,
|
|
5
|
+
* options }`; the worker runs the same {@link transpileSource} body the main
|
|
6
|
+
* thread would (so the output is byte-identical) and posts back
|
|
7
|
+
* `{ type: 'transpile', id, ok, code? | error? }`.
|
|
8
|
+
*
|
|
9
|
+
* Stateless and dependency-free of any handshake: sucrase is pure, so — unlike
|
|
10
|
+
* the syntax-highlight worker — there is no `init`/grammar payload and no request
|
|
11
|
+
* queue. The heavy `sucrase` (and the import parser) are imported here, inside the
|
|
12
|
+
* worker chunk, so they stay off the main thread's bundle.
|
|
13
|
+
*/
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Worker entry point for off-main-thread transpilation.
|
|
3
|
+
*
|
|
4
|
+
* For each request the main thread posts `{ type: 'transpile', id, source,
|
|
5
|
+
* options }`; the worker runs the same {@link transpileSource} body the main
|
|
6
|
+
* thread would (so the output is byte-identical) and posts back
|
|
7
|
+
* `{ type: 'transpile', id, ok, code? | error? }`.
|
|
8
|
+
*
|
|
9
|
+
* Stateless and dependency-free of any handshake: sucrase is pure, so — unlike
|
|
10
|
+
* the syntax-highlight worker — there is no `init`/grammar payload and no request
|
|
11
|
+
* queue. The heavy `sucrase` (and the import parser) are imported here, inside the
|
|
12
|
+
* worker chunk, so they stay off the main thread's bundle.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { transpileSource } from "./transpileSource.mjs";
|
|
16
|
+
// `self` in a dedicated worker exposes `postMessage`/`addEventListener` on the
|
|
17
|
+
// global scope, but the default `tsconfig` `lib` here doesn't include the
|
|
18
|
+
// `webworker` lib so we cast once via a typed alias instead of per-call.
|
|
19
|
+
const workerScope = self;
|
|
20
|
+
workerScope.addEventListener('message', event => {
|
|
21
|
+
const data = event.data;
|
|
22
|
+
if (data.type !== 'transpile') {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
try {
|
|
26
|
+
const code = transpileSource(data.source, data.options);
|
|
27
|
+
workerScope.postMessage({
|
|
28
|
+
type: 'transpile',
|
|
29
|
+
id: data.id,
|
|
30
|
+
ok: true,
|
|
31
|
+
code
|
|
32
|
+
});
|
|
33
|
+
} catch (error) {
|
|
34
|
+
workerScope.postMessage({
|
|
35
|
+
type: 'transpile',
|
|
36
|
+
id: data.id,
|
|
37
|
+
ok: false,
|
|
38
|
+
error
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The set of identifiers exposed to evaluated source as local variables.
|
|
3
|
+
*
|
|
4
|
+
* Every own-enumerable key becomes a binding in the generated function, with two
|
|
5
|
+
* reserved keys handled specially: `import` is the registry consulted by
|
|
6
|
+
* (transpiled) `import` statements, and `default` is never bound because it is a
|
|
7
|
+
* reserved word and cannot name a function parameter.
|
|
8
|
+
*
|
|
9
|
+
* A cleanroom reimplementation of the `react-runner` runtime (MIT, © 2019 Neo);
|
|
10
|
+
* the public surface (`useRunner`, `importCode`, `Runner`, …) mirrors that
|
|
11
|
+
* library so it can be vendored as a drop-in replacement.
|
|
12
|
+
*/
|
|
13
|
+
export type Scope = Record<string, unknown> & {
|
|
14
|
+
/** Registry consulted by transpiled `import` statements, keyed by specifier. */
|
|
15
|
+
import?: Record<string, unknown>;
|
|
16
|
+
};
|
|
17
|
+
/** Inputs shared by every entry point that transpiles and evaluates a source string. */
|
|
18
|
+
export interface RunnerOptions {
|
|
19
|
+
/** The TypeScript/JSX source to transpile and evaluate. */
|
|
20
|
+
code: string;
|
|
21
|
+
/** Identifiers (and an `import` registry) exposed to the evaluated source. */
|
|
22
|
+
scope?: Scope;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Evaluates a compiled module against the `import` registry, writing its exports
|
|
26
|
+
* into the passed `exports` object. The registry registers one of these per
|
|
27
|
+
* module; it runs on first `require`, so circular imports resolve via the
|
|
28
|
+
* in-progress `exports` set before the body runs.
|
|
29
|
+
*/
|
|
30
|
+
export type ModuleRun = (imports: Record<string, unknown>, exports: Scope) => void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import type { ControlledCode } from '@mui/internal-docs-infra/CodeHighlighter/types';
|
|
3
|
+
import type { CodeControllerContext } from '@mui/internal-docs-infra/CodeControllerContext';
|
|
4
|
+
export interface UseDemoControllerResult {
|
|
5
|
+
/** The controlled source, keyed by variant. `null` until the first edit. */
|
|
6
|
+
code: ControlledCode | null;
|
|
7
|
+
/**
|
|
8
|
+
* Updates the controlled source (e.g. as a reader edits a variant). Typed as the
|
|
9
|
+
* context's `setCode` (`Dispatch<SetStateAction>`) so the whole result drops straight
|
|
10
|
+
* into a `CodeControllerContext.Provider` with no cast.
|
|
11
|
+
*/
|
|
12
|
+
setCode: NonNullable<CodeControllerContext['setCode']>;
|
|
13
|
+
/**
|
|
14
|
+
* One live preview node per variant, keyed by variant — for the variants that
|
|
15
|
+
* have finished building. `undefined` until at least one is ready, so a host can
|
|
16
|
+
* keep showing its build-time render in the meantime (an in-flight variant simply
|
|
17
|
+
* has no entry). Drop straight into `CodeControllerContext` as `components`.
|
|
18
|
+
*
|
|
19
|
+
* Each node is the lazy `DemoRunner`, so the host MUST render it under a `Suspense`
|
|
20
|
+
* boundary; that boundary's fallback should be the build-time render so a freshly
|
|
21
|
+
* mounted preview (e.g. the first edit after `reset()`) shows the original rather
|
|
22
|
+
* than an empty frame while the chunk resolves. `CodeHighlighterClient` does this.
|
|
23
|
+
*/
|
|
24
|
+
components: Record<string, React.ReactNode> | undefined;
|
|
25
|
+
/** Current error message per variant (or `null` when it renders cleanly). */
|
|
26
|
+
errors: Record<string, string | null>;
|
|
27
|
+
/**
|
|
28
|
+
* Warms the lazy live-editing engine chunks when a block activates for editing.
|
|
29
|
+
* Drop straight into `CodeControllerContext` as `onActivate`; the host calls it with
|
|
30
|
+
* which file kinds the demo spans (`js`/`css`).
|
|
31
|
+
*/
|
|
32
|
+
onActivate: (deps: {
|
|
33
|
+
js: boolean;
|
|
34
|
+
css: boolean;
|
|
35
|
+
}) => void;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Drives the live previews for a demo controller. Owns the controlled `code` state,
|
|
39
|
+
* transpiles each variant OFF the main thread (via a shared Web Worker, with a
|
|
40
|
+
* main-thread fallback) and renders the ready ones through {@link DemoRunner}, and
|
|
41
|
+
* collects the per-variant `errors` — returning exactly the `{ code, setCode,
|
|
42
|
+
* components, errors }` shape a `CodeControllerContext` provider expects, so a host
|
|
43
|
+
* wires it up in one line and reads results back through `useDemo`.
|
|
44
|
+
*
|
|
45
|
+
* Because transpilation is async, a variant joins `components` only once its build
|
|
46
|
+
* resolves; editing never blocks the UI thread, and an in-flight variant shows its
|
|
47
|
+
* fallback rather than a flash of empty preview. Keeping `code` unset until the
|
|
48
|
+
* first `setCode` lets the host serve its build-time/server render first.
|
|
49
|
+
*
|
|
50
|
+
* By default the controlled code is also mirrored across same-origin tabs of the same
|
|
51
|
+
* page (via the `crossTabSync` option), so a reader editing a demo in a Chrome split
|
|
52
|
+
* view sees it update in both panes.
|
|
53
|
+
*/
|
|
54
|
+
export declare function useDemoController(): UseDemoControllerResult;
|