我想使用 NPM 中的库 ebnf
并使用 rollup
创建 bundle 。由于ebnf
已安装到node_modules
,我还使用了rollup插件rollup-plugin-node-resolve
。
问题是 ebnf
包含代码 require('..')
- 在我的例子中 - 被解析为 dist
我的情况。因此,似乎 ..
是相对于输出文件而不是相对于源文件进行解释的。
这是我的 rollup.config.js
(取 self 的测试存储库 jneuendorf/rollup-broken-resolve ):
import resolve from 'rollup-plugin-node-resolve'
import commonjs from 'rollup-plugin-commonjs'
export default {
input: 'src/index.js',
output: {
file: 'dist/bundle.js',
format: 'cjs'
},
// name: 'MyModule',
plugins: [
resolve(),
commonjs(),
]
}
这是rollup-plugin-node-resolve
中的问题还是我做错了什么?
最佳答案
由于一些所需的外部库仍然只能作为 Common.js 模块使用,因此您也可以将它们转换为 ES 模块:
"Since most packages in your node_modules folder are probably legacy CommonJS rather than JavaScript modules, you may need to use rollup-plugin-commonjs"
https://github.com/rollup/rollup-plugin-commonjs
“将 CommonJS 模块转换为 ES6,以便它们可以包含在 Rollup 包中”
关于node.js - 汇总 - 路径解析不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48652710/