dumi 2.4.49-alpha.0 → 2.4.49-alpha.2

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.
@@ -95,6 +95,7 @@ function getRouteId(opts, fileAbsPath) {
95
95
  }
96
96
  function emitDefault(opts, ret) {
97
97
  const { frontmatter, demos } = ret.meta;
98
+ const sharedDemoProps = ret.meta.demoSharedProps ?? [];
98
99
  const isTabContent = (0, import_tabs.isTabRouteFile)(this.resourcePath);
99
100
  const wrapper = isTabContent ? "" : "DumiPage";
100
101
  const textMode = import_utoopackLoaders.UTOOPACK_LOADER_CTX_KEY in opts && process.env.NODE_ENV !== "production" ? "text-render" : "text";
@@ -131,6 +132,7 @@ import { DumiPage } from 'dumi';
131
132
  import { texts as ${import_rehypeText.CONTENT_TEXTS_OBJ_NAME} } from '${(0, import_plugin_utils.winPath)(
132
133
  this.resourcePath
133
134
  )}?type=${textMode}';
135
+ ${sharedDemoProps.length ? `const __dumi_demo_shared_props__ = ${JSON.stringify(sharedDemoProps)};` : ""}
134
136
 
135
137
  // export named function for fastRefresh
136
138
  // ref: https://github.com/pmmmwh/react-refresh-webpack-plugin/blob/main/docs/TROUBLESHOOTING.md#edits-always-lead-to-full-reload
@@ -43,6 +43,7 @@ declare module 'vfile' {
43
43
  frontmatter: IRouteMeta['frontmatter'];
44
44
  toc: IRouteMeta['toc'];
45
45
  embeds: string[];
46
+ demoSharedProps?: unknown[];
46
47
  }
47
48
  }
48
49
  export interface IMdTransformerOptions {
@@ -49,6 +49,8 @@ var DEMO_NODE_CONTAINER = "$demo-container";
49
49
  var DEMO_PROP_VALUE_KEY = "$demo-prop-value-key";
50
50
  var DEMO_LOADER_PLACEHOLDER = "__DUMI_DEMO_LOADER__";
51
51
  var DEMO_LOADER_PLACEHOLDER_VALUE = DEMO_LOADER_PLACEHOLDER;
52
+ var DEMO_SHARED_PROP_PLACEHOLDER_PREFIX = "__DUMI_DEMO_SHARED_PROP_";
53
+ var MIN_SHARED_PREVIEWER_PROP_SIZE = 1024;
52
54
  var DUMI_DEMO_TAG = "DumiDemo";
53
55
  var DUMI_DEMO_GRID_TAG = "DumiDemoGrid";
54
56
  var SKIP_DEMO_PARSE = "pure";
@@ -66,6 +68,55 @@ var alwaysDemoRE = new RegExp(
66
68
  ({ toString } = await import("hast-util-to-string"));
67
69
  ({ isElement } = await import("hast-util-is-element"));
68
70
  })();
71
+ function getDemoPropsFromNode(node) {
72
+ var _a;
73
+ return [].concat(
74
+ (_a = node.data) == null ? void 0 : _a[DEMO_PROP_VALUE_KEY]
75
+ );
76
+ }
77
+ function hoistSharedPreviewerProps(nodes) {
78
+ const candidates = /* @__PURE__ */ new Map();
79
+ nodes.forEach((node) => {
80
+ getDemoPropsFromNode(node).forEach(({ previewerProps }) => {
81
+ Object.values(previewerProps).forEach((value) => {
82
+ if (!value || typeof value !== "object")
83
+ return;
84
+ const serialized = JSON.stringify(value);
85
+ if (serialized.length < MIN_SHARED_PREVIEWER_PROP_SIZE)
86
+ return;
87
+ const candidate = candidates.get(serialized);
88
+ if (candidate) {
89
+ candidate.count += 1;
90
+ } else {
91
+ candidates.set(serialized, { count: 1, value });
92
+ }
93
+ });
94
+ });
95
+ });
96
+ const sharedValues = [];
97
+ const sharedIndexes = /* @__PURE__ */ new Map();
98
+ candidates.forEach(({ count, value }, serialized) => {
99
+ if (count > 1) {
100
+ sharedIndexes.set(serialized, sharedValues.length);
101
+ sharedValues.push(value);
102
+ }
103
+ });
104
+ if (!sharedValues.length)
105
+ return sharedValues;
106
+ nodes.forEach((node) => {
107
+ getDemoPropsFromNode(node).forEach(({ previewerProps }) => {
108
+ Object.entries(previewerProps).forEach(([key, value]) => {
109
+ if (!value || typeof value !== "object")
110
+ return;
111
+ const index = sharedIndexes.get(JSON.stringify(value));
112
+ if (index !== void 0) {
113
+ previewerProps[key] = `${DEMO_SHARED_PROP_PLACEHOLDER_PREFIX}${index}__`;
114
+ }
115
+ });
116
+ });
117
+ });
118
+ return sharedValues;
119
+ }
69
120
  function getCodeLang(node, opts) {
70
121
  var _a, _b, _c, _d;
71
122
  let lang = "";
@@ -380,6 +431,10 @@ function rehypeDemo(opts) {
380
431
  });
