Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generatecode support import element style #770

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/vue-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
},
"peerDependencies": {
"@babel/parser": "^7.18.13",
"@babel/generator": "^7.18.13",
"@babel/traverse": "^7.18.13"
}
}
8 changes: 7 additions & 1 deletion packages/vue-generator/src/generator/generateApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
genUtilsPlugin,
formatCodePlugin,
parseSchemaPlugin,
genGlobalState
genGlobalState,
appendElePlusStylePlugin
} from '../plugins'
import CodeGenerator from './codeGenerator'

Expand Down Expand Up @@ -63,6 +64,11 @@ export function generateApp(config = {}) {
globalState: globalState || defaultPlugins.globalState
}

// 默认支持 element-plus 注入样式
if (config?.customContext?.injectElementPlusStyle !== false) {
transformEnd.push(appendElePlusStylePlugin(config?.customContext?.injectElementPlusStyle || {}))
}
chilingling marked this conversation as resolved.
Show resolved Hide resolved

const codeGenInstance = new CodeGenerator({
plugins: {
transformStart: [parseSchema || defaultPlugins.parseSchema, ...transformStart],
Expand Down
79 changes: 79 additions & 0 deletions packages/vue-generator/src/plugins/appendElePlusStylePlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import prettier from 'prettier'
import { parse } from '@babel/parser'
import traverse from '@babel/traverse'
import generate from '@babel/generator'
import parserBabel from 'prettier/parser-babel'
import { mergeOptions } from '../utils/mergeOptions'

const defaultOption = {
fileName: 'package.json',
path: '.',
prettierOption: {
singleQuote: true,
printWidth: 120,
semi: false,
trailingComma: 'none'
}
}

function genElementPlusStyleDeps(options = {}) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议提交一个测试用例

const realOptions = mergeOptions(defaultOption, options)

const { prettierOption, fileName, path } = realOptions

return {
name: 'tinyEngine-generateCode-plugin-element-plus-style',
description: 'import element-plus style',
/**
* 注入 element-plus 全局样式依赖
* @param {tinyEngineDslVue.IAppSchema} schema
* @returns
*/
run() {
const originPackageItem = this.getFile(path, fileName)

if (!originPackageItem) {
return
}

let originPackageJSON = JSON.parse(originPackageItem.fileContent)
const hasElementPlusDeps = Object.keys(originPackageJSON.dependencies).includes('element-plus')

if (!hasElementPlusDeps) {
return
}

const mainJsFile = this.getFile('./src', 'main.js') || {}

if (!mainJsFile.fileContent) {
return
}

const ast = parse(mainJsFile.fileContent, { sourceType: 'module' })
let lastImport = null

traverse(ast, {
ImportDeclaration(path) {
lastImport = path
}
})

// 引入 element-plus 样式依赖
if (lastImport) {
lastImport.insertAfter(parse("import 'element-plus/dist/index.css'", { sourceType: 'module' }).program.body[0])
}

const newFileContent = generate(ast).code

const formattedContent = prettier.format(newFileContent, {
parser: 'babel',
plugins: [parserBabel],
...prettierOption
})

this.replaceFile({ ...mainJsFile, fileContent: formattedContent })
}
}
}

export default genElementPlusStyleDeps
1 change: 1 addition & 0 deletions packages/vue-generator/src/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export { default as genTemplatePlugin } from './genTemplatePlugin'
export { default as formatCodePlugin } from './formatCodePlugin'
export { default as genGlobalState } from './genGlobalState'
export { default as parseSchemaPlugin } from './parseSchemaPlugin'
export { default as appendElePlusStylePlugin } from './appendElePlusStylePlugin'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是否只能适用于element plus呢,有没有可能做成通用的?