programing

웹 팩 - 오류: 로더 목록에 '쿼리' 및 여러 로더를 정의할 수 없습니다.

lastcode 2023. 3. 18. 08:41
반응형

웹 팩 - 오류: 로더 목록에 '쿼리' 및 여러 로더를 정의할 수 없습니다.

이 에러는, 을 추가한 후에 표시됩니다.react-hot다음 튜토리얼에 따라 배열 내의 로더: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()
  ]
};

쿼리는 단일 로더의 동작을 커스터마이즈하기 위한 대체 방법인 것 같습니다.이러한 파라미터는 인라인으로 지정하는 것보다 깔끔합니다(아래 참조).여러 로더가 존재하는 경우 웹 팩은 어떤 로더가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/
}

Web pack 2 및3 에서는, 보다 깔끔하게 설정할 수 있습니다.

로더는 로더 객체의 배열로 전달할 수 있습니다.각 로더 오브젝트는,options웹 팩1과 같은 기능을 하는 오브젝트query특정 로더에 대해서요.

예를 들어, 둘 다 사용react-hot-loader그리고.babel-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'
        ]
      }
    }]
  }] 
}

비교를 위해 쿼리 문자열 방식을 사용한 웹 팩1의 동일한 설정을 다음에 나타냅니다.

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

체인 전체에 걸쳐 변경된 속성 이름에 주목하십시오.

또, 이 경우는,es2015로 미리 설정하다.es2015-native-modules에 프리셋되어 있다babel-loader배열.이것은, 다음의 사양과는 무관합니다.options다만 es6 모듈을 포함하면 v2에서 도입된 웹 팩트리 트레이닝 기능을 사용할 수 있습니다.그대로 둘 수도 있고 계속 작동하지만 명백한 업그레이드가 지적되지 않으면 답변이 불완전하다고 느낄 수 있습니다:-)


면책사항:비슷한 질문에 대한 답변과 동일하지만, 이 질문은 투표/뷰/구글 순위가 비슷하기 때문에 여기에 답변을 게시합니다.


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" 파일에서 "presets"를 정의한 경우 "webpack.config.js" 파일에서 "presets"를 지정할 필요가 없습니다.그 후 삭제하면 정상적으로 동작합니다.


그렇지 않은 경우 ".babelrc" 파일로 이동하여 사전 설정을 지정할 것을 권장합니다.

언급URL : https://stackoverflow.com/questions/35266706/webpack-error-cannot-define-query-and-multiple-loaders-in-loaders-list

반응형