我正在尝试构建一个反应组件的组件库。我想将我的道具类型作为文档保留在库中,而不是在构建时删除它们。问题是 rollup 并没有捆绑所有的 prop-types 函数。
我最终在我的包中得到了这些行:
var ReactPropTypesSecret = require('./lib/ReactPropTypesSecret');
var checkPropTypes = require('./checkPropTypes');
Run Code Online (Sandbox Code Playgroud)
我的库的使用者无法解析这些包,因此最终会出现错误。
我的汇总配置如下所示:
import babel from "rollup-plugin-babel";
import commonjs from "rollup-plugin-commonjs";
import resolve from "rollup-plugin-node-resolve";
import pkg from "./package.json";
export default {
input: "src/index.js",
output: [
{
file: pkg.main,
format: "cjs",
sourcemap: true
},
{
file: pkg.module,
format: "es",
sourcemap: true
}
],
external: Object.keys(pkg.peerDependencies || {}),
plugins: [
babel(),
resolve(),
commonjs({ include: ["./index.js", "node_modules/**"] })
]
};
Run Code Online (Sandbox Code Playgroud)
如何require('./lib/ReactPropTypesSecret')在构建时强制 rollup 捆绑和扩展调用?
事实证明这是由于两个问题造成的:
Rollup 插件的排序。Resolve 应该放在第一位,然后是 commonjs,然后是 babel。
Babel 应该排除 node_modules。让 Babel 解析它们可能会导致 commonjs 和解析无法将它们解析为捆绑依赖项。
最终的配置应该是:
import babel from "rollup-plugin-babel";
import commonjs from "rollup-plugin-commonjs";
import resolve from "rollup-plugin-node-resolve";
import pkg from "./package.json";
export default {
input: "src/index.js",
output: [
{
file: pkg.main,
format: "cjs",
sourcemap: true
},
{
file: pkg.module,
format: "es",
sourcemap: true
}
],
external: Object.keys(pkg.peerDependencies || {}),
plugins: [
resolve(),
babel({
exclude: "**/node_modules/**"
}),
commonjs({ include: ["./index.js", "node_modules/**"] })
]
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3411 次 |
| 最近记录: |