UNPKG

@ali-i18n-fe/dada-component

Version:
123 lines (102 loc) 3.4 kB
const { getPackageConfig } = require("./utils"); const { getDefaultPublicPath } = require("./utils/def"); const { spendTime } = require("./webpack/utils/log"); const { isDef } = require("./utils/def"); const fs = require("fs"); const path = require("path"); module.exports = { currentPath: process.cwd(), manifestPath: "", outputPath: "", routeMap: "", async load() { await spendTime("生成Manifest", async () => { this.outputPath = path.resolve(this.currentPath, "dist"); this.manifestPath = path.resolve(this.outputPath, "manifest.json"); this.publicPath = getDefaultPublicPath(); this.manifestConfig = {}; this.buildMetaInfo(); this.buildComponentInfo(); this.write(); })(); }, buildMetaInfo() { const packageConfig = getPackageConfig(); const { name, version, title, description, homepage, screenshot, repository = {}, componentConfig = {}, } = packageConfig; this.manifestConfig.meta = { npm: name, version: version, name: name, title: title || name, homepage, screenshot, description, devMode: 2, // 开发模式:1-LowCode,2-ProCode jsLibrary: 1, // JavaScript 库:1-react, 2-vue,3-rax platform: 3, // 运行平台(多选):1-pc,2-mobile,4-miniapp(3=1+2=pc+mobile, 7=1+2+3=pc+mobile+miniapp) repository: repository.url, ...componentConfig, }; }, buildComponentInfo() { let routeMap = require(path.resolve(this.outputPath, "routeMap.json")); routeMap = Object.entries(routeMap).reduce( (previousValue, [key, value]) => Object.assign(previousValue, { [value]: key }), {} ); const typeMap = require(path.resolve(this.outputPath, "typeFile.json")); this.manifestConfig.components = Object.entries(typeMap).map( ([name, { description, tags = {} }]) => { const { author, category, "vision.title": title } = tags; const routePath = routeMap[name]; const previewFile = path.join(routePath, "..", "preview.png"); const homepage = path.join("stories", routePath, "..", "index.html"); const existPublicPath = (relativePath) => fs.existsSync(path.resolve(this.outputPath, previewFile)) && `${this.publicPath}${relativePath}`; return { name, title, description, category, homepage: existPublicPath(homepage), owners: author, screenshot: existPublicPath(previewFile), logo: "https://g.alicdn.com/fusion-site/next/0.3.3/assets/img/tool-cool-logo.png", extends: JSON.stringify(tags), }; } ); }, write() { fs.writeFileSync( this.manifestPath, JSON.stringify(this.manifestConfig, null, 2), "utf8" ); if (isDef() && false) { fs.copyFileSync( this.manifestPath, path.resolve(this.currentPath, "manifest.json") ); const { getPackageConfig } = require("./utils/getPackageJson"); const pkgJSON = getPackageConfig(); const pkgPath = path.resolve(process.cwd(), "./package.json"); if (pkgJSON.files) { pkgJSON.files = [...pkgJSON.files, "manifest.json"]; } fs.writeFileSync(pkgPath, JSON.stringify(pkgJSON, null, 2), "utf8"); } }, };