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.

106 lines
2.5 KiB

4 years ago
  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;