簡體   English   中英

Webpack - 錯誤:無法在加載器列表中定義“查詢”和多個加載器

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

在本教程之后我在陣列中添加react-hot loader后出現此錯誤: https//thoughtbot.com/blog/setting-up-webpack-for-react-and-hot-module-replacement

我收到Error: Cannot define 'query' and multiple loaders in loaders list

var WebpackDevServer = require("webpack-dev-server");
var webpack = require('webpack');
var path = require('path');
require("babel-polyfill");

var BUILD_DIR = path.resolve(__dirname, 'build');
var APP_DIR = path.resolve(__dirname, 'src');

module.exports = {
  entry: [
    'babel-polyfill',
    'bootstrap-loader',
    'webpack/hot/dev-server',
    APP_DIR + '/import.js',
  ],
  output: {
    path: BUILD_DIR,
    filename: 'bundle.js'
  },
  module: {
    loaders: [{
      test: /\.jsx?$/,
      loaders: ['react-hot', 'babel'],
      exclude: /node_modules/,
      query: {
        plugins: ['transform-runtime'],
        presets: ['es2015', 'stage-0', 'react']
      }
    }, {
      test: /\.css$/,
      loader: "style-loader!css-loader"
    }, {
      test: /\.scss$/,
      loaders: ["style", "css", "sass"]
    }, {
      test: /\.(png|woff|woff2|eot|ttf|svg|jpg|gif)$/,
      loader: 'url-loader?limit=100000'
    }]
  },
  plugins: [
    new webpack.ProvidePlugin({
      $: "jquery",
      jQuery: "jquery"
    }),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoErrorsPlugin()
  ]
};

看起來查詢是一種自定義單個加載器行為的替代方法,比內聯指定這些參數更簡潔(見下文)。 如果存在多個加載器,則Webpack不知道query配置適用於哪個。

以下應解決您的問題:

module: {
    loaders: [{
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loaders: ['react-hot', 'babel?presets[]=es2015,presets[]=stage-0,presets[]=react,plugins[]=transform-runtime']
    }

編輯:雖然此解決方案適用於Webpack 1,但請參閱其他答案,了解適用於更新版本的更清潔解決方案。

我的解決方案

loaders: [{
  test: /\.(js|jsx)$/,
  loaders: ['react-hot', 'babel?' + JSON.stringify({
    cacheDirectory: true,
    plugins: [
      'transform-runtime',
      'transform-decorators-legacy'
    ],
    presets: ['es2015', 'react', 'stage-0'],
    env: {
      production: {
        presets: ['react-optimize']
      }
    }
  }), 'eslint'],
  include: src,
  exclude: /node_modules/
}

webpack 2和3中,這可以更加干凈地配置。

加載器可以在一組加載器對象中傳遞。 每個加載器對象都可以指定一個options對象,其作用類似於該特定加載器的webpack 1 query

例如,使用react-hot-loaderbabel-loader ,以及配置了一些選項的babel-loader ,在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'
        ]
      }
    }]
  }] 
}

為了比較,這里使用查詢字符串方法在webpack 1中使用相同的配置。

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

注意鏈中所有已更改的屬性名稱。

另請注意,我將es2015預設更改為babel-loader配置中預設的es2015-native-modules 這與options的規范無關,只是包括es6模塊允許你使用v2中引入的webpack樹抖動功能。 它可以保持獨立,它仍然可以工作,但如果沒有指出明顯的升級,答案會感覺不完整:-)


免責聲明:這與我對類似問題的回答相同 ,但這個問題有類似的投票/觀點/谷歌排名,所以我也會在這里發布答案。


對於webpack 2 我設法像這樣配置:



    var webpack = require("webpack");
    var path = require("path");

    module.exports = {
        entry: "./src/index.js",
        output: {
            path: path.resolve(__dirname, "dist/assets"),
            filename: "bundle.js",
            publicPath: "/assets/"
        },
        devServer: {
            inline: true,
            contentBase: './dist',
            port: 3000
        },
        module: {
            loaders: [
                {
                    test: /\.js$/,
                    exclude: /(node_modules)/,
                    loader: "babel-loader",
                    options: {
                        presets: ['latest', 'react', 'stage-0']
                    }
                }
            ]
        }
    };

這個解決方案對我有用:

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

和.babelrc中的預設

{
    'presets': ['latest', 'react', 'stage-0']
}

請參閱https://webpack.github.io/docs/usage.html

自從我為自己找到解決方案后,我遇到了同樣的問題。 你可以嘗試一下:

---這是解決方案---

如果您在“.babelrc”文件中定義了“預設”,則無需在“webpack.config.js”文件中指定它,然后將其刪除並且運行良好


如果你不這樣做,我建議你去你的“.babelrc”文件並在那里指定你的預設

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM