rollup / rollup-plugin-commonjs

This module has moved and is now available at @rollup/plugin-commonjs / https://github.com/rollup/plugins/blob/master/packages/commonjs
MIT License
501 stars 126 forks source link

TypeError: Cannot read property 'resolve' of undefined #418

Closed Dan503 closed 4 years ago

Dan503 commented 4 years ago

How Do We Reproduce?

Reproduction steps are outlined in this reduced test case repo:

https://github.com/Dan503/BUG-rollup-plugin-node-resolve/tree/CommonJS-bug

Note this part of gulpfile.js in particular:

const rollupJS = (inputFile, options) => {
    return () => {
        return (
            rollup({
                input: options.basePath + inputFile,
                format: options.format,
                sourcemap: options.sourcemap,
                plugins: [
                    CommonJS(), // <<-- This is the line that calls rollup-plugin-commonjs
                ],
            })
                .pipe(source(inputFile, options.basePath))
                .pipe(buffer())
                .pipe(sourcemaps.init({ loadMaps: true }))
                .pipe(sourcemaps.write('.'))
                .pipe(gulp.dest(options.distPath))
        )
    }
}

Expected Behavior

It should not error

Actual Behavior

It throws this error when trying to run the code:

TypeError: Cannot read property 'resolve' of undefined
    at resolveId (C:\xxx\node_modules\rollup-plugin-commonjs\dist\rollup-plugin-commonjs.cjs.js:157:17)
    at C:\xxx\node_modules\rollup-stream\node_modules\rollup\dist\rollup.js:1750:32
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
khalyomede commented 4 years ago

I can confirm I have the same issue. Here is my gulp file:

import { dest, parallel } from "gulp";
import rollup from "rollup-stream";
import nodeResolve from "rollup-plugin-node-resolve";
import commonjs from "rollup-plugin-commonjs";
import vue from "rollup-plugin-vue";

const js = () =>
  rollup({
    input: "src/js/index.js",
    output: {
      format: "iife",
      file: "index.js"
    },
    plugins: [commonjs(), nodeResolve(), vue()]
  }).pipe(dest("dist/js"));

const build = parallel(js);

export { build };

Here is the error my console prints:

$ yarn build
yarn run v1.19.1
$ gulp build
[22:48:37] Requiring external module @babel/register
[22:48:39] Using gulpfile C:\xampp\htdocs\expense-manager\gulpfile.babel.js
[22:48:39] Starting 'build'...
[22:48:39] Starting 'js'...
[22:48:39] 'js' errored after 38 ms
[22:48:39] TypeError: Cannot read property 'resolve' of undefined
    at resolveId (C:\xampp\htdocs\expense-manager\node_modules\rollup-plugin-commonjs\src\resolve-id.js:54:15)
    at promise.then.result (C:\xampp\htdocs\expense-manager\node_modules\rollup-stream\node_modules\rollup\dist\rollup.js:1750:32)
[22:48:39] 'build' errored after 80 ms
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

However, with the same config but running native rollup, no errors:

// rollup.config.js
import nodeResolve from "rollup-plugin-node-resolve";
import commonjs from "rollup-plugin-commonjs";
import vue from "rollup-plugin-vue";

export default {
  input: "src/js/index.js",
  output: {
    format: "iife",
    file: "index.js"
  },
  plugins: [nodeResolve(), commonjs(), vue()]
};
$ yarn rollup --config
yarn run v1.19.1
$ rollup --config

src/js/index.js → index.js...
created index.js in 621ms
Done in 1.66s.

Do you know if there is something linked with rollup-stream or the plugins?

Temporary solution

Here is the solution I am using to overcome this issue, using rollup itself.

import { parallel } from "gulp";
import { rollup } from "rollup";
import nodeResolve from "rollup-plugin-node-resolve";
import commonjs from "rollup-plugin-commonjs";
import vue from "rollup-plugin-vue";

const js = async () => {
  const bundle = await rollup({
    input: "src/js/index.js",
    plugins: [nodeResolve(), commonjs(), vue()]
  });

  await bundle.write({
    file: "dist/js/index.js",
    format: "iife"
  });
};

const build = parallel(js);

export { build };

Which outputs the desired result.

$ yarn build
yarn run v1.19.1
$ gulp build
[23:16:20] Requiring external module @babel/register
[23:16:21] Using gulpfile C:\xampp\htdocs\expense-manager\gulpfile.babel.js
[23:16:21] Starting 'build'...
[23:16:21] Starting 'js'...
[23:16:22] Finished 'js' after 621 ms
[23:16:22] Finished 'build' after 631 ms
Done in 2.85s.
shellscape commented 4 years ago

Hey folks, we've moved this repo to a new home in our plugins monorepo at https://github.com/rollup/plugins. If this issue is still pertinent, please reopen, by filling out the appropriate issue template, in the new repo. Thanks for originally opening this issue, and we look forward to seeing you in the new repo. This issue is being closed and this repo is being permanently archived.