antvis / G2

📊 The concise and progressive visualization grammar.
https://g2.antv.antgroup.com
MIT License
12.11k stars 1.59k forks source link

require() of ES Module Error when Using @antv/g2 with Vitest #6486

Open 222chaos opened 1 week ago

222chaos commented 1 week ago

问题描述

在使用 @antv/g2 库时,遇到了以下错误: Error: require() of ES Module /path/to/project/node_modules/d3-array/src/index.js from /path/to/project/node_modules/@antv/g2/lib/transform/utils/order.js not supported. Instead change the require of index.js in /path/to/project/node_modules/@antv/g2/lib/transform/utils/order.js to a dynamic import() which is available in all CommonJS modules.

重现链接

No response

重现步骤

1.使用 vitest 进行单元测试。

2.项目中引入了 @ant-design/plots 和 @ant-design/charts。

3.运行测试时,报错与 @antv/g2 依赖 d3-array 模块不兼容。

预期行为

@antv/g2 应该能够正确处理 ES 模块的导入,避免使用 require() 导入 ES 模块,改为使用 import() 来确保兼容性。

平台

No response

屏幕截图或视频(可选)

No response

补充说明(可选)

可能的解决方案是将 require() 替换为动态 import(),以兼容 ES 模块环境。

lxfu1 commented 1 week ago

https://github.com/antvis/G2/issues/6456

hustcc commented 1 week ago

参考:https://github.com/d3/d3-array/issues/218