@mui/internal-docs-infra 0.3.1-canary.3 → 0.3.1-canary.4

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.
Files changed (102) hide show
  1. package/esm/CodeControllerContext/CodeControllerContext.js +2 -2
  2. package/esm/CodeExternalsContext/CodeExternalsContext.js +1 -1
  3. package/esm/CodeHighlighter/CodeHighlighter.js +247 -329
  4. package/esm/CodeHighlighter/CodeHighlighterClient.js +447 -653
  5. package/esm/CodeHighlighter/CodeHighlighterContext.js +2 -2
  6. package/esm/CodeHighlighter/CodeHighlighterFallbackContext.js +2 -2
  7. package/esm/CodeHighlighter/codeToFallbackProps.js +21 -37
  8. package/esm/CodeHighlighter/errors.js +248 -400
  9. package/esm/CodeHighlighter/parseControlledCode.js +12 -20
  10. package/esm/CodeProvider/CodeContext.js +3 -3
  11. package/esm/CodeProvider/CodeProvider.js +31 -40
  12. package/esm/abstractCreateDemo/abstractCreateDemo.js +13 -17
  13. package/esm/abstractCreateDemoClient/abstractCreateDemoClient.js +12 -12
  14. package/esm/cli/index.js +1 -1
  15. package/esm/cli/runValidate.js +160 -264
  16. package/esm/createDemoData/createDemoData.js +11 -12
  17. package/esm/createSitemap/createSitemap.js +2 -2
  18. package/esm/pipeline/getFileConventions/fileConventions.js +1 -1
  19. package/esm/pipeline/getFileConventions/getFileConventions.js +2 -15
  20. package/esm/pipeline/hastUtils/hastUtils.js +16 -17
  21. package/esm/pipeline/loadCodeVariant/addCodeVariantPaths.js +24 -24
  22. package/esm/pipeline/loadCodeVariant/applyCodeTransform.js +12 -22
  23. package/esm/pipeline/loadCodeVariant/calculateMainFilePath.js +30 -37
  24. package/esm/pipeline/loadCodeVariant/computeHastDeltas.js +107 -185
  25. package/esm/pipeline/loadCodeVariant/diffHast.js +18 -53
  26. package/esm/pipeline/loadCodeVariant/examineCodeVariant.js +24 -27
  27. package/esm/pipeline/loadCodeVariant/flattenCodeVariant.js +9 -10
  28. package/esm/pipeline/loadCodeVariant/hasAllCodeVariants.js +5 -5
  29. package/esm/pipeline/loadCodeVariant/loadCodeFallback.js +516 -731
  30. package/esm/pipeline/loadCodeVariant/loadCodeVariant.js +679 -1079
  31. package/esm/pipeline/loadCodeVariant/maybeCodeInitialData.js +14 -20
  32. package/esm/pipeline/loadCodeVariant/mergeCodeMetadata.js +53 -63
  33. package/esm/pipeline/loadCodeVariant/parseCode.js +40 -48
  34. package/esm/pipeline/loadCodeVariant/pathUtils.js +43 -64
  35. package/esm/pipeline/loadCodeVariant/transformSource.js +55 -125
  36. package/esm/pipeline/loadPrecomputedCodeHighlighter/loadPrecomputedCodeHighlighter.js +160 -221
  37. package/esm/pipeline/loadPrecomputedCodeHighlighter/parseCreateFactoryCall.js +377 -479
  38. package/esm/pipeline/loadPrecomputedCodeHighlighter/parseFunctionArguments.js +171 -173
  39. package/esm/pipeline/loadPrecomputedCodeHighlighter/performanceLogger.js +14 -30
  40. package/esm/pipeline/loadPrecomputedCodeHighlighter/replacePrecomputeValue.js +19 -21
  41. package/esm/pipeline/loadPrecomputedCodeHighlighter/serializeFunctionArguments.js +37 -71
  42. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/filterRuntimeExternals.js +3 -9
  43. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/generateImportStatements.js +54 -80
  44. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/generateResolvedExternals.js +71 -98
  45. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/injectImportsIntoSource.js +5 -5
  46. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/loadPrecomputedCodeHighlighterClient.js +161 -211
  47. package/esm/pipeline/loadPrecomputedSitemap/loadPrecomputedSitemap.js +159 -207
  48. package/esm/pipeline/loadServerCodeMeta/loadServerCodeMeta.js +42 -64
  49. package/esm/pipeline/loadServerCodeMeta/resolveModulePathWithFs.js +20 -96
  50. package/esm/pipeline/loadServerPageIndex/loadServerPageIndex.js +66 -85
  51. package/esm/pipeline/loadServerSitemap/loadServerSitemap.js +71 -118
  52. package/esm/pipeline/loadServerSource/loadServerSource.js +121 -148
  53. package/esm/pipeline/loaderUtils/externalsToPackages.js +7 -7
  54. package/esm/pipeline/loaderUtils/extractNameAndSlugFromUrl.js +8 -12
  55. package/esm/pipeline/loaderUtils/fileUrlToPortablePath.js +5 -5
  56. package/esm/pipeline/loaderUtils/getFileNameFromUrl.js +19 -29
  57. package/esm/pipeline/loaderUtils/getLanguageFromExtension.js +3 -4
  58. package/esm/pipeline/loaderUtils/mergeExternals.js +15 -35
  59. package/esm/pipeline/loaderUtils/parseImportsAndComments.js +413 -433
  60. package/esm/pipeline/loaderUtils/processRelativeImports.js +153 -239
  61. package/esm/pipeline/loaderUtils/resolveModulePath.js +544 -1303
  62. package/esm/pipeline/loaderUtils/rewriteImports.js +73 -111
  63. package/esm/pipeline/parseSource/addLineGutters.js +33 -45
  64. package/esm/pipeline/parseSource/grammars.js +3 -3
  65. package/esm/pipeline/parseSource/parseSource.js +13 -31
  66. package/esm/pipeline/syncPageIndex/createMarkdownNodes.js +32 -55
  67. package/esm/pipeline/syncPageIndex/mergeMetadataMarkdown.js +107 -160
  68. package/esm/pipeline/syncPageIndex/metadataToMarkdown.js +846 -1033
  69. package/esm/pipeline/syncPageIndex/syncPageIndex.js +291 -438
  70. package/esm/pipeline/transformHtmlCodePrecomputed/transformHtmlCodePrecomputed.js +213 -311
  71. package/esm/pipeline/transformMarkdownBlockquoteCallouts/transformMarkdownBlockquoteCallouts.js +10 -10
  72. package/esm/pipeline/transformMarkdownCode/transformMarkdownCode.js +133 -193
  73. package/esm/pipeline/transformMarkdownDemoLinks/transformMarkdownDemoLinks.js +25 -27
  74. package/esm/pipeline/transformMarkdownMetadata/transformMarkdownMetadata.js +572 -717
  75. package/esm/pipeline/transformMarkdownRelativePaths/transformMarkdownRelativePaths.js +8 -8
  76. package/esm/pipeline/transformTypescriptToJavascript/removeTypes.js +84 -113
  77. package/esm/pipeline/transformTypescriptToJavascript/transformTypescriptToJavascript.js +10 -26
  78. package/esm/useCode/Pre.js +58 -62
  79. package/esm/useCode/useCode.js +59 -61
  80. package/esm/useCode/useCodeUtils.js +54 -63
  81. package/esm/useCode/useCopyFunctionality.js +10 -9
  82. package/esm/useCode/useFileNavigation.js +150 -212
  83. package/esm/useCode/useSourceEditing.js +17 -14
  84. package/esm/useCode/useTransformManagement.js +23 -26
  85. package/esm/useCode/useUIState.js +12 -20
  86. package/esm/useCode/useVariantSelection.js +62 -79
  87. package/esm/useCopier/index.js +29 -56
  88. package/esm/useDemo/createCodeSandbox.js +12 -15
  89. package/esm/useDemo/createStackBlitz.js +14 -20
  90. package/esm/useDemo/exportVariant.js +200 -180
  91. package/esm/useDemo/exportVariantAsCra.js +22 -25
  92. package/esm/useDemo/useDemo.js +80 -84
  93. package/esm/useErrors/ErrorsContext.js +1 -1
  94. package/esm/useErrors/useErrors.js +3 -3
  95. package/esm/useLocalStorageState/useLocalStorageState.js +23 -39
  96. package/esm/usePreference/PreferencesProvider.js +1 -1
  97. package/esm/usePreference/usePreference.js +9 -11
  98. package/esm/useSearch/useSearch.js +290 -387
  99. package/esm/useUrlHashState/useUrlHashState.js +11 -14
  100. package/esm/withDocsInfra/withDeploymentConfig.js +26 -21
  101. package/esm/withDocsInfra/withDocsInfra.js +99 -101
  102. package/package.json +7 -4
