Mauro Bringolf Mauro Bringolf - 15 days ago 5
Javascript Question

How to add header comments to webpack uglified JavaScript?

I am currently using the following 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}) ]
}


This works exactly how I want it to. But now I want to add some comments with project info to the output file, on top of the one line with uglified code. How do I do this?

Answer

Add the comments to the code after magnification using Webpack's BannerPlugin():

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