index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. 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
  6. no-param-reassign
  7. */
  8. var _crypto = require('crypto');
  9. var _crypto2 = _interopRequireDefault(_crypto);
  10. var _path = require('path');
  11. var _path2 = _interopRequireDefault(_path);
  12. var _sourceMap = require('source-map');
  13. var _webpackSources = require('webpack-sources');
  14. var _RequestShortener = require('webpack/lib/RequestShortener');
  15. var _RequestShortener2 = _interopRequireDefault(_RequestShortener);
  16. var _ModuleFilenameHelpers = require('webpack/lib/ModuleFilenameHelpers');
  17. var _ModuleFilenameHelpers2 = _interopRequireDefault(_ModuleFilenameHelpers);
  18. var _schemaUtils = require('schema-utils');
  19. var _schemaUtils2 = _interopRequireDefault(_schemaUtils);
  20. var _serializeJavascript = require('serialize-javascript');
  21. var _serializeJavascript2 = _interopRequireDefault(_serializeJavascript);
  22. var _options = require('./options.json');
  23. var _options2 = _interopRequireDefault(_options);
  24. var _uglify = require('./uglify');
  25. var _uglify2 = _interopRequireDefault(_uglify);
  26. var _versions = require('./uglify/versions');
  27. var _versions2 = _interopRequireDefault(_versions);
  28. var _utils = require('./utils');
  29. var _utils2 = _interopRequireDefault(_utils);
  30. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  31. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  32. var warningRegex = /\[.+:([0-9]+),([0-9]+)\]/;
  33. var UglifyJsPlugin = function () {
  34. function UglifyJsPlugin() {
  35. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  36. _classCallCheck(this, UglifyJsPlugin);
  37. (0, _schemaUtils2.default)(_options2.default, options, 'UglifyJs Plugin');
  38. var _options$uglifyOption = options.uglifyOptions,
  39. uglifyOptions = _options$uglifyOption === undefined ? {} : _options$uglifyOption,
  40. _options$test = options.test,
  41. test = _options$test === undefined ? /\.js(\?.*)?$/i : _options$test,
  42. _options$warningsFilt = options.warningsFilter,
  43. warningsFilter = _options$warningsFilt === undefined ? function () {
  44. return true;
  45. } : _options$warningsFilt,
  46. _options$extractComme = options.extractComments,
  47. extractComments = _options$extractComme === undefined ? false : _options$extractComme,
  48. _options$sourceMap = options.sourceMap,
  49. sourceMap = _options$sourceMap === undefined ? false : _options$sourceMap,
  50. _options$cache = options.cache,
  51. cache = _options$cache === undefined ? false : _options$cache,
  52. _options$parallel = options.parallel,
  53. parallel = _options$parallel === undefined ? false : _options$parallel,
  54. include = options.include,
  55. exclude = options.exclude;
  56. this.options = {
  57. test,
  58. warningsFilter,
  59. extractComments,
  60. sourceMap,
  61. cache,
  62. parallel,
  63. include,
  64. exclude,
  65. uglifyOptions: Object.assign({
  66. compress: {
  67. inline: 1
  68. },
  69. output: {
  70. comments: extractComments ? false : /^\**!|@preserve|@license|@cc_on/
  71. }
  72. }, uglifyOptions)
  73. };
  74. }
  75. _createClass(UglifyJsPlugin, [{
  76. key: 'apply',
  77. value: function apply(compiler) {
  78. var _this = this;
  79. var requestShortener = new _RequestShortener2.default(compiler.context);
  80. var buildModuleFn = function buildModuleFn(moduleArg) {
  81. // to get detailed location info about errors
  82. moduleArg.useSourceMap = true;
  83. };
  84. var optimizeFn = function optimizeFn(compilation, chunks, callback) {
  85. var uglify = new _uglify2.default({
  86. cache: _this.options.cache,
  87. parallel: _this.options.parallel
  88. });
  89. var uglifiedAssets = new WeakSet();
  90. var tasks = [];
  91. chunks.reduce(function (acc, chunk) {
  92. return acc.concat(chunk.files || []);
  93. }, []).concat(compilation.additionalChunkAssets || []).filter(_ModuleFilenameHelpers2.default.matchObject.bind(null, _this.options)).forEach(function (file) {
  94. var inputSourceMap = void 0;
  95. var asset = compilation.assets[file];
  96. if (uglifiedAssets.has(asset)) {
  97. return;
  98. }
  99. try {
  100. var input = void 0;
  101. if (_this.options.sourceMap && asset.sourceAndMap) {
  102. var _asset$sourceAndMap = asset.sourceAndMap(),
  103. source = _asset$sourceAndMap.source,
  104. map = _asset$sourceAndMap.map;
  105. input = source;
  106. if (_utils2.default.isSourceMap(map)) {
  107. inputSourceMap = map;
  108. } else {
  109. inputSourceMap = map;
  110. compilation.warnings.push(new Error(`${file} contains invalid source map`));
  111. }
  112. } else {
  113. input = asset.source();
  114. inputSourceMap = null;
  115. }
  116. // Handling comment extraction
  117. var commentsFile = false;
  118. if (_this.options.extractComments) {
  119. commentsFile = _this.options.extractComments.filename || `${file}.LICENSE`;
  120. if (typeof commentsFile === 'function') {
  121. commentsFile = commentsFile(file);
  122. }
  123. }
  124. var task = {
  125. file,
  126. input,
  127. inputSourceMap,
  128. commentsFile,
  129. extractComments: _this.options.extractComments,
  130. uglifyOptions: _this.options.uglifyOptions
  131. };
  132. if (_this.options.cache) {
  133. task.cacheKey = (0, _serializeJavascript2.default)({
  134. 'uglify-es': _versions2.default.uglify,
  135. 'uglifyjs-webpack-plugin': _versions2.default.plugin,
  136. 'uglifyjs-webpack-plugin-options': _this.options,
  137. path: compiler.outputPath ? `${compiler.outputPath}/${file}` : file,
  138. hash: _crypto2.default.createHash('md4').update(input).digest('hex')
  139. });
  140. }
  141. tasks.push(task);
  142. } catch (error) {
  143. compilation.errors.push(UglifyJsPlugin.buildError(error, file, UglifyJsPlugin.buildSourceMap(inputSourceMap), requestShortener));
  144. }
  145. });
  146. uglify.runTasks(tasks, function (tasksError, results) {
  147. if (tasksError) {
  148. compilation.errors.push(tasksError);
  149. return;
  150. }
  151. results.forEach(function (data, index) {
  152. var _tasks$index = tasks[index],
  153. file = _tasks$index.file,
  154. input = _tasks$index.input,
  155. inputSourceMap = _tasks$index.inputSourceMap,
  156. commentsFile = _tasks$index.commentsFile;
  157. var error = data.error,
  158. map = data.map,
  159. code = data.code,
  160. warnings = data.warnings,
  161. extractedComments = data.extractedComments;
  162. var sourceMap = null;
  163. if (error || warnings && warnings.length > 0) {
  164. sourceMap = UglifyJsPlugin.buildSourceMap(inputSourceMap);
  165. }
  166. // Handling results
  167. // Error case: add errors, and go to next file
  168. if (error) {
  169. compilation.errors.push(UglifyJsPlugin.buildError(error, file, sourceMap, requestShortener));
  170. return;
  171. }
  172. var outputSource = void 0;
  173. if (map) {
  174. outputSource = new _webpackSources.SourceMapSource(code, file, JSON.parse(map), input, inputSourceMap);
  175. } else {
  176. outputSource = new _webpackSources.RawSource(code);
  177. }
  178. // Write extracted comments to commentsFile
  179. if (commentsFile && extractedComments.length > 0) {
  180. // Add a banner to the original file
  181. if (_this.options.extractComments.banner !== false) {
  182. var banner = _this.options.extractComments.banner || `For license information please see ${_path2.default.posix.basename(commentsFile)}`;
  183. if (typeof banner === 'function') {
  184. banner = banner(commentsFile);
  185. }
  186. if (banner) {
  187. outputSource = new _webpackSources.ConcatSource(`/*! ${banner} */\n`, outputSource);
  188. }
  189. }
  190. var commentsSource = new _webpackSources.RawSource(`${extractedComments.join('\n\n')}\n`);
  191. if (commentsFile in compilation.assets) {
  192. // commentsFile already exists, append new comments...
  193. if (compilation.assets[commentsFile] instanceof _webpackSources.ConcatSource) {
  194. compilation.assets[commentsFile].add('\n');
  195. compilation.assets[commentsFile].add(commentsSource);
  196. } else {
  197. compilation.assets[commentsFile] = new _webpackSources.ConcatSource(compilation.assets[commentsFile], '\n', commentsSource);
  198. }
  199. } else {
  200. compilation.assets[commentsFile] = commentsSource;
  201. }
  202. }
  203. // Updating assets
  204. uglifiedAssets.add(compilation.assets[file] = outputSource);
  205. // Handling warnings
  206. if (warnings && warnings.length > 0) {
  207. warnings.forEach(function (warning) {
  208. var builtWarning = UglifyJsPlugin.buildWarning(warning, file, sourceMap, _this.options.warningsFilter, requestShortener);
  209. if (builtWarning) {
  210. compilation.warnings.push(builtWarning);
  211. }
  212. });
  213. }
  214. });
  215. uglify.exit();
  216. callback();
  217. });
  218. };
  219. if (compiler.hooks) {
  220. var plugin = { name: 'UglifyJSPlugin' };
  221. compiler.hooks.compilation.tap(plugin, function (compilation) {
  222. if (_this.options.sourceMap) {
  223. compilation.hooks.buildModule.tap(plugin, buildModuleFn);
  224. }
  225. compilation.hooks.optimizeChunkAssets.tapAsync(plugin, optimizeFn.bind(_this, compilation));
  226. });
  227. } else {
  228. compiler.plugin('compilation', function (compilation) {
  229. if (_this.options.sourceMap) {
  230. compilation.plugin('build-module', buildModuleFn);
  231. }
  232. compilation.plugin('optimize-chunk-assets', optimizeFn.bind(_this, compilation));
  233. });
  234. }
  235. }
  236. }], [{
  237. key: 'buildSourceMap',
  238. value: function buildSourceMap(inputSourceMap) {
  239. if (!inputSourceMap || !_utils2.default.isSourceMap(inputSourceMap)) {
  240. return null;
  241. }
  242. return new _sourceMap.SourceMapConsumer(inputSourceMap);
  243. }
  244. }, {
  245. key: 'buildError',
  246. value: function buildError(err, file, sourceMap, requestShortener) {
  247. // Handling error which should have line, col, filename and message
  248. if (err.line) {
  249. var original = sourceMap && sourceMap.originalPositionFor({
  250. line: err.line,
  251. column: err.col
  252. });
  253. if (original && original.source) {
  254. return new Error(`${file} from UglifyJs\n${err.message} [${requestShortener.shorten(original.source)}:${original.line},${original.column}][${file}:${err.line},${err.col}]`);
  255. }
  256. return new Error(`${file} from UglifyJs\n${err.message} [${file}:${err.line},${err.col}]`);
  257. } else if (err.stack) {
  258. return new Error(`${file} from UglifyJs\n${err.stack}`);
  259. }
  260. return new Error(`${file} from UglifyJs\n${err.message}`);
  261. }
  262. }, {
  263. key: 'buildWarning',
  264. value: function buildWarning(warning, file, sourceMap, warningsFilter, requestShortener) {
  265. if (!file || !sourceMap) {
  266. return warning;
  267. }
  268. var match = warningRegex.exec(warning);
  269. var line = +match[1];
  270. var column = +match[2];
  271. var original = sourceMap.originalPositionFor({
  272. line,
  273. column
  274. });
  275. if (!warningsFilter(original.source)) {
  276. return null;
  277. }
  278. var warningMessage = warning.replace(warningRegex, '');
  279. if (original && original.source && original.source !== file) {
  280. warningMessage += `[${requestShortener.shorten(original.source)}:${original.line},${original.column}]`;
  281. }
  282. return `UglifyJs Plugin: ${warningMessage} in ${file}`;
  283. }
  284. }]);
  285. return UglifyJsPlugin;
  286. }();
  287. exports.default = UglifyJsPlugin;