@mui/internal-docs-infra 0.13.1-canary.27 → 0.13.1-canary.28

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mui/internal-docs-infra",
3
- "version": "0.13.1-canary.27",
3
+ "version": "0.13.1-canary.28",
4
4
  "author": "MUI Team",
5
5
  "description": "MUI Infra - internal documentation creation tools.",
6
6
  "license": "MIT",
@@ -813,5 +813,5 @@
813
813
  "bin": {
814
814
  "docs-infra": "./cli/index.mjs"
815
815
  },
816
- "gitSha": "b6d33f09a4ab6edc8e98f07f595bc7bc7580d250"
816
+ "gitSha": "5ada69268ff795656026874c514429285e0dba31"
817
817
  }
@@ -14,6 +14,63 @@ import { createLoadServerCodeSource } from "../loadServerCodeSource/index.mjs";
14
14
  import { getFileNameFromUrl, IGNORE_COMMENT_PREFIXES } from "../loaderUtils/index.mjs";
15
15
  import { createPerformanceLogger, logPerformance, performanceMeasure } from "./performanceLogger.mjs";
16
16
  import { fallbackToHast } from "../../CodeHighlighter/fallbackFormat.mjs";
17
+ // Compilation identity prevents reuse across compilers and watch rebuilds.
18
+ const deferredCaches = new WeakMap();
19
+
20
+ /** Rejects options whose behavior would be lost in a JSON cache key. */
21
+ function isSerializable(value, ancestors = new Set()) {
22
+ if (value === null || typeof value === 'string' || typeof value === 'boolean') {
23
+ return true;
24
+ }
25
+ if (typeof value === 'number') {
26
+ return Number.isFinite(value);
27
+ }
28
+ if (typeof value !== 'object' || ancestors.has(value) || Object.getOwnPropertySymbols(value).length) {
29
+ return false;
30
+ }
31
+ const prototype = Object.getPrototypeOf(value);
32
+ if (!Array.isArray(value) && prototype !== Object.prototype && prototype !== null) {
33
+ return false;
34
+ }
35
+ ancestors.add(value);
36
+ const supported = Object.entries(Object.getOwnPropertyDescriptors(value)).every(([name, descriptor]) => {
37
+ if (Array.isArray(value) && name === 'length') {
38
+ return true;
39
+ }
40
+ return descriptor.enumerable && 'value' in descriptor && (
41
+ // JSON drops undefined properties, matching how the loader treats absent options.
42
+ // Arrays serialize undefined as null, so elements still have to be serializable.
43
+ descriptor.value === undefined && !Array.isArray(value) || isSerializable(descriptor.value, ancestors));
44
+ });
45
+ ancestors.delete(value);
46
+ return supported;
47
+ }
48
+
49
+ /** Shares completed source only when webpack provides a compilation boundary. */
50
+ function getDeferredCache(context, source, options) {
51
+ const {
52
+ _compilation: compilation
53
+ } = context;
54
+ if (!options.deferPrecompute || !compilation || !isSerializable(options)) {
55
+ return undefined;
56
+ }
57
+ let entries = deferredCaches.get(compilation);
58
+ if (!entries) {
59
+ entries = new Map();
60
+ deferredCaches.set(compilation, entries);
61
+ }
62
+ return {
63
+ entries,
64
+ key: JSON.stringify([context.resourcePath, source, options])
65
+ };
66
+ }
67
+
68
+ /** Both the shell and deferred module must invalidate when imported source changes. */
69
+ function registerDependencies(context, dependencies) {
70
+ dependencies.forEach(dependency => {
71
+ context.addDependency(dependency.startsWith('file://') ? fileURLToPath(dependency) : dependency);
72
+ });
73
+ }
17
74
 
