babel-loader icon indicating copy to clipboard operation
babel-loader copied to clipboard

README should include documentation on use of `babelrc: false` option

Open yongjun21 opened this issue 9 years ago • 6 comments

I'm submitting a documentation request

With the move to webpack 2, it is recommended to turn off transform commonjs modules in babel-preset-es2016 (ie. "presets": ["es2015"] to "presets: [["es2015", {"modules": false}]]).

However, since node does not support es6 modules yet, when building a full stack node backend + js frontend app, it may be necessary to use two sets of babel settings. One reasonable approach is keep the origin setting in .babelrc and use a different setting for webpack compilation through babel-loader loader options:

// in .babelrc, for use with babelCli
{
  "presets": ["es2015"]
}

// in webpack.config.js, used only in webpack 2
use: {
  loader: "babel-loader",
  options: {
    presets: [["es2015", {modules: false}]]
  }
}

However, this will fail as the setting in .babelrc overwrites the loader options. The solution is to include babelrc: false flag in the loader option.

This is not immediately obvious even for experienced user of babel and webpack. One will assume babel-loader option being closer to the usage location should take precedence over .babelrc even though is not the case.

Therefore it will be helpful to have that in the documentation.

See also: http://stackoverflow.com/questions/33187504/does-a-babel-field-in-package-json-take-precedence-over-a-babelrc-file

Webpack Version: 2.x

Babel Core Version: 6.x

Babel Loader Version: 6.2.x

Please tell us about your environment: OSX 10.x / Linux

yongjun21 avatar Mar 24 '17 02:03 yongjun21

I have a similar issue with the same settings where it throws Invalid options type for foreign error, any pointers would be much appreciated.

Module build failed: TypeError: [BABEL] /Users/satkumar/Dev/onboarding/src/iframe-script.js: Invalid options type for foreign
    at Logger.error (/Users/satkumar/Dev/onboarding/node_modules/babel-core/lib/transformation/file/logger.js:41:11)
    at OptionManager.mergeOptions (/Users/satkumar/Dev/onboarding/node_modules/babel-core/lib/transformation/file/options/option-manager.js:266:16)
    at /Users/satkumar/Dev/onboarding/node_modules/babel-core/lib/transformation/file/options/option-manager.js:371:14
    at /Users/satkumar/Dev/onboarding/node_modules/babel-core/lib/transformation/file/options/option-manager.js:398:22
    at Array.map (native)```

sathify avatar May 20 '17 21:05 sathify

Thanks for your issue, it will indeed be great to document this behavior.

Are you willing to contribute and open a PR for that?

xtuc avatar May 22 '17 14:05 xtuc

I've got a similar issue, I've added babel:false but not working.

			{
                test: /\.js(x)?$/,
                use: [{
                    loader: 'babel-loader',
                    options: {
                        babelrc: false,
                        presets: [
                            [
                                require('babel-preset-es2015'),
                            ],
                            require('babel-preset-stage-3'),
                        ],
                        plugins: [
                            require('babel-plugin-transform-object-rest-spread'),
                            require('babel-plugin-transform-class-properties'),
                            require('babel-plugin-syntax-dynamic-import'),
                        ],
                    },
                }],
                exclude: /node_module/,
            },

When I run webpack, it still read .babelrc options, what's wrong with it?

webpack version: 2.5.1 babel-loader: 7.0.0 babel-core: 6.24.1

Thanks!

iapYang avatar Jun 15 '17 07:06 iapYang

Any updates! @iapYang Did you figure out what's the problem ?

merraysy avatar Sep 19 '17 17:09 merraysy

I've ran into this issue yesterday (https://github.com/babel/babel-loader/issues/542) which was really not obvious and a pain to figure out

@xtuc Before being able to open a PR for the docs there are a few questions that need to be answered:

  • What is the motivation for the babelrc/package.json configs having higher priority than local loader options. As mentioned this seems unnatural
  • When having both - does the loader configuration get fully overwritten? Or is there some merging logic?
  • Is there any option to override babelrc/package.json settings from the loader? A use case for this would be having some common configuration externally (babelrc/package.json) and doing some overrides from loader options for different builds.

dkravcovs avatar Nov 15 '17 07:11 dkravcovs

I'm triaging old issues on babel-loader. Since this has been inactive for ages, I'm going to close it. Feel free to re-open if there's still something to discuss, but I'm assuming at this point it's been too long to address in a useful way.

Our new configuration merging logic in Babel 7 should make the need to disable .babelrc processing less common anyway.

loganfsmyth avatar Sep 02 '18 20:09 loganfsmyth