Webpack2没有生成JS源图

时间:2017-04-26 15:56:44

标签: webpack webpack-2 source-maps

我似乎无法让Webpack2生成JS源图。我想也许删除Closure Compiler插件可以修复它,但是没有。

我的设置:

  • webpack 2.4.1
  • webpack-closure-compiler 2.1.4

命令我正在运行:

  

webpack -d --colors --progress

我的网络包配置:

const path = require('path')
const DefinePlugin = require('webpack').DefinePlugin
const CopyWebpackPlugin = require('copy-webpack-plugin')
const ClosureCompilerPlugin = require('webpack-closure-compiler')
const ExtractTextPlugin = require('extract-text-webpack-plugin')

module.exports = {
  devtool: 'source-map',
  entry: {
    'client-bundle': path.join(__dirname, 'src/index')
  },
  module: {
    rules: [
      {
        test: [ /\.jsx?$/ ],
        include: [/src/],
        loader: 'babel-loader',
        exclude: [/\.test.js$/]
      },
      { test: /\.json$/, loader: 'json-loader' },
      { test: /\.html?$/, loader: 'html-loader' },
      { test: /\.(css|sass)$/, loader: ExtractTextPlugin.extract({ fallback: 'style-loader', use: 'css-loader!postcss-loader!sass-loader' }) },
      { test: /\.(png|jpg|jpeg|gif|ico)$/, loader: 'file-loader?name=[path][name].[ext]' },
      { test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: 'url-loader?limit=10000&minetype=application/font-woff' },
      { test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: 'file-loader' }
    ]
  },
  output: {
    filename: '[name].js',
    library: '[name]',
    libraryTarget: 'this',
    path: path.join(__dirname, 'dist')
  },
  plugins: [
    new CopyWebpackPlugin([
      {from: path.join(__dirname, 'src/index.html')}
    ]),
    new ClosureCompilerPlugin({
      compiler: {
        language_in: 'ECMASCRIPT6',
        language_out: 'ECMASCRIPT5',
        compilation_level: 'SIMPLE'
      },
      concurrency: 3
    }),
    new DefinePlugin({
      'process.env': {
        NODE_ENV: JSON.stringify('development')
      }
    })
  ],
  target: 'web'
}

我已经尝试了devtool所示的所有可能值:https://webpack.js.org/configuration/devtool/。我已经尝试将create_source_map: true添加到Closure Compiler配置中的compiler对象。似乎没什么用。这不是权限问题,因为生成的包很好。

编辑:

所以我更改了webpack命令以使用-p选项而不是-d。这产生了一个错误:

ERROR in client.js from UglifyJs
TypeError: Cannot read property 'sections' of null

这很奇怪,因为我没有使用UglifyJS插件。 Odder,我只能通过删除使用Closure Compiler插件来消除错误。现在事情正确构建,并生成源映射,但我没有压缩。

1 个答案:

答案 0 :(得分:1)

我的Closure Compiler配置发生了一些问题(在我将webpack切换到-p命令行选项之后)。

  1. 由于Babel已经进行了转换,因此language_in属性需要设置为ECMASCRIPT5而不是ECMASCRIPT6
  2. 我还需要将create_source_map: true添加到compiler对象。
  3. 这是我的完整工作webpack配置(注意:我将"客户端捆绑"和#34;客户端")更改了捆绑包的名称:

    const path = require('path')
    const DefinePlugin = require('webpack').DefinePlugin
    const CopyWebpackPlugin = require('copy-webpack-plugin')
    const ClosureCompilerPlugin = require('webpack-closure-compiler')
    const ExtractTextPlugin = require('extract-text-webpack-plugin')
    
    module.exports = {
      devtool: 'source-map',
      entry: {
        'client': path.join(__dirname, 'src/index')
      },
      module: {
        rules: [
          {
            test: [ /\.jsx?$/ ],
            include: [/src/],
            loader: 'babel-loader',
            exclude: [/\.test.js$/, /node_modules/]
          },
          { test: /\.json$/, loader: 'json-loader' },
          { test: /\.html?$/, loader: 'html-loader' },
          { test: /\.(css|sass)$/, loader: ExtractTextPlugin.extract({ fallback: 'style-loader', use: 'css-loader!postcss-loader!sass-loader' }) },
          { test: /\.(png|jpg|jpeg|gif|ico)$/, loader: 'file-loader?name=[path][name].[ext]' },
          { test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: 'url-loader?limit=10000&minetype=application/font-woff' },
          { test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: 'file-loader' }
        ]
      },
      output: {
        filename: '[name].js',
        library: '[name]',
        libraryTarget: 'this',
        path: path.join(__dirname, 'dist')
      },
      plugins: [
        new CopyWebpackPlugin([
          {from: path.join(__dirname, 'src/index.html')}
        ]),
        new ClosureCompilerPlugin({
          compiler: {
            language_in: 'ECMASCRIPT5',
            language_out: 'ECMASCRIPT5',
            compilation_level: 'SIMPLE',
            create_source_map: true
          },
          concurrency: 3
        }),
        new DefinePlugin({
          'process.env': {
            NODE_ENV: JSON.stringify('development')
          }
        })
      ],
      target: 'node'
    }