@@ -1,762 +1,547 @@
1
- import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
2
- import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
3
- import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
4
- import _extends from "@babel/runtime/helpers/esm/extends";
5
- import _regenerator from "@babel/runtime/helpers/esm/regenerator";
6
- import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
7
1
  import { loadCodeVariant } from "./loadCodeVariant.js";
8
2
  import { getFileNameFromUrl, getLanguageFromExtension } from "../loaderUtils/index.js";
9
3
  import { performanceMeasure } from "../loadPrecomputedCodeHighlighter/performanceLogger.js";
10
4
 
11
5
  // Helper function to get the source for a specific filename from a variant
12
- function getFileSource(_x, _x2, _x3) {
13
- return _getFileSource.apply(this, arguments);
6
+ async function getFileSource(variant, requestedFilename, loadSource) {
7
+ const filename = requestedFilename || variant.fileName;
8
+ if (!filename) {
9
+ // If no filename is available, return the main variant source
10
+ if (variant.source !== undefined) {
11
+ return {
12
+ source: variant.source,
13
+ filename: undefined
14
+ };
15
+ }
16
+ throw new Error('No filename available and no source in variant');
17
+ }
18
+
19
+ // If requesting the main file and we have its source
20
+ if (filename === variant.fileName && variant.source !== undefined) {
21
+ return {
22
+ source: variant.source,
23
+ filename
24
+ };
25
+ }
26
+
27
+ // If requesting an extra file and we have its source
28
+ if (filename !== variant.fileName && variant.extraFiles) {
29
+ const extraFile = variant.extraFiles[filename];
30
+ if (extraFile && typeof extraFile !== 'string' && extraFile.source !== undefined) {
31
+ return {
32
+ source: extraFile.source,
33
+ filename
34
+ };
35
+ }
36
+
37
+ // If we have the URL but not the source, we need to load it
38
+ if (typeof extraFile === 'string' && loadSource) {
39
+ const loadResult = await loadSource(extraFile);
40
+ return {
41
+ source: loadResult.source,
42
+ filename
43
+ };
44
+ }
45
+ if (extraFile && typeof extraFile !== 'string' && !extraFile.source && loadSource) {
46
+ // This case shouldn't normally happen, but handle it anyway
47
+ throw new Error(`Extra file ${filename} has no source or URL to load from`);
48
+ }
49
+ }
50
+ throw new Error(`File ${filename} not found in variant or cannot be loaded`);
14
51
  }
15
- function _getFileSource() {
16
- _getFileSource = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(variant, requestedFilename, loadSource) {
17
- var filename, extraFile, loadResult;
18
- return _regenerator().w(function (_context) {
19
- while (1) switch (_context.n) {
20
- case 0:
21
- filename = requestedFilename || variant.fileName;
22
- if (filename) {
23
- _context.n = 2;
24
- break;
25
- }
26
- if (!(variant.source !== undefined)) {
27
- _context.n = 1;
28
- break;
29
- }
30
- return _context.a(2, {
31
- source: variant.source,
32
- filename: undefined
33
- });
34
- case 1:
35
- throw new Error('No filename available and no source in variant');
36
- case 2:
37
- if (!(filename === variant.fileName && variant.source !== undefined)) {
38
- _context.n = 3;
39
- break;
40
- }
41
- return _context.a(2, {
42
- source: variant.source,
43
- filename: filename
44
- });
45
- case 3:
46
- if (!(filename !== variant.fileName && variant.extraFiles)) {
47
- _context.n = 7;
48
- break;
49
- }
50
- extraFile = variant.extraFiles[filename];
51
- if (!(extraFile && typeof extraFile !== 'string' && extraFile.source !== undefined)) {
52
- _context.n = 4;
53
- break;
54
- }
55
- return _context.a(2, {
56
- source: extraFile.source,
57
- filename: filename
58
- });
59
- case 4:
60
- if (!(typeof extraFile === 'string' && loadSource)) {
61
- _context.n = 6;
62
- break;
63
- }
64
- _context.n = 5;
65
- return loadSource(extraFile);
66
- case 5:
67
- loadResult = _context.v;
68
- return _context.a(2, {
69
- source: loadResult.source,
70
- filename: filename
71
- });
72
- case 6:
73
- if (!(extraFile && typeof extraFile !== 'string' && !extraFile.source && loadSource)) {
74
- _context.n = 7;
75
- break;
76
- }
77
- throw new Error("Extra file ".concat(filename, " has no source or URL to load from"));
78
- case 7:
79
- throw new Error("File ".concat(filename, " not found in variant or cannot be loaded"));
80
- case 8:
81
- return _context.a(2);
52
+ export async function loadCodeFallback(url, initialVariant, loaded, options = {}) {
53
+ const {
54
+ shouldHighlight,
55
+ fallbackUsesExtraFiles,
56
+ fallbackUsesAllVariants,
57
+ sourceParser,
58
+ loadSource,
59
+ loadVariantMeta,
60
+ loadCodeMeta,
61
+ initialFilename,
62
+ variants,
63
+ globalsCode,
64
+ output
65
+ } = options;
66
+ loaded = {
67
+ ...loaded
68
+ };
69
+ const functionName = 'Load Fallback Code';
70
+ let currentMark = performanceMeasure(undefined, {
71
+ mark: 'Start',
72
+ measure: 'Start'
73
+ }, [functionName, url], true);
74
+
75
+ // Step 1: Ensure we have the initial variant loaded
76
+ let initial = loaded[initialVariant];
77
+ if (!initial) {
78
+ if (!loadCodeMeta) {
79
+ throw new Error('"loadCodeMeta" function is required when initial variant is not provided');
80
+ }
81
+ try {
82
+ loaded = await loadCodeMeta(url);
83
+ } catch (error) {
84
+ throw new Error(`Failed to load code from URL: ${url}. Error: ${JSON.stringify(error)}`);
85
+ }
86
+ initial = loaded[initialVariant];
87
+ if (!initial) {
88
+ throw new Error(`Initial variant "${initialVariant}" not found in loaded code.`);
89
+ }
90
+ currentMark = performanceMeasure(currentMark, {
91
+ mark: 'Loaded Code Meta',
92
+ measure: 'Code Meta Loading'
93
+ }, [functionName, url]);
94
+ }
95
+
96
+ // Check if we can return early after loadCodeMeta
97
+ if (typeof initial !== 'string' && initial.allFilesListed && !fallbackUsesExtraFiles && !fallbackUsesAllVariants) {
98
+ // Collect all file names from the loaded code
99
+ const allFileNames = new Set();
100
+ if (initial.fileName) {
101
+ allFileNames.add(initial.fileName);
102
+ }
103
+ if (initial.extraFiles) {
104
+ Object.keys(initial.extraFiles).forEach(fileName => allFileNames.add(fileName));
105
+ }
106
+
107
+ // Get the source for the requested filename (or main file if not specified)
108
+ let fileSource;
109
+ let actualFilename;
110
+ try {
111
+ const result = await getFileSource(initial, initialFilename, loadSource);
112
+ fileSource = result.source;
113
+ actualFilename = result.filename;
114
+ } catch (error) {
115
+ throw new Error(`Failed to get source for file ${initialFilename || initial.fileName} in variant ${initialVariant}: ${error}`);
116
+ }
117
+ currentMark = performanceMeasure(currentMark, {
118
+ mark: 'Loaded Main File',
119
+ measure: 'Main File Loading'
120
+ }, [functionName, url]);
121
+
122
+ // If we need highlighting and have a string source, parse it
123
+ if (shouldHighlight && typeof fileSource === 'string' && sourceParser && actualFilename) {
124
+ try {
125
+ const parseSource = await sourceParser;
126
+ fileSource = parseSource(fileSource, actualFilename);
127
+ } catch (error) {
128
+ throw new Error(`Failed to parse source for highlighting (variant: ${initialVariant}, file: ${actualFilename}): ${JSON.stringify(error)}`);
82
129
  }
83
- }, _callee);
84
- }));
85
- return _getFileSource.apply(this, arguments);
86
- }
87
- export function loadCodeFallback(_x4, _x5, _x6) {
88
- return _loadCodeFallback.apply(this, arguments);
89
- }
90
- function _loadCodeFallback() {
91
- _loadCodeFallback = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(url, initialVariant, loaded) {
92
- var options,
93
- shouldHighlight,
94
- fallbackUsesExtraFiles,
95
- fallbackUsesAllVariants,
96
- sourceParser,
97
- loadSource,
98
- loadVariantMeta,
99
- loadCodeMeta,
100
- initialFilename,
101
- variants,
102
- globalsCode,
103
- output,
104
- functionName,
105
- currentMark,
106
- initial,
107
- _allFileNames,
108
- fileSource,
109
- actualFilename,
110
- result,
111
- parseSource,
112
- quickVariant,
113
- derivedFileName,
114
- extension,
115
- beforeInitialVariantMark,
116
- _allFileNames2,
117
- _fileSource,
118
- _actualFilename,
119
- _result,
120
- _parseSource,
121
- beforeGlobalsMark,
122
- globalsCodeObjects,
123
- hasStringUrls,
124
- globalsPromises,
125
- resolvedGlobalsCode,
126
- _iterator,
127
- _step,
128
- codeObj,
129
- targetVariant,
130
- _yield$loadCodeVarian,
131
- loadedVariant,
132
- allFileNames,
133
- beforeAllVariantMark,
134
- allVariants,
135
- variantPromises,
136
- variantResults,
137
- finalInitial,
138
- finalFileSource,
139
- finalFilename,
140
- _result2,
141
- _args4 = arguments,
142
- _t4,
143
- _t5,
144
- _t6,
145
- _t7,
146
- _t8,
147
- _t9,
148
- _t0,
149
- _t1;
150
- return _regenerator().w(function (_context4) {
151
- while (1) switch (_context4.p = _context4.n) {
152
- case 0:
153
- options = _args4.length > 3 && _args4[3] !== undefined ? _args4[3] : {};
154
- shouldHighlight = options.shouldHighlight, fallbackUsesExtraFiles = options.fallbackUsesExtraFiles, fallbackUsesAllVariants = options.fallbackUsesAllVariants, sourceParser = options.sourceParser, loadSource = options.loadSource, loadVariantMeta = options.loadVariantMeta, loadCodeMeta = options.loadCodeMeta, initialFilename = options.initialFilename, variants = options.variants, globalsCode = options.globalsCode, output = options.output;
155
- loaded = _extends({}, loaded);
156
- functionName = 'Load Fallback Code';
157
- currentMark = performanceMeasure(undefined, {
158
- mark: 'Start',
159
- measure: 'Start'
160
- }, [functionName, url], true); // Step 1: Ensure we have the initial variant loaded
161
- initial = loaded[initialVariant];
162
- if (initial) {
163
- _context4.n = 6;
164
- break;
165
- }
166
- if (loadCodeMeta) {
167
- _context4.n = 1;
168
- break;
169
- }
170
- throw new Error('"loadCodeMeta" function is required when initial variant is not provided');
171
- case 1:
172
- _context4.p = 1;
173
- _context4.n = 2;
174
- return loadCodeMeta(url);
175
- case 2:
176
- loaded = _context4.v;
177
- _context4.n = 4;
178
- break;
179
- case 3:
180
- _context4.p = 3;
181
- _t4 = _context4.v;
182
- throw new Error("Failed to load code from URL: ".concat(url, ". Error: ").concat(JSON.stringify(_t4)));
183
- case 4:
184
- initial = loaded[initialVariant];
185
- if (initial) {
186
- _context4.n = 5;
187
- break;
188
- }
189
- throw new Error("Initial variant \"".concat(initialVariant, "\" not found in loaded code."));
190
- case 5:
191
- currentMark = performanceMeasure(currentMark, {
192
- mark: 'Loaded Code Meta',
193
- measure: 'Code Meta Loading'
194
- }, [functionName, url]);
195
- case 6:
196
- if (!(typeof initial !== 'string' && initial.allFilesListed && !fallbackUsesExtraFiles && !fallbackUsesAllVariants)) {
197
- _context4.n = 17;
198
- break;
199
- }
200
- // Collect all file names from the loaded code
201
- _allFileNames = new Set();
202
- if (initial.fileName) {
203
- _allFileNames.add(initial.fileName);
204
- }
205
- if (initial.extraFiles) {
206
- Object.keys(initial.extraFiles).forEach(function (fileName) {
207
- return _allFileNames.add(fileName);
208
- });
209
- }
130
+ currentMark = performanceMeasure(currentMark, {
131
+ mark: 'Parsed Main File',
132
+ measure: 'Main File Parsing'
133
+ }, [functionName, url]);
134
+ } else if (shouldHighlight && typeof fileSource === 'string' && !actualFilename) {
135
+ // Create basic HAST node when we can't parse due to missing filename
136
+ // This marks that the source has passed through the parsing pipeline
137
+ fileSource = {
138
+ type: 'root',
139
+ children: [{
140
+ type: 'text',
141
+ value: fileSource
142
+ }]
143
+ };
144
+ }
210
145
 
211
- // Get the source for the requested filename (or main file if not specified)
212
- _context4.p = 7;
213
- _context4.n = 8;
214
- return getFileSource(initial, initialFilename, loadSource);
215
- case 8:
216
- result = _context4.v;
217
- fileSource = result.source;
218
- actualFilename = result.filename;
219
- _context4.n = 10;
220
- break;
221
- case 9:
222
- _context4.p = 9;
223
- _t5 = _context4.v;
224
- throw new Error("Failed to get source for file ".concat(initialFilename || initial.fileName, " in variant ").concat(initialVariant, ": ").concat(_t5));
225
- case 10:
226
- currentMark = performanceMeasure(currentMark, {
227
- mark: 'Loaded Main File',
228
- measure: 'Main File Loading'
229
- }, [functionName, url]);
146
+ // Update the loaded code with any changes we made
147
+ if (actualFilename && actualFilename === initial.fileName) {
148
+ initial = {
149
+ ...initial,
150
+ source: fileSource
151
+ };
152
+ loaded = {
153
+ ...loaded,
154
+ [initialVariant]: initial
155
+ };
156
+ } else if (!actualFilename && !initial.fileName) {
157
+ // If both are undefined, we're dealing with the main source
158
+ initial = {
159
+ ...initial,
160
+ source: fileSource
161
+ };
162
+ loaded = {
163
+ ...loaded,
164
+ [initialVariant]: initial
165
+ };
166
+ }
230
167
 
231
- // If we need highlighting and have a string source, parse it
232
- if (!(shouldHighlight && typeof fileSource === 'string' && sourceParser && actualFilename)) {
233
- _context4.n = 15;
234
- break;
235
- }
236
- _context4.p = 11;
237
- _context4.n = 12;
238
- return sourceParser;
239
- case 12:
240
- parseSource = _context4.v;
241
- fileSource = parseSource(fileSource, actualFilename);
242
- _context4.n = 14;
243
- break;
244
- case 13:
245
- _context4.p = 13;
246
- _t6 = _context4.v;
247
- throw new Error("Failed to parse source for highlighting (variant: ".concat(initialVariant, ", file: ").concat(actualFilename, "): ").concat(JSON.stringify(_t6)));
248
- case 14:
249
- currentMark = performanceMeasure(currentMark, {
250
- mark: 'Parsed Main File',
251
- measure: 'Main File Parsing'
252
- }, [functionName, url]);
253
- _context4.n = 16;
254
- break;
255
- case 15:
256
- if (shouldHighlight && typeof fileSource === 'string' && !actualFilename) {
257
- // Create basic HAST node when we can't parse due to missing filename
258
- // This marks that the source has passed through the parsing pipeline
259
- fileSource = {
260
- type: 'root',
261
- children: [{
262
- type: 'text',
263
- value: fileSource
264
- }]
265
- };
266
- }
267
- case 16:
268
- // Update the loaded code with any changes we made
269
- if (actualFilename && actualFilename === initial.fileName) {
270
- initial = _extends(_extends({}, initial), {}, {
271
- source: fileSource
272
- });
273
- loaded = _extends(_extends({}, loaded), {}, _defineProperty({}, initialVariant, initial));
274
- } else if (!actualFilename && !initial.fileName) {
275
- // If both are undefined, we're dealing with the main source
276
- initial = _extends(_extends({}, initial), {}, {
277
- source: fileSource
278
- });
279
- loaded = _extends(_extends({}, loaded), {}, _defineProperty({}, initialVariant, initial));
280
- }
168
+ // Early return - we have all the info we need
169
+ return {
170
+ code: loaded,
171
+ initialFilename: actualFilename,
172
+ initialSource: fileSource,
173
+ initialExtraFiles: initial.extraFiles || {},
174
+ allFileNames: Array.from(allFileNames)
175
+ };
176
+ }
281
177
 
282
- // Early return - we have all the info we need
283
- return _context4.a(2, {
284
- code: loaded,
285
- initialFilename: actualFilename,
286
- initialSource: fileSource,
287
- initialExtraFiles: initial.extraFiles || {},
288
- allFileNames: Array.from(_allFileNames)
289
- });
290
- case 17:
291
- if (!(typeof initial === 'string')) {
292
- _context4.n = 34;
293
- break;
294
- }
295
- _context4.p = 18;
296
- if (!loadVariantMeta) {
297
- _context4.n = 20;
298
- break;
299
- }
300
- _context4.n = 19;
301
- return loadVariantMeta(initialVariant, initial);
302
- case 19:
303
- quickVariant = _context4.v;
304
- currentMark = performanceMeasure(currentMark, {
305
- mark: 'Loaded Initial Variant Meta',
306
- measure: 'Initial Variant Meta Loading'
307
- }, [functionName, url]);
308
- _context4.n = 21;
309
- break;
310
- case 20:
311
- // Create a basic variant using fallback logic
312
- derivedFileName = getFileNameFromUrl(initial).fileName;
313
- extension = derivedFileName.slice(derivedFileName.lastIndexOf('.'));
314
- quickVariant = {
315
- url: initial,
316
- fileName: derivedFileName,
317
- language: getLanguageFromExtension(extension)
318
- };
319
- case 21:
320
- beforeInitialVariantMark = currentMark;
321
- loaded = _extends(_extends({}, loaded), {}, _defineProperty({}, initialVariant, quickVariant));
322
- initial = quickVariant;
178
+ // Step 2: Try to get variant metadata quickly first
179
+ if (typeof initial === 'string') {
180
+ try {
181
+ let quickVariant;
182
+ if (loadVariantMeta) {
183
+ // Use provided loadVariantMeta function
184
+ quickVariant = await loadVariantMeta(initialVariant, initial);
185
+ currentMark = performanceMeasure(currentMark, {
186
+ mark: 'Loaded Initial Variant Meta',
187
+ measure: 'Initial Variant Meta Loading'
188
+ }, [functionName, url]);
189
+ } else {
190
+ // Create a basic variant using fallback logic
191
+ const derivedFileName = getFileNameFromUrl(initial).fileName;
192
+ const extension = derivedFileName.slice(derivedFileName.lastIndexOf('.'));
193
+ quickVariant = {
194
+ url: initial,
195
+ fileName: derivedFileName,
196
+ language: getLanguageFromExtension(extension)
197
+ };
198
+ }
199
+ const beforeInitialVariantMark = currentMark;
200
+ loaded = {
201
+ ...loaded,
202
+ [initialVariant]: quickVariant
203
+ };
204
+ initial = quickVariant;
323
205
 
324
- // If we have all files listed and don't need extra file processing, we can optimize
325
- if (!(quickVariant.allFilesListed && !fallbackUsesExtraFiles && !fallbackUsesAllVariants)) {
326
- _context4.n = 32;
327
- break;
328
- }
329
- // Collect all file names from the quick load
330
- _allFileNames2 = new Set();
331
- if (quickVariant.fileName) {
332
- _allFileNames2.add(quickVariant.fileName);
333
- }
334
- if (quickVariant.extraFiles) {
335
- Object.keys(quickVariant.extraFiles).forEach(function (fileName) {
336
- return _allFileNames2.add(fileName);
337
- });
338
- }
206
+ // If we have all files listed and don't need extra file processing, we can optimize
207
+ if (quickVariant.allFilesListed && !fallbackUsesExtraFiles && !fallbackUsesAllVariants) {
208
+ // Collect all file names from the quick load
209
+ const allFileNames = new Set();
210
+ if (quickVariant.fileName) {
211
+ allFileNames.add(quickVariant.fileName);
212
+ }
213
+ if (quickVariant.extraFiles) {
214
+ Object.keys(quickVariant.extraFiles).forEach(fileName => allFileNames.add(fileName));
215
+ }
339
216
 
340
- // Get the source for the requested filename (or main file if not specified)
341
- _context4.p = 22;
342
- _context4.n = 23;
343
- return getFileSource(quickVariant, initialFilename, loadSource);
344
- case 23:
345
- _result = _context4.v;
346
- _fileSource = _result.source;
347
- _actualFilename = _result.filename;
217
+ // Get the source for the requested filename (or main file if not specified)
218
+ let fileSource;
219
+ let actualFilename;
220
+ try {
221
+ const result = await getFileSource(quickVariant, initialFilename, loadSource);
222
+ fileSource = result.source;
223
+ actualFilename = result.filename;
348
224
  currentMark = performanceMeasure(currentMark, {
349
225
  mark: 'Loaded Initial File',
350
226
  measure: 'Initial File Loading'
351
227
  }, [functionName, initialFilename || 'unknown', url]);
352
- _context4.n = 25;
353
- break;
354
- case 24:
355
- _context4.p = 24;
356
- _t7 = _context4.v;
357
- throw new Error("Failed to get source for file ".concat(initialFilename || quickVariant.fileName, " in variant ").concat(initialVariant, ": ").concat(_t7));
358
- case 25:
359
- if (!(shouldHighlight && typeof _fileSource === 'string' && sourceParser && _actualFilename)) {
360
- _context4.n = 30;
361
- break;
362
- }
363
- _context4.p = 26;
364
- _context4.n = 27;
365
- return sourceParser;
366
- case 27:
367
- _parseSource = _context4.v;
368
- _fileSource = _parseSource(_fileSource, _actualFilename);
228
+ } catch (error) {
229
+ throw new Error(`Failed to get source for file ${initialFilename || quickVariant.fileName} in variant ${initialVariant}: ${error}`);
230
+ }
231
+
232
+ // If we need highlighting and have a string source, parse it
233
+ if (shouldHighlight && typeof fileSource === 'string' && sourceParser && actualFilename) {
234
+ try {
235
+ const parseSource = await sourceParser;
236
+ fileSource = parseSource(fileSource, actualFilename);
237
+ currentMark = performanceMeasure(currentMark, {
238
+ mark: 'Parsed Initial File',
239
+ measure: 'Initial File Parsing'
240
+ }, [functionName, initialFilename || 'unknown', url]);
241
+ } catch (error) {
242
+ throw new Error(`Failed to parse source for highlighting (variant: ${initialVariant}, file: ${actualFilename}): ${JSON.stringify(error)}`);
243
+ }
244
+ } else if (shouldHighlight && typeof fileSource === 'string' && !actualFilename) {
245
+ // Create basic HAST node when we can't parse due to missing filename
246
+ // This marks that the source has passed through the parsing pipeline
247
+ fileSource = {
248
+ type: 'root',
249
+ children: [{
250
+ type: 'text',
251
+ value: fileSource
252
+ }]
253
+ };
254
+ }
255
+
256
+ // Update the loaded code with any changes we made
257
+ if (actualFilename && actualFilename === quickVariant.fileName) {
258
+ initial = {
259
+ ...quickVariant,
260
+ source: fileSource
261
+ };
262
+ loaded = {
263
+ ...loaded,
264
+ [initialVariant]: initial
265
+ };
266
+ } else if (!actualFilename && !quickVariant.fileName) {
267
+ // If both are undefined, we're dealing with the main source
268
+ initial = {
269
+ ...quickVariant,
270
+ source: fileSource
271
+ };
272
+ loaded = {
273
+ ...loaded,
274
+ [initialVariant]: initial
275
+ };
276
+ }
277
+ currentMark = performanceMeasure(beforeInitialVariantMark, {
278
+ mark: 'Loaded Initial Files',
279
+ measure: 'Initial Files Loading'
280
+ }, [functionName, url], true);
281
+
282
+ // Early return - we have all the info we need
283
+ return {
284
+ code: loaded,
285
+ initialFilename: actualFilename,
286
+ initialSource: fileSource,
287
+ initialExtraFiles: quickVariant.extraFiles || {},
288
+ allFileNames: Array.from(allFileNames)
289
+ };
290
+ }
291
+ } catch (error) {
292
+ throw new Error(`Failed to load initial variant code (variant: ${initialVariant}, url: ${initial}): ${JSON.stringify(error)}`);
293
+ }
294
+ }
295
+ const beforeGlobalsMark = currentMark;
296
+
297
+ // Step 2b: Fall back to full loadCodeVariant processing
298
+ // Load globalsCode - convert string URLs to Code objects, keep Code objects as-is
299
+ let globalsCodeObjects;
300
+ if (globalsCode && globalsCode.length > 0) {
301
+ const hasStringUrls = globalsCode.some(item => typeof item === 'string');
302
+ if (hasStringUrls && !loadCodeMeta) {
303
+ throw new Error('loadCodeMeta function is required when globalsCode contains string URLs');
304
+ }
305
+
306
+ // Load all string URLs in parallel, keep Code objects as-is
307
+ const globalsPromises = globalsCode.map(async globalItem => {
308
+ if (typeof globalItem === 'string') {
309
+ // String URL - load Code object via loadCodeMeta
310
+ try {
311
+ const codeMeta = await loadCodeMeta(globalItem);
369
312
  currentMark = performanceMeasure(currentMark, {
370
- mark: 'Parsed Initial File',
371
- measure: 'Initial File Parsing'
372
- }, [functionName, initialFilename || 'unknown', url]);
373
- _context4.n = 29;
374
- break;
375
- case 28:
376
- _context4.p = 28;
377
- _t8 = _context4.v;
378
- throw new Error("Failed to parse source for highlighting (variant: ".concat(initialVariant, ", file: ").concat(_actualFilename, "): ").concat(JSON.stringify(_t8)));
379
- case 29:
380
- _context4.n = 31;
381
- break;
382
- case 30:
383
- if (shouldHighlight && typeof _fileSource === 'string' && !_actualFilename) {
384
- // Create basic HAST node when we can't parse due to missing filename
385
- // This marks that the source has passed through the parsing pipeline
386
- _fileSource = {
387
- type: 'root',
388
- children: [{
389
- type: 'text',
390
- value: _fileSource
391
- }]
392
- };
393
- }
394
- case 31:
395
- // Update the loaded code with any changes we made
396
- if (_actualFilename && _actualFilename === quickVariant.fileName) {
397
- initial = _extends(_extends({}, quickVariant), {}, {
398
- source: _fileSource
399
- });
400
- loaded = _extends(_extends({}, loaded), {}, _defineProperty({}, initialVariant, initial));
401
- } else if (!_actualFilename && !quickVariant.fileName) {
402
- // If both are undefined, we're dealing with the main source
403
- initial = _extends(_extends({}, quickVariant), {}, {
404
- source: _fileSource
405
- });
406
- loaded = _extends(_extends({}, loaded), {}, _defineProperty({}, initialVariant, initial));
407
- }
408
- currentMark = performanceMeasure(beforeInitialVariantMark, {
409
- mark: 'Loaded Initial Files',
410
- measure: 'Initial Files Loading'
411
- }, [functionName, url], true);
313
+ mark: 'Loaded Global Code Meta',
314
+ measure: 'Global Code Meta Loading'
315
+ }, [functionName, globalItem, url]);
316
+ return codeMeta;
317
+ } catch (error) {
318
+ throw new Error(`Failed to load globalsCode from URL: ${globalItem}. Error: ${JSON.stringify(error)}`);
319
+ }
320
+ } else {
321
+ // Code object - return as-is
322
+ return globalItem;
323
+ }
324
+ });
325
+ globalsCodeObjects = await Promise.all(globalsPromises);
326
+ currentMark = performanceMeasure(beforeGlobalsMark, {
327
+ mark: 'Loaded Globals Meta',
328
+ measure: 'Globals Meta Loading'
329
+ }, [functionName, url], true);
330
+ }
412
331
 
413
- // Early return - we have all the info we need
414
- return _context4.a(2, {
415
- code: loaded,
416
- initialFilename: _actualFilename,
417
- initialSource: _fileSource,
418
- initialExtraFiles: quickVariant.extraFiles || {},
419
- allFileNames: Array.from(_allFileNames2)
420
- });
421
- case 32:
422
- _context4.n = 34;
423
- break;
424
- case 33:
425
- _context4.p = 33;
426
- _t9 = _context4.v;
427
- throw new Error("Failed to load initial variant code (variant: ".concat(initialVariant, ", url: ").concat(initial, "): ").concat(JSON.stringify(_t9)));
428
- case 34:
429
- beforeGlobalsMark = currentMark; // Step 2b: Fall back to full loadCodeVariant processing
430
- // Load globalsCode - convert string URLs to Code objects, keep Code objects as-is
431
- if (!(globalsCode && globalsCode.length > 0)) {
432
- _context4.n = 37;
433
- break;
434
- }
435
- hasStringUrls = globalsCode.some(function (item) {
436
- return typeof item === 'string';
437
- });
438
- if (!(hasStringUrls && !loadCodeMeta)) {
439
- _context4.n = 35;
440
- break;
441
- }
442
- throw new Error('loadCodeMeta function is required when globalsCode contains string URLs');
443
- case 35:
444
- // Load all string URLs in parallel, keep Code objects as-is
445
- globalsPromises = globalsCode.map(/*#__PURE__*/function () {
446
- var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(globalItem) {
447
- var codeMeta, _t;
448
- return _regenerator().w(function (_context2) {
449
- while (1) switch (_context2.p = _context2.n) {
450
- case 0:
451
- if (!(typeof globalItem === 'string')) {
452
- _context2.n = 4;
453
- break;
454
- }
455
- _context2.p = 1;
456
- _context2.n = 2;
457
- return loadCodeMeta(globalItem);
458
- case 2:
459
- codeMeta = _context2.v;
460
- currentMark = performanceMeasure(currentMark, {
461
- mark: 'Loaded Global Code Meta',
462
- measure: 'Global Code Meta Loading'
463
- }, [functionName, globalItem, url]);
464
- return _context2.a(2, codeMeta);
465
- case 3:
466
- _context2.p = 3;
467
- _t = _context2.v;
468
- throw new Error("Failed to load globalsCode from URL: ".concat(globalItem, ". Error: ").concat(JSON.stringify(_t)));
469
- case 4:
470
- return _context2.a(2, globalItem);
471
- case 5:
472
- return _context2.a(2);
473
- }
474
- }, _callee2, null, [[1, 3]]);
475
- }));
476
- return function (_x7) {
477
- return _ref.apply(this, arguments);
332
+ // Convert globalsCodeObjects to VariantCode | string for this specific variant
333
+ let resolvedGlobalsCode;
334
+ if (globalsCodeObjects && globalsCodeObjects.length > 0) {
335
+ resolvedGlobalsCode = [];
336
+ for (const codeObj of globalsCodeObjects) {
337
+ // Only use the variant that matches the current initialVariant
338
+ const targetVariant = codeObj[initialVariant];
339
+ if (targetVariant) {
340
+ resolvedGlobalsCode.push(targetVariant);
341
+ }
342
+ }
343
+ }
344
+ try {
345
+ const {
346
+ code: loadedVariant
347
+ } = await loadCodeVariant(url, initialVariant, initial, {
348
+ sourceParser,
349
+ loadSource,
350
+ loadVariantMeta,
351
+ sourceTransformers: undefined,
352
+ // sourceTransformers - skip transforms for fallback
353
+ disableTransforms: true,
354
+ // Don't apply transforms for fallback
355
+ disableParsing: !shouldHighlight,
356
+ // Only parse if highlighting is needed
357
+ globalsCode: resolvedGlobalsCode,
358
+ // Pass resolved globalsCode
359
+ output
360
+ });
361
+ currentMark = performanceMeasure(currentMark, {
362
+ mark: 'Loaded Initial Variant',
363
+ measure: 'Initial Variant Loading'
364
+ }, [functionName, url], true);
365
+
366
+ // Update the loaded code with the processed variant
367
+ loaded = {
368
+ ...loaded,
369
+ [initialVariant]: loadedVariant
370
+ };
371
+ initial = loadedVariant;
372
+ } catch (error) {
373
+ throw new Error(`Failed to load initial variant using loadCodeVariant (variant: ${initialVariant}, url: ${url}): ${JSON.stringify(error)}`);
374
+ }
375
+
376
+ // Step 3: Collect all file names
377
+ const allFileNames = new Set();
378
+ if (initial.fileName) {
379
+ allFileNames.add(initial.fileName);
380
+ }
381
+
382
+ // Add extra files from the initial variant
383
+ if (initial.extraFiles) {
384
+ Object.keys(initial.extraFiles).forEach(fileName => allFileNames.add(fileName));
385
+ }
386
+
387
+ // Step 4: Handle fallbackUsesAllVariants - load all variants to get all possible files
388
+ if (fallbackUsesAllVariants) {
389
+ const beforeAllVariantMark = currentMark;
390
+
391
+ // Determine all variants to process - use provided variants or infer from loaded code
392
+ const allVariants = variants || Object.keys(loaded || {});
393
+ if (allVariants.length === 0) {
394
+ console.warn('No variants found for fallbackUsesAllVariants processing');
395
+ } else {
396
+ // Process all required variants, not just the ones already loaded
397
+ const variantPromises = allVariants.map(async variantName => {
398
+ if (variantName === initialVariant) {
399
+ // Skip initial variant as it's already processed
400
+ return {
401
+ variantName,
402
+ loadedVariant: null,
403
+ fileNames: []
404
+ };
405
+ }
406
+ let variant = loaded?.[variantName];
407
+
408
+ // If variant is not loaded yet, load it first using loadCodeMeta
409
+ if (!variant && loadCodeMeta) {
410
+ try {
411
+ const allCode = await loadCodeMeta(url);
412
+ variant = allCode[variantName];
413
+ // Update loaded with all variants from loadCodeMeta
414
+ loaded = {
415
+ ...loaded,
416
+ ...allCode
417
+ };
418
+ currentMark = performanceMeasure(currentMark, {
419
+ mark: 'Loaded Initial Code Meta',
420
+ measure: 'Initial Code Meta Loading'
421
+ }, [functionName, url]);
422
+ } catch (error) {
423
+ console.warn(`Failed to load code meta for variant ${variantName}: ${error}`);
424
+ return {
425
+ variantName,
426
+ loadedVariant: null,
427
+ fileNames: []
478
428
  };
479
- }());
480
- _context4.n = 36;
481
- return Promise.all(globalsPromises);
482
- case 36:
483
- globalsCodeObjects = _context4.v;
484
- currentMark = performanceMeasure(beforeGlobalsMark, {
485
- mark: 'Loaded Globals Meta',
486
- measure: 'Globals Meta Loading'
487
- }, [functionName, url], true);
488
- case 37:
489
- if (globalsCodeObjects && globalsCodeObjects.length > 0) {
490
- resolvedGlobalsCode = [];
491
- _iterator = _createForOfIteratorHelper(globalsCodeObjects);
492
- try {
493
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
494
- codeObj = _step.value;
495
- // Only use the variant that matches the current initialVariant
496
- targetVariant = codeObj[initialVariant];
497
- if (targetVariant) {
498
- resolvedGlobalsCode.push(targetVariant);
499
- }
500
- }
501
- } catch (err) {
502
- _iterator.e(err);
503
- } finally {
504
- _iterator.f();
505
- }
506
429
  }
507
- _context4.p = 38;
508
- _context4.n = 39;
509
- return loadCodeVariant(url, initialVariant, initial, {
510
- sourceParser: sourceParser,
511
- loadSource: loadSource,
512
- loadVariantMeta: loadVariantMeta,
430
+ }
431
+ if (!variant) {
432
+ console.warn(`Variant ${variantName} not found after loading code meta`);
433
+ return {
434
+ variantName,
435
+ loadedVariant: null,
436
+ fileNames: []
437
+ };
438
+ }
439
+ try {
440
+ const {
441
+ code: loadedVariant
442
+ } = await loadCodeVariant(url, variantName, variant, {
443
+ sourceParser,
444
+ loadSource,
445
+ loadVariantMeta,
513
446
  sourceTransformers: undefined,
514
- // sourceTransformers - skip transforms for fallback
447
+ // sourceTransformers
515
448
  disableTransforms: true,
516
- // Don't apply transforms for fallback
517
449
  disableParsing: !shouldHighlight,
518
- // Only parse if highlighting is needed
519
- globalsCode: resolvedGlobalsCode,
520
- // Pass resolved globalsCode
521
- output: output
450
+ output,
451
+ globalsCode: globalsCodeObjects && globalsCodeObjects.length > 0 ? (() => {
452
+ // Convert globalsCodeObjects to VariantCode | string for this specific variant
453
+ const variantGlobalsCode = [];
454
+ for (const codeObj of globalsCodeObjects) {
455
+ // Only use the variant that matches the current variantName
456
+ const targetVariant = codeObj[variantName];
457
+ if (targetVariant) {
458
+ variantGlobalsCode.push(targetVariant);
459
+ }
460
+ }
461
+ return variantGlobalsCode;
462
+ })() : undefined
522
463
  });
523
- case 39:
524
- _yield$loadCodeVarian = _context4.v;
525
- loadedVariant = _yield$loadCodeVarian.code;
464
+
465
+ // Collect file names from this variant
466
+ const fileNames = loadedVariant.fileName ? [loadedVariant.fileName] : [];
467
+ if (loadedVariant.extraFiles) {
468
+ fileNames.push(...Object.keys(loadedVariant.extraFiles));
469
+ }
526
470
  currentMark = performanceMeasure(currentMark, {
527
471
  mark: 'Loaded Initial Variant',
528
472
  measure: 'Initial Variant Loading'
529
- }, [functionName, url], true);
473
+ }, [functionName, variantName, url], true);
474
+ return {
475
+ variantName,
476
+ loadedVariant,
477
+ fileNames
478
+ };
479
+ } catch (error) {
480
+ // Log but don't fail - we want to get as many file names as possible
481
+ console.warn(`Failed to load variant ${variantName} for file listing: ${error}`);
482
+ return {
483
+ variantName,
484
+ loadedVariant: null,
485
+ fileNames: []
486
+ };
487
+ }
488
+ });
489
+ const variantResults = await Promise.all(variantPromises);
530
490
 
531
- // Update the loaded code with the processed variant
532
- loaded = _extends(_extends({}, loaded), {}, _defineProperty({}, initialVariant, loadedVariant));
533
- initial = loadedVariant;
534
- _context4.n = 41;
535
- break;
536
- case 40:
537
- _context4.p = 40;
538
- _t0 = _context4.v;
539
- throw new Error("Failed to load initial variant using loadCodeVariant (variant: ".concat(initialVariant, ", url: ").concat(url, "): ").concat(JSON.stringify(_t0)));
540
- case 41:
541
- // Step 3: Collect all file names
542
- allFileNames = new Set();
543
- if (initial.fileName) {
544
- allFileNames.add(initial.fileName);
545
- }
491
+ // Update loaded code and collect file names
492
+ variantResults.forEach(({
493
+ variantName,
494
+ loadedVariant,
495
+ fileNames
496
+ }) => {
497
+ if (loadedVariant) {
498
+ loaded = {
499
+ ...loaded,
500
+ [variantName]: loadedVariant
501
+ };
502
+ }
503
+ fileNames.forEach(fileName => allFileNames.add(fileName));
504
+ });
505
+ }
506
+ currentMark = performanceMeasure(beforeAllVariantMark, {
507
+ mark: 'Loaded Initial Variants',
508
+ measure: 'Initial Variants Loading'
509
+ }, [functionName, url], true);
510
+ }
546
511
 
547
- // Add extra files from the initial variant
548
- if (initial.extraFiles) {
549
- Object.keys(initial.extraFiles).forEach(function (fileName) {
550
- return allFileNames.add(fileName);
551
- });
552
- }
512
+ // Ensure we have the latest initial variant data
513
+ const finalInitial = loaded[initialVariant];
514
+ if (!finalInitial || typeof finalInitial === 'string') {
515
+ throw new Error(`Failed to process initial variant: ${initialVariant}`);
516
+ }
553
517
 
554
- // Step 4: Handle fallbackUsesAllVariants - load all variants to get all possible files
555
- if (!fallbackUsesAllVariants) {
556
- _context4.n = 45;
557
- break;
558
- }
559
- beforeAllVariantMark = currentMark; // Determine all variants to process - use provided variants or infer from loaded code
560
- allVariants = variants || Object.keys(loaded || {});
561
- if (!(allVariants.length === 0)) {
562
- _context4.n = 42;
563
- break;
564
- }
565
- console.warn('No variants found for fallbackUsesAllVariants processing');
566
- _context4.n = 44;
567
- break;
568
- case 42:
569
- // Process all required variants, not just the ones already loaded
570
- variantPromises = allVariants.map(/*#__PURE__*/function () {
571
- var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(variantName) {
572
- var _loaded;
573
- var variant, allCode, _yield$loadCodeVarian2, _loadedVariant, fileNames, _t2, _t3;
574
- return _regenerator().w(function (_context3) {
575
- while (1) switch (_context3.p = _context3.n) {
576
- case 0:
577
- if (!(variantName === initialVariant)) {
578
- _context3.n = 1;
579
- break;
580
- }
581
- return _context3.a(2, {
582
- variantName: variantName,
583
- loadedVariant: null,
584
- fileNames: []
585
- });
586
- case 1:
587
- variant = (_loaded = loaded) == null ? void 0 : _loaded[variantName]; // If variant is not loaded yet, load it first using loadCodeMeta
588
- if (!(!variant && loadCodeMeta)) {
589
- _context3.n = 5;
590
- break;
591
- }
592
- _context3.p = 2;
593
- _context3.n = 3;
594
- return loadCodeMeta(url);
595
- case 3:
596
- allCode = _context3.v;
597
- variant = allCode[variantName];
598
- // Update loaded with all variants from loadCodeMeta
599
- loaded = _extends(_extends({}, loaded), allCode);
600
- currentMark = performanceMeasure(currentMark, {
601
- mark: 'Loaded Initial Code Meta',
602
- measure: 'Initial Code Meta Loading'
603
- }, [functionName, url]);
604
- _context3.n = 5;
605
- break;
606
- case 4:
607
- _context3.p = 4;
608
- _t2 = _context3.v;
609
- console.warn("Failed to load code meta for variant ".concat(variantName, ": ").concat(_t2));
610
- return _context3.a(2, {
611
- variantName: variantName,
612
- loadedVariant: null,
613
- fileNames: []
614
- });
615
- case 5:
616
- if (variant) {
617
- _context3.n = 6;
618
- break;
619
- }
620
- console.warn("Variant ".concat(variantName, " not found after loading code meta"));
621
- return _context3.a(2, {
622
- variantName: variantName,
623
- loadedVariant: null,
624
- fileNames: []
625
- });
626
- case 6:
627
- _context3.p = 6;
628
- _context3.n = 7;
629
- return loadCodeVariant(url, variantName, variant, {
630
- sourceParser: sourceParser,
631
- loadSource: loadSource,
632
- loadVariantMeta: loadVariantMeta,
633
- sourceTransformers: undefined,
634
- // sourceTransformers
635
- disableTransforms: true,
636
- disableParsing: !shouldHighlight,
637
- output: output,
638
- globalsCode: globalsCodeObjects && globalsCodeObjects.length > 0 ? function () {
639
- // Convert globalsCodeObjects to VariantCode | string for this specific variant
640
- var variantGlobalsCode = [];
641
- var _iterator2 = _createForOfIteratorHelper(globalsCodeObjects),
642
- _step2;
643
- try {
644
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
645
- var _codeObj = _step2.value;
646
- // Only use the variant that matches the current variantName
647
- var _targetVariant = _codeObj[variantName];
648
- if (_targetVariant) {
649
- variantGlobalsCode.push(_targetVariant);
650
- }
651
- }
652
- } catch (err) {
653
- _iterator2.e(err);
654
- } finally {
655
- _iterator2.f();
656
- }
657
- return variantGlobalsCode;
658
- }() : undefined
659
- });
660
- case 7:
661
- _yield$loadCodeVarian2 = _context3.v;
662
- _loadedVariant = _yield$loadCodeVarian2.code;
663
- // Collect file names from this variant
664
- fileNames = _loadedVariant.fileName ? [_loadedVariant.fileName] : [];
665
- if (_loadedVariant.extraFiles) {
666
- fileNames.push.apply(fileNames, _toConsumableArray(Object.keys(_loadedVariant.extraFiles)));
667
- }
668
- currentMark = performanceMeasure(currentMark, {
669
- mark: 'Loaded Initial Variant',
670
- measure: 'Initial Variant Loading'
671
- }, [functionName, variantName, url], true);
672
- return _context3.a(2, {
673
- variantName: variantName,
674
- loadedVariant: _loadedVariant,
675
- fileNames: fileNames
676
- });
677
- case 8:
678
- _context3.p = 8;
679
- _t3 = _context3.v;
680
- // Log but don't fail - we want to get as many file names as possible
681
- console.warn("Failed to load variant ".concat(variantName, " for file listing: ").concat(_t3));
682
- return _context3.a(2, {
683
- variantName: variantName,
684
- loadedVariant: null,
685
- fileNames: []
686
- });
687
- }
688
- }, _callee3, null, [[6, 8], [2, 4]]);
689
- }));
690
- return function (_x8) {
691
- return _ref2.apply(this, arguments);
692
- };
693
- }());
694
- _context4.n = 43;
695
- return Promise.all(variantPromises);
696
- case 43:
697
- variantResults = _context4.v;
698
- // Update loaded code and collect file names
699
- variantResults.forEach(function (_ref3) {
700
- var variantName = _ref3.variantName,
701
- loadedVariant = _ref3.loadedVariant,
702
- fileNames = _ref3.fileNames;
703
- if (loadedVariant) {
704
- loaded = _extends(_extends({}, loaded), {}, _defineProperty({}, variantName, loadedVariant));
705
- }
706
- fileNames.forEach(function (fileName) {
707
- return allFileNames.add(fileName);
708
- });
709
- });
710
- case 44:
711
- currentMark = performanceMeasure(beforeAllVariantMark, {
712
- mark: 'Loaded Initial Variants',
713
- measure: 'Initial Variants Loading'
714
- }, [functionName, url], true);
715
- case 45:
716
- // Ensure we have the latest initial variant data
717
- finalInitial = loaded[initialVariant];
718
- if (!(!finalInitial || typeof finalInitial === 'string')) {
719
- _context4.n = 46;
720
- break;
721
- }
722
- throw new Error("Failed to process initial variant: ".concat(initialVariant));
723
- case 46:
724
- _context4.p = 46;
725
- _context4.n = 47;
726
- return getFileSource(finalInitial, initialFilename, loadSource);
727
- case 47:
728
- _result2 = _context4.v;
729
- finalFileSource = _result2.source;
730
- finalFilename = _result2.filename;
731
- _context4.n = 50;
732
- break;
733
- case 48:
734
- _context4.p = 48;
735
- _t1 = _context4.v;
736
- if (!(!finalInitial.fileName && !finalInitial.source)) {
737
- _context4.n = 49;
738
- break;
739
- }
740
- throw new Error("Cannot determine filename for initial variant \"".concat(initialVariant, "\". ") + "No fileName available in variant definition, no initialFilename provided, and no source available.");
741
- case 49:
742
- // Fall back to the main file with proper validation
743
- finalFileSource = finalInitial.source || '';
744
- finalFilename = finalInitial.fileName;
745
- case 50:
746
- currentMark = performanceMeasure(currentMark, {
747
- mark: 'Loaded Initial File',
748
- measure: 'Initial File Loading'
749
- }, [functionName, url]);
750
- return _context4.a(2, {
751
- code: loaded,
752
- initialFilename: finalFilename,
753
- initialSource: finalFileSource,
754
- initialExtraFiles: finalInitial.extraFiles || {},
755
- allFileNames: Array.from(allFileNames),
756
- processedGlobalsCode: globalsCodeObjects
757
- });
758
- }
759
- }, _callee4, null, [[46, 48], [38, 40], [26, 28], [22, 24], [18, 33], [11, 13], [7, 9], [1, 3]]);
760
- }));
761
- return _loadCodeFallback.apply(this, arguments);
518
+ // Get the source for the requested filename (or main file if not specified) for the final return
519
+ let finalFileSource;
520
+ let finalFilename;
521
+ try {
522
+ const result = await getFileSource(finalInitial, initialFilename, loadSource);
523
+ finalFileSource = result.source;
524
+ finalFilename = result.filename;
525
+ } catch (error) {
526
+ // If we can't get the specific file, fall back to main file
527
+ if (!finalInitial.fileName && !finalInitial.source) {
528
+ throw new Error(`Cannot determine filename for initial variant "${initialVariant}". ` + `No fileName available in variant definition, no initialFilename provided, and no source available.`);
529
+ }
530
+
531
+ // Fall back to the main file with proper validation
532
+ finalFileSource = finalInitial.source || '';
533
+ finalFilename = finalInitial.fileName;
534
+ }
535
+ currentMark = performanceMeasure(currentMark, {
536
+ mark: 'Loaded Initial File',
537
+ measure: 'Initial File Loading'
538
+ }, [functionName, url]);
539
+ return {
540
+ code: loaded,
541
+ initialFilename: finalFilename,
542
+ initialSource: finalFileSource,
543
+ initialExtraFiles: finalInitial.extraFiles || {},
544
+ allFileNames: Array.from(allFileNames),
545
+ processedGlobalsCode: globalsCodeObjects
546
+ };
762
547
  }