简体   繁体   English

从Webpack v1迁移到v2

[英]Moving from webpack v1 to v2

I'm trying to migrate my code from webpack v1 to v2 and add in the sass-loader, however I get the error 我正在尝试将我的代码从webpack v1迁移到v2并添加sass-loader,但是出现错误

throw new WebpackOptionsValidationError(webpackOptionsValidationErrors);

I'm very confused as to what the final file is supposed to look like: 我对最终文件的外观感到非常困惑:

let webpack = require('webpack');
let path = require('path');


module.exports = {
    devtool: 'eval-source-map',
  entry: [
    './src/index'
  ],
  module: {

   rules: [
       {
           test: /\.scss$/,
           use: [
               "style-loader", // creates style nodes from JS strings
               "css-loader", // translates CSS into CommonJS
               "sass-loader" // compiles Sass to CSS
           ]
       }],

      test: /\.js?$/,
      loader: 'babel-loader',
      exclude: /node_modules/


 },

  resolve: {
    extensions: ['.js'],
      options: {
          enforceExtension: false
      }
  },

  output: {
    path: path.join(__dirname, '/dist'),
    publicPath: '/',
    filename: 'bundle.js'
  },

  devServer: {
    contentBase: './dist',
    hot: true,
      historyApiFallback: true
  },

  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoErrorsPlugin(),
      new webpack.LoaderOptionsPlugin({
          debug: true,
               options: {
           context: __dirname
         }
   })
  ]
};

At the moment the code is a mix of the two versions. 目前,代码是两个版本的混合。 I am using webpack version 2.2.1. 我正在使用2.2.1版的webpack。 Thanks. 谢谢。

There are several things you need to change: 您需要更改几件事:

Your test: /\\.js?$/ and the corresponding loader and exclude should be another object inside the rules array: 您的test: /\\.js?$/以及相应的loaderexclude对象应该是rules数组内的另一个对象:

module: {
  rules: [
    {
      test: /\.scss$/,
      use: [
        "style-loader", // creates style nodes from JS strings
        "css-loader", // translates CSS into CommonJS
        "sass-loader" // compiles Sass to CSS
      ]
    },
    {
      test: /\.js?$/,
      loader: 'babel-loader',
      exclude: /node_modules/
    }
  ]
},

resolve.options does not exist, it is just resolve.enforceExtension directly: resolve.options不存在,仅是resolve.enforceExtension

resolve: {
  extensions: ['.js'],
  enforceExtension: false
},

And finally, although it's not an error but just a warning, new webpack.NoErrorsPlugin() is deprecated and has been replaced with: 最后,尽管这不是错误,而只是警告,但不推荐使用new webpack.NoErrorsPlugin()并替换为:

new webpack.NoEmitOnErrorsPlugin()

Also if you haven't yet, you should have a look at the migration guide from the official docs https://webpack.js.org/guides/migrating/ . 另外,如果您还没有的话,还应该查看官方文档https://webpack.js.org/guides/migrating/中的迁移指南。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM