Webpack - Error: Cannot define 'query' and multiple loaders in loaders list

前端 未结 6 706
梦谈多话
梦谈多话 2021-01-30 16:28

This error appeared after I added the react-hot loader in an array following this tutorial: https://thoughtbot.com/blog/setting-up-webpack-for-react-and-hot-module-

6条回答
  •  借酒劲吻你
    2021-01-30 17:03

    In webpack 2 & 3 this can be configured much more cleanly.

    Loaders can be passed in an array of loader objects. Each loader object can specify an options object that acts like the webpack 1 query for that particular loader.

    For example, using both react-hot-loader and babel-loader, with babel-loader configured with some options, in webpack 2/3

    module: {
      rules: [{
        test: /\.js$/,
        exclude: /node_modules/,
        use: [{
          loader: 'react-hot-loader'
        }, {
          loader: 'babel-loader',
          options: {
            babelrc: false,
            presets: [
              'es2015-native-modules'
              'stage-0',
              'react'
            ]
          }
        }]
      }] 
    }
    

    For comparison, here is the same configuration in webpack 1, using the query string method.

    module: {
      rules: [{
        test: /\.js$/,
        exclude: /node_modules/,
        loaders: [
          'react-hot',
          'babel-loader?' +
            'babelrc=false,' +
            'presets[]=es2015,' +
            'presets[]=stage-0,' +
            'presets[]=react'
          ]
      }] 
    }
    

    Notice the changed property names all down the chain.

    Also, note that I changed the es2015 preset to es2015-native-modules preset in the babel-loader configuration. This has nothing to do with the specification of options, it's just that including es6 modules allows you to use webpack tree-shaking feature introduced in v2. It could be left alone and it would still work, but the answer would feel incomplete without that obvious upgrade being pointed out :-)


    Disclaimer: This is the same as my answer to a similar question, but this question has similar votes/views/google ranking, so I'll post the answer here too.


提交回复
热议问题