381
432
  await Promise.all(deferrers).then((demos) => {
382
433
  vFile.data.demos = demos;
434
+ const sharedPreviewerProps = opts.useUtoopackDemoHMR ? hoistSharedPreviewerProps(replaceNodes) : [];
435
+ if (sharedPreviewerProps.length) {
436
+ vFile.data.demoSharedProps = sharedPreviewerProps;
437
+ }
383
438
  replaceNodes.forEach((node) => {
384
439
  const demoLoader = `() => import('${(0, import_plugin_utils.winPath)(
385
440
  opts.fileAbsPath
@@ -390,6 +445,9 @@ function rehypeDemo(opts) {
390
445
  new RegExp(`"${DEMO_LOADER_PLACEHOLDER}"`, "g"),
391
446
  demoLoader
392
447
  );
448
+ sharedPreviewerProps.forEach((_, index2) => {
449
+ value = value.split(`"${DEMO_SHARED_PROP_PLACEHOLDER_PREFIX}${index2}__"`).join(`__dumi_demo_shared_props__[${index2}]`);
450
+ });
393
451
  }
394
452
  if (node.JSXAttributes[0].type === "JSXAttribute") {
395
453
  node.JSXAttributes[0].value = value;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dumi",
3
- "version": "2.4.49-alpha.0",
3
+ "version": "2.4.49-alpha.2",
4
4
  "description": "📖 Documentation Generator of React Component",
5
5
  "keywords": [
6
6
  "generator",
@@ -69,9 +69,9 @@
69
69
  "@swc/core": "1.9.2",
70
70
  "@types/hast": "^2.3.5",
71
71
  "@types/mdast": "^3.0.12",
72
- "@umijs/bundler-utils": "^4.7.3",
73
- "@umijs/core": "^4.7.3",
74
- "@umijs/utils": "^4.7.3",
72
+ "@umijs/bundler-utils": "^4.7.4",
73
+ "@umijs/core": "^4.7.4",
74
+ "@umijs/utils": "^4.7.4",
75
75
  "animated-scroll-to": "^2.3.0",
76
76
  "classnames": "2.3.2",
77
77
  "codesandbox-import-utils": "^2.2.3",
@@ -123,7 +123,7 @@
123
123
  "sass": "^1.64.1",
124
124
  "sitemap": "^7.1.1",
125
125
  "sucrase": "^3.34.0",
126
- "umi": "^4.7.3",
126
+ "umi": "^4.7.4",
127
127
  "unified": "^10.1.2",
128
128
  "unist-util-visit": "^4.1.2",
129
129
  "unist-util-visit-parents": "^5.1.3",
@@ -147,8 +147,8 @@
147
147
  "@types/react-copy-to-clipboard": "^5.0.4",
148
148
  "@types/react-dom": "^18.2.7",
149
149
  "@typescript/native-preview": "7.0.0-dev.20260623.1",
150
- "@umijs/lint": "^4.7.3",
151
- "@umijs/plugins": "4.7.3",
150
+ "@umijs/lint": "^4.7.4",
151
+ "@umijs/plugins": "4.7.4",
152
152
  "eslint": "^8.46.0",
153
153
  "esno": "^4.7.0",
154
154
  "fast-glob": "^3.3.1",