当d3.js迁移到版本4时,对Api进行了重大重写,并对模块化包装进行了更改.
我不想升级到D3.js的版本4,但我必须使用Vite和D3.js的版本3重写我的项目的Reaction/Redux Web应用程序部分,而且它对global的使用与Vite使用的JavaScript模块系统不兼容.
我得到的错误是没有定义"global",而D3.js的版本3使用了"global".
这就是它所说的node_modules/d3/index.js (index.js:4:13)
var globals = {};
// Stash old global.
if ("d3" in global) globals.d3 = global.d3;
module.exports = require("./d3");
// Restore old global.
if ("d3" in globals) global.d3 = globals.d3; else delete global.d3;
因此,我在d3/index.js文件中注释掉了对global的引用,看看会发生什么,我得到了一个类似的属性,"Cannot Read Properties of unfined(Document)"这是源代码
var d3_document = this.document;
function d3_documentElement(node) {
return node && (node.ownerDocument || node.document || node).documentElement;
}
我猜这与版本4中向模块化d3的转变有关,但D3.js版本3.5.5在我不与Vite.js一起使用时工作得很好.例如,如果我只是将其放在index.html文件中并使用http服务器运行它.
问:有没有办法对Vite.js进行配置,使其与D3.js版本3.5.5兼容?或者,我可以更改D3.js版本3.5.5的源代码,使其在Vite.js上运行吗?
更新
我将此代码添加到了Vite.config.js中
define: {
global: {},
document: {}
}
现在,VITE投诉无法在#<Window>.
上设置属性document
3env.ts:24 Uncaught TypeError: Cannot set property document of #<Window> which has only a getter
个
这StackOverflow 个问题有帮助,但它不能解释什么时候"文档"是未定义的,它不能在窗口上"设置".
可能无关紧要,但这里是unpkg.com上的d3版本3.5.5的源代码 d3 version 3.5.5`
这是unpkg.com上d3版本4的源代码 d3 version 4个