rollup / rollup-plugin-babel

This package has moved and is now available at @rollup/plugin-babel / https://github.com/rollup/plugins/tree/master/packages/babel

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Recursive require calls in node_modules aren't resolved when the @babel/env preset is used

sgravrock opened this issue · comments

I'm trying to package some code that includes a tree of modules inside node_modules, using rollup-plugin-node-resolve, rollup-plugin-babel, and rollup-plugin-commonjs. It works until I add the @babel/env preset, at which point require() calls sufficiently deep in the tree are left in the generated code.

  • Rollup Version: 1.20.3
  • Operating System (or Browser): OS X 10.13.6
  • Node Version: 10.16.3

How Do We Reproduce?

  1. Clone https://github.com/sgravrock/rollup-babel-node-resolve-bug.
  2. Run npm install and rollup -c.
  3. Notice the presence of require() calls in dist/built.js.
  4. Remove the @babel/env preset from rollup.config.js and re-run rollup -c.
  5. Notice that the require() calls in dist/built.js have been replaced with the contents of the corresponding modules.

Alternately, if you prefer not to clone the repo, create rollup.config.js:

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

export default {
    input: 'index.js',
    output: {
        format: 'iife',
        name: 'foo',
        file: 'dist/built.js'
    },
    plugins: [
        resolve({
            preferBuiltins: false
        }),
        babel({
            "presets": [
                ["@babel/env"]
            ]
        }),
        commonjs()
    ],
};

and index.js:

const URL = require("whatwg-url").URL;

Expected Behavior

All require() calls are replaced with the contents of the resolved module.

Actual Behavior

Some require() calls are left in the generated code.

(Transferring to rollup-plugin-babel)

You should put a commonjs plugin before babel one. Otherwise, commonjs plugin might recognize some CJS files as modules (as they were already transformed by babel and some import statements could have been added to them).