Socket
Socket
Sign inDemoInstall

uglifyjs-webpack-plugin

Package Overview
Dependencies
309
Maintainers
3
Versions
50
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.0-beta.0 to 1.0.0-beta.1

10

CHANGELOG.md

@@ -5,2 +5,12 @@ # Change Log

<a name="1.0.0-beta.1"></a>
# [1.0.0-beta.1](https://github.com/webpack-contrib/uglifyjs-webpack-plugin/compare/v1.0.0-beta.0...v1.0.0-beta.1) (2017-07-06)
### Features
* update to `uglify-es` ([#63](https://github.com/webpack-contrib/uglifyjs-webpack-plugin/issues/63)) ([1d62560](https://github.com/webpack-contrib/uglifyjs-webpack-plugin/commit/1d62560))
<a name="1.0.0-beta.0"></a>

@@ -7,0 +17,0 @@ # [1.0.0-beta.0](https://github.com/webpack-contrib/uglifyjs-webpack-plugin/compare/v0.1.4...v1.0.0-beta.0) (2017-06-29)

428

dist/index.js

@@ -12,11 +12,2 @@ 'use strict';

// TODO: temporarily disabled rules
/* eslint-disable
no-undefined,
no-param-reassign,
no-underscore-dangle,
import/order
*/
var _sourceMap = require('source-map');

@@ -34,5 +25,5 @@

var _uglifyJs = require('uglify-js');
var _uglifyEs = require('uglify-es');
var _uglifyJs2 = _interopRequireDefault(_uglifyJs);
var _uglifyEs2 = _interopRequireDefault(_uglifyEs);

@@ -43,2 +34,17 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/* eslint-disable
no-param-reassign
*/
var warningRegex = /\[.+:([0-9]+),([0-9]+)\]/;
var defaultUglifyOptions = {
output: {
comments: /^\**!|@preserve|@license|@cc_on/,
beautify: false,
semicolons: true,
shebang: true
}
};
var UglifyJsPlugin = function () {

@@ -49,9 +55,13 @@ function UglifyJsPlugin(options) {

if (typeof options !== 'object' || Array.isArray(options)) {
options = {};
this.options = {};
} else {
this.options = options || {};
}
if (typeof options.compressor !== 'undefined') {
options.compress = options.compressor;
}
this.options = options;
this.options.test = this.options.test || /\.js($|\?)/i;
this.options.warningsFilter = this.options.warningsFilter || function () {
return true;
};
this.uglifyOptions = this.options.uglifyOptions || {};
}

@@ -62,39 +72,37 @@

value: function apply(compiler) {
var options = this.options;
var _this = this;
options.test = options.test || /\.js($|\?)/i;
var warningsFilter = options.warningsFilter || function () {
return true;
};
var requestShortener = new _RequestShortener2.default(compiler.context);
// Copy uglify options
var uglifyOptions = UglifyJsPlugin.buildDefaultUglifyOptions(this.uglifyOptions);
// Making sure output options exists if there is an extractComments options
if (this.options.extractComments) {
uglifyOptions.output = uglifyOptions.output || {};
}
var requestShortener = new _RequestShortener2.default(compiler.context);
compiler.plugin('compilation', function (compilation) {
if (options.sourceMap) {
compilation.plugin('build-module', function (module) {
if (_this.options.sourceMap) {
compilation.plugin('build-module', function (moduleArg) {
// to get detailed location info about errors
module.useSourceMap = true;
moduleArg.useSourceMap = true;
});
}
compilation.plugin('optimize-chunk-assets', function (chunks, callback) {
var files = [];
// eslint-disable-next-line prefer-spread
chunks.forEach(function (chunk) {
return files.push.apply(files, chunk.files);
});
// eslint-disable-next-line prefer-spread
files.push.apply(files, compilation.additionalChunkAssets);
var filteredFiles = files.filter(_ModuleFilenameHelpers2.default.matchObject.bind(undefined, options));
var uglifiedAssets = new WeakSet();
filteredFiles.forEach(function (file) {
var oldWarnFunction = _uglifyJs2.default.AST_Node.warn_function;
var warnings = [];
chunks.reduce(function (acc, chunk) {
return acc.concat(chunk.files || []);
}, []).concat(compilation.additionalChunkAssets || []).filter(_ModuleFilenameHelpers2.default.matchObject.bind(null, _this.options)).forEach(function (file) {
// Reseting sourcemap to null
uglifyOptions.sourceMap = null;
var sourceMap = void 0;
var asset = compilation.assets[file];
if (uglifiedAssets.has(asset)) {
return;
}
try {
var asset = compilation.assets[file];
if (uglifiedAssets.has(asset)) {
return;
}
var input = void 0;
var inputSourceMap = void 0;
if (options.sourceMap) {
if (_this.options.sourceMap) {
if (asset.sourceAndMap) {

@@ -109,132 +117,59 @@ var sourceAndMap = asset.sourceAndMap();

sourceMap = new _sourceMap.SourceMapConsumer(inputSourceMap);
_uglifyJs2.default.AST_Node.warn_function = function (warning) {
// eslint-disable-line camelcase
var match = /\[.+:([0-9]+),([0-9]+)\]/.exec(warning);
var line = +match[1];
var column = +match[2];
var original = sourceMap.originalPositionFor({
line,
column
});
if (!original || !original.source || original.source === file) return;
if (!warningsFilter(original.source)) return;
warnings.push(`${warning.replace(/\[.+:([0-9]+),([0-9]+)\]/, '')}[${requestShortener.shorten(original.source)}:${original.line},${original.column}]`);
// Add source map data
uglifyOptions.sourceMap = {
content: inputSourceMap
};
} else {
input = asset.source();
_uglifyJs2.default.AST_Node.warn_function = function (warning) {
// eslint-disable-line camelcase
warnings.push(warning);
};
}
_uglifyJs2.default.base54.reset();
var ast = _uglifyJs2.default.parse(input, {
filename: file
});
if (options.compress !== false) {
ast.figure_out_scope();
var compress = _uglifyJs2.default.Compressor(options.compress || {
warnings: false
}); // eslint-disable-line new-cap
ast = compress.compress(ast);
}
if (options.mangle !== false) {
ast.figure_out_scope(options.mangle || {});
ast.compute_char_frequency(options.mangle || {});
ast.mangle_names(options.mangle || {});
if (options.mangle && options.mangle.props) {
_uglifyJs2.default.mangle_properties(ast, options.mangle.props);
// Handling comment extraction
var extractedComments = [];
var commentsFile = false;
if (_this.options.extractComments) {
uglifyOptions.output.comments = UglifyJsPlugin.buildCommentsFunction(_this.options, uglifyOptions, extractedComments);
commentsFile = _this.options.extractComments.filename || `${file}.LICENSE`;
if (typeof commentsFile === 'function') {
commentsFile = commentsFile(file);
}
}
var output = {};
output.comments = Object.prototype.hasOwnProperty.call(options, 'comments') ? options.comments : /^\**!|@preserve|@license/;
output.beautify = options.beautify;
for (var k in options.output) {
// eslint-disable-line guard-for-in
output[k] = options.output[k];
// Calling uglify
var _uglify$minify = _uglifyEs2.default.minify({ [file]: input }, uglifyOptions),
error = _uglify$minify.error,
map = _uglify$minify.map,
code = _uglify$minify.code,
warnings = _uglify$minify.warnings;
// Handling results
// Error case: add errors, and go to next file
if (error) {
compilation.errors.push(UglifyJsPlugin.buildError(error, file, sourceMap, compilation, requestShortener));
return;
}
var extractedComments = [];
if (options.extractComments) {
var condition = {};
if (typeof options.extractComments === 'string' || options.extractComments instanceof RegExp) {
// extractComments specifies the extract condition and output.comments specifies the preserve condition
condition.preserve = output.comments;
condition.extract = options.extractComments;
} else if (Object.prototype.hasOwnProperty.call(options.extractComments, 'condition')) {
// Extract condition is given in extractComments.condition
condition.preserve = output.comments;
condition.extract = options.extractComments.condition;
} else {
// No extract condition is given. Extract comments that match output.comments instead of preserving them
condition.preserve = false;
condition.extract = output.comments;
}
// Ensure that both conditions are functions
['preserve', 'extract'].forEach(function (key) {
switch (typeof condition[key]) {
case 'boolean':
{
var b = condition[key];
condition[key] = function () {
return b;
};
break;
}
case 'function':
break;
case 'string':
{
if (condition[key] === 'all') {
condition[key] = function () {
return true;
};
break;
}
var regex = new RegExp(condition[key]);
condition[key] = function (astNode, comment) {
return regex.test(comment.value);
};
break;
}
default:
{
var defaultRegex = condition[key];
condition[key] = function (astNode, comment) {
return defaultRegex.test(comment.value);
};
}
}
});
var outputSource = void 0;
if (map) {
outputSource = new _webpackSources.SourceMapSource(code, file, JSON.parse(map), input, inputSourceMap);
} else {
outputSource = new _webpackSources.RawSource(code);
}
// Redefine the comments function to extract and preserve
// comments according to the two conditions
output.comments = function (astNode, comment) {
if (condition.extract(astNode, comment)) {
extractedComments.push(comment.type === 'comment2' ? `/*${comment.value}*/` : `//${comment.value}`);
// Write extracted comments to commentsFile
if (commentsFile && extractedComments.length > 0) {
// Add a banner to the original file
if (_this.options.extractComments.banner !== false) {
var banner = _this.options.extractComments.banner || `For license information please see ${commentsFile}`;
if (typeof banner === 'function') {
banner = banner(commentsFile);
}
return condition.preserve(astNode, comment);
};
}
var map = void 0;
if (options.sourceMap) {
map = _uglifyJs2.default.SourceMap({ // eslint-disable-line new-cap
file,
root: ''
});
output.source_map = map; // eslint-disable-line camelcase
}
var stream = _uglifyJs2.default.OutputStream(output); // eslint-disable-line new-cap
ast.print(stream);
if (map) map += '';
var stringifiedStream = `${stream}`;
var outputSource = map ? new _webpackSources.SourceMapSource(stringifiedStream, file, JSON.parse(map), input, inputSourceMap // eslint-disable-line comma-dangle
) : new _webpackSources.RawSource(stringifiedStream);
if (extractedComments.length > 0) {
var commentsFile = options.extractComments.filename || `${file}.LICENSE`;
if (typeof commentsFile === 'function') {
commentsFile = commentsFile(file);
if (banner) {
outputSource = new _webpackSources.ConcatSource(`/*! ${banner} */\n`, outputSource);
}
}
// Write extracted comments to commentsFile
var commentsSource = new _webpackSources.RawSource(`${extractedComments.join('\n\n')}\n`);

@@ -252,36 +187,16 @@ if (commentsFile in compilation.assets) {

}
}
// Add a banner to the original file
if (options.extractComments.banner !== false) {
var banner = options.extractComments.banner || `For license information please see ${commentsFile}`;
if (typeof banner === 'function') {
banner = banner(commentsFile);
}
if (banner) {
outputSource = new _webpackSources.ConcatSource(`/*! ${banner} */\n`, outputSource);
}
}
}
// Updating assets
uglifiedAssets.add(compilation.assets[file] = outputSource);
if (warnings.length > 0) {
compilation.warnings.push(new Error(`${file} from UglifyJs\n${warnings.join('\n')}`));
}
} catch (err) {
if (err.line) {
var original = sourceMap && sourceMap.originalPositionFor({
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}][${file}:${err.line},${err.col}]`));
} else {
compilation.errors.push(new Error(`${file} from UglifyJs\n${err.message} [${file}:${err.line},${err.col}]`));
// Handling warnings
if (warnings) {
var warnArr = UglifyJsPlugin.buildWarnings(warnings, file, sourceMap, _this.options.warningsFilter, requestShortener);
if (warnArr.length > 0) {
compilation.warnings.push(new Error(`${file} from UglifyJs\n${warnArr.join('\n')}`));
}
} else if (err.msg) {
compilation.errors.push(new Error(`${file} from UglifyJs\n${err.msg}`));
} else {
compilation.errors.push(new Error(`${file} from UglifyJs\n${err.stack}`));
}
} finally {
_uglifyJs2.default.AST_Node.warn_function = oldWarnFunction; // eslint-disable-line camelcase
} catch (error) {
compilation.errors.push(UglifyJsPlugin.buildError(error, file, sourceMap, compilation, requestShortener));
}

@@ -293,2 +208,137 @@ });

}
}], [{
key: 'buildDefaultUglifyOptions',
value: function buildDefaultUglifyOptions(_ref) {
var ecma = _ref.ecma,
warnings = _ref.warnings,
_ref$parse = _ref.parse,
parse = _ref$parse === undefined ? {} : _ref$parse,
_ref$compress = _ref.compress,
compress = _ref$compress === undefined ? {} : _ref$compress,
mangle = _ref.mangle,
output = _ref.output,
toplevel = _ref.toplevel,
ie8 = _ref.ie8;
return {
ecma,
warnings,
parse,
compress,
mangle: mangle == null ? true : mangle,
// Ignoring sourcemap from options
sourceMap: null,
output: Object.assign({}, defaultUglifyOptions.output, output),
toplevel,
ie8
};
}
}, {
key: 'buildError',
value: function buildError(err, file, sourceMap, requestShortener) {
// Handling error which should have line, col, filename and message
if (err.line) {
var original = sourceMap && sourceMap.originalPositionFor({
line: err.line,
column: err.col
});
if (original && original.source) {
return new Error(`${file} from UglifyJs\n${err.message} [${requestShortener.shorten(original.source)}:${original.line},${original.column}][${file}:${err.line},${err.col}]`);
}
return new Error(`${file} from UglifyJs\n${err.message} [${file}:${err.line},${err.col}]`);
} else if (err.msg) {
return new Error(`${file} from UglifyJs\n${err.msg}`);
}
return new Error(`${file} from UglifyJs\n${err.stack}`);
}
}, {
key: 'buildWarnings',
value: function buildWarnings(warnings, file, sourceMap, warningsFilter, requestShortener) {
if (!sourceMap) {
return warnings;
}
return warnings.reduce(function (accWarnings, warning) {
var match = warningRegex.exec(warning);
var line = +match[1];
var column = +match[2];
var original = sourceMap.originalPositionFor({
line,
column
});
if (original && original.source && original.source !== file && warningsFilter(original.source)) {
accWarnings.push(`${warning.replace(warningRegex, '')}[${requestShortener.shorten(original.source)}:${original.line},${original.column}]`);
}
return accWarnings;
}, []);
}
}, {
key: 'buildCommentsFunction',
value: function buildCommentsFunction(options, uglifyOptions, extractedComments) {
var condition = {};
var commentsOpts = uglifyOptions.output.comments;
if (typeof options.extractComments === 'string' || options.extractComments instanceof RegExp) {
// extractComments specifies the extract condition and commentsOpts specifies the preserve condition
condition.preserve = commentsOpts;
condition.extract = options.extractComments;
} else if (Object.prototype.hasOwnProperty.call(options.extractComments, 'condition')) {
// Extract condition is given in extractComments.condition
condition.preserve = commentsOpts;
condition.extract = options.extractComments.condition;
} else {
// No extract condition is given. Extract comments that match commentsOpts instead of preserving them
condition.preserve = false;
condition.extract = commentsOpts;
}
// Ensure that both conditions are functions
['preserve', 'extract'].forEach(function (key) {
var regexStr = void 0;
var regex = void 0;
switch (typeof condition[key]) {
case 'boolean':
condition[key] = condition[key] ? function () {
return true;
} : function () {
return false;
};
break;
case 'function':
break;
case 'string':
if (condition[key] === 'all') {
condition[key] = function () {
return true;
};
break;
}
if (condition[key] === 'some') {
condition[key] = function (astNode, comment) {
return comment.type === 'comment2' && /@preserve|@license|@cc_on/i.test(comment.value);
};
break;
}
regexStr = condition[key];
condition[key] = function (astNode, comment) {
return new RegExp(regexStr).test(comment.value);
};
break;
default:
regex = condition[key];
condition[key] = function (astNode, comment) {
return regex.test(comment.value);
};
}
});
// Redefine the comments function to extract and preserve
// comments according to the two conditions
return function (astNode, comment) {
if (condition.extract(astNode, comment)) {
extractedComments.push(comment.type === 'comment2' ? `/*${comment.value}*/` : `//${comment.value}`);
}
return condition.preserve(astNode, comment);
};
}
}]);

@@ -295,0 +345,0 @@

{
"name": "uglifyjs-webpack-plugin",
"version": "1.0.0-beta.0",
"version": "1.0.0-beta.1",
"description": "UglifyJS plugin for webpack",

@@ -34,3 +34,3 @@ "author": "webpack Contrib Team",

"source-map": "^0.5.6",
"uglify-js": "^2.8.29",
"uglify-es": "^3.0.21",
"webpack-sources": "^1.0.1"

@@ -55,3 +55,2 @@ },

"standard-version": "^4.1.0",
"uglify-js": "^2.8.18",
"webpack": "^3.0.0",

@@ -58,0 +57,0 @@ "webpack-defaults": "^1.4.0"

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc