Skip to content

feat: external all dependencies of core package #1236

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

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

chilingling
Copy link
Member

@chilingling chilingling commented Mar 20, 2025

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?

Issue Number: N/A

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Summary by CodeRabbit

  • New Features

    • Improved handling of script injection for canvas initialization, with conditional support for inline scripts and character escaping in different build modes.
  • Refactor

    • Updated how script content is exported in the build process for better compatibility and clarity.
  • Chores

    • Expanded and updated external dependency lists across multiple build configurations to optimize bundling and prevent unnecessary modules from being included in library outputs.

Copy link
Contributor

coderabbitai bot commented Mar 20, 2025

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

This update primarily expands the list of external dependencies across multiple Vite configuration files in various packages. The changes ensure that a broader set of modules—such as those under @vue, @babel, @vueuse, and several specific libraries—are marked as external, preventing them from being bundled into the output artifacts. Additionally, the initCanvas function in the canvas package was modified to handle script injection differently based on the environment, and the build plugin was updated to simplify how bundled code is exported. These adjustments refine the build process and module resolution throughout the project.

Changes

File(s) Change Summary
packages/canvas/init-canvas/init-canvas.ts Modified initCanvas to conditionally inject scripts based on environment and escape $ in inline scripts.
packages/canvas/scripts/vite-plugin-separate-build.ts Added explicit type to cleanUrl parameter; changed export logic to use JSON-stringified code instead of base64 data URI.
packages/canvas/vite.config.ts
packages/common/vite.config.ts
packages/design-core/vite.config.js
packages/layout/vite.config.ts
packages/plugins/block/vite.config.ts
packages/plugins/datasource/vite.config.js
packages/plugins/i18n/vite.config.ts
packages/plugins/schema/vite.config.ts
packages/plugins/tutorial/vite.config.ts
packages/register/vite.config.ts
packages/settings/props/vite.config.ts
packages/settings/styles/vite.config.ts
packages/toolbars/lang/vite.config.ts
packages/toolbars/view-setting/vite.config.ts
packages/utils/vite.config.ts
packages/vue-generator/vite.config.js
packages/webcomponent/vite.config.js
Expanded the external array in Rollup/Vite config to include more packages and regex patterns, such as @vue, @babel, @vueuse, and various specific libraries, to prevent them from being bundled.

Sequence Diagram(s)

sequenceDiagram
    participant App
    participant initCanvas
    participant Env
    participant Script

    App->>initCanvas: Call with importMap, importStyleUrls
    initCanvas->>Env: Check development mode
    alt Development mode
        initCanvas->>Script: Inject script tag with src attribute
    else Non-development mode
        initCanvas->>Script: Inject inline script with $ replaced by $$
    end
    initCanvas-->>App: Return constructed HTML string
Loading

Poem

A rabbit hops with nimble feet,
Through configs vast, externals neat.
Scripts now slip in, sly or bold,
With dollars doubled, stories told.
No bundle bloat, just lighter fare—
The garden grows with thoughtful care!
🥕


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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 generate docstrings to generate docstrings for this 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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.

@github-actions github-actions bot added the enhancement New feature or request label Mar 20, 2025
@chilingling chilingling force-pushed the feat/externalAllDepsFromCore branch from 3624474 to da90453 Compare April 10, 2025 02:10
@chilingling chilingling force-pushed the feat/externalAllDepsFromCore branch from da90453 to d986aa0 Compare April 21, 2025 08:53
@chilingling chilingling marked this pull request as ready for review April 21, 2025 09:08
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: 1

🧹 Nitpick comments (1)
packages/layout/vite.config.ts (1)

35-35: Consider consistent externalization pattern across packages

While this change is good, I notice that different packages are externalizing different dependencies. Consider standardizing a common set of core dependencies that should be externalized across all packages to ensure consistency.

For example, if '@vueuse/core' is being externalized here, should it also be externalized in other packages where it might be used?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9d2dc98 and d986aa0.

