dumi 2.4.51 → 2.4.53

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 (34) hide show
  1. package/compiled/file-system-cache/LICENSE +134 -0
  2. package/compiled/file-system-cache/index.d.ts +3 -0
  3. package/compiled/file-system-cache/index.js +1 -0
  4. package/compiled/file-system-cache/package.json +1 -0
  5. package/dist/assetParsers/block.d.ts +3 -0
  6. package/dist/assetParsers/block.js +82 -15
  7. package/dist/features/compile/index.js +6 -1
  8. package/dist/features/compile/utoopackLoaders.d.ts +3 -2
  9. package/dist/features/compile/utoopackLoaders.js +8 -6
  10. package/dist/features/meta.js +4 -4
  11. package/dist/features/tabs/utils.d.ts +3 -0
  12. package/dist/features/tabs/utils.js +41 -0
  13. package/dist/features/tabs.d.ts +1 -3
  14. package/dist/features/tabs.js +12 -19
  15. package/dist/loaders/constants.d.ts +2 -0
  16. package/dist/loaders/constants.js +32 -0
  17. package/dist/loaders/demo/index.js +3 -3
  18. package/dist/loaders/markdown/index.js +161 -68
  19. package/dist/loaders/markdown/transformer/index.d.ts +2 -0
  20. package/dist/loaders/markdown/transformer/index.js +1 -0
  21. package/dist/loaders/markdown/transformer/rehypeDemo.d.ts +1 -0
  22. package/dist/loaders/markdown/transformer/rehypeDemo.js +17 -9
  23. package/dist/loaders/markdown/transformer/rehypeJsxify.js +1 -1
  24. package/dist/loaders/markdown/transformer/rehypeLink.js +32 -18
  25. package/dist/loaders/markdown/transformer/rehypeRaw.js +2 -2
  26. package/dist/loaders/markdown/transformer/rehypeText.d.ts +2 -1
  27. package/dist/loaders/markdown/transformer/rehypeText.js +3 -3
  28. package/dist/loaders/markdown/transformer/remarkBreaks.js +4 -4
  29. package/dist/loaders/markdown/transformer/remarkEmbed.js +2 -2
  30. package/dist/loaders/markdown/transformer/remarkMeta.js +9 -9
  31. package/dist/techStacks/utils.js +2 -2
  32. package/dist/utils.d.ts +2 -1
  33. package/dist/utils.js +16 -16
  34. package/package.json +1 -1
@@ -38,14 +38,14 @@ __export(markdown_exports, {
38
38
  getMdLoaderCacheSync: () => getMdLoaderCacheSync
39
39
  });
40
40
  module.exports = __toCommonJS(markdown_exports);
41
- var import_utoopackLoaders = require("../../features/compile/utoopackLoaders");
42
- var import_tabs = require("../../features/tabs");
43
- var import_utils = require("../../utils");
41
+ var import_utils = require("../../features/tabs/utils");
42
+ var import_constants = require("../constants");
43
+ var import_utils2 = require("../../utils");
44
+ var import_utils3 = require("@umijs/utils");
44
45
  var import_fs = __toESM(require("fs"));
45
46
  var import_path = __toESM(require("path"));
46
- var import_plugin_utils = require("umi/plugin-utils");
47
47
  var import_transformer = __toESM(require("./transformer"));
