dumi
Version:
📖 Documentation Generator of React Component
164 lines (159 loc) • 6.11 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/loaders/markdown/index.ts
var markdown_exports = {};
__export(markdown_exports, {
default: () => mdLoader
});
module.exports = __toCommonJS(markdown_exports);
var import_tabs = require("../../features/tabs");
var import_utils = require("../../utils");
var import_fs = __toESM(require("fs"));
var import_plugin_utils = require("umi/plugin-utils");
var import_transformer = __toESM(require("./transformer"));
var import_rehypeText = require("./transformer/rehypeText");
function getDemoSourceFiles(demos = []) {
return demos.reduce((ret, demo) => {
if ("sources" in demo) {
ret.push(...Object.values(demo.sources));
}
return ret;
}, []);
}
function emit(opts, ret) {
const { demos, embeds } = ret.meta;
embeds.forEach((file) => this.addDependency(file));
getDemoSourceFiles(demos).forEach((file) => this.addDependency(file));
if (opts.mode === "meta") {
const { frontmatter, toc, texts } = ret.meta;
if (demos && opts.onResolveDemos) {
opts.onResolveDemos(demos);
}
if (frontmatter.atomId && opts.onResolveAtomMeta) {
opts.onResolveAtomMeta(frontmatter.atomId, frontmatter);
}
return import_plugin_utils.Mustache.render(
`import React from 'react';
export const demos = {
{{#demos}}
'{{{id}}}': {
component: {{{component}}},
asset: {{{renderAsset}}}
},
{{/demos}}
};
export const frontmatter = {{{frontmatter}}};
export const toc = {{{toc}}};
export const texts = {{{texts}}};
`,
{
demos,
frontmatter: JSON.stringify(frontmatter),
toc: JSON.stringify(toc),
texts: JSON.stringify(texts),
renderAsset: function renderAsset() {
if (!("asset" in this))
return "null";
let { asset } = this;
const { sources } = this;
Object.keys(this.sources).forEach((file) => {
if (!asset.dependencies[file])
return;
asset = import_plugin_utils.lodash.cloneDeep(asset);
asset.dependencies[file].value = `{{{require('-!${sources[file]}?dumi-raw').default}}}`;
});
return JSON.stringify(asset, null, 2).replace(/"{{{|}}}"/g, "");
}
}
);
} else {
const isTabContent = (0, import_tabs.isTabRouteFile)(this.resourcePath);
return `${Object.values(opts.builtins).map((item) => `import ${item.specifier} from '${item.source}';`).join("\n")}
import React from 'react';
${isTabContent ? `import { useTabMeta } from 'dumi';` : `import { DumiPage, useRouteMeta } from 'dumi';`}
// export named function for fastRefresh
// ref: https://github.com/pmmmwh/react-refresh-webpack-plugin/blob/main/docs/TROUBLESHOOTING.md#edits-always-lead-to-full-reload
function DumiMarkdownContent() {
const { texts: ${import_rehypeText.CONTENT_TEXTS_OBJ_NAME} } = use${isTabContent ? "TabMeta" : "RouteMeta"}();
return ${isTabContent ? ret.content : `<DumiPage>${ret.content}</DumiPage>`};
}
export default DumiMarkdownContent;`;
}
}
function getDepsCacheKey(deps = []) {
return JSON.stringify(
deps.map(
(file) => `${file}:${(0, import_utils.getContentHash)(import_fs.default.readFileSync(file, "utf-8"))}`
)
);
}
var deferrer = {};
var depsMapping = {};
function mdLoader(content) {
const opts = this.getOptions();
const cb = this.async();
if (process.env.NODE_ENV === "production" && ["onResolveDemos", "onResolveAtomMeta"].some((k) => k in opts)) {
this.cacheable(false);
}
const cache = (0, import_utils.getCache)("md-loader");
const baseCacheKey = [
this.resourcePath,
(0, import_utils.getContentHash)(content),
JSON.stringify(import_plugin_utils.lodash.omit(opts, ["mode", "builtins", "onResolveDemos"]))
].join(":");
const cacheKey = [
baseCacheKey,
getDepsCacheKey(depsMapping[this.resourcePath])
].join(":");
const cacheRet = cache.getSync(cacheKey, "");
if (cacheRet) {
cb(null, emit.call(this, opts, cacheRet));
return;
} else if (cacheKey in deferrer) {
deferrer[cacheKey].then((res) => {
cb(null, emit.call(this, opts, res));
}).catch(cb);
return;
}
deferrer[cacheKey] = (0, import_transformer.default)(content, {
...import_plugin_utils.lodash.omit(opts, ["mode", "builtins", "onResolveDemos"]),
fileAbsPath: (0, import_plugin_utils.winPath)(this.resourcePath)
});
deferrer[cacheKey].then((ret) => {
depsMapping[this.resourcePath] = ret.meta.embeds.concat(
getDemoSourceFiles(ret.meta.demos)
);
const finalCacheKey = [
baseCacheKey,
getDepsCacheKey(depsMapping[this.resourcePath])
].join(":");
cache.setSync(finalCacheKey, ret);
cb(null, emit.call(this, opts, ret));
delete deferrer[cacheKey];
}).catch(cb);
}