nachocab
nachocab

Reputation: 14374

How to make rollup expand `require` statements?

I'm trying to wrap my head around rollup.

I'm using a library that generates a file with this format: IIFE with a require statement. For example

// index.js
(function() {
  const myThing = require('./thing');
})()

//thing.js
module.exports = { a: 3 };

I'm trying to use rollup with a bunch of other things, but my bundle.js ends up looking like this:

(function () {
  var myThing = require('./thing');
})();

What do I need to do so that my bundle.js ends up looking like this?:

(function () {
  var myThing = { a: 3 };
})();

In case there is a problem with my setup, here is the rollup.config.js that I'm using:

var babel = require('rollup-plugin-babel');

export default {
  input: 'index.js',
  output: {
    file: 'dist/bundle.js',
    format: 'es'
  },
  plugins: [
    babel({
      exclude: 'node_modules/**'
    })
  ]
};

These are the packages I have installed:

"babel-core": "^6.26.3",
"babel-plugin-external-helpers": "^6.22.0",
"babel-preset-env": "^1.6.1",
"rollup": "^0.58.2",
"rollup-plugin-babel": "^3.0.4"

And my babel config:

{
  "presets": [
    [
      "env",
      {
        "modules": false
      }
    ]
  ],
  "plugins": [
    "external-helpers"
  ]
}

To build, I'm just calling rollup -c.

Upvotes: 22

Views: 11959

Answers (1)

nachocab
nachocab

Reputation: 14374

Okay, I figured it out. All I had to use was use the CommonJS plugin:

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

export default {
  input: 'index.js',
  output: {
    file: 'dist/bundle.js',
    format: 'cjs'
  },
  plugins: [
    resolve(),
    commonjs(),
    babel({
      exclude: 'node_modules/**'
    })
  ]
};

Upvotes: 13

Related Questions