如何将标题注释添加到webpack uglified JavaScript?

时间:2016-11-19 09:58:04

标签: javascript webpack babeljs minify uglifyjs

我目前正在使用以下webpack.config.js:

var webpack = require('webpack');

module.exports = {
  entry: __dirname + "/src/index.js",
    output: {
      path: __dirname,
      filename: "index.js"
    },
  module: {
    loaders: [
      {
        test: /\.js$/,
        loader: 'babel',
        exclude: '/node_modules/',
        query: {
          presets: ['latest']
        }
      }
    ]
  },
  plugins: [ new webpack.optimize.UglifyJsPlugin({minimize: true}) ]
}

这正是我想要的方式。但是现在我想在输出文件中添加一些带有项目信息的注释,并在一行中添加了uglified代码。我该怎么做?

1 个答案:

答案 0 :(得分:7)

使用Webpack BannerlPlugin()缩小后将注释添加到代码中:

Webpack 1 - BannerPlugin()

plugins: [
    new webpack.BannerPlugin("The head comment", { entryOnly: true })
]

Webpack 2 - BannerPlugin()

plugins: [
    new webpack.BannerPlugin({ 
        banner: "The head comment", 
        entryOnly: true 
    })
]