@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,89 @@
|
|
|
1
|
+
import postcss from 'postcss';
|
|
2
|
+
import autoprefixer from 'autoprefixer';
|
|
3
|
+
import modulesValues from 'postcss-modules-values';
|
|
4
|
+
import localByDefault from 'postcss-modules-local-by-default';
|
|
5
|
+
import extractImports from 'postcss-modules-extract-imports';
|
|
6
|
+
import scope from 'postcss-modules-scope';
|
|
7
|
+
import relativeColorSyntax from '@csstools/postcss-relative-color-syntax';
|
|
8
|
+
import lightDarkFunction from '@csstools/postcss-light-dark-function';
|
|
9
|
+
import steppedValueFunctions from '@csstools/postcss-stepped-value-functions';
|
|
10
|
+
import { extractICSS } from 'icss-utils';
|
|
11
|
+
import { hashString } from "./hashString.mjs";
|
|
12
|
+
import { currentBrowserTarget } from "./currentBrowserTarget.mjs";
|
|
13
|
+
/**
|
|
14
|
+
* Shared autoprefixer plugin, created once when this lazy chunk first loads. It
|
|
15
|
+
* targets the EXACT browser the preview is running in (see
|
|
16
|
+
* {@link currentBrowserTarget}) rather than a broad range — a demo's CSS only has to
|
|
17
|
+
* work in the visitor's own browser, so a current browser gets no prefixes at all.
|
|
18
|
+
* `ignoreUnknownVersions` makes a browser newer than the bundled caniuse-lite resolve
|
|
19
|
+
* to no prefixes (correct) instead of throwing.
|
|
20
|
+
*/
|
|
21
|
+
const autoprefix = autoprefixer({
|
|
22
|
+
overrideBrowserslist: currentBrowserTarget(),
|
|
23
|
+
ignoreUnknownVersions: true
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Lowers the handful of modern CSS features that aren't yet Baseline Widely Available
|
|
28
|
+
* — relative color syntax (`rgb(from …)`), `light-dark()`, and the stepped-value math
|
|
29
|
+
* (`round()`/`mod()`/`rem()`) — so a demo using them still renders while live-editing
|
|
30
|
+
* on a browser older than the feature, matching what Lightning CSS does at build time.
|
|
31
|
+
* `preserve: true` keeps the modern syntax alongside the fallback, so a current browser
|
|
32
|
+
* uses it natively and the fallback is harmless dead weight. Everything else on
|
|
33
|
+
* Lightning CSS's lowering list (nesting, `color-mix()`, lab/lch, media-query ranges,
|
|
34
|
+
* `:is()`/`:not()`, …) is already widely available and needs no transform.
|
|
35
|
+
*/
|
|
36
|
+
const lowerModernCss = [relativeColorSyntax({
|
|
37
|
+
preserve: true
|
|
38
|
+
}), lightDarkFunction({
|
|
39
|
+
preserve: true
|
|
40
|
+
}), steppedValueFunctions({
|
|
41
|
+
preserve: true
|
|
42
|
+
})];
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The PostCSS implementation behind {@link import('./compileCssModule').compileCssModule}.
|
|
46
|
+
*
|
|
47
|
+
* This module statically imports the WHOLE CSS toolchain (postcss + autoprefixer +
|
|
48
|
+
* the ICSS Modules plugins), so it is loaded behind ONE dynamic `import()` from the
|
|
49
|
+
* light `compileCssModule` shell — the bundler then emits a single `compileCss`
|
|
50
|
+
* chunk instead of a separate chunk per npm package. None of this enters the main
|
|
51
|
+
* bundle; it is paid for only when a demo first compiles CSS.
|
|
52
|
+
*
|
|
53
|
+
* Runs the same ICSS plugin chain css-loader uses (values → local-by-default →
|
|
54
|
+
* extract-imports → scope) followed by {@link lowerModernCss} and autoprefixer, then
|
|
55
|
+
* `extractICSS` for the exports. See the shell's docs for the full semantics.
|
|
56
|
+
*/
|
|
57
|
+
export async function compileCssModuleWithPostcss(source, options = {}) {
|
|
58
|
+
const suffix = hashString(options.hashSeed ?? source).padStart(5, '0');
|
|
59
|
+
const result = await postcss([modulesValues, localByDefault({
|
|
60
|
+
mode: 'local'
|
|
61
|
+
}), extractImports(), scope({
|
|
62
|
+
generateScopedName: name => `${name}-${suffix}`,
|
|
63
|
+
exportGlobals: false
|
|
64
|
+
}), ...lowerModernCss, autoprefix
|
|
65
|
+
// `from` is the file name, so a `CssSyntaxError` reads `name.module.css:L:C: …`.
|
|
66
|
+
]).process(source, {
|
|
67
|
+
from: options.fileName
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// `extractICSS` mutates the root (removing the `:export`/`:import` rules), so
|
|
71
|
+
// re-stringify from the root afterwards rather than reading the stale `css`.
|
|
72
|
+
const {
|
|
73
|
+
icssImports,
|
|
74
|
+
icssExports
|
|
75
|
+
} = extractICSS(result.root, true);
|
|
76
|
+
return {
|
|
77
|
+
css: result.root.toString(),
|
|
78
|
+
exports: icssExports,
|
|
79
|
+
imports: icssImports
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** The PostCSS implementation behind `prefixCss` — {@link lowerModernCss} + autoprefix, no scoping. */
|
|
84
|
+
export async function prefixCssWithPostcss(source, fileName) {
|
|
85
|
+
const result = await postcss([...lowerModernCss, autoprefix]).process(source, {
|
|
86
|
+
from: fileName
|
|
87
|
+
});
|
|
88
|
+
return result.css;
|
|
89
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ModuleRun } from "./types.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* Transpiles a module's source and returns a runner that evaluates it against a
|
|
4
|
+
* `require` registry, writing into a caller-provided `exports`. A synchronous
|
|
5
|
+
* convenience that pairs the two halves — `transformCode` (the heavy sucrase
|
|
6
|
+
* transpile) and {@link instantiateModule} (`new Function` + run) — for callers
|
|
7
|
+
* that transpile on the main thread. The async/worker path splits them: it
|
|
8
|
+
* transpiles off-thread and calls `instantiateModule` directly with the result.
|
|
9
|
+
*
|
|
10
|
+
* The returned runner injects `React` and a `require` shim backed by the passed
|
|
11
|
+
* `imports`, mirroring the `{ import }` scope that {@link importCode} evaluates
|
|
12
|
+
* against.
|
|
13
|
+
*/
|
|
14
|
+
export declare function compileModule(code: string): ModuleRun;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { instantiateModule } from "./instantiateModule.mjs";
|
|
2
|
+
import { transformCode } from "./transformCode.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* Transpiles a module's source and returns a runner that evaluates it against a
|
|
5
|
+
* `require` registry, writing into a caller-provided `exports`. A synchronous
|
|
6
|
+
* convenience that pairs the two halves — `transformCode` (the heavy sucrase
|
|
7
|
+
* transpile) and {@link instantiateModule} (`new Function` + run) — for callers
|
|
8
|
+
* that transpile on the main thread. The async/worker path splits them: it
|
|
9
|
+
* transpiles off-thread and calls `instantiateModule` directly with the result.
|
|
10
|
+
*
|
|
11
|
+
* The returned runner injects `React` and a `require` shim backed by the passed
|
|
12
|
+
* `imports`, mirroring the `{ import }` scope that {@link importCode} evaluates
|
|
13
|
+
* against.
|
|
14
|
+
*/
|
|
15
|
+
export function compileModule(code) {
|
|
16
|
+
return instantiateModule(transformCode(code));
|
|
17
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Specifier prefix under which an extra file's exports are registered in the
|
|
3
|
+
* runner scope. Relative imports between extra files (and from the main source)
|
|
4
|
+
* are rewritten to `<prefix><path-from-demo-root>`, so the runner's exact-key
|
|
5
|
+
* `require` resolves them no matter where the importing file sits.
|
|
6
|
+
*
|
|
7
|
+
* Kept in this tiny module — not in `absolutizeImports` — so the static-graph
|
|
8
|
+
* `collectSources`, which only needs this constant, doesn't pull the heavy
|
|
9
|
+
* `absolutizeImports` (sucrase-adjacent: the import parser + rewriter) into the
|
|
10
|
+
* main bundle. That function then folds into the lazy `transpileSource` chunk
|
|
11
|
+
* instead of hoisting out as its own.
|
|
12
|
+
*/
|
|
13
|
+
export declare const SCOPE_IMPORT_PREFIX = "@mui/internal-docs-infra/useDemoController/imports/";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Specifier prefix under which an extra file's exports are registered in the
|
|
3
|
+
* runner scope. Relative imports between extra files (and from the main source)
|
|
4
|
+
* are rewritten to `<prefix><path-from-demo-root>`, so the runner's exact-key
|
|
5
|
+
* `require` resolves them no matter where the importing file sits.
|
|
6
|
+
*
|
|
7
|
+
* Kept in this tiny module — not in `absolutizeImports` — so the static-graph
|
|
8
|
+
* `collectSources`, which only needs this constant, doesn't pull the heavy
|
|
9
|
+
* `absolutizeImports` (sucrase-adjacent: the import parser + rewriter) into the
|
|
10
|
+
* main bundle. That function then folds into the lazy `transpileSource` chunk
|
|
11
|
+
* instead of hoisting out as its own.
|
|
12
|
+
*/
|
|
13
|
+
export const SCOPE_IMPORT_PREFIX = '@mui/internal-docs-infra/useDemoController/imports/';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Builds the `require` shim handed to transpiled `import` statements. There is no
|
|
3
|
+
* real module system at runtime, so specifiers are resolved by exact lookup in
|
|
4
|
+
* the provided registry; anything missing throws.
|
|
5
|
+
*
|
|
6
|
+
* Lookup uses `Object.prototype.hasOwnProperty` so inherited members (e.g.
|
|
7
|
+
* `toString`) are never mistaken for registered modules, and a registry created
|
|
8
|
+
* with `Object.create(null)` still works.
|
|
9
|
+
*/
|
|
10
|
+
export declare function createRequire(modules?: Record<string, unknown>): (specifier: string) => unknown;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Builds the `require` shim handed to transpiled `import` statements. There is no
|
|
3
|
+
* real module system at runtime, so specifiers are resolved by exact lookup in
|
|
4
|
+
* the provided registry; anything missing throws.
|
|
5
|
+
*
|
|
6
|
+
* Lookup uses `Object.prototype.hasOwnProperty` so inherited members (e.g.
|
|
7
|
+
* `toString`) are never mistaken for registered modules, and a registry created
|
|
8
|
+
* with `Object.create(null)` still works.
|
|
9
|
+
*/
|
|
10
|
+
export function createRequire(modules = {}) {
|
|
11
|
+
return specifier => {
|
|
12
|
+
if (!Object.prototype.hasOwnProperty.call(modules, specifier)) {
|
|
13
|
+
throw new Error(`Module not found: '${specifier}'`);
|
|
14
|
+
}
|
|
15
|
+
return modules[specifier];
|
|
16
|
+
};
|
|
17
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Transpile } from "./transpileSource.mjs";
|
|
2
|
+
export interface TranspileWorkerClient {
|
|
3
|
+
/**
|
|
4
|
+
* Transpiles one source inside the worker. Returns the same string the
|
|
5
|
+
* synchronous `transpileSource` would. If `signal` aborts before the worker
|
|
6
|
+
* responds, the in-flight request is dropped and the promise rejects with
|
|
7
|
+
* `signal.reason`.
|
|
8
|
+
*/
|
|
9
|
+
transpile: Transpile;
|
|
10
|
+
terminate(): void;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Creates a worker-backed {@link Transpile}. The caller owns the lifecycle and
|
|
14
|
+
* must call `terminate()` when done. Each client owns exactly one `Worker`;
|
|
15
|
+
* concurrent in-flight requests are demuxed by a monotonically increasing `id`.
|
|
16
|
+
*
|
|
17
|
+
* If the worker dies unexpectedly (a load/parse failure, or a message that fails
|
|
18
|
+
* to deserialize), every in-flight request is rejected and `onFatal` fires — so a
|
|
19
|
+
* crash surfaces as a rejected build rather than a request that hangs forever. The
|
|
20
|
+
* client then latches `dead`: later `transpile` calls reject immediately instead of
|
|
21
|
+
* posting to a worker that will never reply. The owner (`transpileClientSingleton`)
|
|
22
|
+
* uses `onFatal` to swap in the main-thread transpile so editing keeps working.
|
|
23
|
+
*
|
|
24
|
+
* Mirrors `CodeProvider/createParseSourceWorkerClient` — minus the grammar
|
|
25
|
+
* init/register handshake, since transpilation is stateless.
|
|
26
|
+
*/
|
|
27
|
+
export declare function createTranspileWorkerClient(onFatal?: (error: Error) => void): TranspileWorkerClient;
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a worker-backed {@link Transpile}. The caller owns the lifecycle and
|
|
3
|
+
* must call `terminate()` when done. Each client owns exactly one `Worker`;
|
|
4
|
+
* concurrent in-flight requests are demuxed by a monotonically increasing `id`.
|
|
5
|
+
*
|
|
6
|
+
* If the worker dies unexpectedly (a load/parse failure, or a message that fails
|
|
7
|
+
* to deserialize), every in-flight request is rejected and `onFatal` fires — so a
|
|
8
|
+
* crash surfaces as a rejected build rather than a request that hangs forever. The
|
|
9
|
+
* client then latches `dead`: later `transpile` calls reject immediately instead of
|
|
10
|
+
* posting to a worker that will never reply. The owner (`transpileClientSingleton`)
|
|
11
|
+
* uses `onFatal` to swap in the main-thread transpile so editing keeps working.
|
|
12
|
+
*
|
|
13
|
+
* Mirrors `CodeProvider/createParseSourceWorkerClient` — minus the grammar
|
|
14
|
+
* init/register handshake, since transpilation is stateless.
|
|
15
|
+
*/
|
|
16
|
+
export function createTranspileWorkerClient(onFatal) {
|
|
17
|
+
// Module workers are required (the worker uses `import` statements). On browsers
|
|
18
|
+
// that expose `Worker` but reject `{ type: 'module' }` (older Safari, some
|
|
19
|
+
// embedded webviews) the constructor throws synchronously. Surface that as a
|
|
20
|
+
// tagged error so callers can fall back cleanly to the main-thread transpile.
|
|
21
|
+
let worker;
|
|
22
|
+
try {
|
|
23
|
+
worker = new Worker(new URL('./transpileWorker', import.meta.url), {
|
|
24
|
+
type: 'module'
|
|
25
|
+
});
|
|
26
|
+
} catch (cause) {
|
|
27
|
+
throw new Error('Module workers are not supported in this environment', {
|
|
28
|
+
cause
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
const pending = new Map();
|
|
32
|
+
let nextId = 1;
|
|
33
|
+
// Latched once the worker dies or is terminated, so later `transpile` calls reject
|
|
34
|
+
// immediately rather than hanging on a worker that will never respond.
|
|
35
|
+
let dead = false;
|
|
36
|
+
|
|
37
|
+
// Reject + forget every in-flight request with `reason`, detaching their abort
|
|
38
|
+
// listeners first. Shared by `terminate()` (intentional) and the fatal-error path.
|
|
39
|
+
const rejectAllPending = reason => {
|
|
40
|
+
for (const entry of pending.values()) {
|
|
41
|
+
if (entry.signal && entry.onAbort) {
|
|
42
|
+
entry.signal.removeEventListener('abort', entry.onAbort);
|
|
43
|
+
}
|
|
44
|
+
entry.reject(reason);
|
|
45
|
+
}
|
|
46
|
+
pending.clear();
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// The worker died unexpectedly — a load/parse failure, or a message that could not
|
|
50
|
+
// be deserialized. (A transpile that merely throws is caught inside the worker and
|
|
51
|
+
// reported as an `ok: false` response, so it never reaches here.) Reject everything
|
|
52
|
+
// in flight, latch `dead`, terminate the husk, and let the owner recover via
|
|
53
|
+
// `onFatal`. Idempotent: a second fatal event (or a later `terminate`) is a no-op.
|
|
54
|
+
const handleFatal = error => {
|
|
55
|
+
if (dead) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
dead = true;
|
|
59
|
+
rejectAllPending(error);
|
|
60
|
+
worker.terminate();
|
|
61
|
+
onFatal?.(error);
|
|
62
|
+
};
|
|
63
|
+
worker.addEventListener('message', event => {
|
|
64
|
+
const data = event.data;
|
|
65
|
+
if (data.type !== 'transpile') {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const entry = pending.get(data.id);
|
|
69
|
+
if (!entry) {
|
|
70
|
+
// Aborted before the response arrived — drop silently.
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
pending.delete(data.id);
|
|
74
|
+
if (entry.signal && entry.onAbort) {
|
|
75
|
+
entry.signal.removeEventListener('abort', entry.onAbort);
|
|
76
|
+
}
|
|
77
|
+
if (data.ok) {
|
|
78
|
+
entry.resolve(data.code);
|
|
79
|
+
} else {
|
|
80
|
+
entry.reject(data.error);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
worker.addEventListener('error', event => {
|
|
84
|
+
handleFatal(new Error(event.message || 'Transpile worker errored'));
|
|
85
|
+
});
|
|
86
|
+
worker.addEventListener('messageerror', () => {
|
|
87
|
+
handleFatal(new Error('Transpile worker received an undeserializable message'));
|
|
88
|
+
});
|
|
89
|
+
const transpile = (source, options, signal) => {
|
|
90
|
+
if (dead) {
|
|
91
|
+
return Promise.reject(new Error('Transpile worker is no longer available'));
|
|
92
|
+
}
|
|
93
|
+
if (signal?.aborted) {
|
|
94
|
+
return Promise.reject(signal.reason);
|
|
95
|
+
}
|
|
96
|
+
const id = nextId;
|
|
97
|
+
nextId += 1;
|
|
98
|
+
return new Promise((resolve, reject) => {
|
|
99
|
+
const onAbort = signal ? () => {
|
|
100
|
+
const entry = pending.get(id);
|
|
101
|
+
if (!entry) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
pending.delete(id);
|
|
105
|
+
if (entry.signal && entry.onAbort) {
|
|
106
|
+
entry.signal.removeEventListener('abort', entry.onAbort);
|
|
107
|
+
}
|
|
108
|
+
reject(signal.reason);
|
|
109
|
+
} : undefined;
|
|
110
|
+
pending.set(id, {
|
|
111
|
+
resolve,
|
|
112
|
+
reject,
|
|
113
|
+
signal,
|
|
114
|
+
onAbort
|
|
115
|
+
});
|
|
116
|
+
if (signal && onAbort) {
|
|
117
|
+
signal.addEventListener('abort', onAbort, {
|
|
118
|
+
once: true
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
const message = {
|
|
122
|
+
type: 'transpile',
|
|
123
|
+
id,
|
|
124
|
+
source,
|
|
125
|
+
options
|
|
126
|
+
};
|
|
127
|
+
worker.postMessage(message);
|
|
128
|
+
});
|
|
129
|
+
};
|
|
130
|
+
function terminate() {
|
|
131
|
+
dead = true;
|
|
132
|
+
worker.terminate();
|
|
133
|
+
rejectAllPending(new Error('Worker terminated'));
|
|
134
|
+
}
|
|
135
|
+
return {
|
|
136
|
+
transpile,
|
|
137
|
+
terminate
|
|
138
|
+
};
|
|
139
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// Ambient declarations for the ICSS plugin trio + helpers that power CSS Modules
|
|
2
|
+
// scoping. These are the exact packages css-loader composes internally; none ship
|
|
3
|
+
// their own types, and all are pure PostCSS AST transforms (no fs/path), so they
|
|
4
|
+
// run identically in the browser and Node. Typed minimally to what the compiler
|
|
5
|
+
// here uses — see `compileCssModule.ts`.
|
|
6
|
+
|
|
7
|
+
declare module 'postcss-modules-values' {
|
|
8
|
+
import type { Plugin } from 'postcss';
|
|
9
|
+
|
|
10
|
+
// Resolves `@value` definitions/usages. Used directly as a plugin (no options).
|
|
11
|
+
const plugin: Plugin;
|
|
12
|
+
export default plugin;
|
|
13
|
+
}
|
|
14
|
+
declare module 'postcss-modules-local-by-default' {
|
|
15
|
+
import type { Plugin } from 'postcss';
|
|
16
|
+
const plugin: (options?: {
|
|
17
|
+
mode?: 'local' | 'global' | 'pure';
|
|
18
|
+
}) => Plugin;
|
|
19
|
+
export default plugin;
|
|
20
|
+
}
|
|
21
|
+
declare module 'postcss-modules-extract-imports' {
|
|
22
|
+
import type { Plugin } from 'postcss';
|
|
23
|
+
const plugin: (options?: {
|
|
24
|
+
failOnWrongOrder?: boolean;
|
|
25
|
+
}) => Plugin;
|
|
26
|
+
export default plugin;
|
|
27
|
+
}
|
|
28
|
+
declare module 'postcss-modules-scope' {
|
|
29
|
+
import type { Plugin } from 'postcss';
|
|
30
|
+
const plugin: (options?: {
|
|
31
|
+
generateScopedName?: (name: string, path: string, css: string) => string;
|
|
32
|
+
generateExportEntry?: unknown;
|
|
33
|
+
exportGlobals?: boolean;
|
|
34
|
+
}) => Plugin;
|
|
35
|
+
export default plugin;
|
|
36
|
+
}
|
|
37
|
+
declare module 'icss-utils' {
|
|
38
|
+
import type { Root } from 'postcss';
|
|
39
|
+
|
|
40
|
+
/** Pulls the `:import`/`:export` ICSS rules out of a processed root. */
|
|
41
|
+
export function extractICSS(css: Root, removeRules?: boolean): {
|
|
42
|
+
icssImports: Record<string, Record<string, string>>;
|
|
43
|
+
icssExports: Record<string, string>;
|
|
44
|
+
};
|
|
45
|
+
export function replaceValueSymbols(css: Root, replacements: Record<string, string>): void;
|
|
46
|
+
export function replaceSymbols(css: Root, replacements: Record<string, string>): void;
|
|
47
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The autoprefixer/browserslist target for the live-demo CSS compiler: the EXACT
|
|
3
|
+
* browser + version the preview is running in.
|
|
4
|
+
*
|
|
5
|
+
* A demo's compiled CSS only has to work in the visitor's own browser, so prefixing
|
|
6
|
+
* for that single engine adds exactly what it needs — usually nothing on a current
|
|
7
|
+
* browser — instead of the whole "baseline" range. Off the main thread (SSR/Node) or
|
|
8
|
+
* in an unrecognized engine it falls back to `baseline widely available`, a
|
|
9
|
+
* broad-but-safe default. On iOS every browser is WebKit, so the `safari` branch
|
|
10
|
+
* (matched for any `Version/x Safari` UA) is the correct target there too.
|
|
11
|
+
*
|
|
12
|
+
* Pair with autoprefixer's `ignoreUnknownVersions`, so a browser newer than the
|
|
13
|
+
* bundled caniuse-lite resolves to no prefixes (correct — current browsers need none)
|
|
14
|
+
* rather than throwing.
|
|
15
|
+
*/
|
|
16
|
+
export declare function currentBrowserTarget(): string[];
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The autoprefixer/browserslist target for the live-demo CSS compiler: the EXACT
|
|
3
|
+
* browser + version the preview is running in.
|
|
4
|
+
*
|
|
5
|
+
* A demo's compiled CSS only has to work in the visitor's own browser, so prefixing
|
|
6
|
+
* for that single engine adds exactly what it needs — usually nothing on a current
|
|
7
|
+
* browser — instead of the whole "baseline" range. Off the main thread (SSR/Node) or
|
|
8
|
+
* in an unrecognized engine it falls back to `baseline widely available`, a
|
|
9
|
+
* broad-but-safe default. On iOS every browser is WebKit, so the `safari` branch
|
|
10
|
+
* (matched for any `Version/x Safari` UA) is the correct target there too.
|
|
11
|
+
*
|
|
12
|
+
* Pair with autoprefixer's `ignoreUnknownVersions`, so a browser newer than the
|
|
13
|
+
* bundled caniuse-lite resolves to no prefixes (correct — current browsers need none)
|
|
14
|
+
* rather than throwing.
|
|
15
|
+
*/
|
|
16
|
+
export function currentBrowserTarget() {
|
|
17
|
+
const userAgent = typeof navigator === 'undefined' ? '' : navigator.userAgent;
|
|
18
|
+
const edge = userAgent.match(/Edg\/(\d+)/);
|
|
19
|
+
if (edge) {
|
|
20
|
+
return [`edge ${edge[1]}`];
|
|
21
|
+
}
|
|
22
|
+
const firefox = userAgent.match(/Firefox\/(\d+)/);
|
|
23
|
+
if (firefox) {
|
|
24
|
+
return [`firefox ${firefox[1]}`];
|
|
25
|
+
}
|
|
26
|
+
// Edge is matched above, so a remaining `Chrome/` is Chromium proper.
|
|
27
|
+
const chrome = userAgent.match(/Chrome\/(\d+)/);
|
|
28
|
+
if (chrome) {
|
|
29
|
+
return [`chrome ${chrome[1]}`];
|
|
30
|
+
}
|
|
31
|
+
const safari = userAgent.match(/Version\/(\d+\.\d+) Safari/);
|
|
32
|
+
if (safari) {
|
|
33
|
+
return [`safari ${safari[1]}`];
|
|
34
|
+
}
|
|
35
|
+
return ['baseline widely available'];
|
|
36
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Scope } from "./types.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* Evaluates already-transpiled CommonJS-style code with `scope` bound as local
|
|
4
|
+
* variables. `React` and a `require` shim (backed by `scope.import`) are always
|
|
5
|
+
* injected. The `import` key is consumed by the shim rather than bound, and
|
|
6
|
+
* `default` is skipped because it is a reserved word and cannot name a function
|
|
7
|
+
* parameter.
|
|
8
|
+
*
|
|
9
|
+
* Returns the evaluated function's return value; callers that need a module's
|
|
10
|
+
* exports pass an `exports` object in `scope` and read it back after the call.
|
|
11
|
+
*/
|
|
12
|
+
export declare function evalCode(code: string, scope: Scope): unknown;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { createRequire } from "./createRequire.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* Evaluates already-transpiled CommonJS-style code with `scope` bound as local
|
|
5
|
+
* variables. `React` and a `require` shim (backed by `scope.import`) are always
|
|
6
|
+
* injected. The `import` key is consumed by the shim rather than bound, and
|
|
7
|
+
* `default` is skipped because it is a reserved word and cannot name a function
|
|
8
|
+
* parameter.
|
|
9
|
+
*
|
|
10
|
+
* Returns the evaluated function's return value; callers that need a module's
|
|
11
|
+
* exports pass an `exports` object in `scope` and read it back after the call.
|
|
12
|
+
*/
|
|
13
|
+
export function evalCode(code, scope) {
|
|
14
|
+
const boundScope = {};
|
|
15
|
+
for (const key of Object.keys(scope)) {
|
|
16
|
+
if (key !== 'import' && key !== 'default') {
|
|
17
|
+
boundScope[key] = scope[key];
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Inject AFTER copying scope so the always-injected bindings WIN over any
|
|
22
|
+
// same-named scope entry: JSX in the evaluated code compiles to `React.*`, and
|
|
23
|
+
// its transpiled `import`s call this `require` shim — a scope `React`/`require`
|
|
24
|
+
// must not shadow either.
|
|
25
|
+
boundScope.React = React;
|
|
26
|
+
boundScope.require = createRequire(scope.import);
|
|
27
|
+
const names = Object.keys(boundScope);
|
|
28
|
+
const values = names.map(name => boundScope[name]);
|
|
29
|
+
|
|
30
|
+
// Evaluating transpiled demo source is the whole purpose of the runner;
|
|
31
|
+
// `new Function` runs it without leaking the surrounding lexical scope.
|
|
32
|
+
// eslint-disable-next-line no-new-func
|
|
33
|
+
const evaluate = new Function(...names, code);
|
|
34
|
+
return evaluate(...values);
|
|
35
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Serializes a CSS module's class-name exports into ESM source text — the
|
|
3
|
+
* optional companion to `compileCssModule` for when a literal JS string is
|
|
4
|
+
* needed (to display in a source viewer, or to run a `.module.css` file through
|
|
5
|
+
* the same `importCode` path as `.ts`/`.js` files).
|
|
6
|
+
*
|
|
7
|
+
* Emits a named export for every identifier-safe class name and a default export
|
|
8
|
+
* object containing all of them (so hyphenated names like `my-button` remain
|
|
9
|
+
* reachable via `styles['my-button']`).
|
|
10
|
+
*/
|
|
11
|
+
export declare function generateCssModuleSource(exports: Record<string, string>): string;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reserved words that cannot be bound by `export const <name>` in a module
|
|
3
|
+
* (modules are always strict mode). Such class names are still reachable through
|
|
4
|
+
* the default-export object, just not as a named export.
|
|
5
|
+
*/
|
|
6
|
+
const RESERVED_WORDS = new Set(['break', 'case', 'catch', 'class', 'const', 'continue', 'debugger', 'default', 'delete', 'do', 'else', 'enum', 'export', 'extends', 'false', 'finally', 'for', 'function', 'if', 'import', 'in', 'instanceof', 'new', 'null', 'return', 'super', 'switch', 'this', 'throw', 'true', 'try', 'typeof', 'var', 'void', 'while', 'with', 'implements', 'interface', 'let', 'package', 'private', 'protected', 'public', 'static', 'yield', 'await', 'eval', 'arguments']);
|
|
7
|
+
|
|
8
|
+
/** Whether `name` is a (conservative, ASCII) JavaScript identifier. */
|
|
9
|
+
function isJsIdentifier(name) {
|
|
10
|
+
if (name.length === 0) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
for (let index = 0; index < name.length; index += 1) {
|
|
14
|
+
const code = name.charCodeAt(index);
|
|
15
|
+
const isLetter = code >= 97 && code <= 122 || code >= 65 && code <= 90;
|
|
16
|
+
const isUnderscoreOrDollar = code === 95 || code === 36;
|
|
17
|
+
const isDigit = code >= 48 && code <= 57;
|
|
18
|
+
if (!(isLetter || isUnderscoreOrDollar || index > 0 && isDigit)) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Wraps a value in a single-quoted JS string literal, escaping it safely. */
|
|
26
|
+
function quote(value) {
|
|
27
|
+
const escaped = value.replaceAll('\\', '\\\\').replaceAll("'", "\\'").replaceAll('\n', '\\n').replaceAll('\r', '\\r')
|
|
28
|
+
// U+2028/U+2029 are line terminators that were illegal in pre-ES2019 string
|
|
29
|
+
// literals; escape them so the generated source stays valid for any input.
|
|
30
|
+
.replaceAll('\u2028', '\\u2028').replaceAll('\u2029', '\\u2029');
|
|
31
|
+
return `'${escaped}'`;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Serializes a CSS module's class-name exports into ESM source text — the
|
|
36
|
+
* optional companion to `compileCssModule` for when a literal JS string is
|
|
37
|
+
* needed (to display in a source viewer, or to run a `.module.css` file through
|
|
38
|
+
* the same `importCode` path as `.ts`/`.js` files).
|
|
39
|
+
*
|
|
40
|
+
* Emits a named export for every identifier-safe class name and a default export
|
|
41
|
+
* object containing all of them (so hyphenated names like `my-button` remain
|
|
42
|
+
* reachable via `styles['my-button']`).
|
|
43
|
+
*/
|
|
44
|
+
export function generateCssModuleSource(exports) {
|
|
45
|
+
const entries = Object.entries(exports);
|
|
46
|
+
const lines = [];
|
|
47
|
+
for (const [local, scoped] of entries) {
|
|
48
|
+
if (isJsIdentifier(local) && !RESERVED_WORDS.has(local)) {
|
|
49
|
+
lines.push(`export const ${local} = ${quote(scoped)};`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const objectBody = entries.map(([local, scoped]) => `${quote(local)}: ${quote(scoped)}`).join(', ');
|
|
53
|
+
lines.push(`export default {${objectBody ? ` ${objectBody} ` : ''}};`);
|
|
54
|
+
return `${lines.join('\n')}\n`;
|
|
55
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type * as React from 'react';
|
|
2
|
+
import { ENTRY_EXPORTS_KEY } from "./instantiateElement.mjs";
|
|
3
|
+
import type { RunnerOptions } from "./types.mjs";
|
|
4
|
+
export { ENTRY_EXPORTS_KEY };
|
|
5
|
+
/**
|
|
6
|
+
* Transpiles and evaluates a source string, returning the React node it exports
|
|
7
|
+
* as its default. A synchronous convenience that pairs the two halves —
|
|
8
|
+
* `normalizeCode` + `transformCode` (the heavy sucrase transpile) and
|
|
9
|
+
* {@link instantiateElement} (`evalCode` + node coercion) — for callers that
|
|
10
|
+
* transpile on the main thread. The async/worker path splits them: it transpiles
|
|
11
|
+
* off-thread and calls `instantiateElement` directly with the result.
|
|
12
|
+
*
|
|
13
|
+
* A source can expose its default three ways: an explicit `export default`, a
|
|
14
|
+
* call to the injected `render(value)` callback, or a bare leading expression
|
|
15
|
+
* promoted to a default export (see `normalizeCode`). An empty source yields
|
|
16
|
+
* `null`.
|
|
17
|
+
*/
|
|
18
|
+
export declare function generateElement({
|
|
19
|
+
code,
|
|
20
|
+
scope
|
|
21
|
+
}: RunnerOptions): React.ReactNode;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { instantiateElement, ENTRY_EXPORTS_KEY } from "./instantiateElement.mjs";
|
|
2
|
+
import { normalizeCode, transformCode } from "./transformCode.mjs";
|
|
3
|
+
export { ENTRY_EXPORTS_KEY };
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Transpiles and evaluates a source string, returning the React node it exports
|
|
7
|
+
* as its default. A synchronous convenience that pairs the two halves —
|
|
8
|
+
* `normalizeCode` + `transformCode` (the heavy sucrase transpile) and
|
|
9
|
+
* {@link instantiateElement} (`evalCode` + node coercion) — for callers that
|
|
10
|
+
* transpile on the main thread. The async/worker path splits them: it transpiles
|
|
11
|
+
* off-thread and calls `instantiateElement` directly with the result.
|
|
12
|
+
*
|
|
13
|
+
* A source can expose its default three ways: an explicit `export default`, a
|
|
14
|
+
* call to the injected `render(value)` callback, or a bare leading expression
|
|
15
|
+
* promoted to a default export (see `normalizeCode`). An empty source yields
|
|
16
|
+
* `null`.
|
|
17
|
+
*/
|
|
18
|
+
export function generateElement({
|
|
19
|
+
code,
|
|
20
|
+
scope
|
|
21
|
+
}) {
|
|
22
|
+
if (!code.trim()) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
return instantiateElement(transformCode(normalizeCode(code)), scope);
|
|
26
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Computes a short, deterministic base36 hash of a string.
|
|
3
|
+
*
|
|
4
|
+
* Deterministic and dependency-free: the same input always yields the same
|
|
5
|
+
* output, which matters when the result must agree across server render and
|
|
6
|
+
* client hydration. Uses a polynomial rolling hash modulo a Mersenne prime
|
|
7
|
+
* (2^31 - 1) — only arithmetic (no bitwise ops), staying within `Number`'s
|
|
8
|
+
* exact-integer range. Not cryptographic; meant for cheap, stable identifiers
|
|
9
|
+
* such as scoped CSS class suffixes.
|
|
10
|
+
*/
|
|
11
|
+
export declare function hashString(input: string): string;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Computes a short, deterministic base36 hash of a string.
|
|
3
|
+
*
|
|
4
|
+
* Deterministic and dependency-free: the same input always yields the same
|
|
5
|
+
* output, which matters when the result must agree across server render and
|
|
6
|
+
* client hydration. Uses a polynomial rolling hash modulo a Mersenne prime
|
|
7
|
+
* (2^31 - 1) — only arithmetic (no bitwise ops), staying within `Number`'s
|
|
8
|
+
* exact-integer range. Not cryptographic; meant for cheap, stable identifiers
|
|
9
|
+
* such as scoped CSS class suffixes.
|
|
10
|
+
*/
|
|
11
|
+
export function hashString(input) {
|
|
12
|
+
const MODULUS = 2147483647; // 2^31 - 1
|
|
13
|
+
let hash = 0;
|
|
14
|
+
for (let index = 0; index < input.length; index += 1) {
|
|
15
|
+
// `hash < MODULUS` and the multiplier keep the product well under 2^53, so
|
|
16
|
+
// every step is exact.
|
|
17
|
+
hash = (hash * 131 + input.charCodeAt(index)) % MODULUS;
|
|
18
|
+
}
|
|
19
|
+
return hash.toString(36);
|
|
20
|
+
}
|