Webpack 2 devtool无法正常工作

时间:2017-01-30 18:20:18

标签: javascript node.js webpack webpack-2

我尝试从webpack 1转换为2但webpack似乎没有使用devtool: 'source-map'生成源地图文件。如果我使用以下配置运行webpack,bundle.js文件在/public中按预期创建,但没有bundle.js.map文件,也没有错误。使用webpack 1,它按预期工作:

const webpack = require('webpack')

module.exports = {
  entry: ['babel-polyfill', './src/index.js'],
  output: {
    path: './public',
    filename: 'bundle.js'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader'
      }
    ]
  },
  devtool: 'source-map',
  plugins: [
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
        API_BASE_URL: `"${process.env.API_BASE_URL || '/api/v1'}"`
      }
    }),
    new webpack.optimize.UglifyJsPlugin()
  ]
}

CLI输出:

> cross-env NODE_ENV=production webpack
Hash: 738da5a3824ffac20236
Version: webpack 2.2.0
Time: 13819ms
    Asset    Size  Chunks                    Chunk Names
bundle.js  611 kB       0  [emitted]  [big]  main
   [2] ./~/react/react.js 56 bytes {0} [built]

1 个答案:

答案 0 :(得分:7)

看起来如果你正在使用UglifyJSPlugin源代码地图会被静默禁用,除非你在那里重新启用它们:

plugins: [    
    new webpack.optimize.UglifyJsPlugin({sourceMap: true})
]