plugin.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. 'use strict';
  2. const { EVENT, PLUGIN } = require('./constants.js');
  3. const debug = require('./common.js').debug;
  4. const matches = require('./common.js').matches;
  5. const normaliseOptions = require('./config.js');
  6. const shouldAddResourceHints = require('./resource-hints.js').shouldAddResourceHints;
  7. const addInitialChunkResourceHints = require('./initial-chunk-resource-hints.js');
  8. const addAsyncChunkResourceHints = require('./async-chunk-resource-hints.js');
  9. const elements = require('./elements.js');
  10. const customAttributes = require('./custom-attributes.js');
  11. const debugEvent = msg => debug(`${EVENT}: ${msg}`);
  12. const falsySafeConcat = arrays =>
  13. arrays.reduce(
  14. (combined, array) => array ? combined.concat(array) : combined,
  15. []
  16. );
  17. const getHtmlWebpackOptions = pluginArgs =>
  18. (pluginArgs && pluginArgs.plugin && pluginArgs.plugin.options)
  19. ? pluginArgs.plugin.options
  20. : {};
  21. const getCompilationOptions = compilation =>
  22. (compilation && compilation.options) ? compilation.options : {};
  23. class ScriptExtHtmlWebpackPlugin {
  24. constructor (options) {
  25. this.options = normaliseOptions(options);
  26. }
  27. apply (compiler) {
  28. const compile = this.compilationCallback.bind(this);
  29. const emit = this.emitCallback.bind(this);
  30. if (compiler.hooks) {
  31. compiler.hooks.compilation.tap(PLUGIN, compile);
  32. compiler.hooks.emit.tap(PLUGIN, emit);
  33. } else {
  34. compiler.plugin('compilation', compile);
  35. compiler.plugin('emit', emit);
  36. }
  37. }
  38. compilationCallback (compilation) {
  39. const alterAssetTags = this.alterAssetTagsCallback.bind(this, compilation);
  40. if (compilation.hooks) {
  41. compilation.hooks.htmlWebpackPluginAlterAssetTags.tap(PLUGIN, alterAssetTags);
  42. } else {
  43. compilation.plugin(EVENT, alterAssetTags);
  44. }
  45. }
  46. alterAssetTagsCallback (compilation, pluginArgs, callback) {
  47. const options = this.options;
  48. try {
  49. options.htmlWebpackOptions = getHtmlWebpackOptions(pluginArgs);
  50. options.compilationOptions = getCompilationOptions(compilation);
  51. debugEvent('starting');
  52. if (elements.shouldUpdate(options)) {
  53. debugEvent('replacing <head> <script> elements');
  54. pluginArgs.head = elements.update(compilation.assets, options, pluginArgs.head);
  55. debugEvent('replacing <body> <script> elements');
  56. pluginArgs.body = elements.update(compilation.assets, options, pluginArgs.body);
  57. }
  58. if (shouldAddResourceHints(options)) {
  59. debugEvent('adding resource hints');
  60. pluginArgs.head = falsySafeConcat([
  61. pluginArgs.head,
  62. addInitialChunkResourceHints(options, pluginArgs.head),
  63. addInitialChunkResourceHints(options, pluginArgs.body),
  64. addAsyncChunkResourceHints(compilation.chunks, options)
  65. ]);
  66. }
  67. if (customAttributes.shouldAdd(options)) {
  68. debugEvent('adding custom attribues to <head> <script> elements');
  69. pluginArgs.head = customAttributes.add(options, pluginArgs.head);
  70. debugEvent('adding custom attributes to <body> <script> elements');
  71. pluginArgs.body = customAttributes.add(options, pluginArgs.body);
  72. }
  73. debugEvent('completed');
  74. if (callback) {
  75. callback(null, pluginArgs);
  76. }
  77. } catch (err) {
  78. if (callback) {
  79. callback(err);
  80. } else {
  81. compilation.errors.push(err);
  82. }
  83. }
  84. }
  85. emitCallback (compilation, callback) {
  86. const options = this.options;
  87. if (options.inline.test.length > 0 && options.removeInlinedAssets) {
  88. debug('emit: deleting assets');
  89. Object.keys(compilation.assets).forEach((assetName) => {
  90. if (matches(assetName, options.inline.test)) {
  91. debug(`emit: deleting asset '${assetName}'`);
  92. delete compilation.assets[assetName];
  93. }
  94. });
  95. }
  96. if (callback) {
  97. callback();
  98. }
  99. }
  100. }
  101. module.exports = ScriptExtHtmlWebpackPlugin;