是否将node_modules捆绑到bundle.js中? [英] Does rollup bundle node_modules into bundle.js?

查看:86
本文介绍了是否将node_modules捆绑到bundle.js中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试驾 rollupjs 将节点应用打包到 bundle中.js 并感到困惑.

I am testdriving rollupjs to package a node app into a bundle.js and am confused.

汇总是否支持捆绑整个节点应用程序(包括 node_modules ),或者仅捆绑项目中的js文件?

Does rollup support bundling a full node app (including node_modules), or just the js files that are part of your project?

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

I have a standard node project (1 index.js, thousands of files in node_modules) and would like just one bundle.js. I tried:

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
    })
]
};

无论我怎么尝试,汇总都会使此 index.js :

Whatever I try rollup turns this 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 中的任何内容:

to this bundle.js without adding anything from node_modules:

(function () {
'use strict';

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

}());

我已经尝试过:

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

现在,我在想,也许我不正确地理解汇总,并且它不支持我想要的内容.帮助非常感谢!

Now I am thinking, maybe I understand rollup incorrectly and it does not support what I want. Help much appreciated!

推荐答案

尝试一下:

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 需要什么.

The main change is that you need to include index.js in the commonjs call as well, otherwise it won't get converted to an ES6 module (which is what nodeResolve needs).

您还需要设置 moduleName .

NB :我不是专门针对 dat-node 进行测试,而是针对 lodash .

NB: I didn't test specifically with dat-node, but with lodash.

这篇关于是否将node_modules捆绑到bundle.js中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