babel icon indicating copy to clipboard operation
babel copied to clipboard

@babel/core - `options` issue

Open stephen-last opened this issue 9 years ago • 14 comments

Choose one: is this a bug report or feature request? Bug?

Babel Configuration (.babelrc)

{
  "presets": [
    "@babel/preset-react",
    "@babel/preset-env"
  ],
  "plugins": [
    ["@babel/plugin-proposal-object-rest-spread"],
    ["@babel/plugin-proposal-class-properties", { "spec": true }]
  ]
}

Dev deps:

"devDependencies": {
    "@babel/cli": "^7.0.0-beta.32",
    "@babel/core": "^7.0.0-beta.32",
    "@babel/plugin-proposal-class-properties": "^7.0.0-beta.32",
    "@babel/plugin-proposal-object-rest-spread": "^7.0.0-beta.32",
    "@babel/polyfill": "^7.0.0-beta.32",
    "@babel/preset-env": "^7.0.0-beta.32",
    "@babel/preset-react": "^7.0.0-beta.32",
    "babel-loader": "^8.0.0-beta.0",
    "webpack": "^3.9.0"
  }

Expected Behavior

Using webpack.config.babel.js should run.

Current Behavior

Error:

TypeError: Cannot read property 'useBuiltIns' of undefined at _default (C:\node\clcommerce\node_modules@babel\plugin-proposal-object-rest-spread\lib\index.js:13:32) at Function.memoisePluginContainer (C:\node\clcommerce\node_modules\babel-core\lib\transformation\file\options\option-manager.js:113:13)

Removing plugins just changes the error, but always points to an issue with options in @babel/core. For example, removing the plugins changes the error to:

C:\node\clcommerce\node_modules\babel-core\lib\transformation\file\options\option-manager.js:328 throw e; TypeError: Cannot read property 'throwIfNamespace' of undefined (While processing preset: "C:\node\clcommerce\node_modules\@babel\preset-react\lib\index.js")

Going back to webpack.config.js and using require()'s works.

Context

Happening since I installed the new @babel... beta modules.

Your Environment

software version(s)
Babel 7.0.0-beta.32
node 8.9.1
npm 5.5.1
Operating System Win 7 Pro

stephen-last avatar Dec 01 '17 10:12 stephen-last

Hey @stephen-last! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite.

babel-bot avatar Dec 01 '17 10:12 babel-bot

This beta seem to not be passing second options to to plugins. While trying to make it work I faced same issue and some similar ones from different plugins while removing extra presets and plugins. There is always the issue that the second paramter is undefined and it's properties cannot be accessed in a plugin.

4ex:

 Module build failed: TypeError: Cannot read property 'spec' of undefined (While processing preset: "/mnt/test/node_modules/@babel/preset-env/lib/index.js")
    at _default (/mnt/test/node_modules/@babel/plugin-transform-arrow-functions/lib/index.js:7:22)

olegstepura avatar Dec 01 '17 13:12 olegstepura

I had the same issue and tried to dive into the code.

I noted this in babel-core/.../option-manager.js:

OptionManager.normalisePlugins = function normalisePlugins(loc, dirname, plugins) {
    return plugins.map(function (val, i) {
        /* ... */
        // notice that options is not provided
        plugin = OptionManager.normalisePlugin(plugin, loc, i, alias);
        return [plugin, options];
    });
}

deeper under the OptionManager.normalisePlugin call in OptionManager.memoisePluginContainer:

// fn is the plugin function, still no options obviously
obj = fn(context);

In my own case the plugin is 'plugin-transform-modules-commonjs' but the code is pretty much the same for every plugins I came across.

The called function always look like this:

// notice there is TWO parameters
function _default(api, options) {
  // options decomposition
  var loose = options.loose, // <--- FAIL HERE: "TypeError: Cannot read property 'loose' of undefined"
      allowTopLevelThis = options.allowTopLevelThis,
      strict = options.strict;
  
/* ... */

##Steps to reproduce my situation:

index.js

require('babel-register')({
  "presets": [
    ["@babel/preset-env", {
      loose: false,
      // modules: false
    }],
    "@babel/preset-react"
  ],
});
require('./server/server');

with a /server/server.js file, "babel-register": "^6.26.0" and everything else @7.0.0-beta.32

node index.js


I'm not familiar with babel code so I don't know who should fix this behavior but I hope this may help.

VincentCharpentier avatar Dec 01 '17 15:12 VincentCharpentier

@VincentCharpentier you'll want to use @babel/[email protected]

existentialism avatar Dec 01 '17 15:12 existentialism

@existentialism thanks for your help

VincentCharpentier avatar Dec 01 '17 15:12 VincentCharpentier

@VincentCharpentier no problem! we understand it's not straightforward (and we hope to get some migration/upgrade tools done by v7 release), but you'll want to make sure all your @babel-related deps are on the same beta version.

existentialism avatar Dec 01 '17 16:12 existentialism

Seems logical. Note that official doc doesn't mention anything about it yet: https://babeljs.io/docs/usage/babel-register/

Best,

VincentCharpentier avatar Dec 01 '17 16:12 VincentCharpentier

The official docs are for Babel 6 because 7 is still a beta that we're breaking occasionally. Once it is out of beta, we'll be updating the documentation for 7.x.

@stephen-last Your stack trace mentions babel-core\lib\transformation\file\options\option-manager.js, not @babel/core, so something in your build tools is still using Babel 6. Maybe look farther down the stack trace to try to figure out what it is?

loganfsmyth avatar Dec 01 '17 17:12 loganfsmyth

This should be the same issue as https://github.com/babel/babel/issues/6945

hzoo avatar Dec 02 '17 14:12 hzoo

Installing and loading "babel-loader": "^8.0.0-beta.0" instead of "babel-loader": "^7.1.2" fixed the issue for me. Hope this helps someone in need of a quick fix.

michaelpumo avatar Feb 20 '18 16:02 michaelpumo

I can confirm that changing the babel-loader did indeed work

maximgeerinck avatar Feb 24 '18 19:02 maximgeerinck

changing the babel-loader did worked for me too.

rahulkumar993 avatar Feb 26 '18 05:02 rahulkumar993

thanks @michaelpumo. Everyone running into that useBuiltIns issue should definitely install that

silouanwright avatar Feb 26 '18 15:02 silouanwright

Can use https://github.com/babel/babel-upgrade now (since WIP but handles this)

hzoo avatar Feb 26 '18 16:02 hzoo