UmdMainTemplatePlugin.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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").ConcatSource;
  7. const OriginalSource = require("webpack-sources").OriginalSource;
  8. const Template = require("./Template");
  9. function accessorToObjectAccess(accessor) {
  10. return accessor.map(a => `[${JSON.stringify(a)}]`).join("");
  11. }
  12. function accessorAccess(base, accessor) {
  13. accessor = [].concat(accessor);
  14. return accessor.map((a, idx) => {
  15. a = base + accessorToObjectAccess(accessor.slice(0, idx + 1));
  16. if(idx === accessor.length - 1) return a;
  17. return `${a} = ${a} || {}`;
  18. }).join(", ");
  19. }
  20. class UmdMainTemplatePlugin {
  21. constructor(name, options) {
  22. this.name = name;
  23. this.optionalAmdExternalAsGlobal = options.optionalAmdExternalAsGlobal;
  24. this.namedDefine = options.namedDefine;
  25. this.auxiliaryComment = options.auxiliaryComment;
  26. }
  27. apply(compilation) {
  28. const mainTemplate = compilation.mainTemplate;
  29. compilation.templatesPlugin("render-with-entry", function(source, chunk, hash) {
  30. let externals = chunk.modules.filter(m => m.external);
  31. const optionalExternals = [];
  32. let requiredExternals = [];
  33. if(this.optionalAmdExternalAsGlobal) {
  34. externals.forEach(m => {
  35. if(m.optional) {
  36. optionalExternals.push(m);
  37. } else {
  38. requiredExternals.push(m);
  39. }
  40. });
  41. externals = requiredExternals.concat(optionalExternals);
  42. } else {
  43. requiredExternals = externals;
  44. }
  45. function replaceKeys(str) {
  46. return mainTemplate.applyPluginsWaterfall("asset-path", str, {
  47. hash,
  48. chunk
  49. });
  50. }
  51. function externalsDepsArray(modules) {
  52. return `[${replaceKeys(modules.map(m => JSON.stringify(typeof m.request === "object" ? m.request.amd : m.request)).join(", "))}]`;
  53. }
  54. function externalsRootArray(modules) {
  55. return replaceKeys(modules.map(m => {
  56. let request = m.request;
  57. if(typeof request === "object") request = request.root;
  58. return `root${accessorToObjectAccess([].concat(request))}`;
  59. }).join(", "));
  60. }
  61. function externalsRequireArray(type) {
  62. return replaceKeys(externals.map(m => {
  63. let expr;
  64. let request = m.request;
  65. if(typeof request === "object") request = request[type];
  66. if(Array.isArray(request)) {
  67. expr = `require(${JSON.stringify(request[0])})${accessorToObjectAccess(request.slice(1))}`;
  68. } else
  69. expr = `require(${JSON.stringify(request)})`;
  70. if(m.optional) {
  71. expr = `(function webpackLoadOptionalExternalModule() { try { return ${expr}; } catch(e) {} }())`;
  72. }
  73. return expr;
  74. }).join(", "));
  75. }
  76. function externalsArguments(modules) {
  77. return modules.map(m => Template.toIdentifier(`__WEBPACK_EXTERNAL_MODULE_${m.id}__`)).join(", ");
  78. }
  79. function libraryName(library) {
  80. return JSON.stringify(replaceKeys([].concat(library).pop()));
  81. }
  82. let amdFactory;
  83. if(optionalExternals.length > 0) {
  84. const wrapperArguments = externalsArguments(requiredExternals);
  85. const factoryArguments = requiredExternals.length > 0 ?
  86. externalsArguments(requiredExternals) + ", " + externalsRootArray(optionalExternals) :
  87. externalsRootArray(optionalExternals);
  88. amdFactory = `function webpackLoadOptionalExternalModuleAmd(${wrapperArguments}) {\n` +
  89. ` return factory(${factoryArguments});\n` +
  90. " }";
  91. } else {
  92. amdFactory = "factory";
  93. }
  94. return new ConcatSource(new OriginalSource(
  95. "(function webpackUniversalModuleDefinition(root, factory) {\n" +
  96. (this.auxiliaryComment &&
  97. typeof this.auxiliaryComment === "string" ?
  98. " //" + this.auxiliaryComment + "\n" :
  99. this.auxiliaryComment.commonjs2 ?
  100. " //" + this.auxiliaryComment.commonjs2 + "\n" :
  101. ""
  102. ) +
  103. " if(typeof exports === 'object' && typeof module === 'object')\n" +
  104. " module.exports = factory(" + externalsRequireArray("commonjs2") + ");\n" +
  105. (this.auxiliaryComment &&
  106. typeof this.auxiliaryComment === "string" ?
  107. " //" + this.auxiliaryComment + "\n" :
  108. this.auxiliaryComment.amd ?
  109. " //" + this.auxiliaryComment.amd + "\n" :
  110. ""
  111. ) +
  112. " else if(typeof define === 'function' && define.amd)\n" +
  113. (requiredExternals.length > 0 ?
  114. (this.name && this.namedDefine === true ?
  115. " define(" + libraryName(this.name) + ", " + externalsDepsArray(requiredExternals) + ", " + amdFactory + ");\n" :
  116. " define(" + externalsDepsArray(requiredExternals) + ", " + amdFactory + ");\n"
  117. ) :
  118. (this.name && this.namedDefine === true ?
  119. " define(" + libraryName(this.name) + ", [], " + amdFactory + ");\n" :
  120. " define([], " + amdFactory + ");\n"
  121. )
  122. ) +
  123. (this.name ?
  124. (this.auxiliaryComment &&
  125. typeof this.auxiliaryComment === "string" ?
  126. " //" + this.auxiliaryComment + "\n" :
  127. this.auxiliaryComment.commonjs ?
  128. " //" + this.auxiliaryComment.commonjs + "\n" :
  129. ""
  130. ) +
  131. " else if(typeof exports === 'object')\n" +
  132. " exports[" + libraryName(this.name) + "] = factory(" + externalsRequireArray("commonjs") + ");\n" +
  133. (this.auxiliaryComment &&
  134. typeof this.auxiliaryComment === "string" ?
  135. " //" + this.auxiliaryComment + "\n" :
  136. this.auxiliaryComment.root ?
  137. " //" + this.auxiliaryComment.root + "\n" :
  138. ""
  139. ) +
  140. " else\n" +
  141. " " + replaceKeys(accessorAccess("root", this.name)) + " = factory(" + externalsRootArray(externals) + ");\n" :
  142. " else {\n" +
  143. (externals.length > 0 ?
  144. " var a = typeof exports === 'object' ? factory(" + externalsRequireArray("commonjs") + ") : factory(" + externalsRootArray(externals) + ");\n" :
  145. " var a = factory();\n"
  146. ) +
  147. " for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];\n" +
  148. " }\n"
  149. ) +
  150. "})(this, function(" + externalsArguments(externals) + ") {\nreturn ", "webpack/universalModuleDefinition"), source, ";\n})");
  151. }.bind(this));
  152. mainTemplate.plugin("global-hash-paths", function(paths) {
  153. if(this.name) paths = paths.concat(this.name);
  154. return paths;
  155. }.bind(this));
  156. mainTemplate.plugin("hash", function(hash) {
  157. hash.update("umd");
  158. hash.update(`${this.name}`);
  159. }.bind(this));
  160. }
  161. }
  162. module.exports = UmdMainTemplatePlugin;