Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does rollup bundle node_modules into bundle.js?

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

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

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

Whatever I try rollup turns this index.js:

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

with this command:

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

to this bundle.js without adding anything from node_modules:

(function () {
'use strict';

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

}());

And I have tried:

  • swapping plugin sequence
  • all different command line options
  • different formats
  • different config file settings

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

like image 680
Arnold Schrijver Avatar asked Aug 16 '17 07:08

Arnold Schrijver


1 Answers

Try this:

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

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).

You also need to set moduleName.

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

like image 106
robertklep Avatar answered Oct 30 '22 20:10

robertklep