从web pack v1.15 config迁移到v2.6.1

时间:2017-06-14 09:13:29

标签: reactjs webpack

将webpack更新到最新版本(2.6.1),因此样板文件附带的webpack配置文件已经过时......

查看migration上的官方文档,但是我仍然需要更新我的配置文件有点丢失:

'use strict';

const path = require('path');
const webpack = require('webpack');
const NODE_ENV = process.env.NODE_ENV;
const SaveAssetsJson = require('assets-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');

module.exports = {
  devtool: '#source-map',

  // Capture timing information for each module
  profile: false,

  // Switch loaders to debug mode
  debug: false,

  // Report the first error as a hard error instead of tolerating it
  bail: true,

  entry: [
    'babel-polyfill',
    './assets/main.jsx',
  ],

  output: {
    path: 'public/dist/',
    pathInfo: true,
    publicPath: '/dist/',
    filename: 'bundle.[hash].min.js',
  },

  resolve: {
    root: path.join(__dirname, ''),
    modulesDirectories: [
      'web_modules',
      'node_modules',
      'assets',
      'assets/components',
    ],
    extensions: ['', '.webpack.js', '.web.js', '.js', '.jsx'],
  },

  resolveLoader: {

  },

  plugins: [
    new CleanWebpackPlugin(['public/dist'], {
      verbose: true,
      dry: false,
    }),
    new webpack.optimize.UglifyJsPlugin({
      minimize: true,
      output: {
        comments: false,
      },
      compress: {
        warnings: false,
        screw_ie8: true,
      },
    }),
    new SaveAssetsJson({
      path: process.cwd(),
      filename: 'assets.json',
    }),
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: JSON.stringify('production'),
      },
    }),
  ],
  query: {presets: ['es2015', 'react'] },
  module: {

    rules: [
      {
        use: [
          "style-loader",
          "css-loader",
          "autoprefixer-loader",
        ]
      },
      {
        test: /\.scss$/, // sass files
        loader: 'style!css!autoprefixer?browsers=last 2 version!sass?outputStyle=expanded',
      },
      {
        test: /\.(ttf|eot|svg|woff)(\?[a-z0-9]+)?$/, // fonts files
        loader: 'file-loader?name=[path][name].[ext]',
      },
      {
        test: /\.jsx?$/, // react files
        exclude: /node_modules/,
        loaders: ['babel?presets[]=es2015,presets[]=stage-0,presets[]=react'],
        include: path.join(__dirname, 'assets'),
      },

    ],

    noParse: /\.min\.js/,
  }

};

1 个答案:

答案 0 :(得分:0)

最后几乎没有删除已弃用的插件调用并重组某些字段。这是2.6.1兼容版本,已更改的文件部分:

    'use strict';


module.exports = {
  devtool: '#source-map',

  // Capture timing information for each module
  profile: false,

  // Switch loaders to debug mode
  //debug: false,

  // Report the first error as a hard error instead of tolerating it
  bail: true,

  entry: [
    'babel-polyfill',
    './assets/main.jsx',
  ],

  output: {
    path: '/public/dist/', //This has to be an absolute path
    publicPath: '/dist/',
    filename: 'bundle.[hash].min.js',
  },

  resolve: {
  //merging root and modules
    modules: [
      path.join(__dirname, ''),
      'web_modules',
      'node_modules',
      'assets',
      'assets/components',
    ],
    extensions: [ '.webpack.js', '.web.js', '.js', '.jsx'], //Removed empty entry
  },

  resolveLoader: {

  },

  plugins: [
    new CleanWebpackPlugin(['public/dist'], {
      verbose: true,
      dry: false,
    }),
    new webpack.optimize.UglifyJsPlugin({
      minimize: true,
      sourceMap: true, //added this
      output: {
        comments: false,
      },
      compress: {
        warnings: false,
        screw_ie8: true,
      },
    }),
    new SaveAssetsJson({
      path: process.cwd(),
      filename: 'assets.json',
    }),
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: JSON.stringify('production'),
      },
    }),
  ],
  module: {
//Modules are slightly differently listed now
    rules: [
      {
            test: /\.(css|scss)$/,
            use: [{
                loader: "style-loader" // creates style nodes from JS strings
            }, {
                loader: "css-loader" // translates CSS into CommonJS
            }, {
                loader: "sass-loader" // compiles Sass to CSS
            }]
      },
      {
      test: /\.(js|jsx)$/,
      exclude: /(node_modules|bower_components)/,
      use: {
        loader: 'babel-loader',
        options: {
          presets: ['es2015', "stage-0", 'react']
        }
      }
    }

    ],

    noParse: /\.min\.js/,
  }

};