Skip to content

fix: resolve .less import issue in ES build (#2844)#2845

Open
lzxue wants to merge 1 commit intomasterfrom
fix/l7-component-less-import
Open

fix: resolve .less import issue in ES build (#2844)#2845
lzxue wants to merge 1 commit intomasterfrom
fix/l7-component-less-import

Conversation

@lzxue
Copy link
Copy Markdown
Contributor

@lzxue lzxue commented Apr 4, 2026

修复描述

修复 issue #2844:@antv/l7-component ES build 导入 .less 源文件的问题。

问题

从 v2.24.0 开始,@antv/l7-component/es/index.js 导入了 .less 文件:

import "./css/index.less";

这导致任何通过 Vite 或其他 bundler 构建的项目,如果没有安装 less 作为依赖,都会出现构建失败。

解决方案

.fatherrc.ts 中的 babel transformer 改为 esbuild。esbuild 可以正确地将 .less 导入转换为内联 CSS,而不需要消费者安装 less 预处理器。

修改文件

  • packages/component/.fatherrc.ts:将 esm 和 cjs 的 transformer 从 babel 改为 esbuild

测试

此修复需要在发布新版本后进行验证,确保 ES build 输出不再包含对 .less 文件的直接引用。


相关 Issue: #2844

- Change babel transformer to esbuild in .fatherrc.ts
- esbuild can properly handle CSS/LESS imports and inline them
- This fixes the issue where consumers need to install 'less' preprocessor
- Fixes issue #2844
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 4, 2026

⚠️ No Changeset found

Latest commit: 641d6ce

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request updates the build configuration in packages/component/.fatherrc.ts by switching the transformer from babel to esbuild for both ESM and CJS outputs. However, feedback indicates that esbuild in non-bundle mode will fail to inline CSS/LESS imports as intended and will bypass existing Babel plugins. It is also recommended to set the CJS platform to browser to maintain consistency for this browser-oriented component library.

Comment on lines +10 to +15
transformer: 'esbuild',
platform: 'browser',
},
cjs: isProduction
? {
transformer: 'esbuild',
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

PR 描述中提到切换到 esbuild transformer 可以将 .less 导入转换为内联 CSS。但在 father 的非 bundle 模式(即 esmcjs 配置)下,esbuild 是逐文件进行转换的,它并不会解析或内联被导入的 CSS/LESS 文件。这意味着构建产物(如 es/index.js)中很可能依然保留着 import "./css/index.less",无法解决 issue #2844。原本使用的 babel transformer 配合注释中提到的 transform-import-css-l7 插件正是为了处理此类需求,切换到 esbuild 会导致这些 Babel 插件失效。

cjs: isProduction
? {
transformer: 'esbuild',
platform: 'node',
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

对于 @antv/l7-component 这种纯浏览器端的组件库,建议将 cjsplatform 也设置为 browser。虽然 cjs 产物常用于 Node 环境(如 SSR 或测试),但统一设置为 browser 可以确保 esbuild 在转换时采用一致的浏览器端默认行为,避免潜在的环境差异问题。

Suggested change
platform: 'node',
platform: 'browser',

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant