Нет описания

chunk-src-version-param.js 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. const pluginName = 'AsyncChunkSrcVersionParameterPlugin';
  2. /**
  3. * Inspired by: https://github.com/webpack/webpack/issues/8115#issuecomment-663902035.
  4. *
  5. * This plugin modifies the webpack bootstrap code generated by the plugin at
  6. * webpack/lib/web/JsonpMainTemplatePlugin.js and the CSS chunk loading code generated
  7. * by @automattic/mini-css-extract-plugin-with-rtl.
  8. *
  9. * It will rename the function jsonpScriptSrc generated by that to webpackJsonpScriptSrc
  10. * and install a new version that checks a user provided variable containing a script
  11. * version parameter to specify in async chunk URLs.
  12. *
  13. * The jsonpScriptSrc override is only for webpack 4 (tested with 4.43 and 4.44).
  14. *
  15. * Webpack 5 has official support for this https://github.com/webpack/webpack/pull/8462
  16. * so it won't be necessary.
  17. *
  18. * It will also append the ?ver parameter to CSS chunk hrefs loaded by @automattic/mini-css-extract-plugin-with-rtl.
  19. */
  20. class AsyncChunkSrcVersionParameterPlugin {
  21. _applyMainTemplate( mainTemplate ) {
  22. // Append script version to all async JS chunks loaded with jsonpScriptSrc().
  23. mainTemplate.hooks.localVars.tap(
  24. // Use stage 1 to ensure this executes after webpack/lib/web/JsonpMainTemplatePlugin.js.
  25. { name: pluginName, stage: 1 },
  26. ( source ) => {
  27. if ( source.includes( 'function jsonpScriptSrc' ) ) {
  28. const modSource = source.replace(
  29. 'function jsonpScriptSrc',
  30. 'function webpackJsonpScriptSrc'
  31. );
  32. return `${ modSource }
  33. function jsonpScriptSrc(chunkId) {
  34. var src = webpackJsonpScriptSrc(chunkId);
  35. if ( window.wcAdminAssets && window.wcAdminAssets.version ) {
  36. src += '?ver=' + window.wcAdminAssets.version;
  37. }
  38. return src;
  39. }
  40. `;
  41. }
  42. return source;
  43. }
  44. );
  45. // Append script version to all async CSS chunks loaded by @automattic/mini-css-extract-plugin-with-rtl.
  46. mainTemplate.hooks.requireEnsure.tap(
  47. // Use stage 1 to ensure this executes after @automattic/mini-css-extract-plugin-with-rtl.
  48. { name: pluginName, stage: 1 },
  49. ( source ) => {
  50. if (
  51. source.includes( '// mini-css-extract-plugin CSS loading' )
  52. ) {
  53. return source.replace(
  54. 'linkTag.href = fullhref;',
  55. `linkTag.href = fullhref;
  56. if ( window.wcAdminAssets && window.wcAdminAssets.version ) {
  57. linkTag.href += '?ver=' + window.wcAdminAssets.version;
  58. }`
  59. );
  60. }
  61. return source;
  62. }
  63. );
  64. }
  65. apply( compiler ) {
  66. compiler.hooks.thisCompilation.tap( pluginName, ( compilation ) => {
  67. this._applyMainTemplate( compilation.mainTemplate );
  68. } );
  69. }
  70. }
  71. module.exports = AsyncChunkSrcVersionParameterPlugin;