2013-01-31 01:49:25 +08:00
/ *
MIT License http : //www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @ sokra
* /
2013-09-14 17:50:39 +08:00
var SourceMapConsumer = require ( "webpack-core/lib/source-map" ) . SourceMapConsumer ;
2013-03-26 23:54:41 +08:00
var SourceMapSource = require ( "webpack-core/lib/SourceMapSource" ) ;
2014-10-31 19:46:18 +08:00
var RawSource = require ( "webpack-core/lib/RawSource" ) ;
2013-09-14 17:50:39 +08:00
var RequestShortener = require ( "../RequestShortener" ) ;
2015-03-21 02:00:39 +08:00
var ModuleFilenameHelpers = require ( "../ModuleFilenameHelpers" ) ;
2013-01-31 01:49:25 +08:00
var uglify = require ( "uglify-js" ) ;
2013-12-18 06:21:49 +08:00
function UglifyJsPlugin ( options ) {
2013-09-14 17:50:39 +08:00
if ( typeof options !== "object" ) options = { } ;
if ( typeof options . compressor !== "undefined" ) {
options . compress = options . compressor ;
2013-01-31 01:49:25 +08:00
}
this . options = options ;
}
module . exports = UglifyJsPlugin ;
2014-10-30 18:29:33 +08:00
2013-01-31 01:49:25 +08:00
UglifyJsPlugin . prototype . apply = function ( compiler ) {
var options = this . options ;
2014-10-30 18:29:33 +08:00
options . test = options . test || /\.js($|\?)/i ;
2013-12-18 06:21:49 +08:00
var requestShortener = new RequestShortener ( compiler . context ) ;
2013-01-31 01:49:25 +08:00
compiler . plugin ( "compilation" , function ( compilation ) {
2014-10-31 19:46:18 +08:00
if ( options . sourceMap !== false ) {
compilation . plugin ( "build-module" , function ( module ) {
// to get detailed location info about errors
module . useSourceMap = true ;
} ) ;
}
2013-01-31 01:49:25 +08:00
compilation . plugin ( "optimize-chunk-assets" , function ( chunks , callback ) {
var files = [ ] ;
chunks . forEach ( function ( chunk ) {
chunk . files . forEach ( function ( file ) {
files . push ( file ) ;
} ) ;
} ) ;
2013-07-01 19:59:02 +08:00
compilation . additionalChunkAssets . forEach ( function ( file ) {
files . push ( file ) ;
} ) ;
2015-03-21 02:00:39 +08:00
files = files . filter ( ModuleFilenameHelpers . matchObject . bind ( undefined , options ) ) ;
2013-01-31 01:49:25 +08:00
files . forEach ( function ( file ) {
2013-09-14 17:50:39 +08:00
var oldWarnFunction = uglify . AST _Node . warn _function ;
var warnings = [ ] ;
2013-01-31 01:49:25 +08:00
try {
2013-07-04 17:55:37 +08:00
var asset = compilation . assets [ file ] ;
var input = asset . source ( ) ;
if ( asset . _ _UglifyJsPlugin ) {
compilation . assets [ file ] = asset . _ _UglifyJsPlugin ;
return ;
}
2014-10-31 19:46:18 +08:00
if ( options . sourceMap !== false ) {
var inputSourceMap = asset . map ( ) ;
var sourceMap = new SourceMapConsumer ( inputSourceMap ) ;
uglify . AST _Node . warn _function = function ( warning ) {
var match = /\[.+:([0-9]+),([0-9]+)\]/ . exec ( warning ) ;
var line = + match [ 1 ] ;
var column = + match [ 2 ] ;
var original = sourceMap . originalPositionFor ( {
line : line ,
column : column
} ) ;
if ( ! original || ! original . source || original . source === file ) return ;
warnings . push ( warning . replace ( /\[.+:([0-9]+),([0-9]+)\]/ , "" ) +
"[" + requestShortener . shorten ( original . source ) + ":" + original . line + "," + original . column + "]" ) ;
} ;
} else {
uglify . AST _Node . warn _function = function ( warning ) {
warnings . push ( warning ) ;
} ;
}
2013-01-31 01:49:25 +08:00
var ast = uglify . parse ( input , {
filename : file
} ) ;
2013-09-14 17:50:39 +08:00
if ( options . compress !== false ) {
2014-10-31 19:46:18 +08:00
ast . figure _out _scope ( )
2013-09-14 17:50:39 +08:00
var compress = uglify . Compressor ( options . compress ) ;
ast = ast . transform ( compress ) ;
2014-10-31 19:46:18 +08:00
}
if ( options . mangle !== false ) {
2013-01-31 01:49:25 +08:00
ast . figure _out _scope ( ) ;
2014-10-31 19:46:18 +08:00
ast . compute _char _frequency ( options . mangle || { } ) ;
ast . mangle _names ( options . mangle || { } ) ;
2013-01-31 01:49:25 +08:00
}
2014-05-14 05:04:42 +08:00
var output = { } ;
output . comments = options . comments || /^\**!|@preserve|@license/ ;
output . beautify = options . beautify ;
for ( var k in options . output ) {
output [ k ] = options . output [ k ] ;
}
2014-10-31 19:46:18 +08:00
if ( options . sourceMap !== false ) {
var map = uglify . SourceMap ( {
file : file ,
root : ""
} ) ;
output . source _map = map ;
}
2014-05-14 05:04:42 +08:00
var stream = uglify . OutputStream ( output ) ;
2013-03-26 23:54:41 +08:00
ast . print ( stream ) ;
2014-10-31 19:46:18 +08:00
if ( map ) map = map + "" ;
2013-03-26 23:54:41 +08:00
stream = stream + "" ;
2014-10-31 19:46:18 +08:00
asset . _ _UglifyJsPlugin = compilation . assets [ file ] = ( map ?
new SourceMapSource ( stream , file , map , input , inputSourceMap ) :
new RawSource ( stream ) ) ;
2013-09-14 17:50:39 +08:00
if ( warnings . length > 0 ) {
compilation . warnings . push ( new Error ( file + " from UglifyJs\n" + warnings . join ( "\n" ) ) ) ;
}
2013-01-31 01:49:25 +08:00
} catch ( err ) {
2013-12-17 07:53:22 +08:00
if ( err . line ) {
2014-10-31 19:46:18 +08:00
var original = sourceMap && sourceMap . originalPositionFor ( {
2013-12-17 07:53:22 +08:00
line : err . line ,
column : err . col
} ) ;
if ( original && original . source ) {
compilation . errors . push ( new Error ( file + " from UglifyJs\n" + err . message + " [" + requestShortener . shorten ( original . source ) + ":" + original . line + "," + original . column + "]" ) ) ;
} else {
compilation . errors . push ( new Error ( file + " from UglifyJs\n" + err . message + " [" + file + ":" + err . line + "," + err . col + "]" ) ) ;
}
2015-02-21 17:29:53 +08:00
} else if ( err . msg ) {
compilation . errors . push ( new Error ( file + " from UglifyJs\n" + err . msg ) ) ;
2013-12-17 07:53:22 +08:00
} else
2014-07-26 19:59:30 +08:00
compilation . errors . push ( new Error ( file + " from UglifyJs\n" + err . stack ) ) ;
2013-09-14 17:50:39 +08:00
} finally {
uglify . AST _Node . warn _function = oldWarnFunction ;
2013-01-31 01:49:25 +08:00
}
} ) ;
callback ( ) ;
} ) ;
2013-05-13 04:44:10 +08:00
compilation . plugin ( "normal-module-loader" , function ( context ) {
context . minimize = true ;
} ) ;
2013-01-31 01:49:25 +08:00
} ) ;
2014-10-30 18:29:33 +08:00
} ;