如何在汇总中设置为外部所有节点模块?

时间:2017-06-30 10:16:53

标签: node.js module rollup

我希望将从node_modules导入的所有模块都作为外部模块。 这有什么配置?

我尝试没有成功:

import path from "path";
import glob from "glob";
import multiEntry from "rollup-plugin-multi-entry";

export default {
    entry: "src/**/*.js",
    format: "cjs",
    plugins: [
        multiEntry()
    ],
    external: glob.sync("node_modules/**/*.js").map(file => path.resolve(file)),
    dest: "dist/bundle.js"
}];

import multiEntry from "rollup-plugin-multi-entry";

export default {
    entry: "src/**/*.js",
    format: "cjs",
    plugins: [
        multiEntry()
    ],
    external: id => id.indexOf("node_modules") !== -1,
    dest: "dist/bundle.js"
}];

2 个答案:

答案 0 :(得分:1)

您可以使用package.json依赖项字段完成此操作:

const pkg = require('./package.json');

export default {
    // ...
    external : Object.keys(pkg.dependencies),
    // ...
}

答案 1 :(得分:1)

更新:

使用rollup-plugin-auto-external


感谢@IsidroTorregrosahis answer(当然还有@maxkueng)。我通过添加节点内置模块(例如fspath)以及peerDependencies对其进行了改进。使用:

import builtins from 'builtin-modules/static'
const pkg = require('./package.json')
// ...
export default {
    external: builtins.concat(Object.keys(pkg.dependencies || {})).concat(Object.keys(pkg.peerDependencies || {})),
    // ...
}

请参见 npmjs.com 上的builtin-modules,并请参见:

https://github.com/rollup/rollup-plugin-node-resolve#resolving-built-ins-like-fs