【问题标题】:Does rollup bundle node_modules into bundle.js?汇总是否将 node_modules 捆绑到 bundle.js 中?
【发布时间】:2018-01-24 06:02:14
【问题描述】:

我正在试驾rollupjs 将一个节点应用程序打包到bundle.js 中,我很困惑。

rollup 是否支持捆绑一个完整的节点应用程序(包括 node_modules),还是只支持您项目中的 js 文件?

我有一个标准节点项目(1 个index.jsnode_modules 中有数千个文件)并且只想要一个 bundle.js。我试过了:

rollup.config.js

import commonjs from 'rollup-plugin-commonjs';
import nodeResolve from 'rollup-plugin-node-resolve';

export default {
entry: 'index.js',
dest: 'bundle.js',
format: 'iife',
plugins: [

    commonjs({
        // non-CommonJS modules will be ignored, but you can also
        // specifically include/exclude files
        include: 'node_modules/**',  // Default: undefined

        // if true then uses of `global` won't be dealt with by this plugin
        ignoreGlobal: false,  // Default: false

        // if false then skip sourceMap generation for CommonJS modules
        sourceMap: false,  // Default: true
    }),

    nodeResolve({
    jsnext: true,
    main: false
    })
]
};

无论我尝试什么rollup 都会变成index.js

module.exports = require('dat-node') // 88 MB node_modules

使用这个命令:

rollup index.js --format iife --output dist/bundle.js -c

到这个bundle.js 而不添加来自node_modules 的任何内容:

(function () {
'use strict';

module.exports = require('dat-node');

}());

我已经试过了:

  • 交换插件序列
  • 所有不同的命令行选项
  • 不同的格式
  • 不同的配置文件设置

现在我在想,也许我对汇总的理解不正确,它不支持我想要的。非常感谢您的帮助!

【问题讨论】:

    标签: javascript node.js npm bundling-and-minification rollupjs


    【解决方案1】:

    试试这个:

    import commonjs from "rollup-plugin-commonjs";
    import nodeResolve from "rollup-plugin-node-resolve";
    
    export default {
      entry      : "index.js",
      dest       : "bundle.js",
      moduleName : "myModule",
      format     : "iife",
      plugins    : [
        commonjs({
          // non-CommonJS modules will be ignored, but you can also
          // specifically include/exclude files
          include: [ "./index.js", "node_modules/**" ], // Default: undefined
    
          // if true then uses of `global` won't be dealt with by this plugin
          ignoreGlobal: false, // Default: false
    
          // if false then skip sourceMap generation for CommonJS modules
          sourceMap: false // Default: true
        }),
    
        nodeResolve({
          jsnext: true,
          main: false
        })
      ]
    };
    

    主要的变化是你需要在commonjs调用中包含index.js,否则它不会被转换为ES6模块(这是nodeResolve需要的)。

    您还需要设置moduleName

    注意:我没有专门测试dat-node,而是使用lodash

    【讨论】:

    • 谢谢,你太棒了!它正在做它的事情,只是现在遇到了dat-node 的错误,即touch-cookie 包中的Error: Unexpected token
    • @ArnoldSchrijver 你可以使用rollup-plugin-json 来解决这个问题,但是如果你有,它会再次中断(在aws-sign2):-(
    • 哦,这是一个拦截器,看到你的表情符号了吗?
    • 我没有仔细查看它非常,但它不适用于当前发布的aws-sign2 版本。该问题已在 Github 上修复,但由于某种原因从未发布。
    • 说实话,我不确定这是否是汇总问题。这个问题在aws-sign2(使用this commit)中得到了解决,但由于某种原因它从未正确发布到NPM repo。
    猜你喜欢
    • 2017-03-23
    • 2017-09-22
    • 2020-05-02
    • 2021-10-25
    • 2018-09-09
    • 1970-01-01
    • 2021-10-22
    • 2021-10-22
    • 2020-09-15
    相关资源
    最近更新 更多