const fs = require('fs'); const Json5 =require('json5'); /** * 特殊处理, 用于解决 @maxgraph/core 不能正常使用的问题, 会出现以下错误: * ERROR in ./node_modules/.pnpm/@maxgraph+core@0.12.0/node_modules/@maxgraph/core/dist/index.js 174:0-24 * Module not found: Error: Can't resolve './types' in 'io.sc.platform.core.frontend/node_modules/.pnpm/@maxgraph+core@0.12.0/node_modules/@maxgraph/core/dist' * Did you mean 'types.js'? * BREAKING CHANGE: The request './types' failed to resolve only because it was resolved as fully specified * (probably because the origin is strict EcmaScript Module, e. g. a module with javascript mimetype, a '*.mjs' file, or a '*.js' file where the package.json contains '"type": "module"'). * The extension in the request is mandatory for it to be fully specified. * Add the extension to the request. * * 解决方案: 删除 node_modules/@maxgraph/core/package.json 文件中 [ "type" : "module" ] 属性 */ const packageJson =JSON.parse(fs.readFileSync('./node_modules/@maxgraph/core/package.json').toString()); delete packageJson['type']; fs.writeFileSync('./node_modules/@maxgraph/core/package.json', JSON.stringify(packageJson, null, ' ')); /** * 用于自动生成前端组件 * 通过 src/routes/routes.json 文件构建 src/components/index.ts 文件 */ // 解析前端路由配置文件 const routesJson = Json5.parse(fs.readFileSync('./src/routes/routes.json', 'utf8')); let content =''; content +='/**\n'; content +=' * 此文件为自动生成文件,请勿修改\n'; content +=' */\n\n'; for(const route of routesJson){ generateImportComonents(route); } content +='\n'; content +='const localComponents = { \n'; for(const route of routesJson){ generateComonents(route); } content +='}\n\n'; content +='export default localComponents;\n'; fs.writeFileSync('./src/components/index.ts', content); console.info('components generated!'); function generateImportComonents(route){ const componentName =route.component.replaceAll('.','_'); const componentPath =route.componentPath; content +=`import ${componentName} from '${componentPath}';\n`; if(route.children && route.children.length){ for(const child of route.children){ generateImportComonents(child); } } } function generateComonents(route){ const componentName =route.component.replaceAll('.','_'); content +=`'${route.component}': ${componentName},\n`; if(route.children && route.children.length){ for(const child of route.children){ generateComonents(child); } } }