抑制非常长的 webpack/Babel 警告
在项目上运行
npm run start:webpack
脚本会导致长达一页的亮黄色警告:
<w> [webpack.cache.PackFileCacheStrategy/webpack.FileSystemInfo] Resolving '@babel/helper-compilation-targets/lib/filter-items' in node_modules/@babel/plugin-proposal-object-rest-spread/node_modules/@babel/helper-compilation-targets/lib for build dependencies doesn't lead to expected result 'node_modules/@babel/plugin-proposal-object-rest-spread/node_modules/@babel/helper-compilation-targets/lib/filter-items.js', but to 'node_modules/@babel/plugin-proposal-object-rest-spread/node_modules/@babel/helper-compilation-targets/node_modules/@babel/helper-compilation-targets/lib/filter-items.js' instead. Resolving dependencies are ignored for this path. <w> at unknown 4 @babel/helper-compilation-targets/lib/filter-items
(堆栈跟踪继续。)
这使得很难发现其他警告或错误。
我尝试将 @babel/core 升级到 7.19 并更新其插件,二分查找 git 提交以查找此问题的起始位置,搜索网络——但均未成功。
我该如何诊断、修复或抑制此警告?
我有相同的警告,但路径略有不同...添加了换行符和缩进以提高可读性:
Resolving '@babel/helper-compilation-targets/lib/filter-items'
in <PROJECT_PATH>/node_modules/workbox-build/node_modules/@babel/helper-define-polyfill-provider/node_modules/@babel/helper-compilation-targets/lib
for build dependencies doesn't lead to expected result
'<PROJECT_PATH>/node_modules/workbox-build/node_modules/@babel/helper-define-polyfill-provider/node_modules/@babel/helper-compilation-targets/lib/filter-items.js'
but to
'<PROJECT_PATH>/node_modules/workbox-build/node_modules/@babel/helper-define-polyfill-provider/node_modules/@babel/helper-compilation-targets/node_modules/@babel/helper-compilation-targets/lib/filter-items.js'
instead. Resolving dependencies are ignored for this path.
它的要点似乎是 Webpack 正在尝试解决这个问题:
@babel/helper-compilation-targets/lib/filter-items
并且,它期望它解析为这个:
<PROJECT_PATH>/node_modules/workbox-build/node_modules/@babel/helper-define-polyfill-provider/node_modules/@babel/helper-compilation-targets/lib/filter-items.js
但是,相反,它解析为这个:
<PROJECT_PATH>/node_modules/workbox-build/node_modules/@babel/helper-define-polyfill-provider/node_modules/@babel/helper-compilation-targets/node_modules/@babel/helper-compilation-targets/lib/filter-items.js
奇怪的是,看起来
@babel/helper-compilation-targets
本身位于它自己的
node_modules
目录中! 🤔🤷🏻♂️
我设法通过删除该(无关的?)软件包来“修复”该问题:
rm -rf <PROJECT_PATH>/node_modules/workbox-build/node_modules/@babel/helper-define-polyfill-provider/node_modules/@babel/helper-compilation-targets/node_modules/@babel/helper-compilation-targets/
您还可以添加
postinstall
脚本以在每次安装后自动执行此操作:
scripts: {
"postinstall": "rimraf node_modules/workbox-build/node_modules/@babel/helper-define-polyfill-provider/node_modules/@babel/helper-compilation-targets/node_modules/@babel/helper-compilation-targets"
}
我正在使用 pnpm,并且在使用 opentelemetry 的 next.js 项目中也遇到了同样的错误。
对于我来说,解决方法是让 webpack 忽略子路径。因为在这种情况下,删除 node_modules 中的文件夹并不容易,也不是首选
在我的特定情况下,它看起来像这样,但它可能在某种程度上可以通用化。
webpack: (config) => {
config.module.rules?.push({
test: /[email protected]_@opentelemetry\[email protected][email protected][email protected]\node_modules\next/,
loader: "ignore-loader",
});
return config;
},