18
75
  /**
19
76
  * Extracts a string array from structured options data.
@@ -115,6 +172,14 @@ export async function loadPrecomputedCodeHighlighter(source) {
115
172
  const callback = this.async();
116
173
  this.cacheable();
117
174
  const options = this.getOptions();
175
+ const isDeferredRequest = this.resourceQuery === '?mui-deferred-precompute';
176
+ const deferredCache = getDeferredCache(this, source, options);
177
+ const cached = isDeferredRequest ? deferredCache?.entries.get(deferredCache.key) : undefined;
178
+ if (cached) {
179
+ registerDependencies(this, cached.dependencies);
180
+ callback(null, cached.source);
181
+ return;
182
+ }
118
183
  const performanceNotableMs = options.performance?.notableMs ?? 100;
119
184
  const performanceShowWrapperMeasures = options.performance?.showWrapperMeasures ?? false;
120
185
  const relativePath = path.relative(this.rootContext || process.cwd(), this.resourcePath);
@@ -276,9 +341,16 @@ export async function loadPrecomputedCodeHighlighter(source) {
276
341
  measure: 'Complete Variants Loading'
277
342
  }, [functionName, relativePath], true);
278
343
  const deferredRequest = `./${path.basename(this.resourcePath)}?mui-deferred-precompute`;
279
- const isDeferredRequest = this.resourceQuery === '?mui-deferred-precompute';
344
+ const deferredSource = isDeferredRequest || deferredCache ? `export default ${JSON.stringify(variantData)};` : undefined;
345
+ registerDependencies(this, allDependencies);
346
+ if (deferredCache && deferredSource) {
347
+ deferredCache.entries.set(deferredCache.key, {
348
+ source: deferredSource,
349
+ dependencies: allDependencies
350
+ });
351
+ }
280
352
  if (isDeferredRequest) {
281
- callback(null, `export default ${JSON.stringify(variantData)};`);
353
+ callback(null, deferredSource);
282
354
  return;
283
355
  }
284
356
 
@@ -293,13 +365,6 @@ export async function loadPrecomputedCodeHighlighter(source) {
293
365
  measure: 'Precompute Replacement'
294
366
  }, [functionName, relativePath]);
295
367
 
296
- // Add all dependencies to webpack's watch list
297
- allDependencies.forEach(dep => {
298
- // Convert file:// URLs to proper file system paths for webpack's dependency tracking
299
- // Using fileURLToPath handles Windows drive letters correctly (e.g., file:///C:/... → C:\...)
300
- this.addDependency(dep.startsWith('file://') ? fileURLToPath(dep) : dep);
301
- });
302
-
303
368
  // log any pending performance entries before completing
304
369
  observer?.takeRecords()?.forEach(entry => logPerformance(entry, performanceNotableMs, performanceShowWrapperMeasures, relativePath));
305
370
  observer?.disconnect();
@@ -160,7 +160,7 @@ export interface ExportConfig {
160
160
  globals?: VariantExtraFiles;
161
161
  } | undefined;
162
162
  /**
163
- * Version overrides for core packages (react, react-dom, @types/react, @types/react-dom)
163
+ * Version overrides for exported packages, including React and its type packages.
164
164
  * @example
165
165
  * versions: {
166
166
  * '@types/react': '^19',
@@ -171,7 +171,8 @@ export interface ExportConfig {
171
171
  */
172
172
  versions?: Record<string, string>;
173
173
  /**
174
- * Custom dependency resolution function
174
+ * Resolves imported packages and their peers, including packages pinned in `versions`.
175
+ * `importedModules` contains the complete imports belonging to each package.
175
176
  * @example
176
177
  * resolveDependencies: (packageName, envVars) => {
177
178
  * if (packageName === '@mui/material') {
@@ -180,7 +181,7 @@ export interface ExportConfig {
180
181
  * return { [packageName]: 'latest' };
181
182
  * }
182
183
  */
183
- resolveDependencies?: (packageName: string, envVars?: Record<string, string>) => Record<string, string>;
184
+ resolveDependencies?: (packageName: string, envVars?: Record<string, string>, importedModules?: string[]) => Record<string, string>;
184
185
  }
185
186
  /**
186
187
  * Export a variant as a standalone project with metadata files properly scoped
@@ -286,20 +286,25 @@ export function exportVariant(variantCode, config = {}) {
286
286
 
287
287
  // Check if they're providing their own framework
288
288
  const isFramework = 'frameworkFiles' in config;
289
- const externalPackages = externalsToPackages(processedVariantCode.externals || []);
289
+ const importedModules = processedVariantCode.externals || [];
290
+ const externalPackages = externalsToPackages(importedModules);
290
291
  const variantDeps = Object.keys(externalPackages).reduce((acc, pkg) => {
291
- // Check if we have a specific version for this package first
292
- if (versions[pkg]) {
293
- acc[pkg] = versions[pkg];
294
- } else if (resolveDependencies) {
295
- const resolvedDeps = resolveDependencies(pkg);
296
- Object.assign(acc, resolvedDeps);
292
+ const packageImports = importedModules.filter(name => name === pkg || name.startsWith(`${pkg}/`));
293
+ if (resolveDependencies) {
294
+ Object.assign(acc, resolveDependencies(pkg, undefined, packageImports));
297
295
  } else {
298
- // Simple fallback: just use 'latest' for each package
299
296
  acc[pkg] = 'latest';
300
297
  }
298
+ if (versions[pkg] !== undefined) {
299
+ acc[pkg] = versions[pkg];
300
+ }
301
301
  return acc;
302
302
  }, {});
303
+ for (const pkg of Object.keys(variantDeps)) {
304
+ if (versions[pkg] !== undefined) {
305
+ variantDeps[pkg] = versions[pkg];
306
+ }
307
+ }
303
308
 
304
309
  // Collect metadata files to be generated
305
310
  const metadataFiles = {};