48
- var import_rehypeText = require("./transformer/rehypeText");
48
+ var MD_LOADER_CACHE_VERSION = 1;
49
49
  function isMalformedCacheError(err) {
50
50
  return err instanceof SyntaxError || err instanceof Error && /Unexpected end of JSON input|Unterminated string in JSON/.test(
51
51
  err.message
@@ -83,7 +83,7 @@ function isRelativePath(path2) {
83
83
  return /^\.{1,2}(?!\w)/.test(path2);
84
84
  }
85
85
  function normalizeRouteFile(file) {
86
- return (0, import_plugin_utils.winPath)(file).replace(/\.(mdx?)\.js$/, ".$1");
86
+ return (0, import_utils3.winPath)(file).replace(/\.(mdx?)\.js$/, ".$1");
87
87
  }
88
88
  function getRouteId(opts, fileAbsPath) {
89
89
  const normalizedFile = normalizeRouteFile(fileAbsPath);
@@ -96,9 +96,9 @@ function getRouteId(opts, fileAbsPath) {
96
96
  function emitDefault(opts, ret) {
97
97
  const { frontmatter, demos } = ret.meta;
98
98
  const sharedDemoProps = ret.meta.demoSharedProps ?? [];
99
- const isTabContent = (0, import_tabs.isTabRouteFile)(this.resourcePath);
99
+ const isTabContent = (0, import_utils.isTabRouteFile)(this.resourcePath);
100
100
  const wrapper = isTabContent ? "" : "DumiPage";
101
- const textMode = import_utoopackLoaders.UTOOPACK_LOADER_CTX_KEY in opts && process.env.NODE_ENV !== "production" ? "text-render" : "text";
101
+ const textMode = import_constants.UTOOPACK_LOADER_CTX_KEY in opts && process.env.NODE_ENV !== "production" ? "text-render" : "text";
102
102
  if (demos && opts.onResolveDemos) {
103
103
  opts.onResolveDemos(demos);
104
104
  }
@@ -120,16 +120,16 @@ function emitDefault(opts, ret) {
120
120
  const embeddedMdFiles = (ret.meta.embeds ?? []).filter(
121
121
  (filePath) => filePath.endsWith(".md") && !filePath.includes("node_modules")
122
122
  );
123
- return `${Object.values(opts.builtins).map((item) => `import ${item.specifier} from '${(0, import_plugin_utils.winPath)(item.source)}';`).join("\n")}
123
+ return `${Object.values(opts.builtins).map((item) => `import ${item.specifier} from '${(0, import_utils3.winPath)(item.source)}';`).join("\n")}
124
124
  ${embeddedMdFiles.map(
125
125
  (md) => `
126
- import '${(0, import_plugin_utils.winPath)(md)}?watch=parent';
126
+ import '${(0, import_utils3.winPath)(md)}?watch=parent';
127
127
  `
128
128
  ).join("\n")}
129
129
  import LoadingComponent from '@@/dumi/theme/loading';
130
130
  import React, { Suspense } from 'react';
131
131
  import { DumiPage } from 'dumi';
132
- import { texts as ${import_rehypeText.CONTENT_TEXTS_OBJ_NAME} } from '${(0, import_plugin_utils.winPath)(
132
+ import { texts as ${import_constants.CONTENT_TEXTS_OBJ_NAME} } from '${(0, import_utils3.winPath)(
133
133
  this.resourcePath
134
134
  )}?type=${textMode}';
135
135
  ${sharedDemoProps.length ? `const __dumi_demo_shared_props__ = ${JSON.stringify(sharedDemoProps)};` : ""}
@@ -153,6 +153,7 @@ function emitDemo(opts, ret) {
153
153
  const shareDepsMap = {};
154
154
  const demoDepsMap = {};
155
155
  const relativeDepsMap = {};
156
+ const sideEffectDepsMap = /* @__PURE__ */ new Map();
156
157
  demos == null ? void 0 : demos.forEach((demo) => {
157
158
  var _a, _b;
158
159
  if ("resolveMap" in demo && "asset" in demo) {
@@ -160,10 +161,15 @@ function emitDemo(opts, ret) {
160
161
  demoDepsMap[_a = demo.id] ?? (demoDepsMap[_a] = {});
161
162
  relativeDepsMap[_b = demo.id] ?? (relativeDepsMap[_b] = {});
162
163
  Object.keys(demo.resolveMap).forEach((key, index) => {
164
+ var _a2;
163
165
  const specifier = `${demo.id.replace(/[^\w\d]/g, "_")}_deps_${index}`;
164
166
  if (key !== entryFileName) {
165
167
  const isRelative = isRelativePath(key);
166
- const normalizedKey = isRelative ? (0, import_plugin_utils.winPath)(demo.resolveMap[key]) : key;
168
+ const normalizedKey = isRelative ? (0, import_utils3.winPath)(demo.resolveMap[key]) : key;
169
+ sideEffectDepsMap.set(
170
+ normalizedKey,
171
+ sideEffectDepsMap.get(normalizedKey) !== false && ((_a2 = demo.sideEffectStyles) == null ? void 0 : _a2.includes(normalizedKey)) === true
172
+ );
167
173
  if (!shareDepsMap[normalizedKey]) {
168
174
  demoDepsMap[demo.id][normalizedKey] = specifier;
169
175
  shareDepsMap[normalizedKey] = specifier;
@@ -182,18 +188,29 @@ function emitDemo(opts, ret) {
182
188
  Object.entries(deps).map(([key, specifier]) => {
183
189
  const existingIndex = acc.findIndex((obj) => obj.key === key);
184
190
  if (existingIndex === -1) {
185
- return { key, specifier };
191
+ return {
192
+ key,
193
+ specifier,
194
+ // Preserve webpack's namespace behavior, including custom style exports.
195
+ sideEffectOnly: Boolean(opts[import_constants.UTOOPACK_LOADER_CTX_KEY]) && sideEffectDepsMap.get(key) === true
196
+ };
186
197
  }
187
198
  return void 0;
188
199
  }).filter((item) => item !== void 0)
189
200
  );
190
201
  }, []);
191
202
  const routeId = getRouteId(opts, this.resourcePath);
192
- return import_plugin_utils.Mustache.render(
203
+ return import_utils3.Mustache.render(
193
204
  `import React from 'react';
194
- import '${(0, import_plugin_utils.winPath)(this.resourcePath)}?watch=parent';
205
+ import '${(0, import_utils3.winPath)(this.resourcePath)}?watch=parent';
195
206
  {{#dedupedDemosDeps}}
207
+ {{#sideEffectOnly}}
208
+ import '{{{key}}}';
209
+ const {{{specifier}}} = {};
210
+ {{/sideEffectOnly}}
211
+ {{^sideEffectOnly}}
196
212
  import * as {{{specifier}}} from '{{{key}}}';
213
+ {{/sideEffectOnly}}
197
214
  {{/dedupedDemosDeps}}
198
215
  export const demos = {
199
216
  {{#demos}}
@@ -223,10 +240,10 @@ export const demos = {
223
240
  var _a;
224
241
  if (((_a = asset.dependencies[file]) == null ? void 0 : _a.type) === "FILE") {
225
242
  let assetValue = `{{{require('-!${resolveMap[file]}?dumi-raw').default}}}`;
226
- if (process.env.OKAM || opts[import_utoopackLoaders.UTOOPACK_LOADER_CTX_KEY]) {
243
+ if (process.env.OKAM || opts[import_constants.UTOOPACK_LOADER_CTX_KEY]) {
227
244
  assetValue = `{{{require('${resolveMap[file]}?dumi-raw').default}}}`;
228
245
  }
229
- asset = import_plugin_utils.lodash.cloneDeep(asset);
246
+ asset = import_utils3.lodash.cloneDeep(asset);
230
247
  asset.dependencies[file].value = assetValue;
231
248
  }
232
249
  });
@@ -253,20 +270,20 @@ export const demos = {
253
270
  if (renderOpts.compilePath) {
254
271
  propertyArray.push(`
255
272
  compile: async (...args) => {
256
- return (await import('${(0, import_plugin_utils.winPath)(
273
+ return (await import('${(0, import_utils3.winPath)(
257
274
  renderOpts.compilePath
258
275
  )}')).default(...args);
259
276
  },`);
260
277
  }
261
278
  if (renderOpts.rendererPath) {
262
279
  propertyArray.push(`
263
- renderer: (await import('${(0, import_plugin_utils.winPath)(
280
+ renderer: (await import('${(0, import_utils3.winPath)(
264
281
  renderOpts.rendererPath
265
282
  )}')).default,`);
266
283
  }
267
284
  if (renderOpts.preflightPath) {
268
285
  propertyArray.push(`
269
- preflight: (await import('${(0, import_plugin_utils.winPath)(
286
+ preflight: (await import('${(0, import_utils3.winPath)(
270
287
  renderOpts.preflightPath
271
288
  )}')).default,`);
272
289
  }
@@ -280,7 +297,7 @@ export const demos = {
280
297
  );
281
298
  }
282
299
  function renderDemoIndex(resourcePath, opts, demos) {
283
- return import_plugin_utils.Mustache.render(
300
+ return import_utils3.Mustache.render(
284
301
  `
285
302
  export const demoIndex = {
286
303
  ids: {{{ids}}},
@@ -288,31 +305,31 @@ export const demoIndex = {
288
305
  };`,
289
306
  {
290
307
  ids: JSON.stringify(demos == null ? void 0 : demos.map((demo) => demo.id)),
291
- getter: `() => import(/* webpackChunkName: "${(0, import_utils.generateMetaChunkName)(
308
+ getter: `() => import(/* webpackChunkName: "${(0, import_utils2.generateMetaChunkName)(
292
309
  resourcePath,
293
310
  opts.cwd,
294
311
  opts.locales.map(({ id }) => id)
295
- )}" */'${(0, import_plugin_utils.winPath)(resourcePath)}?type=demo')`
312
+ )}" */'${(0, import_utils3.winPath)(resourcePath)}?type=demo')`
296
313
  }
297
314
  );
298
315
  }
299
316
  function emitDemoIndex(opts, ret) {
300
317
  const { demos } = ret.meta;
301
- return `import '${(0, import_plugin_utils.winPath)(this.resourcePath)}?watch=parent';
318
+ return `import '${(0, import_utils3.winPath)(this.resourcePath)}?watch=parent';
302
319
  ${renderDemoIndex(this.resourcePath, opts, demos)}`;
303
320
  }
304
321
  function emitFrontmatter(opts, ret) {
305
322
  const { demos } = ret.meta;
306
323
  const frontmatter = ret.meta.frontmatter;
307
324
  const toc = ret.meta.toc ?? [];
308
- const resourcePath = (0, import_plugin_utils.winPath)(this.resourcePath);
309
- const isUtoopack = import_utoopackLoaders.UTOOPACK_LOADER_CTX_KEY in opts;
325
+ const resourcePath = (0, import_utils3.winPath)(this.resourcePath);
326
+ const isUtoopack = import_constants.UTOOPACK_LOADER_CTX_KEY in opts;
310
327
  const enableUtoopackHMR = isUtoopack && process.env.NODE_ENV !== "production";
311
- const routeMetaHMRPath = enableUtoopackHMR ? (0, import_plugin_utils.winPath)(require.resolve("../../client/theme-api/routeMetaHMR")) : void 0;
328
+ const routeMetaHMRPath = enableUtoopackHMR ? (0, import_utils3.winPath)(require.resolve("../../client/theme-api/routeMetaHMR")) : void 0;
312
329
  const routeId = getRouteId(opts, this.resourcePath);
313
330
  const demoIndex = renderDemoIndex(this.resourcePath, opts, demos);
314
331
  if (isUtoopack) {
315
- const routeStructureHash = (0, import_utils.getContentHash)(
332
+ const routeStructureHash = (0, import_utils2.getContentHash)(
316
333
  JSON.stringify({
317
334
  atomId: frontmatter.atomId,
318
335
  demoIds: demos == null ? void 0 : demos.map(({ id }) => id),
@@ -323,7 +340,7 @@ function emitFrontmatter(opts, ret) {
323
340
  title: frontmatter.title
324
341
  })
325
342
  );
326
- const rendered = import_plugin_utils.Mustache.render(
343
+ const rendered = import_utils3.Mustache.render(
327
344
  `{{#enableUtoopackHMR}}
328
345
  import { notifyRouteMetaHMR } from {{{routeMetaHMRSpecifier}}};
329
346
  {{/enableUtoopackHMR}}
@@ -381,7 +398,7 @@ if (module.hot) {
381
398
  );
382
399
  return rendered;
383
400
  }
384
- return import_plugin_utils.Mustache.render(
401
+ return import_utils3.Mustache.render(
385
402
  `import '${resourcePath}?watch=parent';
386
403
  export const toc = new Proxy({{{toc}}}, {});
387
404
  export const frontmatter = new Proxy({{{frontmatter}}}, {});
@@ -395,14 +412,14 @@ export const frontmatter = new Proxy({{{frontmatter}}}, {});
395
412
  }
396
413
  function emitText(opts, ret) {
397
414
  const texts = ret.meta.texts ?? [];
398
- const resourcePath = (0, import_plugin_utils.winPath)(this.resourcePath);
399
- const enableUtoopackHMR = import_utoopackLoaders.UTOOPACK_LOADER_CTX_KEY in opts && process.env.NODE_ENV !== "production";
415
+ const resourcePath = (0, import_utils3.winPath)(this.resourcePath);
416
+ const enableUtoopackHMR = import_constants.UTOOPACK_LOADER_CTX_KEY in opts && process.env.NODE_ENV !== "production";
400
417
  if (enableUtoopackHMR) {
401
418
  const routeId = getRouteId(opts, this.resourcePath);
402
419
  const routeMetaHMRSpecifier = JSON.stringify(
403
- (0, import_plugin_utils.winPath)(require.resolve("../../client/theme-api/routeMetaHMR"))
420
+ (0, import_utils3.winPath)(require.resolve("../../client/theme-api/routeMetaHMR"))
404
421
  );
405
- return import_plugin_utils.Mustache.render(
422
+ return import_utils3.Mustache.render(
406
423
  `import { notifyRouteMetaHMR } from {{{routeMetaHMRSpecifier}}};
407
424
  import {{{watchSpecifier}}};
408
425
  globalThis.__DUMI_TEXTS__ = globalThis.__DUMI_TEXTS__ || {};
@@ -430,7 +447,7 @@ if (module.hot) {
430
447
  }
431
448
  );
432
449
  }
433
- return import_plugin_utils.Mustache.render(
450
+ return import_utils3.Mustache.render(
434
451
  `
435
452
  import '${resourcePath}?watch=parent';
436
453
  export const texts = {{{texts}}};
@@ -441,7 +458,7 @@ if (module.hot) {
441
458
  );
442
459
  }
443
460
  function emitRenderText(_opts, ret) {
444
- return import_plugin_utils.Mustache.render(
461
+ return import_utils3.Mustache.render(
445
462
  `
446
463
  import {{{watchSpecifier}}};
447
464
  export const texts = {{{texts}}};
@@ -449,7 +466,7 @@ function emitRenderText(_opts, ret) {
449
466
  {
450
467
  texts: JSON.stringify(ret.meta.texts ?? []),
451
468
  watchSpecifier: JSON.stringify(
452
- `${(0, import_plugin_utils.winPath)(this.resourcePath)}?watch=parent`
469
+ `${(0, import_utils3.winPath)(this.resourcePath)}?watch=parent`
453
470
  )
454
471
  }
455
472
  );
@@ -477,25 +494,73 @@ function emit(opts, ret) {
477
494
  return emitDefault.call(this, opts, ret);
478
495
  }
479
496
  }
480
- function getDepsCacheKey(deps = []) {
481
- return JSON.stringify(
482
- deps.map(
483
- (file) => `${file}:${(0, import_utils.getContentHash)(import_fs.default.readFileSync(file, "utf-8"))}`
484
- )
497
+ function getDepsCacheKey(deps = [], cacheSession) {
498
+ if (!cacheSession) {
499
+ return JSON.stringify(
500
+ deps.map(
501
+ (file) => `${file}:${(0, import_utils2.getContentHash)(import_fs.default.readFileSync(file, "utf-8"))}`
502
+ )
503
+ );
504
+ }
505
+ const files = Array.from(
506
+ new Set(deps.concat(deps.flatMap(getDemoSidecarFiles)))
485
507
  );
508
+ try {
509
+ return JSON.stringify(
510
+ files.map(
511
+ (file) => `${file}:${(0, import_utils2.getContentHash)(import_fs.default.readFileSync(file, "utf-8"))}`
512
+ )
513
+ );
514
+ } catch (err) {
515
+ const code = err.code;
516
+ if (code === "ENOENT" || code === "ENOTDIR") {
517
+ return void 0;
518
+ }
519
+ throw err;
520
+ }
486
521
  }
487
522
  var deferrer = {};
488
523
  var depsMapping = {};
524
+ var loaderContextHashes = /* @__PURE__ */ new WeakMap();
489
525
  function mdLoader(content) {
490
526
  var _a, _b, _c;
491
527
  let opts = this.getOptions();
492
- const loaderContextPath = opts[import_utoopackLoaders.UTOOPACK_LOADER_CTX_KEY];
528
+ let cacheSession;
529
+ const loaderContextPath = opts[import_constants.UTOOPACK_LOADER_CTX_KEY];
493
530
  const useUtoopackDemoHMR = process.env.NODE_ENV !== "production" && Boolean(loaderContextPath);
494
531
  if (loaderContextPath) {
495
- const ctx = require(loaderContextPath);
532
+ const contextModuleId = useUtoopackDemoHMR && process.env.DUMI_CACHE !== "none" ? require.resolve(loaderContextPath) : void 0;
533
+ const cachedContextModule = contextModuleId ? require.cache[contextModuleId] : void 0;
534
+ const initialContextHash = contextModuleId && !cachedContextModule ? (0, import_utils2.getContentHash)(import_fs.default.readFileSync(loaderContextPath, "utf-8"), 32) : void 0;
535
+ let ctx = require(loaderContextPath);
536
+ if (contextModuleId && ctx.cacheSession) {
537
+ const contextHash = initialContextHash ?? (0, import_utils2.getContentHash)(import_fs.default.readFileSync(loaderContextPath, "utf-8"), 32);
538
+ if (cachedContextModule && loaderContextHashes.get(cachedContextModule) !== contextHash) {
539
+ const parents = /* @__PURE__ */ new Set([
540
+ cachedContextModule.parent,
541
+ typeof module === "undefined" ? void 0 : module,
542
+ ...Object.values(require.cache)
543
+ ]);
544
+ for (const parent of parents) {
545
+ const children = parent == null ? void 0 : parent.children;
546
+ const index = (children == null ? void 0 : children.indexOf(cachedContextModule)) ?? -1;
547
+ if (index !== -1)
548
+ children.splice(index, 1);
549
+ }
550
+ delete require.cache[contextModuleId];
551
+ ctx = require(contextModuleId);
552
+ }
553
+ const contextModule = require.cache[contextModuleId];
554
+ if (contextModule)
555
+ loaderContextHashes.set(contextModule, contextHash);
556
+ }
557
+ cacheSession = useUtoopackDemoHMR && process.env.DUMI_CACHE !== "none" ? ctx.cacheSession : void 0;
496
558
  if (!((_a = opts.techStacks) == null ? void 0 : _a.length)) {
497
559
  opts.techStacks = ctx.techStacks;
498
560
  }
561
+ if (ctx.alias !== void 0) {
562
+ opts = { ...opts, alias: ctx.alias };
563
+ }
499
564
  if (ctx.builtins && !Object.keys(opts.builtins ?? {}).length) {
500
565
  opts.builtins = ctx.builtins;
501
566
  }
@@ -518,44 +583,72 @@ function mdLoader(content) {
518
583
  )) {
519
584
  this.cacheable(false);
520
585
  }
521
- const cache = (0, import_utils.getCache)("md-loader");
586
+ const cache = (0, import_utils2.getCache)("md-loader");
587
+ let optionsCacheKey = JSON.stringify(
588
+ import_utils3.lodash.omit(opts, ["mode", "builtins", "onResolveDemos"])
589
+ );
590
+ if (cacheSession)
591
+ optionsCacheKey = (0, import_utils2.getContentHash)(optionsCacheKey, 32);
522
592
  const baseCacheKey = [
593
+ MD_LOADER_CACHE_VERSION,
523
594
  this.resourcePath,
524
- (0, import_utils.getContentHash)(content),
595
+ (0, import_utils2.getContentHash)(content),
525
596
  useUtoopackDemoHMR,
526
- JSON.stringify(import_plugin_utils.lodash.omit(opts, ["mode", "builtins", "onResolveDemos"]))
527
- ].join(":");
528
- const cacheKey = [
529
- baseCacheKey,
530
- getDepsCacheKey(depsMapping[this.resourcePath])
597
+ optionsCacheKey
531
598
  ].join(":");
532
- const cacheRet = getMdLoaderCacheSync(cache, cacheKey, "");
533
- if (cacheRet) {
599
+ const depsCacheKey = cacheSession ? `deps:${baseCacheKey}` : void 0;
600
+ const depsMappingKey = cacheSession ? `${cacheSession}:${this.resourcePath}` : this.resourcePath;
601
+ const cachedDeps = depsCacheKey ? getMdLoaderCacheSync(
602
+ cache,
603
+ depsCacheKey,
604
+ void 0
605
+ ) : void 0;
606
+ const deps = cachedDeps && cachedDeps.cacheSession === cacheSession && Array.isArray(cachedDeps.deps) && cachedDeps.deps.every((file) => typeof file === "string") ? cachedDeps.deps : depsMapping[depsMappingKey];
607
+ const depsKey = getDepsCacheKey(deps, cacheSession);
608
+ const cacheKey = [baseCacheKey, depsKey].join(":");
609
+ const cacheRet = depsKey === void 0 ? "" : getMdLoaderCacheSync(cache, cacheKey, "");
610
+ const matchesSession = !cacheSession || !(deps == null ? void 0 : deps.length) || cacheRet && cacheRet.__dumiCacheSession === cacheSession;
611
+ const deferrerKey = cacheSession ? `${cacheSession}:${cacheKey}` : cacheKey;
612
+ if (cacheRet && matchesSession) {
534
613
  cb(null, emit.call(this, opts, cacheRet));
535
614
  return;
536
- } else if (cacheKey in deferrer) {
537
- deferrer[cacheKey].then((res) => {
615
+ } else if (deferrerKey in deferrer) {
616
+ deferrer[deferrerKey].then((res) => {
538
617
  cb(null, emit.call(this, opts, res));
539
618
  }).catch(cb);
540
619
  return;
541
620
  }
542
- deferrer[cacheKey] = (0, import_transformer.default)(content, {
543
- ...import_plugin_utils.lodash.omit(opts, ["mode", "builtins", "onResolveDemos"]),
544
- fileAbsPath: (0, import_plugin_utils.winPath)(this.resourcePath),
545
- useUtoopackDemoHMR
621
+ deferrer[deferrerKey] = (0, import_transformer.default)(content, {
622
+ ...import_utils3.lodash.omit(opts, ["mode", "builtins", "onResolveDemos"]),
623
+ fileAbsPath: (0, import_utils3.winPath)(this.resourcePath),
624
+ useUtoopackDemoHMR,
625
+ collectSideEffectStyles: Boolean(loaderContextPath)
546
626
  });
547
- deferrer[cacheKey].then((ret) => {
548
- depsMapping[this.resourcePath] = ret.meta.embeds.concat(
627
+ deferrer[deferrerKey].then((ret) => {
628
+ depsMapping[depsMappingKey] = ret.meta.embeds.concat(
549
629
  getDemoSourceFiles(ret.meta.demos)
550
630
  );
551
- const finalCacheKey = [
552
- baseCacheKey,
553
- getDepsCacheKey(depsMapping[this.resourcePath])
554
- ].join(":");
555
- cache.setSync(finalCacheKey, ret);
631
+ const finalDepsKey = getDepsCacheKey(
632
+ depsMapping[depsMappingKey],
633
+ cacheSession
634
+ );
635
+ if (finalDepsKey !== void 0) {
636
+ cache.setSync(
637
+ [baseCacheKey, finalDepsKey].join(":"),
638
+ cacheSession && depsMapping[depsMappingKey].length ? { ...ret, __dumiCacheSession: cacheSession } : ret
639
+ );
640
+ if (depsCacheKey)
641
+ cache.setSync(depsCacheKey, {
642
+ cacheSession,
643
+ deps: depsMapping[depsMappingKey]
644
+ });
645
+ }
556
646
  cb(null, emit.call(this, opts, ret));
557
- delete deferrer[cacheKey];
558
- }).catch(cb);
647
+ delete deferrer[deferrerKey];
648
+ }).catch((err) => {
649
+ delete deferrer[deferrerKey];
650
+ cb(err);
651
+ });
559
652
  }
560
653
  // Annotate the CommonJS export names for ESM import in node:
561
654
  0 && (module.exports = {
@@ -24,6 +24,7 @@ declare module 'vfile' {
24
24
  component: string;
25
25
  asset: IParsedBlockAsset['asset'];
26
26
  resolveMap: IParsedBlockAsset['resolveMap'];
27
+ sideEffectStyles?: IParsedBlockAsset['sideEffectStyles'];
27
28
  renderOpts: {
28
29
  type?: string;
29
30
  rendererPath?: string;
@@ -51,6 +52,7 @@ export interface IMdTransformerOptions {
51
52
  cwd: string;
52
53
  fileAbsPath: string;
53
54
  useUtoopackDemoHMR?: boolean;
55
+ collectSideEffectStyles?: boolean;
54
56
  alias: ResolveOptions['alias'];
55
57
  parentAbsPath?: string;
56
58
  techStacks: IDumiTechStack[];
@@ -114,6 +114,7 @@ var transformer_default = async (raw, opts) => {
114
114
  fileLocaleLessPath,
115
115
  fileLocale,
116
116
  useUtoopackDemoHMR: opts.useUtoopackDemoHMR,
117
+ collectSideEffectStyles: opts.collectSideEffectStyles,
117
118
  resolve: opts.resolve,
118
119
  resolver,
119
120
  codeBlockBabelPlugins: opts.codeBlockBabelPlugins
@@ -11,6 +11,7 @@ type IRehypeDemoOptions = Pick<IMdTransformerOptions, 'techStacks' | 'cwd' | 'fi
11
11
  fileLocaleLessPath: string;
12
12
  fileLocale?: string;
13
13
  useUtoopackDemoHMR?: boolean;
14
+ collectSideEffectStyles?: boolean;
14
15
  };
15
16
  export default function rehypeDemo(opts: IRehypeDemoOptions): Transformer<Root>;
16
17
  export {};
@@ -38,8 +38,8 @@ __export(rehypeDemo_exports, {
38
38
  module.exports = __toCommonJS(rehypeDemo_exports);
39
39
  var import_block = __toESM(require("../../../assetParsers/block"));
40
40
  var import_utils = require("../../../utils");
41
+ var import_utils2 = require("@umijs/utils");
41
42
  var import_path = __toESM(require("path"));
42
- var import_plugin_utils = require("umi/plugin-utils");
43
43
  var visit;
44
44
  var SKIP;
45
45
  var EXIT;
@@ -227,7 +227,7 @@ function rehypeDemo(opts) {
227
227
  }
228
228
  });
229
229
  if (process.env.NODE_ENV === "production" && (hasOnlySign || hasSkipSign)) {
230
- import_plugin_utils.logger.warn(
230
+ import_utils2.logger.warn(
231
231
  `The 'only' or 'skip' mark is not supported in production environment, please remove it. at ${vFile.data.frontmatter.filename}`
232
232
  );
233
233
  }
@@ -259,7 +259,7 @@ function rehypeDemo(opts) {
259
259
  let component = "";
260
260
  if (codeType === "external") {
261
261
  const chunkName = [vFile.data.frontmatter.atomId, "demos"].filter(Boolean).join("__");
262
- parseOpts.fileAbsPath = (0, import_plugin_utils.winPath)(
262
+ parseOpts.fileAbsPath = (0, import_utils2.winPath)(
263
263
  codeNode.properties.src
264
264
  );
265
265
  let localId = ((_b = codeNode.properties) == null ? void 0 : _b.id) ?? import_path.default.parse(
@@ -271,7 +271,7 @@ function rehypeDemo(opts) {
271
271
  localId,
272
272
  vFile.data.frontmatter.atomId
273
273
  );
274
- const importChunk = `import( /* webpackChunkName: "${chunkName}" */ '${(0, import_plugin_utils.winPath)(
274
+ const importChunk = `import( /* webpackChunkName: "${chunkName}" */ '${(0, import_utils2.winPath)(
275
275
  parseOpts.fileAbsPath
276
276
  )}?techStack=${techStack.name}')`;
277
277
  if (runtimeOpts == null ? void 0 : runtimeOpts.rendererPath) {
@@ -281,7 +281,7 @@ function rehypeDemo(opts) {
281
281
  }
282
282
  if (codeValue)
283
283
  codeNode.properties.title = codeValue;
284
- (_c = codeNode.properties).filename ?? (_c.filename = (0, import_plugin_utils.winPath)(
284
+ (_c = codeNode.properties).filename ?? (_c.filename = (0, import_utils2.winPath)(
285
285
  import_path.default.relative(opts.cwd, parseOpts.fileAbsPath)
286
286
  ));
287
287
  } else {
@@ -310,14 +310,20 @@ function rehypeDemo(opts) {
310
310
  deferrers.push(
311
311
  (0, import_block.default)({
312
312
  ...parseOpts,
313
- cacheable: opts.useUtoopackDemoHMR
313
+ cacheable: opts.useUtoopackDemoHMR,
314
+ collectSideEffectStyles: opts.collectSideEffectStyles
314
315
  }).then(
315
- async ({ asset, resolveMap, frontmatter }) => {
316
+ async ({
317
+ asset,
318
+ resolveMap,
319
+ frontmatter,
320
+ sideEffectStyles
321
+ }) => {
316
322
  var _a2, _b2, _c2;
317
323
  if (demoIds.indexOf(parseOpts.id) !== demoIds.lastIndexOf(parseOpts.id)) {
318
324
  const startLine = (_a2 = node.position) == null ? void 0 : _a2.start.line;
319
325
  const suffix = startLine ? `:${startLine}` : "";
320
- import_plugin_utils.logger.warn(
326
+ import_utils2.logger.warn(
321
327
  `Duplicate demo id found due to filename conflicts, please consider adding a unique id to code tag to resolve this.
322
328
  at ${opts.fileAbsPath}${suffix}`
323
329
  );
@@ -396,6 +402,8 @@ function rehypeDemo(opts) {
396
402
  resolveMap,
397
403
  techStackOpts
398
404
  ) : resolveMap,
405
+ // Custom source mappings may replace the original dependencies.
406
+ sideEffectStyles: techStack.generateSources ? void 0 : sideEffectStyles,
399
407
  renderOpts: {
400
408
  rendererPath: runtimeOpts == null ? void 0 : runtimeOpts.rendererPath,
401
409
  compilePath: runtimeOpts == null ? void 0 : runtimeOpts.compilePath,
@@ -437,7 +445,7 @@ function rehypeDemo(opts) {
437
445
  vFile.data.demoSharedProps = sharedPreviewerProps;
438
446
  }
439
447
  replaceNodes.forEach((node) => {
440
- const demoLoader = `() => import('${(0, import_plugin_utils.winPath)(
448
+ const demoLoader = `() => import('${(0, import_utils2.winPath)(
441
449
  opts.fileAbsPath
442
450
  )}?type=demo')`;
443
451
  let value = JSON.stringify(node.data[DEMO_PROP_VALUE_KEY]);
@@ -32,7 +32,6 @@ __export(rehypeJsxify_exports, {
32
32
  default: () => rehypeJsxify
33
33
  });
34
34
  module.exports = __toCommonJS(rehypeJsxify_exports);
35
- var parser = __toESM(require("@umijs/bundler-utils/compiled/babel/parser"));
36
35
  var visitUnist;
37
36
  var visitEstree;
38
37
  var toEstree;
@@ -48,6 +47,7 @@ var JSX_SPREAD_PROP_PREFIX = "$jsx-spread-prop-";
48
47
  })();
49
48
  function getJSXAttrAST(node) {
50
49
  const tmpCode = `<a ${node.type === "JSXAttribute" ? `${node.name}={${node.value}}` : `{...${node.argument}}`} />`;
50
+ const parser = require("@umijs/bundler-utils/compiled/babel/parser");
51
51
  const ast = parser.parseExpression(tmpCode, {
52
52
  plugins: ["jsx", "estree"]
53
53
  });