You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AmdMainTemplatePlugin.js 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { ConcatSource } = require("webpack-sources");
  7. const Template = require("./Template");
  8. /** @typedef {import("./Compilation")} Compilation */
  9. /**
  10. * @typedef {Object} AmdMainTemplatePluginOptions
  11. * @param {string=} name the library name
  12. * @property {boolean=} requireAsWrapper
  13. */
  14. class AmdMainTemplatePlugin {
  15. /**
  16. * @param {AmdMainTemplatePluginOptions} options the plugin options
  17. */
  18. constructor(options) {
  19. if (!options || typeof options === "string") {
  20. this.name = options;
  21. this.requireAsWrapper = false;
  22. } else {
  23. this.name = options.name;
  24. this.requireAsWrapper = options.requireAsWrapper;
  25. }
  26. }
  27. /**
  28. * @param {Compilation} compilation the compilation instance
  29. * @returns {void}
  30. */
  31. apply(compilation) {
  32. const { mainTemplate, chunkTemplate } = compilation;
  33. const onRenderWithEntry = (source, chunk, hash) => {
  34. const externals = chunk.getModules().filter(m => m.external);
  35. const externalsDepsArray = JSON.stringify(
  36. externals.map(m =>
  37. typeof m.request === "object" ? m.request.amd : m.request
  38. )
  39. );
  40. const externalsArguments = externals
  41. .map(
  42. m => `__WEBPACK_EXTERNAL_MODULE_${Template.toIdentifier(`${m.id}`)}__`
  43. )
  44. .join(", ");
  45. if (this.requireAsWrapper) {
  46. return new ConcatSource(
  47. `require(${externalsDepsArray}, function(${externalsArguments}) { return `,
  48. source,
  49. "});"
  50. );
  51. } else if (this.name) {
  52. const name = mainTemplate.getAssetPath(this.name, {
  53. hash,
  54. chunk
  55. });
  56. return new ConcatSource(
  57. `define(${JSON.stringify(
  58. name
  59. )}, ${externalsDepsArray}, function(${externalsArguments}) { return `,
  60. source,
  61. "});"
  62. );
  63. } else if (externalsArguments) {
  64. return new ConcatSource(
  65. `define(${externalsDepsArray}, function(${externalsArguments}) { return `,
  66. source,
  67. "});"
  68. );
  69. } else {
  70. return new ConcatSource("define(function() { return ", source, "});");
  71. }
  72. };
  73. for (const template of [mainTemplate, chunkTemplate]) {
  74. template.hooks.renderWithEntry.tap(
  75. "AmdMainTemplatePlugin",
  76. onRenderWithEntry
  77. );
  78. }
  79. mainTemplate.hooks.globalHashPaths.tap("AmdMainTemplatePlugin", paths => {
  80. if (this.name) {
  81. paths.push(this.name);
  82. }
  83. return paths;
  84. });
  85. mainTemplate.hooks.hash.tap("AmdMainTemplatePlugin", hash => {
  86. hash.update("exports amd");
  87. if (this.name) {
  88. hash.update(this.name);
  89. }
  90. });
  91. }
  92. }
  93. module.exports = AmdMainTemplatePlugin;