aboutsummaryrefslogtreecommitdiff
path: root/node_modules/uglifyjs-webpack-plugin/dist/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/uglifyjs-webpack-plugin/dist/index.js')
-rw-r--r--node_modules/uglifyjs-webpack-plugin/dist/index.js619
1 files changed, 375 insertions, 244 deletions
diff --git a/node_modules/uglifyjs-webpack-plugin/dist/index.js b/node_modules/uglifyjs-webpack-plugin/dist/index.js
index b3790e62b..7b87cfa6f 100644
--- a/node_modules/uglifyjs-webpack-plugin/dist/index.js
+++ b/node_modules/uglifyjs-webpack-plugin/dist/index.js
@@ -1,253 +1,384 @@
-/*
- MIT License http://www.opensource.org/licenses/mit-license.php
- Author Tobias Koppers @sokra
-*/
-"use strict";
+'use strict';
-var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
+Object.defineProperty(exports, "__esModule", {
+ value: true
+});
-var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
+var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /* eslint-disable
+ no-param-reassign
+ */
+
+
+var _crypto = require('crypto');
+
+var _crypto2 = _interopRequireDefault(_crypto);
+
+var _path = require('path');
+
+var _path2 = _interopRequireDefault(_path);
+
+var _sourceMap = require('source-map');
+
+var _webpackSources = require('webpack-sources');
+
+var _RequestShortener = require('webpack/lib/RequestShortener');
+
+var _RequestShortener2 = _interopRequireDefault(_RequestShortener);
+
+var _ModuleFilenameHelpers = require('webpack/lib/ModuleFilenameHelpers');
+
+var _ModuleFilenameHelpers2 = _interopRequireDefault(_ModuleFilenameHelpers);
+
+var _schemaUtils = require('schema-utils');
+
+var _schemaUtils2 = _interopRequireDefault(_schemaUtils);
+
+var _options = require('./options.json');
+
+var _options2 = _interopRequireDefault(_options);
+
+var _Runner = require('./uglify/Runner');
+
+var _Runner2 = _interopRequireDefault(_Runner);
+
+function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
-var SourceMapConsumer = require("source-map").SourceMapConsumer;
-var SourceMapSource = require("webpack-sources").SourceMapSource;
-var RawSource = require("webpack-sources").RawSource;
-var ConcatSource = require("webpack-sources").ConcatSource;
-var RequestShortener = require("webpack/lib/RequestShortener");
-var ModuleFilenameHelpers = require("webpack/lib/ModuleFilenameHelpers");
-var uglify = require("uglify-js");
+var warningRegex = /\[.+:([0-9]+),([0-9]+)\]/;
var UglifyJsPlugin = function () {
- function UglifyJsPlugin(options) {
- _classCallCheck(this, UglifyJsPlugin);
-
- if ((typeof options === "undefined" ? "undefined" : _typeof(options)) !== "object" || Array.isArray(options)) options = {};
- if (typeof options.compressor !== "undefined") options.compress = options.compressor;
- this.options = options;
- }
-
- _createClass(UglifyJsPlugin, [{
- key: "apply",
- value: function apply(compiler) {
- var options = this.options;
- options.test = options.test || /\.js($|\?)/i;
- var warningsFilter = options.warningsFilter || function () {
- return true;
- };
-
- var requestShortener = new RequestShortener(compiler.context);
- compiler.plugin("compilation", function (compilation) {
- if (options.sourceMap) {
- compilation.plugin("build-module", function (module) {
- // to get detailed location info about errors
- module.useSourceMap = true;
- });
- }
- compilation.plugin("optimize-chunk-assets", function (chunks, callback) {
- var files = [];
- chunks.forEach(function (chunk) {
- return files.push.apply(files, chunk.files);
- });
- files.push.apply(files, compilation.additionalChunkAssets);
- var filteredFiles = files.filter(ModuleFilenameHelpers.matchObject.bind(undefined, options));
- filteredFiles.forEach(function (file) {
- var oldWarnFunction = uglify.AST_Node.warn_function;
- var warnings = [];
- var sourceMap = void 0;
- try {
- var asset = compilation.assets[file];
- if (asset.__UglifyJsPlugin) {
- compilation.assets[file] = asset.__UglifyJsPlugin;
- return;
- }
- var input = void 0;
- var inputSourceMap = void 0;
- if (options.sourceMap) {
- if (asset.sourceAndMap) {
- var sourceAndMap = asset.sourceAndMap();
- inputSourceMap = sourceAndMap.map;
- input = sourceAndMap.source;
- } else {
- inputSourceMap = asset.map();
- input = asset.source();
- }
- sourceMap = new SourceMapConsumer(inputSourceMap);
- uglify.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: line,
- column: 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 + "]");
- };
- } else {
- input = asset.source();
- uglify.AST_Node.warn_function = function (warning) {
- // eslint-disable-line camelcase
- warnings.push(warning);
- };
- }
- uglify.base54.reset();
- var ast = uglify.parse(input, {
- filename: file
- });
- if (options.compress !== false) {
- ast.figure_out_scope();
- var compress = uglify.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) {
- uglify.mangle_properties(ast, options.mangle.props);
- }
- }
- var output = {};
- output.comments = Object.prototype.hasOwnProperty.call(options, "comments") ? options.comments : /^\**!|@preserve|@license/;
- output.beautify = options.beautify;
- for (var k in options.output) {
- output[k] = options.output[k];
- }
- 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:
- 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
- output.comments = function (astNode, comment) {
- if (condition.extract(astNode, comment)) {
- extractedComments.push(comment.type === "comment2" ? "/*" + comment.value + "*/" : "//" + comment.value);
- }
- return condition.preserve(astNode, comment);
- };
- }
- var map = void 0;
- if (options.sourceMap) {
- map = uglify.SourceMap({ // eslint-disable-line new-cap
- file: file,
- root: ""
- });
- output.source_map = map; // eslint-disable-line camelcase
- }
- var stream = uglify.OutputStream(output); // eslint-disable-line new-cap
- ast.print(stream);
- if (map) map = map + "";
- var stringifiedStream = stream + "";
- var outputSource = map ? new SourceMapSource(stringifiedStream, file, JSON.parse(map), input, inputSourceMap) : new RawSource(stringifiedStream);
- if (extractedComments.length > 0) {
- var commentsFile = options.extractComments.filename || file + ".LICENSE";
- if (typeof commentsFile === "function") {
- commentsFile = commentsFile(file);
- }
-
- // Write extracted comments to commentsFile
- var commentsSource = new RawSource(extractedComments.join("\n\n") + "\n");
- if (commentsFile in compilation.assets) {
- // commentsFile already exists, append new comments...
- if (compilation.assets[commentsFile] instanceof ConcatSource) {
- compilation.assets[commentsFile].add("\n");
- compilation.assets[commentsFile].add(commentsSource);
- } else {
- compilation.assets[commentsFile] = new ConcatSource(compilation.assets[commentsFile], "\n", commentsSource);
- }
- } else {
- compilation.assets[commentsFile] = commentsSource;
- }
-
- // 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 ConcatSource("/*! " + banner + " */\n", outputSource);
- }
- }
- }
- asset.__UglifyJsPlugin = 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 + "]"));
- }
- } 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 {
- uglify.AST_Node.warn_function = oldWarnFunction; // eslint-disable-line camelcase
- }
- });
- callback();
- });
- });
- }
- }]);
-
- return UglifyJsPlugin;
+ function UglifyJsPlugin() {
+ var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
+
+ _classCallCheck(this, UglifyJsPlugin);
+
+ (0, _schemaUtils2.default)(_options2.default, options, 'UglifyJs Plugin');
+
+ var minify = options.minify,
+ _options$uglifyOption = options.uglifyOptions,
+ uglifyOptions = _options$uglifyOption === undefined ? {} : _options$uglifyOption,
+ _options$test = options.test,
+ test = _options$test === undefined ? /\.js(\?.*)?$/i : _options$test,
+ _options$warningsFilt = options.warningsFilter,
+ warningsFilter = _options$warningsFilt === undefined ? function () {
+ return true;
+ } : _options$warningsFilt,
+ _options$extractComme = options.extractComments,
+ extractComments = _options$extractComme === undefined ? false : _options$extractComme,
+ _options$sourceMap = options.sourceMap,
+ sourceMap = _options$sourceMap === undefined ? false : _options$sourceMap,
+ _options$cache = options.cache,
+ cache = _options$cache === undefined ? false : _options$cache,
+ _options$cacheKeys = options.cacheKeys,
+ cacheKeys = _options$cacheKeys === undefined ? function (defaultCacheKeys) {
+ return defaultCacheKeys;
+ } : _options$cacheKeys,
+ _options$parallel = options.parallel,
+ parallel = _options$parallel === undefined ? false : _options$parallel,
+ include = options.include,
+ exclude = options.exclude;
+
+
+ this.options = {
+ test,
+ warningsFilter,
+ extractComments,
+ sourceMap,
+ cache,
+ cacheKeys,
+ parallel,
+ include,
+ exclude,
+ minify,
+ uglifyOptions: Object.assign({
+ compress: {
+ inline: 1
+ },
+ output: {
+ comments: extractComments ? false : /^\**!|@preserve|@license|@cc_on/
+ }
+ }, uglifyOptions)
+ };
+ }
+
+ _createClass(UglifyJsPlugin, [{
+ key: 'apply',
+ value: function apply(compiler) {
+ var _this = this;
+
+ var buildModuleFn = function buildModuleFn(moduleArg) {
+ // to get detailed location info about errors
+ moduleArg.useSourceMap = true;
+ };
+
+ var optimizeFn = function optimizeFn(compilation, chunks, callback) {
+ var runner = new _Runner2.default({
+ cache: _this.options.cache,
+ parallel: _this.options.parallel
+ });
+
+ var uglifiedAssets = new WeakSet();
+ var tasks = [];
+
+ chunks.reduce(function (acc, chunk) {
+ return acc.concat(chunk.files || []);
+ }, []).concat(compilation.additionalChunkAssets || []).filter(_ModuleFilenameHelpers2.default.matchObject.bind(null, _this.options)).forEach(function (file) {
+ var inputSourceMap = void 0;
+
+ var asset = compilation.assets[file];
+
+ if (uglifiedAssets.has(asset)) {
+ return;
+ }
+
+ try {
+ var input = void 0;
+
+ if (_this.options.sourceMap && asset.sourceAndMap) {
+ var _asset$sourceAndMap = asset.sourceAndMap(),
+ source = _asset$sourceAndMap.source,
+ map = _asset$sourceAndMap.map;
+
+ input = source;
+
+ if (UglifyJsPlugin.isSourceMap(map)) {
+ inputSourceMap = map;
+ } else {
+ inputSourceMap = map;
+
+ compilation.warnings.push(new Error(`${file} contains invalid source map`));
+ }
+ } else {
+ input = asset.source();
+ inputSourceMap = null;
+ }
+
+ // Handling comment extraction
+ var commentsFile = false;
+
+ if (_this.options.extractComments) {
+ commentsFile = _this.options.extractComments.filename || `${file}.LICENSE`;
+
+ if (typeof commentsFile === 'function') {
+ commentsFile = commentsFile(file);
+ }
+ }
+
+ var task = {
+ file,
+ input,
+ inputSourceMap,
+ commentsFile,
+ extractComments: _this.options.extractComments,
+ uglifyOptions: _this.options.uglifyOptions,
+ minify: _this.options.minify
+ };
+
+ if (_this.options.cache) {
+ var defaultCacheKeys = {
+ // eslint-disable-next-line global-require
+ 'uglify-es': require('uglify-es/package.json').version,
+ // eslint-disable-next-line global-require
+ 'uglifyjs-webpack-plugin': require('../package.json').version,
+ 'uglifyjs-webpack-plugin-options': _this.options,
+ path: compiler.outputPath ? `${compiler.outputPath}/${file}` : file,
+ hash: _crypto2.default.createHash('md4').update(input).digest('hex')
+ };
+
+ task.cacheKeys = _this.options.cacheKeys(defaultCacheKeys, file);
+ }
+
+ tasks.push(task);
+ } catch (error) {
+ compilation.errors.push(UglifyJsPlugin.buildError(error, file, UglifyJsPlugin.buildSourceMap(inputSourceMap), new _RequestShortener2.default(compiler.context)));
+ }
+ });
+
+ runner.runTasks(tasks, function (tasksError, results) {
+ if (tasksError) {
+ compilation.errors.push(tasksError);
+
+ return;
+ }
+
+ results.forEach(function (data, index) {
+ var _tasks$index = tasks[index],
+ file = _tasks$index.file,
+ input = _tasks$index.input,
+ inputSourceMap = _tasks$index.inputSourceMap,
+ commentsFile = _tasks$index.commentsFile;
+ var error = data.error,
+ map = data.map,
+ code = data.code,
+ warnings = data.warnings,
+ extractedComments = data.extractedComments;
+
+
+ var sourceMap = null;
+
+ if (error || warnings && warnings.length > 0) {
+ sourceMap = UglifyJsPlugin.buildSourceMap(inputSourceMap);
+ }
+
+ // Handling results
+ // Error case: add errors, and go to next file
+ if (error) {
+ compilation.errors.push(UglifyJsPlugin.buildError(error, file, sourceMap, new _RequestShortener2.default(compiler.context)));
+
+ return;
+ }
+
+ var outputSource = void 0;
+
+ if (map) {
+ outputSource = new _webpackSources.SourceMapSource(code, file, JSON.parse(map), input, inputSourceMap);
+ } else {
+ outputSource = new _webpackSources.RawSource(code);
+ }
+
+ // 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 ${_path2.default.posix.basename(commentsFile)}`;
+
+ if (typeof banner === 'function') {
+ banner = banner(commentsFile);
+ }
+
+ if (banner) {
+ outputSource = new _webpackSources.ConcatSource(`/*! ${banner} */\n`, outputSource);
+ }
+ }
+
+ var commentsSource = new _webpackSources.RawSource(`${extractedComments.join('\n\n')}\n`);
+
+ if (commentsFile in compilation.assets) {
+ // commentsFile already exists, append new comments...
+ if (compilation.assets[commentsFile] instanceof _webpackSources.ConcatSource) {
+ compilation.assets[commentsFile].add('\n');
+ compilation.assets[commentsFile].add(commentsSource);
+ } else {
+ compilation.assets[commentsFile] = new _webpackSources.ConcatSource(compilation.assets[commentsFile], '\n', commentsSource);
+ }
+ } else {
+ compilation.assets[commentsFile] = commentsSource;
+ }
+ }
+
+ // Updating assets
+ uglifiedAssets.add(compilation.assets[file] = outputSource);
+
+ // Handling warnings
+ if (warnings && warnings.length > 0) {
+ warnings.forEach(function (warning) {
+ var builtWarning = UglifyJsPlugin.buildWarning(warning, file, sourceMap, _this.options.warningsFilter, new _RequestShortener2.default(compiler.context));
+
+ if (builtWarning) {
+ compilation.warnings.push(builtWarning);
+ }
+ });
+ }
+ });
+
+ runner.exit();
+
+ callback();
+ });
+ };
+
+ /* istanbul ignore if */
+ if (compiler.hooks) {
+ var plugin = { name: 'UglifyJSPlugin' };
+
+ compiler.hooks.compilation.tap(plugin, function (compilation) {
+ if (_this.options.sourceMap) {
+ compilation.hooks.buildModule.tap(plugin, buildModuleFn);
+ }
+
+ compilation.hooks.optimizeChunkAssets.tapAsync(plugin, optimizeFn.bind(_this, compilation));
+ });
+ } else {
+ compiler.plugin('compilation', function (compilation) {
+ if (_this.options.sourceMap) {
+ compilation.plugin('build-module', buildModuleFn);
+ }
+
+ compilation.plugin('optimize-chunk-assets', optimizeFn.bind(_this, compilation));
+ });
+ }
+ }
+ }], [{
+ key: 'isSourceMap',
+ value: function isSourceMap(input) {
+ // All required options for `new SourceMapConsumer(...options)`
+ // https://github.com/mozilla/source-map#new-sourcemapconsumerrawsourcemap
+ return Boolean(input && input.version && input.sources && Array.isArray(input.sources) && typeof input.mappings === 'string');
+ }
+ }, {
+ key: 'buildSourceMap',
+ value: function buildSourceMap(inputSourceMap) {
+ if (!inputSourceMap || !UglifyJsPlugin.isSourceMap(inputSourceMap)) {
+ return null;
+ }
+
+ return new _sourceMap.SourceMapConsumer(inputSourceMap);
+ }
+ }, {
+ 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 && requestShortener) {
+ 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.stack) {
+ return new Error(`${file} from UglifyJs\n${err.stack}`);
+ }
+
+ return new Error(`${file} from UglifyJs\n${err.message}`);
+ }
+ }, {
+ key: 'buildWarning',
+ value: function buildWarning(warning, file, sourceMap, warningsFilter, requestShortener) {
+ if (!file || !sourceMap) {
+ return `UglifyJs Plugin: ${warning}`;
+ }
+
+ var warningMessage = warning;
+
+ var match = warningRegex.exec(warning);
+
+ if (match) {
+ var line = +match[1];
+ var column = +match[2];
+ var original = sourceMap.originalPositionFor({
+ line,
+ column
+ });
+
+ if (warningsFilter && !warningsFilter(original.source)) {
+ return null;
+ }
+
+ if (original && original.source && original.source !== file && requestShortener) {
+ warningMessage = `${warningMessage.replace(warningRegex, '')}[${requestShortener.shorten(original.source)}:${original.line},${original.column}]`;
+ }
+ }
+
+ return `UglifyJs Plugin: ${warningMessage} in ${file}`;
+ }
+ }]);
+
+ return UglifyJsPlugin;
}();
-module.exports = UglifyJsPlugin; \ No newline at end of file
+exports.default = UglifyJsPlugin; \ No newline at end of file