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
MIT License
702 stars 87 forks source link

Rollup-plugin-babel is not loading identified modules from node_modules #330

Closed neeeeecka closed 5 years ago

neeeeecka commented 5 years ago

Bug Report

Current Behavior During build, I am getting errors:

'core-js/modules/es.array.includes' is imported by _jsSrc\boug2.js, but could not be resolved – treating it as an external dependency
'core-js/modules/es.object.keys' is imported by _jsSrc\boug2.js, but could not be resolved – treating it as an external dependency
'core-js/modules/es.object.to-string' is imported by _jsSrc\boug2.js, but could not be resolved – treating it as an external dependency
...

etc... My package .json:

  "dependencies": {
    "@babel/core": "^7.6.2",
    "@babel/plugin-proposal-class-properties": "^7.5.5",
    "@babel/polyfill": "^7.6.0",
    "@babel/runtime": "^7.6.2",
    "core-js": "^3.2.1",
    "rollup": "^1.23.1"
  },
  "devDependencies": {
    "@babel/cli": "^7.6.2",
    "@babel/core": "^7.6.2",
    "@babel/runtime": "^7.6.2",
    "@babel/plugin-external-helpers": "^7.2.0",
    "@babel/plugin-transform-runtime": "^7.6.2",
    "@babel/preset-env": "^7.6.2",
    "@babel/preset-es2015": "^7.0.0-beta.53",
    "rollup-plugin-babel": "^4.3.3"
  }

And a part of a build script:

require("rollup")
  .rollup({
    input: "./_jsSrc/boug2.js",
    plugins: [
      require("rollup-plugin-babel")({
        exclude: "node_modules/**",
        presets: [["@babel/preset-env", { corejs: 3, useBuiltIns: "usage" }]],
        // runtimeHelpers: true,
        plugins: [
          //   "@babel/external-helpers",
          "@babel/plugin-proposal-class-properties"
          //   "@babel/transform-runtime"
        ]
      })
    ]
  })
  .then(bundle => {

if I disable core.js3 plug-in, and manually try to import library from node_modules from inside my app's source code using import {something} from "../../node_modules/bla/bla/" Into my source code, it still doesn't work. Same error. Can't resolve. Also corejs has nothing to do with it because I also have tried using @babel-transform-runtime.

But if I provide plugins into rollup like this:

require("rollup")
  .rollup({
    input: "./_jsSrc/boug2.js",
    external: [
      "core-js/modules/es.array.includes",
      "core-js/modules/es.object.keys"
    ],
    plugins: [
      require("rollup-plugin-babel")({

(I provided them in externals array)

Then suddenly errors are disappearing one by one, because rollup loads these externals. But you know better than me that this is not how job is done. I am not going to list tens or hundreds of externals like this.

OS: windows 10

My file structure: ----node_modules ----jsBuild -------bundle.js ----jsSrc -------boug2.js(main src file) -------modules ----------module1 ----------module2 ----------.... ----tools -------build.js (I use it to bundle and transpile files using babel and rollup)

Thanks in advance.

Andarist commented 5 years ago

For that you need https://github.com/rollup/rollup-plugin-node-resolve

neeeeecka commented 5 years ago

@Andarist Thank you so much, that is what I need, but how do I use it in my build.js file? Currently if I try to import resolve from "rollup-plugin-node-resolve"; I get

(function (exports, require, module, __filename, __dirname) { import resolve from "rollup-plugin-node-resolve";
                                                              ^^^^^^
SyntaxError: Unexpected token import

this error.

Andarist commented 5 years ago

From what I see you are using require in that build.js file - so just require it, similarly to what you do with rollup-plugin-babel

neeeeecka commented 5 years ago

@Andarist, sorry for bothering you so much. I included this plugin as const resolve = require("rollup-plugin-node-resolve");, then in plugins[] i passed:


resolve({
            browser: true,
            modulesOnly: true
          }),

Now I get another error: .buildStart is not a valid Plugin property

Andarist commented 5 years ago

You'd have to share your repository with the issue reproduced in it - without that, I can't quite help you more. It seems like maybe some rollup vs plugins compatibility issue. Please check if you are using the latest versions of everything.

neeeeecka commented 5 years ago

@Andarist here, https://github.com/blackstormx/neoglass.ge, checked versions, everything seems to be latest. Installed every plugin and library today.

Andarist commented 5 years ago

I've fixed your script (you've tried to pass rollup-plugin-node-resolve to Babel instead of to Rolluo):

const resolve = require("rollup-plugin-node-resolve");
const commonjs = require("rollup-plugin-commonjs");

require("rollup")
  .rollup({
    input: "./_jsSrc/boug2.js",

    plugins: [
      resolve(),
      require("rollup-plugin-babel")({
        exclude: "node_modules/**",
        presets: ["@babel/preset-env"],
        runtimeHelpers: true,
        plugins: [
          //   "@babel/external-helpers",
          "@babel/plugin-proposal-class-properties",
          ["@babel/transform-runtime", { useESModules: true }]
        ]
      }),
      commonjs(),
    ]
  })
  .then(bundle => {
    bundle
      .generate({
        format: "iife",
        name: "mainModule"
      })
      .then(function(result) {
        require("fs").writeFileSync(
          "./_jsBuild/bundle.js",
          result.output[0].code
        );
      });
  })
  .then(null, err => console.error(err));

Had to install rollup-plugin-commonjs too.

neeeeecka commented 5 years ago

@Andarist Thank you very much! You solved my issue and made my day!