index.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 _options = require('./options.json');
  21. var _options2 = _interopRequireDefault(_options);
  22. var _Runner = require('./uglify/Runner');
  23. var _Runner2 = _interopRequireDefault(_Runner);
  24. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  25. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  26. var warningRegex = /\[.+:([0-9]+),([0-9]+)\]/;
  27. var UglifyJsPlugin = function () {
  28. function UglifyJsPlugin() {
  29. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  30. _classCallCheck(this, UglifyJsPlugin);
  31. (0, _schemaUtils2.default)(_options2.default, options, 'UglifyJs Plugin');
  32. var minify = options.minify,
  33. _options$uglifyOption = options.uglifyOptions,
  34. uglifyOptions = _options$uglifyOption === undefined ? {} : _options$uglifyOption,
  35. _options$test = options.test,
  36. test = _options$test === undefined ? /\.js(\?.*)?$/i : _options$test,
  37. _options$warningsFilt = options.warningsFilter,
  38. warningsFilter = _options$warningsFilt === undefined ? function () {
  39. return true;
  40. } : _options$warningsFilt,
  41. _options$extractComme = options.extractComments,
  42. extractComments = _options$extractComme === undefined ? false : _options$extractComme,
  43. _options$sourceMap = options.sourceMap,
  44. sourceMap = _options$sourceMap === undefined ? false : _options$sourceMap,
  45. _options$cache = options.cache,
  46. cache = _options$cache === undefined ? false : _options$cache,
  47. _options$cacheKeys = options.cacheKeys,
  48. cacheKeys = _options$cacheKeys === undefined ? function (defaultCacheKeys) {
  49. return defaultCacheKeys;
  50. } : _options$cacheKeys,
  51. _options$parallel = options.parallel,
  52. parallel = _options$parallel === undefined ? false : _options$parallel,
  53. include = options.include,
  54. exclude = options.exclude;
  55. this.options = {
  56. test,
  57. warningsFilter,
  58. extractComments,
  59. sourceMap,
  60. cache,
  61. cacheKeys,
  62. parallel,
  63. include,
  64. exclude,
  65. minify,
  66. uglifyOptions: Object.assign({
  67. compress: {
  68. inline: 1
  69. },
  70. output: {
  71. comments: extractComments ? false : /^\**!|@preserve|@license|@cc_on/
  72. }
  73. }, uglifyOptions)
  74. };
  75. }
  76. _createClass(UglifyJsPlugin, [{
  77. key: 'apply',
  78. value: function apply(compiler) {
  79. var _this = this;
  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 runner = new _Runner2.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 (UglifyJsPlugin.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. minify: _this.options.minify
  132. };
  133. if (_this.options.cache) {
  134. var defaultCacheKeys = {
  135. // eslint-disable-next-line global-require
  136. 'uglify-es': require('uglify-es/package.json').version,
  137. // eslint-disable-next-line global-require
  138. 'uglifyjs-webpack-plugin': require('../package.json').version,
  139. 'uglifyjs-webpack-plugin-options': _this.options,
  140. path: compiler.outputPath ? `${compiler.outputPath}/${file}` : file,
  141. hash: _crypto2.default.createHash('md4').update(input).digest('hex')
  142. };
  143. task.cacheKeys = _this.options.cacheKeys(defaultCacheKeys, file);
  144. }
  145. tasks.push(task);
  146. } catch (error) {
  147. compilation.errors.push(UglifyJsPlugin.buildError(error, file, UglifyJsPlugin.buildSourceMap(inputSourceMap), new _RequestShortener2.default(compiler.context)));
  148. }
  149. });
  150. runner.runTasks(tasks, function (tasksError, results) {
  151. if (tasksError) {
  152. compilation.errors.push(tasksError);
  153. return;
  154. }
  155. results.forEach(function (data, index) {
  156. var _tasks$index = tasks[index],
  157. file = _tasks$index.file,
  158. input = _tasks$index.input,
  159. inputSourceMap = _tasks$index.inputSourceMap,
  160. commentsFile = _tasks$index.commentsFile;
  161. var error = data.error,
  162. map = data.map,
  163. code = data.code,
  164. warnings = data.warnings,
  165. extractedComments = data.extractedComments;
  166. var sourceMap = null;
  167. if (error || warnings && warnings.length > 0) {
  168. sourceMap = UglifyJsPlugin.buildSourceMap(inputSourceMap);
  169. }
  170. // Handling results
  171. // Error case: add errors, and go to next file
  172. if (error) {
  173. compilation.errors.push(UglifyJsPlugin.buildError(error, file, sourceMap, new _RequestShortener2.default(compiler.context)));
  174. return;
  175. }
  176. var outputSource = void 0;
  177. if (map) {
  178. outputSource = new _webpackSources.SourceMapSource(code, file, JSON.parse(map), input, inputSourceMap);
  179. } else {
  180. outputSource = new _webpackSources.RawSource(code);
  181. }
  182. // Write extracted comments to commentsFile
  183. if (commentsFile && extractedComments.length > 0) {
  184. // Add a banner to the original file
  185. if (_this.options.extractComments.banner !== false) {
  186. var banner = _this.options.extractComments.banner || `For license information please see ${_path2.default.posix.basename(commentsFile)}`;
  187. if (typeof banner === 'function') {
  188. banner = banner(commentsFile);
  189. }
  190. if (banner) {
  191. outputSource = new _webpackSources.ConcatSource(`/*! ${banner} */\n`, outputSource);
  192. }
  193. }
  194. var commentsSource = new _webpackSources.RawSource(`${extractedComments.join('\n\n')}\n`);
  195. if (commentsFile in compilation.assets) {
  196. // commentsFile already exists, append new comments...
  197. if (compilation.assets[commentsFile] instanceof _webpackSources.ConcatSource) {
  198. compilation.assets[commentsFile].add('\n');
  199. compilation.assets[commentsFile].add(commentsSource);
  200. } else {
  201. compilation.assets[commentsFile] = new _webpackSources.ConcatSource(compilation.assets[commentsFile], '\n', commentsSource);
  202. }
  203. } else {
  204. compilation.assets[commentsFile] = commentsSource;
  205. }
  206. }
  207. // Updating assets
  208. uglifiedAssets.add(compilation.assets[file] = outputSource);
  209. // Handling warnings
  210. if (warnings && warnings.length > 0) {
  211. warnings.forEach(function (warning) {
  212. var builtWarning = UglifyJsPlugin.buildWarning(warning, file, sourceMap, _this.options.warningsFilter, new _RequestShortener2.default(compiler.context));
  213. if (builtWarning) {
  214. compilation.warnings.push(builtWarning);
  215. }
  216. });
  217. }
  218. });
  219. runner.exit();
  220. callback();
  221. });
  222. };
  223. /* istanbul ignore if */
  224. if (compiler.hooks) {
  225. var plugin = { name: 'UglifyJSPlugin' };
  226. compiler.hooks.compilation.tap(plugin, function (compilation) {
  227. if (_this.options.sourceMap) {
  228. compilation.hooks.buildModule.tap(plugin, buildModuleFn);
  229. }
  230. compilation.hooks.optimizeChunkAssets.tapAsync(plugin, optimizeFn.bind(_this, compilation));
  231. });
  232. } else {
  233. compiler.plugin('compilation', function (compilation) {
  234. if (_this.options.sourceMap) {
  235. compilation.plugin('build-module', buildModuleFn);
  236. }
  237. compilation.plugin('optimize-chunk-assets', optimizeFn.bind(_this, compilation));
  238. });
  239. }
  240. }
  241. }], [{
  242. key: 'isSourceMap',
  243. value: function isSourceMap(input) {
  244. // All required options for `new SourceMapConsumer(...options)`
  245. // https://github.com/mozilla/source-map#new-sourcemapconsumerrawsourcemap
  246. return Boolean(input && input.version && input.sources && Array.isArray(input.sources) && typeof input.mappings === 'string');
  247. }
  248. }, {
  249. key: 'buildSourceMap',
  250. value: function buildSourceMap(inputSourceMap) {
  251. if (!inputSourceMap || !UglifyJsPlugin.isSourceMap(inputSourceMap)) {
  252. return null;
  253. }
  254. return new _sourceMap.SourceMapConsumer(inputSourceMap);
  255. }
  256. }, {
  257. key: 'buildError',
  258. value: function buildError(err, file, sourceMap, requestShortener) {
  259. // Handling error which should have line, col, filename and message
  260. if (err.line) {
  261. var original = sourceMap && sourceMap.originalPositionFor({
  262. line: err.line,
  263. column: err.col
  264. });
  265. if (original && original.source && requestShortener) {
  266. return new Error(`${file} from UglifyJs\n${err.message} [${requestShortener.shorten(original.source)}:${original.line},${original.column}][${file}:${err.line},${err.col}]`);
  267. }
  268. return new Error(`${file} from UglifyJs\n${err.message} [${file}:${err.line},${err.col}]`);
  269. } else if (err.stack) {
  270. return new Error(`${file} from UglifyJs\n${err.stack}`);
  271. }
  272. return new Error(`${file} from UglifyJs\n${err.message}`);
  273. }
  274. }, {
  275. key: 'buildWarning',
  276. value: function buildWarning(warning, file, sourceMap, warningsFilter, requestShortener) {
  277. if (!file || !sourceMap) {
  278. return `UglifyJs Plugin: ${warning}`;
  279. }
  280. var warningMessage = warning;
  281. var match = warningRegex.exec(warning);
  282. if (match) {
  283. var line = +match[1];
  284. var column = +match[2];
  285. var original = sourceMap.originalPositionFor({
  286. line,
  287. column
  288. });
  289. if (warningsFilter && !warningsFilter(original.source)) {
  290. return null;
  291. }
  292. if (original && original.source && original.source !== file && requestShortener) {
  293. warningMessage = `${warningMessage.replace(warningRegex, '')}[${requestShortener.shorten(original.source)}:${original.line},${original.column}]`;
  294. }
  295. }
  296. return `UglifyJs Plugin: ${warningMessage} in ${file}`;
  297. }
  298. }]);
  299. return UglifyJsPlugin;
  300. }();
  301. exports.default = UglifyJsPlugin;