📒 Files selected for processing (19)
  • packages/canvas/init-canvas/init-canvas.ts (1 hunks)
  • packages/canvas/scripts/vite-plugin-separate-build.ts (2 hunks)
  • packages/canvas/vite.config.ts (1 hunks)
  • packages/common/vite.config.ts (1 hunks)
  • packages/design-core/vite.config.js (1 hunks)
  • packages/layout/vite.config.ts (1 hunks)
  • packages/plugins/block/vite.config.ts (1 hunks)
  • packages/plugins/datasource/vite.config.js (1 hunks)
  • packages/plugins/i18n/vite.config.ts (1 hunks)
  • packages/plugins/schema/vite.config.ts (1 hunks)
  • packages/plugins/tutorial/vite.config.ts (1 hunks)
  • packages/register/vite.config.ts (1 hunks)
  • packages/settings/props/vite.config.ts (1 hunks)
  • packages/settings/styles/vite.config.ts (1 hunks)
  • packages/toolbars/lang/vite.config.ts (1 hunks)
  • packages/toolbars/view-setting/vite.config.ts (1 hunks)
  • packages/utils/vite.config.ts (1 hunks)
  • packages/vue-generator/vite.config.js (1 hunks)
  • packages/webcomponent/vite.config.js (1 hunks)
🔇 Additional comments (20)
packages/settings/props/vite.config.ts (1)

35-35: Good externalization of @vueuse/core dependency.

Adding '@vueuse/core' to the externals list is consistent with the PR's objective to externalize all core package dependencies. This ensures the package won't bundle @vueuse/core, reducing the final bundle size and preventing duplicate code when multiple packages use the same dependency.

packages/plugins/schema/vite.config.ts (1)

35-35: Good externalization of @vueuse/core dependency.

Adding '@vueuse/core' to the externals list for this package maintains consistency with the other package configurations and aligns with the PR's goal of externalizing all core dependencies.

packages/toolbars/view-setting/vite.config.ts (1)

32-32: Good externalization of @vueuse/core dependency.

Adding '@vueuse/core' to the externals list is consistent with the other packages and properly supports the PR's objective to externalize all core package dependencies.

packages/utils/vite.config.ts (1)

28-28: Good addition of jszip as external dependency

Adding 'jszip' to the external dependencies list is consistent with the PR objective to externalize all core package dependencies. This ensures that jszip isn't bundled with the utils package, which should lead to smaller bundle sizes.

This change is part of a breaking change as noted in the PR objectives, so consumers of this package will now need to provide jszip themselves.

packages/register/vite.config.ts (1)

29-29: Correctly externalizing lodash-es

Adding 'lodash-es' to the external dependencies is appropriate and aligns with the PR objective. Lodash is a commonly used utility library that's better served as an external dependency to avoid duplication across packages.

Note that this change is part of a breaking change as mentioned in the PR objectives, so consumers will now need to provide lodash-es themselves.

packages/plugins/tutorial/vite.config.ts (1)

35-35: Good addition of marked as external dependency

Adding 'marked' to the external dependencies list is consistent with the PR's goal to externalize all core package dependencies. This markdown parser library is commonly used and makes sense as an external dependency.

This is part of the breaking change mentioned in the PR objectives, so consumers will now need to ensure the 'marked' package is available.

packages/layout/vite.config.ts (1)

35-35: Correctly externalizing @vueuse/core and vue-draggable-next

Adding '@vueuse/core' and 'vue-draggable-next' to the external dependencies list is consistent with the PR objective. These are commonly used Vue ecosystem libraries that are sensible choices for externalization.

As part of the breaking change noted in the PR, consumers will need to provide these dependencies themselves going forward.

packages/plugins/i18n/vite.config.ts (1)

35-35: LGTM! New external dependency is properly added.

Adding 'vue-clipboard3' to the external dependencies list is consistent with the PR objective of externalizing all dependencies of the core package. This will prevent bundling this dependency, reducing the package size and avoiding potential duplicate dependencies in the final application.

Just ensure that 'vue-clipboard3' is properly documented as a peer dependency in the package.json file.

packages/plugins/block/vite.config.ts (1)

35-35: LGTM! External dependencies properly expanded.

Adding '@vue/shared', 'html2canvas', and 'pinyin-pro' to the external dependencies list is aligned with the PR objective of externalizing core package dependencies. This approach reduces bundle size and gives consumers more flexibility in managing dependency versions.

Ensure these newly externalized dependencies are documented as peer dependencies in the package.json file.

packages/plugins/datasource/vite.config.js (1)

