index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. output: {
  67. comments: extractComments ? false : /^\**!|@preserve|@license|@cc_on/
  68. }
  69. }, uglifyOptions)
  70. };
  71. this.sourceMapsCache = new WeakMap();
  72. }
  73. _createClass(UglifyJsPlugin, [{
  74. key: 'buildError',
  75. value: function buildError(err, file, inputSourceMap, requestShortener) {
  76. // Handling error which should have line, col, filename and message
  77. if (err.line) {
  78. var sourceMapCacheKey = { file };
  79. var sourceMap = this.sourceMapsCache.get(sourceMapCacheKey);
  80. if (!sourceMap) {
  81. sourceMap = new _sourceMap.SourceMapConsumer(inputSourceMap);
  82. this.sourceMapsCache.set(sourceMapCacheKey, sourceMap);
  83. }
  84. var original = sourceMap && sourceMap.originalPositionFor({
  85. line: err.line,
  86. column: err.col
  87. });
  88. if (original && original.source) {
  89. return new Error(`${file} from UglifyJs\n${err.message} [${requestShortener.shorten(original.source)}:${original.line},${original.column}][${file}:${err.line},${err.col}]`);
  90. }
  91. return new Error(`${file} from UglifyJs\n${err.message} [${file}:${err.line},${err.col}]`);
  92. } else if (err.stack) {
  93. return new Error(`${file} from UglifyJs\n${err.stack}`);
  94. }
  95. return new Error(`${file} from UglifyJs\n${err.message}`);
  96. }
  97. }, {
  98. key: 'buildWarning',
  99. value: function buildWarning(warning, file, inputSourceMap, warningsFilter, requestShortener) {
  100. if (!file || !inputSourceMap) {
  101. return warning;
  102. }
  103. var sourceMapCacheKey = { file };
  104. var sourceMap = this.sourceMapsCache.get(sourceMapCacheKey);
  105. if (!sourceMap) {
  106. sourceMap = new _sourceMap.SourceMapConsumer(inputSourceMap);
  107. this.sourceMapsCache.set(sourceMapCacheKey, sourceMap);
  108. }
  109. var match = warningRegex.exec(warning);
  110. var line = +match[1];
  111. var column = +match[2];
  112. var original = sourceMap.originalPositionFor({
  113. line,
  114. column
  115. });
  116. var warningMessage = null;
  117. if (warningsFilter(original.source)) {
  118. warningMessage = warning.replace(warningRegex, '');
  119. if (original && original.source && original.source !== file) {
  120. warningMessage += `[${requestShortener.shorten(original.source)}:${original.line},${original.column}]`;
  121. }
  122. }
  123. return warningMessage;
  124. }
  125. }, {
  126. key: 'apply',
  127. value: function apply(compiler) {
  128. var _this = this;
  129. var requestShortener = new _RequestShortener2.default(compiler.context);
  130. var buildModuleFn = function buildModuleFn(moduleArg) {
  131. // to get detailed location info about errors
  132. moduleArg.useSourceMap = true;
  133. };
  134. var optimizeFn = function optimizeFn(compilation, chunks, callback) {
  135. var uglify = new _uglify2.default({
  136. cache: _this.options.cache,
  137. parallel: _this.options.parallel
  138. });
  139. var uglifiedAssets = new WeakSet();
  140. var tasks = [];
  141. chunks.reduce(function (acc, chunk) {
  142. return acc.concat(chunk.files || []);
  143. }, []).concat(compilation.additionalChunkAssets || []).filter(_ModuleFilenameHelpers2.default.matchObject.bind(null, _this.options)).forEach(function (file) {
  144. var inputSourceMap = void 0;
  145. var asset = compilation.assets[file];
  146. if (uglifiedAssets.has(asset)) {
  147. return;
  148. }
  149. try {
  150. var input = void 0;
  151. if (_this.options.sourceMap && asset.sourceAndMap) {
  152. var _asset$sourceAndMap = asset.sourceAndMap(),
  153. source = _asset$sourceAndMap.source,
  154. map = _asset$sourceAndMap.map;
  155. input = source;
  156. if (_utils2.default.isSourceMap(map)) {
  157. inputSourceMap = map;
  158. } else {
  159. inputSourceMap = map;
  160. compilation.warnings.push(new Error(`${file} contain invalid source map`));
  161. }
  162. } else {
  163. input = asset.source();
  164. inputSourceMap = null;
  165. }
  166. // Handling comment extraction
  167. var commentsFile = false;
  168. if (_this.options.extractComments) {
  169. commentsFile = _this.options.extractComments.filename || `${file}.LICENSE`;
  170. if (typeof commentsFile === 'function') {
  171. commentsFile = commentsFile(file);
  172. }
  173. }
  174. var task = {
  175. file,
  176. input,
  177. inputSourceMap,
  178. commentsFile,
  179. extractComments: _this.options.extractComments,
  180. uglifyOptions: _this.options.uglifyOptions
  181. };
  182. if (_this.options.cache) {
  183. task.cacheKey = (0, _serializeJavascript2.default)({
  184. 'uglify-es': _versions2.default.uglify,
  185. 'uglifyjs-webpack-plugin': _versions2.default.plugin,
  186. 'uglifyjs-webpack-plugin-options': _this.options,
  187. path: compiler.outputPath ? `${compiler.outputPath}/${file}` : file,
  188. hash: _crypto2.default.createHash('md4').update(input).digest('hex')
  189. });
  190. }
  191. tasks.push(task);
  192. } catch (error) {
  193. compilation.errors.push(_this.buildError(error, file, inputSourceMap, requestShortener));
  194. }
  195. });
  196. uglify.runTasks(tasks, function (tasksError, results) {
  197. if (tasksError) {
  198. compilation.errors.push(tasksError);
  199. return;
  200. }
  201. results.forEach(function (data, index) {
  202. var _tasks$index = tasks[index],
  203. file = _tasks$index.file,
  204. input = _tasks$index.input,
  205. inputSourceMap = _tasks$index.inputSourceMap,
  206. commentsFile = _tasks$index.commentsFile;
  207. var error = data.error,
  208. map = data.map,
  209. code = data.code,
  210. warnings = data.warnings,
  211. extractedComments = data.extractedComments;
  212. // Handling results
  213. // Error case: add errors, and go to next file
  214. if (error) {
  215. compilation.errors.push(_this.buildError(error, file, inputSourceMap, requestShortener));
  216. return;
  217. }
  218. var outputSource = void 0;
  219. if (map) {
  220. outputSource = new _webpackSources.SourceMapSource(code, file, JSON.parse(map), input, inputSourceMap);
  221. } else {
  222. outputSource = new _webpackSources.RawSource(code);
  223. }
  224. // Write extracted comments to commentsFile
  225. if (commentsFile && extractedComments.length > 0) {
  226. // Add a banner to the original file
  227. if (_this.options.extractComments.banner !== false) {
  228. var banner = _this.options.extractComments.banner || `For license information please see ${_path2.default.posix.basename(commentsFile)}`;
  229. if (typeof banner === 'function') {
  230. banner = banner(commentsFile);
  231. }
  232. if (banner) {
  233. outputSource = new _webpackSources.ConcatSource(`/*! ${banner} */\n`, outputSource);
  234. }
  235. }
  236. var commentsSource = new _webpackSources.RawSource(`${extractedComments.join('\n\n')}\n`);
  237. if (commentsFile in compilation.assets) {
  238. // commentsFile already exists, append new comments...
  239. if (compilation.assets[commentsFile] instanceof _webpackSources.ConcatSource) {
  240. compilation.assets[commentsFile].add('\n');
  241. compilation.assets[commentsFile].add(commentsSource);
  242. } else {
  243. compilation.assets[commentsFile] = new _webpackSources.ConcatSource(compilation.assets[commentsFile], '\n', commentsSource);
  244. }
  245. } else {
  246. compilation.assets[commentsFile] = commentsSource;
  247. }
  248. }
  249. // Updating assets
  250. uglifiedAssets.add(compilation.assets[file] = outputSource);
  251. // Handling warnings
  252. if (warnings && warnings.length > 0) {
  253. warnings.forEach(function (warning) {
  254. var builtWarning = _this.buildWarning(warning, file, inputSourceMap, _this.options.warningsFilter, requestShortener);
  255. if (builtWarning) {
  256. compilation.warnings.push(builtWarning);
  257. }
  258. });
  259. }
  260. });
  261. uglify.exit();
  262. callback();
  263. });
  264. };
  265. if (compiler.hooks) {
  266. var plugin = { name: 'UglifyJSPlugin' };
  267. compiler.hooks.compilation.tap(plugin, function (compilation) {
  268. if (_this.options.sourceMap) {
  269. compilation.hooks.buildModule.tap(plugin, buildModuleFn);
  270. }
  271. compilation.hooks.optimizeChunkAssets.tapAsync(plugin, optimizeFn.bind(_this, compilation));
  272. });
  273. } else {
  274. compiler.plugin('compilation', function (compilation) {
  275. if (_this.options.sourceMap) {
  276. compilation.plugin('build-module', buildModuleFn);
  277. }
  278. compilation.plugin('optimize-chunk-assets', optimizeFn.bind(_this, compilation));
  279. });
  280. }
  281. }
  282. }]);
  283. return UglifyJsPlugin;
  284. }();
  285. exports.default = UglifyJsPlugin;