Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.babelrc or config package.json is not considered #624

Closed
matsp opened this issue May 28, 2018 · 12 comments
Closed

.babelrc or config package.json is not considered #624

matsp opened this issue May 28, 2018 · 12 comments

Comments

@matsp
Copy link

matsp commented May 28, 2018

I'm submitting a bug report

Webpack Version:
4.9.1

Babel Core Version:
7.0.0-beta.49

Babel Loader Version:
8.0.0-beta.3

Please tell us about your environment:
Windows 10

Current behavior:
babel-loader doesn't include config options from ".babelrc" or "package.json

Expected/desired behavior:
babel-loader uses ".babelrc" or "package.jso"

webpack:

{
        test: /\.js$/,
        loader: 'babel-loader',
        options: {
          babelrc: true,
          cacheDirectory: true
        }
 }

workaround:

{
        test: /\.js$/,
        loader: 'babel-loader',
        options: {
          babelrc: true,
          extends: path.join(__dirname + '/.babelrc'),
          cacheDirectory: true
        }
 }
  • What is the expected behavior?

Before I configured babel trough "package.json". I just regonized this issue because I disabled comments in babel and found comments in my output.

  • What is the motivation / use case for changing the behavior?
@sebinsua
Copy link

sebinsua commented Jun 5, 2018

I'm affected by this issue, too. However, it was appearing with beta.2 in my case.

The workaround you posted fixes it for me, however I would like to debug this more - if anybody can tell me where the configuration is loaded?

@matsp
Copy link
Author

matsp commented Jun 13, 2018

@sebinsua

Only using the new api with babel.config.js also fix this problem for me.

@swashcap
Copy link

Still seeing this problem with babel-loader@8.0.0-beta.4. My temporary workaround is to load the .babelrc contents manually and merge them into options:

diff --git a/webpack.config.js b/webpack.config.js
index d8535a8..43d7a84 100644
--- a/webpack.config.js
+++ b/webpack.config.js
@@ -1,11 +1,23 @@
+const fs = require('fs')
+const path = require('path')
+
 module.exports = {
   module: {
     rules: [{
       exclude: /node_modules/,
       loader: 'babel-loader',
-      options: {
-        cacheDirectory: true
-      },
+      /**
+       * babel-loader doesn't load the .babelrc
+       * @todo Remove once the issue is addressed
+       * {@link https://github.com/babel/babel-loader/issues/624}
+       */
+      options: Object.assign(
+        {
+          babelrc: false,
+          cacheDirectory: true
+        },
+        JSON.parse(fs.readFileSync(path.join(__dirname, '.babelrc'), 'utf-8'))
+      ),
       test: /\.js$/
     }, {
       exclude: /node_modules/,

@EvHaus
Copy link

EvHaus commented Jun 25, 2018

I have a similar problem. babel-loader works correctly if the .babelrc is right next to my package.json directory, but if it's located 1 directory above (I use a monorepo) -- then it's not loaded.

Not sure if that's intentional or not.

babel-loader@8.0.0-beta.4
@babel/core@7.0.0-beta.51

UPDATE: Our solution was to migrate from .babelrc to babel.config.js which is the new recommended configuration structure for monorepos.

@felixfbecker
Copy link

I'm affected by this too after trying to upgrade everything to v7. I think that this is only happening when transpiling node_modules (which I need to do for a dependency) - I don't get any errors for the files in my project, but the config is not considered for files in node_modules and I get errors that plugins are missing.

Could it be that because these files are in node_modules, the config search "stops" at the package.json of the dependency, instead of traversing up? Or maybe looking in parent directories is broken in general?

@loganfsmyth
Copy link
Member

For those having issues here, I'd recommend that you read over http://babeljs.io/docs/en/config-files#file-relative-configuration It is most likely the limitations on how .babelrc files are resolved that are tripping you up, and those docs should be able to give you some direction and understanding.

@vzaidman
Copy link

also take a look at this:
https://babeljs.io/docs/en/options#babelrcroots

@felixfbecker
Copy link

I've read that, but I don't understand what changed here between v7 -> v8?

@loganfsmyth
Copy link
Member

@felixfbecker The differences between Babel 6 and Babel 7 in http://babeljs.io/docs/en/config-files#6x-vs-7x-babelrc-loading should help a bit. If compilation of node_modules is expected, then chances are you'll want to use a project-wide config file instead of a .babelrc.

@felixfbecker
Copy link

So it matter whether the config file is a JS file or a JSON file? I prefer JSON because it can get autocompletion and validation against JSON schema

@loganfsmyth
Copy link
Member

loganfsmyth commented Sep 2, 2018

No, the important differentiation is that .babelrc and package.json#babel config values only apply to the specific package that they are inside of, and will no longer affect node_modules files. For that, you'll either need to use Babel auto-searching for babel.config.js files, or pass the name of an explicit file. If you want a babel.config.json file instead, that's fine, but you'd need to pass the path to the file explicitly, e.g. configFile: "./babel.config.json" in your Webpack config babel-loader options.

EDIT by @nicolo-ribaudo babel.config.json is supported by default now.

@mohsenuss91
Copy link

@sebinsua

Only using the new api with babel.config.js also fix this problem for me.

babelrc still ignored,
@matsp how to fix that please!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants