javascript - Webpack 使用库的 ESM 部分,尽管它需要 CommonJS?
问题描述
我目前遇到一个奇怪的问题:
Uncaught (in promise) ReferenceError: Cannot access 'Agile' before initialization
,这可能是由于webpack
试图将ESM
我的库的一部分正确转换为CommonJs
. 不幸的是,它做得不好,我最终遇到了上面提到的错误。
我的库同时支持:CommonJs
和ESM
. 所以我想知道为什么webpack
使用该ESM
部分并将其转换为CommonJs
,而不是直接使用CommonJs
我的库的部分?!
为什么我如此确定它与ESM
toCommonJs
转换有关?
好吧,我曾经被迫通过删除支持webpack
来使用CommonJs
我的库的一部分。ESM
通过这样做,我只是删除了库中ESM
模块的路径package.json
。
"module": "dist/esm/index.js"
在 unsupporting 之后ESM
,webpack 被迫使用该CommonJs
部分并且它按预期工作,因为 webpack 不再需要转换任何东西。(见图)
由于我的库应该同时支持 :ESM
和CommonJS
,因此根本不支持ESM
是没有解决方案的。
这是package.json
我的图书馆:
{
"name": "@agile-ts/core",
"version": "0.2.0+17b078aa",
"author": "BennoDev",
"license": "MIT",
"homepage": "https://agile-ts.org/",
"description": "Spacy, Simple, Scalable State Management Framework",
"keywords": [
// ..
],
"main": "dist/index.js", // <!-- CommonJs
"module": "dist/esm/index.js", // <!-- ESM
"types": "dist/index.d.ts",
"scripts": {
// ..
},
"dependencies": {
"@agile-ts/utils": "^0.0.8"
},
"peerDependencies": {
"@agile-ts/logger": "^0.0.8"
},
"peerDependenciesMeta": {
"@agile-ts/logger": {
"optional": true
}
},
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "https://github.com/agile-ts/agile.git"
},
"bugs": {
"url": "https://github.com/agile-ts/agile/issues"
},
"files": [
"dist",
"LICENSE",
"README.md",
"CHANGELOG.md"
],
"sideEffects": false
}
也许我在package.json
webpack中配置了一些错误的东西,ESM
尽管它需要CommonJs
?
- 节点版本:
v16.3.0
- Github 仓库:https ://github.com/agile-ts/agile/tree/master/packages/core
解决方案
好的,我通过解决所有问题解决了这个问题circular dependencies
^^
推荐阅读
- python-3.x - 在python中的append()函数之后清除()
- vb.net - 我可以将一个变量声明为同一个变量吗?
- r - 如何理解哪个图例是哪个图例并在 R 的 ggplot 中删除其中一个?
- python - 有没有在 Cloud Functions 上运行 Tensorflow 的好方法?尤其是偶尔打电话的时候
- django - django中基于函数的视图中的分页问题
- mysql - MYSQL 枚举;无效的语法
- php - 从打开的文件句柄中获取路径信息
- r - 您如何将代码(如编码,编码)转换为编码字符串?
- html - SELECT OPTION 有条件地应用 CSS
- reactjs - 在打字稿模块中导入 ReactNode 失败