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

Conversation

chilingling
Copy link
Member

@chilingling chilingling commented Aug 28, 2024

English | 简体中文

PR

PR Checklist

Please check if your PR fulfills the following requirements:

  • The commit message follows our Commit Message Guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)
  • Built its own designer, fully self-validated

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

Background and solution

What is the current behavior?

出码包含 element-plus 时,没有引入样式。

Issue Number: close #762

What is the new behavior?

增加出码插件,检测到 package.json 中含有 element-plus 插件的时候,往 main.js 中增加 import 语句:
import 'element-plus/dist/index.css'

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Summary by CodeRabbit

  • New Features

    • Introduced a new plugin for including Element Plus styles in Vue.js applications, enhancing customization options for styling.
    • Added a new peer dependency to support improved functionality within the vue-generator package.
  • Bug Fixes

    • Improved handling of scenarios where required files may be missing, ensuring the application exits gracefully without errors.
  • Documentation

    • Updated exports to include the new styling plugin, making it readily available for use in other parts of the application.

Copy link
Contributor

coderabbitai bot commented Aug 28, 2024

Walkthrough

The changes include the addition of a new peer dependency, @babel/generator, to the vue-generator package, enhancing its functionality. A new plugin, appendElePlusStylePlugin, is introduced to facilitate the injection of Element Plus styles into Vue.js applications. This plugin modifies the main.js file by adding necessary import statements for the styles based on user configuration. Additionally, the plugin is exported for use in other parts of the application.

Changes

File Change Summary
packages/vue-generator/package.json Added @babel/generator as a peer dependency with version ^7.18.13.
packages/vue-generator/src/generator/generateApp.js Enhanced generateApp function with new logic to conditionally use appendElePlusStylePlugin.
packages/vue-generator/src/plugins/appendElePlusStylePlugin.js Introduced new plugin for injecting Element Plus styles into main.js, implementing AST modification.
packages/vue-generator/src/plugins/index.js Exported appendElePlusStylePlugin for broader accessibility within the module.

Assessment against linked issues

Objective Addressed Explanation
Ensure Element Plus styles are automatically included when using components (#[762])
Provide a user-friendly way to configure CSS in the engine output (#[762])

Poem

🐰 In the garden of code, new blooms arise,
With styles from Element, the app now flies.
A plugin to weave, like threads in a seam,
Transforming our Vue into a stylish dream.
Hopping with joy, we dance in delight,
For changes like these make our coding bright! ✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 76c23b7 and 96a3d2a.

Files selected for processing (4)
  • packages/vue-generator/package.json (1 hunks)
  • packages/vue-generator/src/generator/generateApp.js (2 hunks)
  • packages/vue-generator/src/plugins/appendElePlusStylePlugin.js (1 hunks)
  • packages/vue-generator/src/plugins/index.js (1 hunks)
Additional comments not posted (3)
packages/vue-generator/src/plugins/index.js (1)

12-12: Export of appendElePlusStylePlugin looks good.

The new export statement is correctly added and follows the existing pattern of module exports in this file.

packages/vue-generator/package.json (1)

56-56: Addition of @babel/generator to peer dependencies is appropriate.

The specified version aligns with the existing Babel dependencies, ensuring compatibility. It is crucial to verify that all peer dependencies are compatible with each other to avoid potential conflicts.

Verification successful

Addition of @babel/generator to peer dependencies is compatible.

The Babel-related dependencies in packages/vue-generator/package.json are all aligned to version 7.18.13, indicating compatibility. The addition of @babel/generator as a peer dependency is appropriate.

  • @babel/parser: ^7.18.13
  • @babel/generator: ^7.18.13
  • @babel/traverse: ^7.18.13
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify compatibility of peer dependencies.

# Test: Check for known issues with Babel dependencies. Expect: No conflicts reported.
npm info "@babel/generator@^7.18.13" peerDependencies

Length of output: 126


Script:

#!/bin/bash
# Description: List all Babel-related dependencies in package.json for manual verification.

# Extract Babel-related dependencies from package.json
jq '.dependencies + .devDependencies + .peerDependencies | to_entries[] | select(.key | test("^@babel/"))' packages/vue-generator/package.json

Length of output: 305

packages/vue-generator/src/generator/generateApp.js (1)

12-13: Review import of new plugin appendElePlusStylePlugin.

The import of appendElePlusStylePlugin is correctly placed among other plugin imports, maintaining a clean and organized structure. This ensures that the new plugin is available for use within the generateApp function.

The import statement is correctly implemented.

Comment on lines 19 to 75
function genElementPlusStyleDeps(options = {}) {
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 'elemetn-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 })
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Overall structure and logic of genElementPlusStyleDeps function are sound.

The function is well-structured and logically sound for injecting Element Plus styles based on the presence of the dependency. Ensure thorough testing to confirm the functionality works as expected in various scenarios.

Would you like assistance in setting up or executing the tests?

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 96a3d2a and ce8dfe7.

Files selected for processing (1)
  • packages/vue-generator/src/plugins/appendElePlusStylePlugin.js (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/vue-generator/src/plugins/appendElePlusStylePlugin.js

}
}

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.

建议提交一个测试用例

@@ -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呢,有没有可能做成通用的?

@chilingling chilingling changed the base branch from develop to refactor/develop September 25, 2024 09:32
@github-actions github-actions bot added enhancement New feature or request refactor-main refactor/develop branch feature labels Sep 25, 2024
@chilingling chilingling changed the base branch from refactor/develop to develop September 25, 2024 09:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request refactor-main refactor/develop branch feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 [Bug]: 使用element-plus组件出码后,样式没有引入问题
2 participants