35-43: LGTM! External dependencies reformatted and expanded.

The reformatted and expanded external dependencies list with additions of '@vue/shared', 'axios', 'vue-clipboard3', and 'xlsx' improves readability and aligns with the PR objective. Externalizing these dependencies provides better separation of concerns and more flexibility for consumers of this package.

Remember to update the package.json to include these dependencies as peer dependencies with appropriate version constraints.

packages/vue-generator/vite.config.js (1)

47-55: LGTM! Comprehensive external dependencies configuration.

The expanded external dependencies list now properly includes Vue packages and namespace patterns. Using regex patterns for scoped packages (/@vue\/.*/ and /@opentiny\/tiny-engine.*/) is a good approach that avoids listing individual packages under these namespaces.

This change will significantly impact how this package is consumed, so ensure that:

  1. These externalized dependencies are documented as peer dependencies in package.json
  2. Documentation is updated to inform consumers about these new external requirements
packages/toolbars/lang/vite.config.ts (1)

35-35: External dependency '@vueuse/core' added correctly.

Adding '@vueuse/core' to the external dependencies list aligns with the PR objective of externalizing all dependencies of the core package. This ensures '@vueuse/core' is not bundled with the output, reducing bundle size and avoiding potential duplicate dependencies.

packages/settings/styles/vite.config.ts (1)

35-35: External dependencies '@vueuse/core' and 'postcss' added correctly.

Adding '@vueuse/core' and 'postcss' to the external dependencies list ensures these modules won't be bundled with the output. This change is consistent with the PR objective of externalizing all dependencies of the core package.

packages/common/vite.config.ts (1)

69-76: External dependencies expansion looks good.

The additions to the external dependencies list are comprehensive and well-organized:

  • Added regex patterns for '@babel.', '@eslint.', '@vueuse.', and '@VUE.' packages
  • Added specific modules: 'axios', 'css-tree', 'eslint-linter-browserify', and 'vue-draggable-next'

This implementation correctly fulfills the PR objective of externalizing all dependencies.

packages/canvas/vite.config.ts (1)

54-66: External dependencies structured clearly and comprehensively.

The external dependencies array has been well-structured with:

  • Core Vue-related packages ('vue', '@vueuse/core', 'vue-i18n')
  • Organization-specific packages via regex patterns (OpenTiny packages)
  • Tooling-related packages ('@babel.', '@VUE.')
  • Specific utility packages ('diff-match-patch', 'jsondiffpatch', 'postcss', 'postcss-selector-parser')

This comprehensive externalization aligns with the PR objective and follows a consistent pattern with other package configurations.

packages/design-core/vite.config.js (1)

82-98: Expanded external dependencies list aligns with PR objective

The updated external array in rollup options now includes a comprehensive list of modules to be treated as external during bundling. This change properly externalizes all dependencies of the core package as intended in the PR objectives.

The externalization strategy uses both specific module names and regex patterns, which is an effective approach to cover related packages under common namespaces.

packages/canvas/scripts/vite-plugin-separate-build.ts (2)

8-8: Type annotation added for improved type safety

Adding the string type annotation to the cleanUrl parameter enhances code readability and type safety.


91-92: Simplified code export format

The previous implementation that encoded bundled code as a base64 data URI has been replaced with a direct JSON-stringified export. This simplification:

  1. Makes debugging easier since the code is no longer base64-encoded
  2. Coordinates with the changes in the canvas initialization that conditionally injects scripts
  3. Aligns with the overall externalization strategy in the PR

This change streamlines how bundled code is included in the build output.

packages/canvas/init-canvas/init-canvas.ts (2)

5-5: Improved code structure with local variable

Using a local variable res before returning improves code structure and readability without changing functionality.

Also applies to: 20-21


12-18: Environment-specific script loading strategy

The implementation now uses different script embedding strategies based on environment:

  • Development: External script tag with src attribute
  • Production: Inline script with properly escaped content

This approach optimizes for both development (easier debugging) and production (fewer HTTP requests) while properly handling escaping issues by doubling $ characters in the inline script.

This change works well with the simplified export format in the vite-plugin-separate-build.ts file.

