dumi
Version:
📖 Documentation Generator of React Component
168 lines (166 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/assetParsers/atom.ts
var atom_exports = {};
__export(atom_exports, {
default: () => atom_default
});
module.exports = __toCommonJS(atom_exports);
var import_utils = require("../utils");
var import_parser = require("dumi-afx-deps/compiled/parser");
var import_path = __toESM(require("path"));
var import_plugin_utils = require("umi/plugin-utils");
var MAX_PARSE_SIZE = 1024 * 512;
var AtomAssetsParser = class {
constructor(opts) {
this.unresolvedFiles = [];
this.isParsing = false;
this.watcher = null;
this.cbs = [];
const absEntryFile = import_path.default.resolve(opts.resolveDir, opts.entryFile);
this.resolveDir = opts.resolveDir;
this.resolveFilter = opts.resolveFilter || (() => true);
this.entryDir = import_path.default.relative(opts.resolveDir, import_path.default.dirname(absEntryFile));
this.parser = new import_parser.SchemaParser({
entryPath: absEntryFile,
basePath: (0, import_utils.getProjectRoot)(opts.resolveDir),
unPkgHost: opts.unpkgHost ?? "https://unpkg.com",
mode: "worker",
// @ts-ignore
parseOptions: opts.parseOptions
});
this.watchArgs = {
paths: this.entryDir,
options: {
cwd: this.resolveDir,
ignored: [
"**/.*",
"**/.*/**",
"**/_*",
"**/_*/**",
"**/*.{md,less,scss,sass,styl,css}"
],
ignoreInitial: true
}
};
}
async parse() {
if (!this.parseDeferrer || this.unresolvedFiles.length && !this.isParsing) {
this.isParsing = true;
this.parseDeferrer = (async () => {
await this.parser.patch(this.unresolvedFiles.splice(0));
const resolver = new import_parser.SchemaResolver(await this.parser.parse(), {
mode: "worker"
});
const result = {
components: {},
functions: {}
};
const fallbackProps = { type: "object", properties: {} };
const fallbackSignature = { arguments: [] };
const componentList = await resolver.componentList;
const functionList = await resolver.functionList;
for (const id of componentList) {
const needResolve = this.resolveFilter({
id,
type: "COMPONENT",
ids: componentList
});
let propsConfig = needResolve ? (await resolver.getComponent(id)).props : fallbackProps;
const size = Buffer.byteLength(JSON.stringify(propsConfig));
if (size > MAX_PARSE_SIZE) {
propsConfig = fallbackProps;
import_plugin_utils.logger.warn(
`Parsed component ${id} props size ${size} exceeds 512KB, skip it.`
);
}
result.components[id] = {
type: "COMPONENT",
id,
title: id,
propsConfig
};
}
for (const id of functionList) {
const needResolve = this.resolveFilter({
id,
type: "FUNCTION",
ids: functionList
});
let signature = needResolve ? (await resolver.getFunction(id)).signature : fallbackSignature;
const size = Buffer.byteLength(JSON.stringify(signature));
if (size > MAX_PARSE_SIZE) {
signature = fallbackSignature;
import_plugin_utils.logger.warn(
`Parsed function ${id} signature size ${size} exceeds 512KB, skip it.`
);
}
result.functions[id] = {
type: "FUNCTION",
id,
title: id,
signature
};
}
resolver.$$destroyWorker();
this.isParsing = false;
return result;
})();
}
return this.parseDeferrer;
}
watch(cb) {
this.cbs.push(cb);
if (!this.watcher) {
const lazyParse = import_plugin_utils.lodash.debounce(() => {
this.parse().then((data) => this.cbs.forEach((cb2) => cb2(data)));
}, 100);
this.watcher = import_plugin_utils.chokidar.watch(this.watchArgs.paths, this.watchArgs.options).on("all", (ev, file) => {
if (["add", "change"].includes(ev) && /((?<!\.d)\.ts|\.(jsx?|tsx))$/.test(file)) {
this.unresolvedFiles.push(import_path.default.join(this.watchArgs.options.cwd, file));
lazyParse();
}
});
lazyParse();
}
}
unwatch(cb) {
this.cbs.splice(this.cbs.indexOf(cb), 1);
}
patchWatchArgs(handler) {
this.watchArgs = handler(this.watchArgs);
}
destroyWorker() {
if (this.parseDeferrer) {
this.parseDeferrer.finally(() => this.parser.$$destroyWorker());
} else {
this.parser.$$destroyWorker();
}
}
};
var atom_default = AtomAssetsParser;