19

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!

Arnold Schrijver
  • 3,588
  • 3
  • 36
  • 65

3 Answers3

14

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.

robertklep
  • 198,204
  • 35
  • 394
  • 381
  • Thanx, you rock! It is doing it's thing, only now bumping into an error with `dat-node`, namely `Error: Unexpected token` in `touch-cookie` package. – Arnold Schrijver Aug 16 '17 at 08:13
  • 1
    @ArnoldSchrijver you can solve that issue by using `rollup-plugin-json`, but when you have, it will break again (on `aws-sign2`) :-( – robertklep Aug 16 '17 at 08:20
  • Ow, and that's a blocker, seeing your emoji? – Arnold Schrijver Aug 16 '17 at 08:23
  • I didn't look at it _very_ closely, but it's not working with the current published version of `aws-sign2`. The issue has been fixed on Github, but it was never published for some reason. – robertklep Aug 16 '17 at 08:30
  • Thx, I added json plugin, but get `Error: Could not load crypto. No such file or directory, open 'crypto'`. Probably need to look at build-ins config. Will check. – Arnold Schrijver Aug 16 '17 at 08:31
  • And that is fixed with `external: ['crypto']` and now I have the `aws-sign2` error.. – Arnold Schrijver Aug 16 '17 at 08:44
  • No idea if it's going to solve your problem, but you might try with another bundler, like Webpack. – robertklep Aug 16 '17 at 09:06
  • Thanks, I've created a rollup issue: https://github.com/rollup/rollup/issues/1556 – Arnold Schrijver Aug 16 '17 at 09:33
  • 1
    To be honest, I'm not sure if it's a Rollup issue. The problem got solved in `aws-sign2` (using [this commit](https://github.com/request/aws-sign/commit/03755f299624b43dd30c33611963727f6bb6d877)), but it was never properly published to the NPM repo for some reason. – robertklep Aug 16 '17 at 09:34
  • You are correct. I just found out. Will close it again. – Arnold Schrijver Aug 16 '17 at 09:39
  • Why can't there be a no-config option that everyone uses since they're all basically trying to do the same thing anyway? Seriously. – Steve Nov 08 '21 at 08:24
2

I was facing the same problem and searched a lot but the answers were mostly of the old syntax. After some searching, this is what worked for me. I am not 100% sure that this is the best way to do it.

It would be very helpful if someone more knowledgeable on rollup would verify it

So what i found does the trick is adding the modulesOnly option to the nodeResolve plugin something like this :

import nodeResolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";

export default {
  input: "src/index.js",
  output: [
    {
      format: "cjs",
      file: "dist/index.cjs.js",
    },
    {
      format: "esm",
      file: "dist/index.esm.js",
    },
  ],
  plugins: [
    commonjs(),
    nodeResolve({ modulesOnly: true }),
  ],
};

Sayan Das
  • 51
  • 5
2

others didn't work for me, though i worked out a minimal solution

  • rollup.config.js:
import nodeResolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";

export default {
  plugins: [
    commonjs(), // <-- this handles some parsing of js syntax or something (necessary for `export { init } from "mathjax";`)
    nodeResolve(), // <-- this allows npm modules to be added to bundle
  ],
};
  • package.json
{
    "type": "module",
    "dependencies": {
        "@rollup/plugin-commonjs": "^24.0.0",
        "@rollup/plugin-node-resolve": "^15.0.1",
        "mathjax": "^3.2.2"
    }
}
  • main.js
export { init } from "mathjax";
  • shell command
rollup main.js --file bundle.js -c
yes
  • 241
  • 2
  • 11