@@ -32,7 +32,7 @@ export default defineConfig({
},
rollupOptions: {
// 确保外部化处理那些你不想打包进库的依赖
external: ['vue', 'vue-i18n'],
external: ['vue', 'vue-i18n', '@vue/shared'],
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Good externalization of @vue/shared dependency.

Adding '@vue/shared' to the externals list is appropriate for this package. However, I notice that you've updated the globals mapping for 'vue' and 'vue-i18n' but not for '@vue/shared'.

Consider whether you need to add '@vue/shared' to the globals object for UMD builds:


🏁 Script executed:

#!/bin/bash
# Check if @vue/shared is used in UMD builds and needs a global variable mapping

# Search for imports of @vue/shared in the webcomponent package
rg -A 2 "from ['\"]\@vue/shared['\"]" packages/webcomponent/

Length of output: 167


I’ll check the Vite config and scan for any actual imports of @vue/shared before deciding if a UMD global is needed.


🏁 Script executed:

#!/bin/bash
set -e

echo "1) Locate Vite config for webcomponent package"
rg -l "vite.config.js" 

echo -e "\n2) Show externals & globals in vite.config.js"
sed -n '1,200p' packages/webcomponent/vite.config.js | sed -n '30,60p'

echo -e "\n3) Find any usage of @vue/shared in source files"
rg -n "@vue/shared" packages/webcomponent

Length of output: 1407


Add missing UMD global for @vue/shared

The webcomponent bundle imports utilities from @vue/shared (e.g. in src/defineCustomElement.js), and you’ve externalized it in Rollup, but you haven’t provided a corresponding global in the UMD build. This will lead to a runtime “undefined” when consumers use the UMD script.

• File: packages/webcomponent/vite.config.js
• Location: under rollupOptions.output.globals

Suggested diff:

 output: {
   globals: {
     vue: 'Vue',
     'vue-i18n': 'VueI18n',
+    '@vue/shared': 'VueShared'
   }
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
external: ['vue', 'vue-i18n', '@vue/shared'],
// packages/webcomponent/vite.config.js
export default defineConfig({
build: {
rollupOptions: {
external: ['vue', 'vue-i18n', '@vue/shared'],
output: {
globals: {
vue: 'Vue',
'vue-i18n': 'VueI18n',
'@vue/shared': 'VueShared'
}
}
}
}
})

import.meta.env.MODE === 'development'
? `<script type="module" src="${canvasScript}"></script>`
: // 将 $ 替换为 $$,然后在上一层的 $$ 再转义回来 $,避免被转义
`<script type="module">${canvasScript.replace(/\$/g, '$$$$')}</script>`
Copy link
Collaborator

Choose a reason for hiding this comment

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

没太理解这一步,需要咨询一下

Copy link
Member Author

Choose a reason for hiding this comment

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

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#description

String.prototype.replace 会将字符串里的 $1$2、... $n$$$&$\``、$'$` 等转义。

模式 插入值
$$ 插入一个 "$"。
$& 插入匹配的子字符串。
`$`` 插入匹配子字符串之前的字符串片段。
$' 插入匹配子字符串之后的字符串片段。
$n 插入名称为 Name 的命名捕获组。

但是我们不希望源码里的 $x 被当做特殊的替换模式。所以我们将源码里的所有 $ 符号替换成 $$。即:

canvasScript.replace(/\$/g, '$$$$')

然后在上一层的替换中,将 $$ 转换回来正常的 $ 符号,从而避免被当前特殊模式替换。

// 这一层 replace 将 `$$` 转换为原来的 `$`
.replace(
        '<!--%MAIN_SCRIPT%-->',
        import.meta.env.MODE === 'development'
          ? `<script type="module" src="${canvasScript}"></script>`
          // 这一层将 `$` 转换为 `$$`
          :  `<script type="module">${canvasScript.replace(/\$/g, '$$$$')}</script>`
      )

@@ -32,7 +32,7 @@ export default defineConfig({
},
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个包是不是可以删除了,已经没有用到了

Copy link
Member Author

Choose a reason for hiding this comment

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

可以,下次 PR 变更上来

@chilingling chilingling marked this pull request as draft April 22, 2025 02:00
@chilingling chilingling force-pushed the feat/externalAllDepsFromCore branch from d986aa0 to 50e44f1 Compare April 22, 2025 02:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants