问题
esm在将我的 npm 包(Typescript)与 target捆绑在一起时,我目前面临一个有趣的问题ES2015。对于捆绑,我使用rollupand esbuild。见 GitHub;(线71-101)
tsconfig.json
{
"extends": "../tsconfig.default.json",
"compilerOptions": {
"target": "esnext",
"outDir": "dist",
"rootDir": "src"
},
"include": [
"./src/**/*" // Only include what is in src (-> dist, tests, .. will be excluded)
]
}
无论出于何种原因,几乎每个捆绑esm的文件都会检查是否require在undefined文件头中(见图),即使require未捆绑源代码中的任何地方都没有使用它,实际上esm也不能与require.
// Added require check
typeof require !== "undefined" ? require : (x) => {
throw new Error('Dynamic require of "' + x + '" is not supported');
};
如果不是每个导入的 React 应用程序都npm package收到 100 个警告告诉他们require is used incorrectly. (见图)
// Warning Message
Critical dependency: require function is used in a way in which dependencies cannot be statically extracted
问题
有谁知道为什么将描述require的检查添加到文件头以及如何摆脱它。
注意:我已经看过这篇文章,但它与 Angular 有关,所以我认为它对我没有多大帮助:关键依赖:require 函数的使用方式是无法静态提取依赖关系

