UNPKG

@ali-i18n-fe/dada-component

Version:
44 lines (36 loc) 1.21 kB
const path = require("path"); const fs = require("fs"); const parser = require("@ali-i18n-fe/react-docgen-typescript-loader-add-tag/dist/docgen-typescript"); const { getDefaultTSConfigFile } = require("@ali-i18n-fe/react-docgen-typescript-loader-add-tag/dist/loader"); exports.buildTypeFile = function(webpackConfig) { const basePath = path.dirname(webpackConfig.entry.index); const { fileNames } = getDefaultTSConfigFile(basePath); const componentDoc = parser .withCompilerOptions({ jsx: 2, // react module: 1, // commonjs target: 99 }) .parse(fileNames); const { path: output } = webpackConfig.output; const routeMap = webpackConfig.routeMap || require(path.resolve(output, "routeMap.json")); const componentNameSet = new Set(Object.values(routeMap)); const componentMap = componentDoc .filter(({ displayName }) => componentNameSet.has(displayName)) .reduce( (prev, current) => Object.assign(prev, { [current.displayName]: current }), {} ); fs.writeFileSync( path.resolve(webpackConfig.output.path, "typeFile.json"), JSON.stringify(componentMap), { encoding: "utf8" } ); return componentDoc; };