Skip to content

fix(rollup): CommonJS not supported in transforms #11

@JamieMason

Description

@JamieMason

Description

Using ES Modules is fine, but require calls are behaving unexpectedly.

  1. Create a file eg /path/to/transform.js:

    const { a } = require('./a');
    
    export default babel => {
      const { types: t } = babel;
      console.log(a());
      return {
        visitor: {
          Program(path) {
            path.traverse({
              enter(path) {
                t.removeComments(path.node);
              }
            });
          }
        }
      };
    };
  2. Create a file eg /path/to/a.js:

    module.exports = {
      a: () => 'AAAA'
    };
  3. Select babelv7 in the Transform Menu

  4. Choose /path/to/transform.js

Expected Output

'use strict';

const a = () => 'AAAA';

var main = babel => {
  const { types: t } = babel;
  console.log(a());
  return {
    visitor: {
      Program(path) {
        path.traverse({
          enter(path) {
            t.removeComments(path.node);
          }
        });
      }
    }
  };
};

module.exports = main;

Actual Output

Cannot find module './a'

rollup.js REPL Examples

  1. This ES Modules example is fine.

  2. This CommonJS example doesn't error like it does in Electron (Electron could be a factor) but it also doesn't bundle in the contents of /path/to/a.js – it leaves the require calls there.

Suggested Solution

https://github.com/rollup/rollup-plugin-node-resolve and/or https://github.com/rollup/rollup-plugin-commonj might possibly be needed.

Help Needed

Ideas welcome, I'm stuck so far after an hour or two working on this.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions