From b350a882d64fd6c7c4c4e663e6beab88350334e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=A5=E5=B8=8C?= Date: Tue, 31 Mar 2020 03:14:44 +0800 Subject: [PATCH 1/6] fix: use webpack for package --- packages/code-generator/package.json | 9 +++++-- packages/code-generator/tsconfig.json | 15 ++++++++---- packages/code-generator/webpack.config.js | 29 +++++++++++++++++++++++ 3 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 packages/code-generator/webpack.config.js diff --git a/packages/code-generator/package.json b/packages/code-generator/package.json index b247ac1c4..6ca2cdd94 100644 --- a/packages/code-generator/package.json +++ b/packages/code-generator/package.json @@ -7,7 +7,8 @@ "lib" ], "scripts": { - "build": "rimraf lib && tsc", + "compile": "rimraf lib && tsc", + "build": "rimraf lib && webpack", "demo": "ts-node -r tsconfig-paths/register ./src/demo/main.ts", "test": "ava" }, @@ -19,8 +20,12 @@ "devDependencies": { "ava": "^1.0.1", "rimraf": "^3.0.2", + "ts-loader": "^6.2.2", "ts-node": "^7.0.1", - "tsconfig-paths": "^3.9.0" + "tsconfig-paths": "^3.9.0", + "tsconfig-paths-webpack-plugin": "^3.2.0", + "webpack": "^4.42.1", + "webpack-cli": "^3.3.11" }, "ava": { "compileEnhancements": false, diff --git a/packages/code-generator/tsconfig.json b/packages/code-generator/tsconfig.json index ddd9bd3f8..f04b1db45 100644 --- a/packages/code-generator/tsconfig.json +++ b/packages/code-generator/tsconfig.json @@ -1,15 +1,22 @@ { "extends": "../../tsconfig.json", "compilerOptions": { + "module": "es2015", + "noImplicitAny": true, + "sourceMap": true, + "moduleResolution": "node", + "isolatedModules": true, + "target": "es5", + "strictNullChecks": true, + "inlineSources": false, + "lib": ["es2015"], + "downlevelIteration": true, "paths": { "@/*": ["./src/*"] }, "outDir": "./lib", - "lib": [ - "es6" - ], "types": ["node"], - "baseUrl": ".", /* Base directory to resolve non-absolute module names. */ + "baseUrl": "." /* Base directory to resolve non-absolute module names. */ }, "include": [ "src/**/*" diff --git a/packages/code-generator/webpack.config.js b/packages/code-generator/webpack.config.js new file mode 100644 index 000000000..ea8081dbc --- /dev/null +++ b/packages/code-generator/webpack.config.js @@ -0,0 +1,29 @@ +const path = require('path'); +const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin'); + +module.exports = { + // entry: './src/index.ts', + target: 'node', + entry: { + index: './src/index.ts', + // demo: './src/demo/main.ts', + }, + module: { + rules: [ + { + test: /\.tsx?$/, + use: 'ts-loader', + exclude: /node_modules/, + }, + ], + }, + resolve: { + extensions: [ '.tsx', '.ts', '.js' ], + plugins: [new TsconfigPathsPlugin({/* options: see below */})], + }, + output: { + // filename: 'bundle.js', + filename: '[name].js', + path: path.resolve(__dirname, 'lib'), + }, +}; From 49ac9a3f089aea9ec5b9310a921d9b6f0c680bae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=A5=E5=B8=8C?= Date: Tue, 31 Mar 2020 04:54:31 +0800 Subject: [PATCH 2/6] feat: add prettier post processor --- packages/code-generator/package.json | 2 ++ .../src/generator/ModuleBuilder.ts | 17 ++++++++++++- .../src/generator/ProjectBuilder.ts | 24 +++++++++++++++++-- .../src/{parse => parser}/SchemaParser.ts | 0 .../code-generator/src/postprocessor/index.ts | 3 +++ .../src/postprocessor/prettier/index.ts | 22 +++++++++++++++++ .../code-generator/src/solutions/icejs.ts | 3 +++ packages/code-generator/src/types/core.ts | 2 ++ 8 files changed, 70 insertions(+), 3 deletions(-) rename packages/code-generator/src/{parse => parser}/SchemaParser.ts (100%) create mode 100644 packages/code-generator/src/postprocessor/index.ts create mode 100644 packages/code-generator/src/postprocessor/prettier/index.ts diff --git a/packages/code-generator/package.json b/packages/code-generator/package.json index 6ca2cdd94..aa4770f19 100644 --- a/packages/code-generator/package.json +++ b/packages/code-generator/package.json @@ -14,7 +14,9 @@ }, "dependencies": { "@ali/am-eslint-config": "*", + "@types/prettier": "^1.19.1", "change-case": "^3.1.0", + "prettier": "^2.0.2", "short-uuid": "^3.1.1" }, "devDependencies": { diff --git a/packages/code-generator/src/generator/ModuleBuilder.ts b/packages/code-generator/src/generator/ModuleBuilder.ts index d52913d35..da40d4df1 100644 --- a/packages/code-generator/src/generator/ModuleBuilder.ts +++ b/packages/code-generator/src/generator/ModuleBuilder.ts @@ -5,6 +5,7 @@ import { ICompiledModule, IModuleBuilder, IResultFile, + PostProcessor, } from '../types'; import { COMMON_SUB_MODULE_NAME } from '../const/generator'; @@ -17,9 +18,11 @@ import ResultFile from '../model/ResultFile'; export function createModuleBuilder( options: { plugins: BuilderComponentPlugin[]; + postProcessors: PostProcessor[]; mainFileName?: string; } = { plugins: [], + postProcessors: [], }, ): IModuleBuilder { const chunkGenerator = new ChunkBuilder(options.plugins); @@ -33,7 +36,7 @@ export function createModuleBuilder( ); } - const files: IResultFile[] = []; + let files: IResultFile[] = []; const { chunks } = await chunkGenerator.run(input); chunks.forEach(fileChunkList => { @@ -46,6 +49,18 @@ export function createModuleBuilder( files.push(file); }); + if (options.postProcessors.length > 0) { + files = files.map(file => { + let content = file.content; + const type = file.ext; + options.postProcessors.forEach(processer => { + content = processer(content, type); + }); + + return new ResultFile(content, type, file.name); + }); + } + return { files, }; diff --git a/packages/code-generator/src/generator/ProjectBuilder.ts b/packages/code-generator/src/generator/ProjectBuilder.ts index bc3513603..98f4dc0a4 100644 --- a/packages/code-generator/src/generator/ProjectBuilder.ts +++ b/packages/code-generator/src/generator/ProjectBuilder.ts @@ -8,10 +8,11 @@ import { IResultDir, IResultFile, ISchemaParser, + PostProcessor, } from '../types'; import ResultDir from '@/model/ResultDir'; -import SchemaParser from '@/parse/SchemaParser'; +import SchemaParser from '@/parser/SchemaParser'; import { createModuleBuilder } from '@/generator/ModuleBuilder'; @@ -40,16 +41,20 @@ function getDirFromRoot(root: IResultDir, path: string[]): IResultDir { export class ProjectBuilder implements IProjectBuilder { private template: IProjectTemplate; private plugins: IProjectPlugins; + private postProcessors: PostProcessor[]; constructor({ template, plugins, + postProcessors, }: { template: IProjectTemplate; plugins: IProjectPlugins; + postProcessors: PostProcessor[]; }) { this.template = template; this.plugins = plugins; + this.postProcessors = postProcessors; } public async generateProject(schema: IProjectSchema): Promise { @@ -212,45 +217,57 @@ export class ProjectBuilder implements IProjectBuilder { builders.components = createModuleBuilder({ plugins: this.plugins.components, + postProcessors: this.postProcessors, + }); + builders.pages = createModuleBuilder({ + plugins: this.plugins.pages, + postProcessors: this.postProcessors, }); - builders.pages = createModuleBuilder({ plugins: this.plugins.pages }); builders.router = createModuleBuilder({ plugins: this.plugins.router, mainFileName: this.template.slots.router.fileName, + postProcessors: this.postProcessors, }); builders.entry = createModuleBuilder({ plugins: this.plugins.entry, mainFileName: this.template.slots.entry.fileName, + postProcessors: this.postProcessors, }); builders.globalStyle = createModuleBuilder({ plugins: this.plugins.globalStyle, mainFileName: this.template.slots.globalStyle.fileName, + postProcessors: this.postProcessors, }); builders.htmlEntry = createModuleBuilder({ plugins: this.plugins.htmlEntry, mainFileName: this.template.slots.htmlEntry.fileName, + postProcessors: this.postProcessors, }); builders.packageJSON = createModuleBuilder({ plugins: this.plugins.packageJSON, mainFileName: this.template.slots.packageJSON.fileName, + postProcessors: this.postProcessors, }); if (this.template.slots.constants && this.plugins.constants) { builders.constants = createModuleBuilder({ plugins: this.plugins.constants, mainFileName: this.template.slots.constants.fileName, + postProcessors: this.postProcessors, }); } if (this.template.slots.utils && this.plugins.utils) { builders.utils = createModuleBuilder({ plugins: this.plugins.utils, mainFileName: this.template.slots.utils.fileName, + postProcessors: this.postProcessors, }); } if (this.template.slots.i18n && this.plugins.i18n) { builders.i18n = createModuleBuilder({ plugins: this.plugins.i18n, mainFileName: this.template.slots.i18n.fileName, + postProcessors: this.postProcessors, }); } @@ -261,12 +278,15 @@ export class ProjectBuilder implements IProjectBuilder { export function createProjectBuilder({ template, plugins, + postProcessors, }: { template: IProjectTemplate; plugins: IProjectPlugins; + postProcessors: PostProcessor[]; }): IProjectBuilder { return new ProjectBuilder({ template, plugins, + postProcessors, }); } diff --git a/packages/code-generator/src/parse/SchemaParser.ts b/packages/code-generator/src/parser/SchemaParser.ts similarity index 100% rename from packages/code-generator/src/parse/SchemaParser.ts rename to packages/code-generator/src/parser/SchemaParser.ts diff --git a/packages/code-generator/src/postprocessor/index.ts b/packages/code-generator/src/postprocessor/index.ts new file mode 100644 index 000000000..5b122f637 --- /dev/null +++ b/packages/code-generator/src/postprocessor/index.ts @@ -0,0 +1,3 @@ +import prettier from './prettier'; + +export { prettier }; diff --git a/packages/code-generator/src/postprocessor/prettier/index.ts b/packages/code-generator/src/postprocessor/prettier/index.ts new file mode 100644 index 000000000..694038aa0 --- /dev/null +++ b/packages/code-generator/src/postprocessor/prettier/index.ts @@ -0,0 +1,22 @@ +import prettier from 'prettier'; + +import { PostProcessor } from '@/types'; + +const PARSERS = ['css', 'scss', 'less', 'json', 'html', 'vue']; + +const codePrettier: PostProcessor = (content: string, fileType: string) => { + let parser: prettier.BuiltInParserName; + if (fileType === 'js' || fileType === 'jsx') { + parser = 'babel'; + } else if (PARSERS.indexOf(fileType) >= 0) { + parser = fileType as prettier.BuiltInParserName; + } else { + return content; + } + + return prettier.format(content, { + parser, + }); +}; + +export default codePrettier; diff --git a/packages/code-generator/src/solutions/icejs.ts b/packages/code-generator/src/solutions/icejs.ts index d778fabac..e8091e69b 100644 --- a/packages/code-generator/src/solutions/icejs.ts +++ b/packages/code-generator/src/solutions/icejs.ts @@ -21,6 +21,8 @@ import template from '@/plugins/project/framework/icejs/template'; import i18n from '@/plugins/project/i18n'; import utils from '@/plugins/project/utils'; +import { prettier } from '@/postprocessor'; + export default function createIceJsProjectBuilder(): IProjectBuilder { return createProjectBuilder({ template, @@ -56,5 +58,6 @@ export default function createIceJsProjectBuilder(): IProjectBuilder { htmlEntry: [iceJsEntryHtml], packageJSON: [iceJsPackageJSON], }, + postProcessors: [prettier], }); } diff --git a/packages/code-generator/src/types/core.ts b/packages/code-generator/src/types/core.ts index fbe5ac151..8fedf317b 100644 --- a/packages/code-generator/src/types/core.ts +++ b/packages/code-generator/src/types/core.ts @@ -141,3 +141,5 @@ export interface IProjectPlugins { export interface IProjectBuilder { generateProject(schema: IProjectSchema): Promise; } + +export type PostProcessor = (content: string, fileType: string) => string; From 2899149b9ca925e54a3e77fd4ed1e77b6ed182f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=A5=E5=B8=8C?= Date: Tue, 31 Mar 2020 21:27:13 +0800 Subject: [PATCH 3/6] fix: enhance compile config --- packages/code-generator/package.json | 3 ++- packages/code-generator/tsconfig.json | 7 +------ packages/code-generator/webpack.config.js | 4 +++- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/packages/code-generator/package.json b/packages/code-generator/package.json index aa4770f19..a3959d2e6 100644 --- a/packages/code-generator/package.json +++ b/packages/code-generator/package.json @@ -27,7 +27,8 @@ "tsconfig-paths": "^3.9.0", "tsconfig-paths-webpack-plugin": "^3.2.0", "webpack": "^4.42.1", - "webpack-cli": "^3.3.11" + "webpack-cli": "^3.3.11", + "webpack-node-externals": "^1.7.2" }, "ava": { "compileEnhancements": false, diff --git a/packages/code-generator/tsconfig.json b/packages/code-generator/tsconfig.json index f04b1db45..2660ef448 100644 --- a/packages/code-generator/tsconfig.json +++ b/packages/code-generator/tsconfig.json @@ -1,15 +1,10 @@ { "extends": "../../tsconfig.json", "compilerOptions": { - "module": "es2015", - "noImplicitAny": true, - "sourceMap": true, - "moduleResolution": "node", - "isolatedModules": true, "target": "es5", "strictNullChecks": true, "inlineSources": false, - "lib": ["es2015"], + "lib": ["es6"], "downlevelIteration": true, "paths": { "@/*": ["./src/*"] diff --git a/packages/code-generator/webpack.config.js b/packages/code-generator/webpack.config.js index ea8081dbc..b4c35274e 100644 --- a/packages/code-generator/webpack.config.js +++ b/packages/code-generator/webpack.config.js @@ -1,4 +1,5 @@ const path = require('path'); +const nodeExternals = require('webpack-node-externals'); const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin'); module.exports = { @@ -6,7 +7,7 @@ module.exports = { target: 'node', entry: { index: './src/index.ts', - // demo: './src/demo/main.ts', + demo: './src/demo/main.ts', }, module: { rules: [ @@ -26,4 +27,5 @@ module.exports = { filename: '[name].js', path: path.resolve(__dirname, 'lib'), }, + externals: [nodeExternals()], // in order to ignore all modules in node_modules folder }; From 4a53faac85530fd4637ed636e7f382f4b8fcb7d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=A5=E5=B8=8C?= Date: Tue, 31 Mar 2020 21:28:06 +0800 Subject: [PATCH 4/6] feat: export publisher --- packages/code-generator/src/index.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/code-generator/src/index.ts b/packages/code-generator/src/index.ts index 12217cc69..f6d467bc7 100644 --- a/packages/code-generator/src/index.ts +++ b/packages/code-generator/src/index.ts @@ -3,6 +3,7 @@ * */ import { createProjectBuilder } from '@/generator/ProjectBuilder'; +import { createDiskPublisher } from '@/publisher/disk'; import createIceJsProjectBuilder from '@/solutions/icejs'; export * from './types'; @@ -12,4 +13,7 @@ export default { solutions: { icejs: createIceJsProjectBuilder, }, + publishers: { + disk: createDiskPublisher, + }, }; From 389eaf7104f4e8fc340d5a927328b354fb319643 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=A5=E5=B8=8C?= Date: Tue, 31 Mar 2020 21:45:47 +0800 Subject: [PATCH 5/6] fix: post process file error --- packages/code-generator/src/generator/ModuleBuilder.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/code-generator/src/generator/ModuleBuilder.ts b/packages/code-generator/src/generator/ModuleBuilder.ts index da40d4df1..8115f0def 100644 --- a/packages/code-generator/src/generator/ModuleBuilder.ts +++ b/packages/code-generator/src/generator/ModuleBuilder.ts @@ -57,7 +57,7 @@ export function createModuleBuilder( content = processer(content, type); }); - return new ResultFile(content, type, file.name); + return new ResultFile(file.name, type, content); }); } From 55630d6347c51b07a47d48c63c94ab91aa9694f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=A5=E5=B8=8C?= Date: Tue, 31 Mar 2020 21:45:57 +0800 Subject: [PATCH 6/6] fix: rm demo in lib --- packages/code-generator/webpack.config.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/code-generator/webpack.config.js b/packages/code-generator/webpack.config.js index b4c35274e..00f21d2e1 100644 --- a/packages/code-generator/webpack.config.js +++ b/packages/code-generator/webpack.config.js @@ -3,11 +3,11 @@ const nodeExternals = require('webpack-node-externals'); const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin'); module.exports = { - // entry: './src/index.ts', + mode: 'production', target: 'node', entry: { index: './src/index.ts', - demo: './src/demo/main.ts', + // demo: './src/demo/main.ts', }, module: { rules: [