Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack babel-loader need babel.config.js or not?

I have a webpack setting like below

test: /\.m?js$/,
exclude: /(node_modules|bower_components)/,
use: {
  loader: 'babel-loader?cacheDirectory',
  options: {
    presets: [
      '@babel/preset-env',
      '@babel/react', // Error: Plugin/Preset files are not allowed to export objects, only functions.
    ],
    plugins: [
      [
        '@babel/plugin-proposal-decorators',
        {
          legacy: true,
        },
      ],
      [
        '@babel/plugin-proposal-class-properties',
        {
          loose: true,
        },
      ],
    ],
  },
},

So do i still need add a file babel.config.js or .babelrc to double the config again?

if i use vscode, do i need this file babel.config.js for some plugin?

like image 579
aboutjquery Avatar asked Sep 29 '18 04:09

aboutjquery


People also ask

What is the use of Babel config JS?

config. json is useful if you have multiple packages (ie multiple package. json) directories in your project that utilize a single babel config.

Can we use Webpack without Babel?

No unless all dependencies of your project supports ES6. There is one serious incompatibility: import is asynchronous while require() is synchronous.

Do I need Babel with TS loader?

If you need custom transformations, you'll need to use Babel. The good news is that most TypeScript tools allow you to both use TypeScript and then run the code through Babel afterwards, to get the best of both worlds. But this obviously comes with additional complexity in your build-chain.


1 Answers

No that is redundant. I see you are using the example in babel-loader docs. I'd recommend to use the example in official babel docs (click on webpack). That keeps your webpack config simple and allows over easy overrides of babel in folders by placing a .babelrc in directories where'd you like different settings.

.babelrc

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

Your webpack config file

module: {
    loaders: [
        {
            test: /\.js$/, exclude: /node_modules/, loader: "babel-loader"
        }
    ]
}

Considering that error: make sure you've at least installed @babel/preset-react and @babel/preset-env, @babel/core and @babel/plugin-proposal-class-properties! You might be using babel 6 plugins. Could you share your package.json?

like image 142
axm__ Avatar answered Jan 02 '23 20:01

axm__