dumi 2.4.37 → 2.4.38-beta.1

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.
@@ -40,6 +40,8 @@ var import_utils = require("@umijs/utils");
40
40
  var import_path = __toESM(require("path"));
41
41
  var import_utils2 = require("./utils");
42
42
  var mdLoaderPath = require.resolve("../../loaders/markdown");
43
+ var utilsRegisterPath = require.resolve("@umijs/utils");
44
+ var esbuildImplementorPath = require.resolve("@umijs/bundler-utils/compiled/esbuild");
43
45
  var UTOOPACK_LOADER_CTX_KEY = "__dumiLoaderContextPath";
44
46
  var LOADER_CTX_FILENAME = "dumi-loader-ctx.cjs";
45
47
  function toSerializable(value) {
@@ -115,6 +117,33 @@ function toRequireRef(found) {
115
117
  const modRef = `require(${JSON.stringify(found.modulePath)})`;
116
118
  return found.exportName === "module.exports" ? modRef : `(${modRef})[${JSON.stringify(found.exportName)}]`;
117
119
  }
120
+ function toTechStackRefs(techStacks) {
121
+ const refs = [];
122
+ for (const ts of techStacks) {
123
+ const ctor = ts.constructor;
124
+ let ref;
125
+ if (ctor !== Object) {
126
+ const found = findInRequireCache(ctor);
127
+ if (found) {
128
+ ref = `new (${toRequireRef(found)})()`;
129
+ }
130
+ } else {
131
+ const found = findInRequireCache(ts);
132
+ if (found) {
133
+ ref = toRequireRef(found);
134
+ }
135
+ }
136
+ if (ref) {
137
+ refs.push(ref);
138
+ } else {
139
+ const name = ts.constructor.name ? ` (${ts.constructor.name})` : "";
140
+ console.warn(
141
+ `[dumi] Utoopack markdown loader cannot serialize tech stack "${ts.name}"${name}. Please export the tech stack class from a module.`
142
+ );
143
+ }
144
+ }
145
+ return refs;
146
+ }
118
147
  function toPluginTargetRef(target, sourceFiles) {
119
148
  if (typeof target === "string")
120
149
  return JSON.stringify(target);
@@ -141,25 +170,17 @@ function toPluginRefs(plugins = [], sourceFiles = []) {
141
170
  }).join(", ")}]`;
142
171
  }
143
172
  function buildLoaderContextContent(techStacks, builtins = {}, routes = {}, extraRemarkPlugins = [], extraRehypePlugins = [], sourceFiles = []) {
144
- const refs = [];
145
- for (const ts of techStacks) {
146
- const ctor = ts.constructor;
147
- if (ctor !== Object) {
148
- const found = findInRequireCache(ctor);
149
- if (found) {
150
- refs.push(`new (${toRequireRef(found)})()`);
151
- }
152
- } else {
153
- const found = findInRequireCache(ts);
154
- if (found) {
155
- refs.push(toRequireRef(found));
156
- }
157
- }
158
- }
173
+ const refs = toTechStackRefs(techStacks);
159
174
  return `'use strict';
160
175
  try {
161
- require('@umijs/utils').register.register({ implementor: require('@umijs/bundler-utils/compiled/esbuild') });
162
- } catch (_) {}
176
+ require(${JSON.stringify(
177
+ utilsRegisterPath
178
+ )}).register.register({ implementor: require(${JSON.stringify(
179
+ esbuildImplementorPath
180
+ )}) });
181
+ } catch (e) {
182
+ console.warn('[dumi] failed to register TS require hook for utoopack loader context:', e);
183
+ }
163
184
  exports.techStacks = [${refs.join(", ")}];
164
185
  exports.builtins = ${JSON.stringify(builtins)};
165
186
  exports.routes = ${JSON.stringify(routes)};
@@ -23,5 +23,9 @@ interface IMdLoaderTextModeOptions extends Omit<IMdLoaderDefaultModeOptions, 'bu
23
23
  mode: 'text';
24
24
  }
25
25
  export type IMdLoaderOptions = IMdLoaderDefaultModeOptions | IMdLoaderDemosModeOptions | IMdLoaderDemoModeOptions | IMdLoaderFrontmatterModeOptions | IMdLoaderTextModeOptions | IMdLoaderDemoIndexModeOptions;
26
+ type MdLoaderCache = {
27
+ getSync: (key: string, defaultValue: any) => any;
28
+ };
29
+ export declare function getMdLoaderCacheSync<T>(cache: MdLoaderCache, key: string, defaultValue: T): T;
26
30
  export default function mdLoader(this: any, content: string): void;
27
31
  export {};
@@ -29,7 +29,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
29
29
  // src/loaders/markdown/index.ts
30
30
  var markdown_exports = {};
31
31
  __export(markdown_exports, {
32
- default: () => mdLoader
32
+ default: () => mdLoader,
33
+ getMdLoaderCacheSync: () => getMdLoaderCacheSync
33
34
  });
34
35
  module.exports = __toCommonJS(markdown_exports);
35
36
  var import_utoopackLoaders = require("../../features/compile/utoopackLoaders");
@@ -40,6 +41,20 @@ var import_path = __toESM(require("path"));
40
41
  var import_plugin_utils = require("umi/plugin-utils");
41
42
  var import_transformer = __toESM(require("./transformer"));
42
43
  var import_rehypeText = require("./transformer/rehypeText");
44
+ function isMalformedCacheError(err) {
45
+ return err instanceof SyntaxError || err instanceof Error && /Unexpected end of JSON input|Unterminated string in JSON/.test(
46
+ err.message
47
+ );
48
+ }
49
+ function getMdLoaderCacheSync(cache, key, defaultValue) {
50
+ try {
51
+ return cache.getSync(key, defaultValue);
52
+ } catch (err) {
53
+ if (isMalformedCacheError(err))
54
+ return defaultValue;
55
+ throw err;
56
+ }
57
+ }
43
58
  function getDemoSourceFiles(demos = []) {
44
59
  return demos.reduce((ret, demo) => {
45
60
  if ("resolveMap" in demo) {
@@ -372,7 +387,7 @@ function mdLoader(content) {
372
387
  baseCacheKey,
373
388
  getDepsCacheKey(depsMapping[this.resourcePath])
374
389
  ].join(":");
375
- const cacheRet = cache.getSync(cacheKey, "");
390
+ const cacheRet = getMdLoaderCacheSync(cache, cacheKey, "");
376
391
  if (cacheRet) {
377
392
  cb(null, emit.call(this, opts, cacheRet));
378
393
  return;
@@ -400,3 +415,7 @@ function mdLoader(content) {
400
415
  delete deferrer[cacheKey];
401
416
  }).catch(cb);
402
417
  }
418
+ // Annotate the CommonJS export names for ESM import in node:
419
+ 0 && (module.exports = {
420
+ getMdLoaderCacheSync
421
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dumi",
3
- "version": "2.4.37",
3
+ "version": "2.4.38-beta.1",
4
4
  "description": "📖 Documentation Generator of React Component",
5
5
  "keywords": [
6
6
  "generator",