如何将 webpack 自定义配置与 react js 一起使用?

How to use webpack custom config with react js?

我正在使用一个自定义的 webpack 配置文件,它是:

var webpack = require('webpack');
var path = require('path');
module.exports = {
    entry: {
        app: './src/index.js'
    },
    output: {
        filename: 'dist/bundle.js',
        sourceMapFilename: 'dist/bundle.map'
    },
    devtool: '#source-map',
    module: {
        loaders: [
            {
                loader: 'babel-loader',
                exclude: /(node_modules)/,
                query: {
                    presets: [ 'react', 'es2015' ]
                }
            }
        ]
    }
};

当我在终端中 运行wepback 时抛出错误:

Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
 - configuration.module has an unknown property 'loaders'. These properties are valid:
   object { defaultRules?, exprContextCritical?, exprContextRecursive?, exprContextRegExp?, exprContextRequest?, noParse?, rules?, strictExportPresence?, strictThisContextOnImports?, unknownContextCritical?, unknownContextRecursive?, unknownContextRegExp?, unknownContextRequest?, unsafeCache?, wrappedContextCritical?, wrappedContextRecursive?, wrappedContextRegExp? }
   -> Options affecting the normal modules (`NormalModuleFactory`).

如何正确使用 webpack 的自定义配置文件并修复此错误?

要使用 webpack 的加载程序,您应该添加 rules 属性 以及指示要转换哪些文件的测试正则表达式。

 module: {
    rules: [
        {
            test: /\.(jsx|js)$/
            exclude: /node_modules/,
            use: {
                loader: 'babel-loader',
                options: {
                   presets: [ 'react', 'es2015' ]
                 } 
            }
        }
    ]
}

要了解更多和准确的信息,请查看与您使用的版本相关的 documentation