index.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. 'use strict';
  2. var vm = require('vm');
  3. var fs = require('fs');
  4. var _ = require('lodash');
  5. var Promise = require('bluebird');
  6. var path = require('path');
  7. var childCompiler = require('./lib/compiler.js');
  8. var prettyError = require('./lib/errors.js');
  9. var chunkSorter = require('./lib/chunksorter.js');
  10. Promise.promisifyAll(fs);
  11. function HtmlWebpackPlugin (options) {
  12. // Default options
  13. this.options = _.extend({
  14. template: path.join(__dirname, 'default_index.ejs'),
  15. filename: 'index.html',
  16. hash: false,
  17. inject: true,
  18. compile: true,
  19. favicon: false,
  20. minify: false,
  21. cache: true,
  22. showErrors: true,
  23. chunks: 'all',
  24. excludeChunks: [],
  25. title: 'Webpack App',
  26. xhtml: false
  27. }, options);
  28. }
  29. HtmlWebpackPlugin.prototype.apply = function (compiler) {
  30. var self = this;
  31. var isCompilationCached = false;
  32. var compilationPromise;
  33. this.options.template = this.getFullTemplatePath(this.options.template, compiler.context);
  34. // convert absolute filename into relative so that webpack can
  35. // generate it at correct location
  36. var filename = this.options.filename;
  37. if (path.resolve(filename) === path.normalize(filename)) {
  38. this.options.filename = path.relative(compiler.options.output.path, filename);
  39. }
  40. compiler.plugin('make', function (compilation, callback) {
  41. // Compile the template (queued)
  42. compilationPromise = childCompiler.compileTemplate(self.options.template, compiler.context, self.options.filename, compilation)
  43. .catch(function (err) {
  44. compilation.errors.push(prettyError(err, compiler.context).toString());
  45. return {
  46. content: self.options.showErrors ? prettyError(err, compiler.context).toJsonHtml() : 'ERROR',
  47. outputName: self.options.filename
  48. };
  49. })
  50. .then(function (compilationResult) {
  51. // If the compilation change didnt change the cache is valid
  52. isCompilationCached = compilationResult.hash && self.childCompilerHash === compilationResult.hash;
  53. self.childCompilerHash = compilationResult.hash;
  54. self.childCompilationOutputName = compilationResult.outputName;
  55. callback();
  56. return compilationResult.content;
  57. });
  58. });
  59. compiler.plugin('emit', function (compilation, callback) {
  60. var applyPluginsAsyncWaterfall = self.applyPluginsAsyncWaterfall(compilation);
  61. // Get all chunks
  62. var allChunks = compilation.getStats().toJson().chunks;
  63. // Filter chunks (options.chunks and options.excludeCHunks)
  64. var chunks = self.filterChunks(allChunks, self.options.chunks, self.options.excludeChunks);
  65. // Sort chunks
  66. chunks = self.sortChunks(chunks, self.options.chunksSortMode);
  67. // Let plugins alter the chunks and the chunk sorting
  68. chunks = compilation.applyPluginsWaterfall('html-webpack-plugin-alter-chunks', chunks, { plugin: self });
  69. // Get assets
  70. var assets = self.htmlWebpackPluginAssets(compilation, chunks);
  71. // If this is a hot update compilation, move on!
  72. // This solves a problem where an `index.html` file is generated for hot-update js files
  73. // It only happens in Webpack 2, where hot updates are emitted separately before the full bundle
  74. if (self.isHotUpdateCompilation(assets)) {
  75. return callback();
  76. }
  77. // If the template and the assets did not change we don't have to emit the html
  78. var assetJson = JSON.stringify(self.getAssetFiles(assets));
  79. if (isCompilationCached && self.options.cache && assetJson === self.assetJson) {
  80. return callback();
  81. } else {
  82. self.assetJson = assetJson;
  83. }
  84. Promise.resolve()
  85. // Favicon
  86. .then(function () {
  87. if (self.options.favicon) {
  88. return self.addFileToAssets(self.options.favicon, compilation)
  89. .then(function (faviconBasename) {
  90. var publicPath = compilation.mainTemplate.getPublicPath({hash: compilation.hash}) || '';
  91. if (publicPath && publicPath.substr(-1) !== '/') {
  92. publicPath += '/';
  93. }
  94. assets.favicon = publicPath + faviconBasename;
  95. });
  96. }
  97. })
  98. // Wait for the compilation to finish
  99. .then(function () {
  100. return compilationPromise;
  101. })
  102. .then(function (compiledTemplate) {
  103. // Allow to use a custom function / string instead
  104. if (self.options.templateContent !== undefined) {
  105. return self.options.templateContent;
  106. }
  107. // Once everything is compiled evaluate the html factory
  108. // and replace it with its content
  109. return self.evaluateCompilationResult(compilation, compiledTemplate);
  110. })
  111. // Allow plugins to make changes to the assets before invoking the template
  112. // This only makes sense to use if `inject` is `false`
  113. .then(function (compilationResult) {
  114. return applyPluginsAsyncWaterfall('html-webpack-plugin-before-html-generation', false, {
  115. assets: assets,
  116. outputName: self.childCompilationOutputName,
  117. plugin: self
  118. })
  119. .then(function () {
  120. return compilationResult;
  121. });
  122. })
  123. // Execute the template
  124. .then(function (compilationResult) {
  125. // If the loader result is a function execute it to retrieve the html
  126. // otherwise use the returned html
  127. return typeof compilationResult !== 'function'
  128. ? compilationResult
  129. : self.executeTemplate(compilationResult, chunks, assets, compilation);
  130. })
  131. // Allow plugins to change the html before assets are injected
  132. .then(function (html) {
  133. var pluginArgs = {html: html, assets: assets, plugin: self, outputName: self.childCompilationOutputName};
  134. return applyPluginsAsyncWaterfall('html-webpack-plugin-before-html-processing', true, pluginArgs);
  135. })
  136. .then(function (result) {
  137. var html = result.html;
  138. var assets = result.assets;
  139. // Prepare script and link tags
  140. var assetTags = self.generateAssetTags(assets);
  141. var pluginArgs = {head: assetTags.head, body: assetTags.body, plugin: self, chunks: chunks, outputName: self.childCompilationOutputName};
  142. // Allow plugins to change the assetTag definitions
  143. return applyPluginsAsyncWaterfall('html-webpack-plugin-alter-asset-tags', true, pluginArgs)
  144. .then(function (result) {
  145. // Add the stylesheets, scripts and so on to the resulting html
  146. return self.postProcessHtml(html, assets, { body: result.body, head: result.head })
  147. .then(function (html) {
  148. return _.extend(result, {html: html, assets: assets});
  149. });
  150. });
  151. })
  152. // Allow plugins to change the html after assets are injected
  153. .then(function (result) {
  154. var html = result.html;
  155. var assets = result.assets;
  156. var pluginArgs = {html: html, assets: assets, plugin: self, outputName: self.childCompilationOutputName};
  157. return applyPluginsAsyncWaterfall('html-webpack-plugin-after-html-processing', true, pluginArgs)
  158. .then(function (result) {
  159. return result.html;
  160. });
  161. })
  162. .catch(function (err) {
  163. // In case anything went wrong the promise is resolved
  164. // with the error message and an error is logged
  165. compilation.errors.push(prettyError(err, compiler.context).toString());
  166. // Prevent caching
  167. self.hash = null;
  168. return self.options.showErrors ? prettyError(err, compiler.context).toHtml() : 'ERROR';
  169. })
  170. .then(function (html) {
  171. // Replace the compilation result with the evaluated html code
  172. compilation.assets[self.childCompilationOutputName] = {
  173. source: function () {
  174. return html;
  175. },
  176. size: function () {
  177. return html.length;
  178. }
  179. };
  180. })
  181. .then(function () {
  182. // Let other plugins know that we are done:
  183. return applyPluginsAsyncWaterfall('html-webpack-plugin-after-emit', false, {
  184. html: compilation.assets[self.childCompilationOutputName],
  185. outputName: self.childCompilationOutputName,
  186. plugin: self
  187. }).catch(function (err) {
  188. console.error(err);
  189. return null;
  190. }).then(function () {
  191. return null;
  192. });
  193. })
  194. // Let webpack continue with it
  195. .finally(function () {
  196. callback();
  197. // Tell blue bird that we don't want to wait for callback.
  198. // Fixes "Warning: a promise was created in a handler but none were returned from it"
  199. // https://github.com/petkaantonov/bluebird/blob/master/docs/docs/warning-explanations.md#warning-a-promise-was-created-in-a-handler-but-none-were-returned-from-it
  200. return null;
  201. });
  202. });
  203. };
  204. /**
  205. * Evaluates the child compilation result
  206. * Returns a promise
  207. */
  208. HtmlWebpackPlugin.prototype.evaluateCompilationResult = function (compilation, source) {
  209. if (!source) {
  210. return Promise.reject('The child compilation didn\'t provide a result');
  211. }
  212. // The LibraryTemplatePlugin stores the template result in a local variable.
  213. // To extract the result during the evaluation this part has to be removed.
  214. source = source.replace('var HTML_WEBPACK_PLUGIN_RESULT =', '');
  215. var template = this.options.template.replace(/^.+!/, '').replace(/\?.+$/, '');
  216. var vmContext = vm.createContext(_.extend({HTML_WEBPACK_PLUGIN: true, require: require}, global));
  217. var vmScript = new vm.Script(source, {filename: template});
  218. // Evaluate code and cast to string
  219. var newSource;
  220. try {
  221. newSource = vmScript.runInContext(vmContext);
  222. } catch (e) {
  223. return Promise.reject(e);
  224. }
  225. if (typeof newSource === 'object' && newSource.__esModule && newSource.default) {
  226. newSource = newSource.default;
  227. }
  228. return typeof newSource === 'string' || typeof newSource === 'function'
  229. ? Promise.resolve(newSource)
  230. : Promise.reject('The loader "' + this.options.template + '" didn\'t return html.');
  231. };
  232. /**
  233. * Html post processing
  234. *
  235. * Returns a promise
  236. */
  237. HtmlWebpackPlugin.prototype.executeTemplate = function (templateFunction, chunks, assets, compilation) {
  238. var self = this;
  239. return Promise.resolve()
  240. // Template processing
  241. .then(function () {
  242. var templateParams = {
  243. compilation: compilation,
  244. webpack: compilation.getStats().toJson(),
  245. webpackConfig: compilation.options,
  246. htmlWebpackPlugin: {
  247. files: assets,
  248. options: self.options
  249. }
  250. };
  251. var html = '';
  252. try {
  253. html = templateFunction(templateParams);
  254. } catch (e) {
  255. compilation.errors.push(new Error('Template execution failed: ' + e));
  256. return Promise.reject(e);
  257. }
  258. return html;
  259. });
  260. };
  261. /**
  262. * Html post processing
  263. *
  264. * Returns a promise
  265. */
  266. HtmlWebpackPlugin.prototype.postProcessHtml = function (html, assets, assetTags) {
  267. var self = this;
  268. if (typeof html !== 'string') {
  269. return Promise.reject('Expected html to be a string but got ' + JSON.stringify(html));
  270. }
  271. return Promise.resolve()
  272. // Inject
  273. .then(function () {
  274. if (self.options.inject) {
  275. return self.injectAssetsIntoHtml(html, assets, assetTags);
  276. } else {
  277. return html;
  278. }
  279. })
  280. // Minify
  281. .then(function (html) {
  282. if (self.options.minify) {
  283. var minify = require('html-minifier').minify;
  284. return minify(html, self.options.minify);
  285. }
  286. return html;
  287. });
  288. };
  289. /*
  290. * Pushes the content of the given filename to the compilation assets
  291. */
  292. HtmlWebpackPlugin.prototype.addFileToAssets = function (filename, compilation) {
  293. filename = path.resolve(compilation.compiler.context, filename);
  294. return Promise.props({
  295. size: fs.statAsync(filename),
  296. source: fs.readFileAsync(filename)
  297. })
  298. .catch(function () {
  299. return Promise.reject(new Error('HtmlWebpackPlugin: could not load file ' + filename));
  300. })
  301. .then(function (results) {
  302. var basename = path.basename(filename);
  303. compilation.fileDependencies.push(filename);
  304. compilation.assets[basename] = {
  305. source: function () {
  306. return results.source;
  307. },
  308. size: function () {
  309. return results.size.size;
  310. }
  311. };
  312. return basename;
  313. });
  314. };
  315. /**
  316. * Helper to sort chunks
  317. */
  318. HtmlWebpackPlugin.prototype.sortChunks = function (chunks, sortMode) {
  319. // Sort mode auto by default:
  320. if (typeof sortMode === 'undefined') {
  321. sortMode = 'auto';
  322. }
  323. // Custom function
  324. if (typeof sortMode === 'function') {
  325. return chunks.sort(sortMode);
  326. }
  327. // Disabled sorting:
  328. if (sortMode === 'none') {
  329. return chunkSorter.none(chunks);
  330. }
  331. // Check if the given sort mode is a valid chunkSorter sort mode
  332. if (typeof chunkSorter[sortMode] !== 'undefined') {
  333. return chunkSorter[sortMode](chunks, this.options.chunks);
  334. }
  335. throw new Error('"' + sortMode + '" is not a valid chunk sort mode');
  336. };
  337. /**
  338. * Return all chunks from the compilation result which match the exclude and include filters
  339. */
  340. HtmlWebpackPlugin.prototype.filterChunks = function (chunks, includedChunks, excludedChunks) {
  341. return chunks.filter(function (chunk) {
  342. var chunkName = chunk.names[0];
  343. // This chunk doesn't have a name. This script can't handled it.
  344. if (chunkName === undefined) {
  345. return false;
  346. }
  347. // Skip if the chunk should be lazy loaded
  348. if (typeof chunk.isInitial === 'function') {
  349. if (!chunk.isInitial()) {
  350. return false;
  351. }
  352. } else if (!chunk.initial) {
  353. return false;
  354. }
  355. // Skip if the chunks should be filtered and the given chunk was not added explicity
  356. if (Array.isArray(includedChunks) && includedChunks.indexOf(chunkName) === -1) {
  357. return false;
  358. }
  359. // Skip if the chunks should be filtered and the given chunk was excluded explicity
  360. if (Array.isArray(excludedChunks) && excludedChunks.indexOf(chunkName) !== -1) {
  361. return false;
  362. }
  363. // Add otherwise
  364. return true;
  365. });
  366. };
  367. HtmlWebpackPlugin.prototype.isHotUpdateCompilation = function (assets) {
  368. return assets.js.length && assets.js.every(function (name) {
  369. return /\.hot-update\.js$/.test(name);
  370. });
  371. };
  372. HtmlWebpackPlugin.prototype.htmlWebpackPluginAssets = function (compilation, chunks) {
  373. var self = this;
  374. var compilationHash = compilation.hash;
  375. // Use the configured public path or build a relative path
  376. var publicPath = typeof compilation.options.output.publicPath !== 'undefined'
  377. // If a hard coded public path exists use it
  378. ? compilation.mainTemplate.getPublicPath({hash: compilationHash})
  379. // If no public path was set get a relative url path
  380. : path.relative(path.resolve(compilation.options.output.path, path.dirname(self.childCompilationOutputName)), compilation.options.output.path)
  381. .split(path.sep).join('/');
  382. if (publicPath.length && publicPath.substr(-1, 1) !== '/') {
  383. publicPath += '/';
  384. }
  385. var assets = {
  386. // The public path
  387. publicPath: publicPath,
  388. // Will contain all js & css files by chunk
  389. chunks: {},
  390. // Will contain all js files
  391. js: [],
  392. // Will contain all css files
  393. css: [],
  394. // Will contain the html5 appcache manifest files if it exists
  395. manifest: Object.keys(compilation.assets).filter(function (assetFile) {
  396. return path.extname(assetFile) === '.appcache';
  397. })[0]
  398. };
  399. // Append a hash for cache busting
  400. if (this.options.hash) {
  401. assets.manifest = self.appendHash(assets.manifest, compilationHash);
  402. assets.favicon = self.appendHash(assets.favicon, compilationHash);
  403. }
  404. for (var i = 0; i < chunks.length; i++) {
  405. var chunk = chunks[i];
  406. var chunkName = chunk.names[0];
  407. assets.chunks[chunkName] = {};
  408. // Prepend the public path to all chunk files
  409. var chunkFiles = [].concat(chunk.files).map(function (chunkFile) {
  410. return publicPath + chunkFile;
  411. });
  412. // Append a hash for cache busting
  413. if (this.options.hash) {
  414. chunkFiles = chunkFiles.map(function (chunkFile) {
  415. return self.appendHash(chunkFile, compilationHash);
  416. });
  417. }
  418. // Webpack outputs an array for each chunk when using sourcemaps
  419. // But we need only the entry file
  420. var entry = chunkFiles[0];
  421. assets.chunks[chunkName].size = chunk.size;
  422. assets.chunks[chunkName].entry = entry;
  423. assets.chunks[chunkName].hash = chunk.hash;
  424. assets.js.push(entry);
  425. // Gather all css files
  426. var css = chunkFiles.filter(function (chunkFile) {
  427. // Some chunks may contain content hash in their names, for ex. 'main.css?1e7cac4e4d8b52fd5ccd2541146ef03f'.
  428. // We must proper handle such cases, so we use regexp testing here
  429. return /.css($|\?)/.test(chunkFile);
  430. });
  431. assets.chunks[chunkName].css = css;
  432. assets.css = assets.css.concat(css);
  433. }
  434. // Duplicate css assets can occur on occasion if more than one chunk
  435. // requires the same css.
  436. assets.css = _.uniq(assets.css);
  437. return assets;
  438. };
  439. /**
  440. * Injects the assets into the given html string
  441. */
  442. HtmlWebpackPlugin.prototype.generateAssetTags = function (assets) {
  443. // Turn script files into script tags
  444. var scripts = assets.js.map(function (scriptPath) {
  445. return {
  446. tagName: 'script',
  447. closeTag: true,
  448. attributes: {
  449. type: 'text/javascript',
  450. src: scriptPath
  451. }
  452. };
  453. });
  454. // Make tags self-closing in case of xhtml
  455. var selfClosingTag = !!this.options.xhtml;
  456. // Turn css files into link tags
  457. var styles = assets.css.map(function (stylePath) {
  458. return {
  459. tagName: 'link',
  460. selfClosingTag: selfClosingTag,
  461. attributes: {
  462. href: stylePath,
  463. rel: 'stylesheet'
  464. }
  465. };
  466. });
  467. // Injection targets
  468. var head = [];
  469. var body = [];
  470. // If there is a favicon present, add it to the head
  471. if (assets.favicon) {
  472. head.push({
  473. tagName: 'link',
  474. selfClosingTag: selfClosingTag,
  475. attributes: {
  476. rel: 'shortcut icon',
  477. href: assets.favicon
  478. }
  479. });
  480. }
  481. // Add styles to the head
  482. head = head.concat(styles);
  483. // Add scripts to body or head
  484. if (this.options.inject === 'head') {
  485. head = head.concat(scripts);
  486. } else {
  487. body = body.concat(scripts);
  488. }
  489. return {head: head, body: body};
  490. };
  491. /**
  492. * Injects the assets into the given html string
  493. */
  494. HtmlWebpackPlugin.prototype.injectAssetsIntoHtml = function (html, assets, assetTags) {
  495. var htmlRegExp = /(<html[^>]*>)/i;
  496. var headRegExp = /(<\/head\s*>)/i;
  497. var bodyRegExp = /(<\/body\s*>)/i;
  498. var body = assetTags.body.map(this.createHtmlTag);
  499. var head = assetTags.head.map(this.createHtmlTag);
  500. if (body.length) {
  501. if (bodyRegExp.test(html)) {
  502. // Append assets to body element
  503. html = html.replace(bodyRegExp, function (match) {
  504. return body.join('') + match;
  505. });
  506. } else {
  507. // Append scripts to the end of the file if no <body> element exists:
  508. html += body.join('');
  509. }
  510. }
  511. if (head.length) {
  512. // Create a head tag if none exists
  513. if (!headRegExp.test(html)) {
  514. if (!htmlRegExp.test(html)) {
  515. html = '<head></head>' + html;
  516. } else {
  517. html = html.replace(htmlRegExp, function (match) {
  518. return match + '<head></head>';
  519. });
  520. }
  521. }
  522. // Append assets to head element
  523. html = html.replace(headRegExp, function (match) {
  524. return head.join('') + match;
  525. });
  526. }
  527. // Inject manifest into the opening html tag
  528. if (assets.manifest) {
  529. html = html.replace(/(<html[^>]*)(>)/i, function (match, start, end) {
  530. // Append the manifest only if no manifest was specified
  531. if (/\smanifest\s*=/.test(match)) {
  532. return match;
  533. }
  534. return start + ' manifest="' + assets.manifest + '"' + end;
  535. });
  536. }
  537. return html;
  538. };
  539. /**
  540. * Appends a cache busting hash
  541. */
  542. HtmlWebpackPlugin.prototype.appendHash = function (url, hash) {
  543. if (!url) {
  544. return url;
  545. }
  546. return url + (url.indexOf('?') === -1 ? '?' : '&') + hash;
  547. };
  548. /**
  549. * Turn a tag definition into a html string
  550. */
  551. HtmlWebpackPlugin.prototype.createHtmlTag = function (tagDefinition) {
  552. var attributes = Object.keys(tagDefinition.attributes || {})
  553. .filter(function (attributeName) {
  554. return tagDefinition.attributes[attributeName] !== false;
  555. })
  556. .map(function (attributeName) {
  557. if (tagDefinition.attributes[attributeName] === true) {
  558. return attributeName;
  559. }
  560. return attributeName + '="' + tagDefinition.attributes[attributeName] + '"';
  561. });
  562. // Backport of 3.x void tag definition
  563. var voidTag = tagDefinition.voidTag !== undefined ? tagDefinition.voidTag : !tagDefinition.closeTag;
  564. var selfClosingTag = tagDefinition.voidTag !== undefined ? tagDefinition.voidTag && this.options.xhtml : tagDefinition.selfClosingTag;
  565. return '<' + [tagDefinition.tagName].concat(attributes).join(' ') + (selfClosingTag ? '/' : '') + '>' +
  566. (tagDefinition.innerHTML || '') +
  567. (voidTag ? '' : '</' + tagDefinition.tagName + '>');
  568. };
  569. /**
  570. * Helper to return the absolute template path with a fallback loader
  571. */
  572. HtmlWebpackPlugin.prototype.getFullTemplatePath = function (template, context) {
  573. // If the template doesn't use a loader use the lodash template loader
  574. if (template.indexOf('!') === -1) {
  575. template = require.resolve('./lib/loader.js') + '!' + path.resolve(context, template);
  576. }
  577. // Resolve template path
  578. return template.replace(
  579. /([!])([^/\\][^!?]+|[^/\\!?])($|\?[^!?\n]+$)/,
  580. function (match, prefix, filepath, postfix) {
  581. return prefix + path.resolve(filepath) + postfix;
  582. });
  583. };
  584. /**
  585. * Helper to return a sorted unique array of all asset files out of the
  586. * asset object
  587. */
  588. HtmlWebpackPlugin.prototype.getAssetFiles = function (assets) {
  589. var files = _.uniq(Object.keys(assets).filter(function (assetType) {
  590. return assetType !== 'chunks' && assets[assetType];
  591. }).reduce(function (files, assetType) {
  592. return files.concat(assets[assetType]);
  593. }, []));
  594. files.sort();
  595. return files;
  596. };
  597. /**
  598. * Helper to promisify compilation.applyPluginsAsyncWaterfall that returns
  599. * a function that helps to merge given plugin arguments with processed ones
  600. */
  601. HtmlWebpackPlugin.prototype.applyPluginsAsyncWaterfall = function (compilation) {
  602. var promisedApplyPluginsAsyncWaterfall = Promise.promisify(compilation.applyPluginsAsyncWaterfall, {context: compilation});
  603. return function (eventName, requiresResult, pluginArgs) {
  604. return promisedApplyPluginsAsyncWaterfall(eventName, pluginArgs)
  605. .then(function (result) {
  606. if (requiresResult && !result) {
  607. compilation.warnings.push(new Error('Using ' + eventName + ' without returning a result is deprecated.'));
  608. }
  609. return _.extend(pluginArgs, result);
  610. });
  611. };
  612. };
  613. module.exports = HtmlWebpackPlugin;