UmdLibraryPlugin.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { ConcatSource, OriginalSource } = require("webpack-sources");
  7. const ExternalModule = require("../ExternalModule");
  8. const Template = require("../Template");
  9. const AbstractLibraryPlugin = require("./AbstractLibraryPlugin");
  10. /** @typedef {import("webpack-sources").Source} Source */
  11. /** @typedef {import("../../declarations/WebpackOptions").LibraryCustomUmdCommentObject} LibraryCustomUmdCommentObject */
  12. /** @typedef {import("../../declarations/WebpackOptions").LibraryCustomUmdObject} LibraryCustomUmdObject */
  13. /** @typedef {import("../../declarations/WebpackOptions").LibraryName} LibraryName */
  14. /** @typedef {import("../../declarations/WebpackOptions").LibraryOptions} LibraryOptions */
  15. /** @typedef {import("../../declarations/WebpackOptions").LibraryType} LibraryType */
  16. /** @typedef {import("../javascript/JavascriptModulesPlugin").RenderContext} RenderContext */
  17. /** @typedef {import("../ExternalModule").RequestRecord} RequestRecord */
  18. /**
  19. * @template T
  20. * @typedef {import("./AbstractLibraryPlugin").LibraryContext<T>} LibraryContext<T>
  21. */
  22. /**
  23. * @param {string[]} accessor the accessor to convert to path
  24. * @returns {string} the path
  25. */
  26. const accessorToObjectAccess = (accessor) =>
  27. accessor.map((a) => `[${JSON.stringify(a)}]`).join("");
  28. /** @typedef {string | string[]} Accessor */
  29. /**
  30. * @param {string | undefined} base the path prefix
  31. * @param {Accessor} accessor the accessor
  32. * @param {string=} joinWith the element separator
  33. * @returns {string} the path
  34. */
  35. const accessorAccess = (base, accessor, joinWith = ", ") => {
  36. const accessors = Array.isArray(accessor) ? accessor : [accessor];
  37. return accessors
  38. .map((_, idx) => {
  39. const a = base
  40. ? base + accessorToObjectAccess(accessors.slice(0, idx + 1))
  41. : accessors[0] + accessorToObjectAccess(accessors.slice(1, idx + 1));
  42. if (idx === accessors.length - 1) return a;
  43. if (idx === 0 && base === undefined) {
  44. return `${a} = typeof ${a} === "object" ? ${a} : {}`;
  45. }
  46. return `${a} = ${a} || {}`;
  47. })
  48. .join(joinWith);
  49. };
  50. /**
  51. * @typedef {object} UmdLibraryPluginOptions
  52. * @property {LibraryType} type
  53. * @property {boolean=} optionalAmdExternalAsGlobal
  54. */
  55. /**
  56. * @typedef {object} UmdLibraryPluginParsed
  57. * @property {string | string[] | undefined} name
  58. * @property {LibraryCustomUmdObject} names
  59. * @property {string | LibraryCustomUmdCommentObject | undefined} auxiliaryComment
  60. * @property {boolean | undefined} namedDefine
  61. */
  62. /**
  63. * @typedef {UmdLibraryPluginParsed} T
  64. * @extends {AbstractLibraryPlugin<UmdLibraryPluginParsed>}
  65. */
  66. class UmdLibraryPlugin extends AbstractLibraryPlugin {
  67. /**
  68. * @param {UmdLibraryPluginOptions} options the plugin option
  69. */
  70. constructor(options) {
  71. super({
  72. pluginName: "UmdLibraryPlugin",
  73. type: options.type
  74. });
  75. /** @type {UmdLibraryPluginOptions["optionalAmdExternalAsGlobal"]} */
  76. this.optionalAmdExternalAsGlobal = options.optionalAmdExternalAsGlobal;
  77. }
  78. /**
  79. * @param {LibraryOptions} library normalized library option
  80. * @returns {T} preprocess as needed by overriding
  81. */
  82. parseOptions(library) {
  83. /** @type {LibraryName | undefined} */
  84. let name;
  85. /** @type {LibraryCustomUmdObject} */
  86. let names;
  87. if (typeof library.name === "object" && !Array.isArray(library.name)) {
  88. name = library.name.root || library.name.amd || library.name.commonjs;
  89. names = library.name;
  90. } else {
  91. name = library.name;
  92. const singleName = Array.isArray(name) ? name[0] : name;
  93. names = {
  94. commonjs: singleName,
  95. root: library.name,
  96. amd: singleName
  97. };
  98. }
  99. return {
  100. name,
  101. names,
  102. auxiliaryComment: library.auxiliaryComment,
  103. namedDefine: library.umdNamedDefine
  104. };
  105. }
  106. /**
  107. * @param {Source} source source
  108. * @param {RenderContext} renderContext render context
  109. * @param {LibraryContext<T>} libraryContext context
  110. * @returns {Source} source with library export
  111. */
  112. render(
  113. source,
  114. { chunkGraph, runtimeTemplate, chunk, moduleGraph },
  115. { options, compilation }
  116. ) {
  117. const modules = chunkGraph
  118. .getChunkModules(chunk)
  119. .filter(
  120. (m) =>
  121. m instanceof ExternalModule &&
  122. (m.externalType === "umd" || m.externalType === "umd2")
  123. );
  124. let externals = /** @type {ExternalModule[]} */ (modules);
  125. /** @type {ExternalModule[]} */
  126. const optionalExternals = [];
  127. /** @type {ExternalModule[]} */
  128. let requiredExternals = [];
  129. if (this.optionalAmdExternalAsGlobal) {
  130. for (const m of externals) {
  131. if (m.isOptional(moduleGraph)) {
  132. optionalExternals.push(m);
  133. } else {
  134. requiredExternals.push(m);
  135. }
  136. }
  137. externals = [...requiredExternals, ...optionalExternals];
  138. } else {
  139. requiredExternals = externals;
  140. }
  141. /**
  142. * @param {string} str the string to replace
  143. * @returns {string} the replaced keys
  144. */
  145. const replaceKeys = (str) =>
  146. compilation.getPath(str, {
  147. chunk
  148. });
  149. /**
  150. * @param {ExternalModule[]} modules external modules
  151. * @returns {string} result
  152. */
  153. const externalsDepsArray = (modules) =>
  154. `[${replaceKeys(
  155. modules
  156. .map((m) =>
  157. JSON.stringify(
  158. typeof m.request === "object"
  159. ? /** @type {RequestRecord} */
  160. (m.request).amd
  161. : m.request
  162. )
  163. )
  164. .join(", ")
  165. )}]`;
  166. /**
  167. * @param {ExternalModule[]} modules external modules
  168. * @returns {string} result
  169. */
  170. const externalsRootArray = (modules) =>
  171. replaceKeys(
  172. modules
  173. .map((m) => {
  174. let request = m.request;
  175. if (typeof request === "object") {
  176. request =
  177. /** @type {RequestRecord} */
  178. (request).root;
  179. }
  180. return `root${accessorToObjectAccess([
  181. ...(Array.isArray(request) ? request : [request])
  182. ])}`;
  183. })
  184. .join(", ")
  185. );
  186. /**
  187. * @param {string} type the type
  188. * @returns {string} external require array
  189. */
  190. const externalsRequireArray = (type) =>
  191. replaceKeys(
  192. externals
  193. .map((m) => {
  194. let request = m.request;
  195. if (typeof request === "object") {
  196. request =
  197. /** @type {RequestRecord} */
  198. (request)[type];
  199. }
  200. if (request === undefined) {
  201. throw new Error(
  202. `Missing external configuration for type:${type}`
  203. );
  204. }
  205. let expr = Array.isArray(request)
  206. ? `require(${JSON.stringify(
  207. request[0]
  208. )})${accessorToObjectAccess(request.slice(1))}`
  209. : `require(${JSON.stringify(request)})`;
  210. if (m.isOptional(moduleGraph)) {
  211. expr = `(function webpackLoadOptionalExternalModule() { try { return ${expr}; } catch(e) {} }())`;
  212. }
  213. return expr;
  214. })
  215. .join(", ")
  216. );
  217. /**
  218. * @param {ExternalModule[]} modules external modules
  219. * @returns {string} arguments
  220. */
  221. const externalsArguments = (modules) =>
  222. modules
  223. .map(
  224. (m) =>
  225. `__WEBPACK_EXTERNAL_MODULE_${Template.toIdentifier(
  226. `${chunkGraph.getModuleId(m)}`
  227. )}__`
  228. )
  229. .join(", ");
  230. /**
  231. * @param {Accessor} library library name
  232. * @returns {string} stringified library name
  233. */
  234. const libraryName = (library) =>
  235. JSON.stringify(
  236. replaceKeys(
  237. /** @type {string} */
  238. ([...(Array.isArray(library) ? library : [library])].pop())
  239. )
  240. );
  241. /** @type {string} */
  242. let amdFactory;
  243. if (optionalExternals.length > 0) {
  244. const wrapperArguments = externalsArguments(requiredExternals);
  245. const factoryArguments =
  246. requiredExternals.length > 0
  247. ? `${externalsArguments(requiredExternals)}, ${externalsRootArray(
  248. optionalExternals
  249. )}`
  250. : externalsRootArray(optionalExternals);
  251. amdFactory =
  252. `function webpackLoadOptionalExternalModuleAmd(${wrapperArguments}) {\n` +
  253. ` return factory(${factoryArguments});\n` +
  254. " }";
  255. } else {
  256. amdFactory = "factory";
  257. }
  258. const { auxiliaryComment, namedDefine, names } = options;
  259. /**
  260. * @param {keyof LibraryCustomUmdCommentObject} type type
  261. * @returns {string} comment
  262. */
  263. const getAuxiliaryComment = (type) => {
  264. if (auxiliaryComment) {
  265. if (typeof auxiliaryComment === "string") {
  266. return `\t//${auxiliaryComment}\n`;
  267. }
  268. if (auxiliaryComment[type]) return `\t//${auxiliaryComment[type]}\n`;
  269. }
  270. return "";
  271. };
  272. return new ConcatSource(
  273. new OriginalSource(
  274. `(function webpackUniversalModuleDefinition(root, factory) {\n${getAuxiliaryComment(
  275. "commonjs2"
  276. )} if(typeof exports === 'object' && typeof module === 'object')\n` +
  277. ` module.exports = factory(${externalsRequireArray(
  278. "commonjs2"
  279. )});\n${getAuxiliaryComment(
  280. "amd"
  281. )} else if(typeof define === 'function' && define.amd)\n${
  282. requiredExternals.length > 0
  283. ? names.amd && namedDefine === true
  284. ? ` define(${libraryName(names.amd)}, ${externalsDepsArray(
  285. requiredExternals
  286. )}, ${amdFactory});\n`
  287. : ` define(${externalsDepsArray(requiredExternals)}, ${
  288. amdFactory
  289. });\n`
  290. : names.amd && namedDefine === true
  291. ? ` define(${libraryName(names.amd)}, [], ${amdFactory});\n`
  292. : ` define([], ${amdFactory});\n`
  293. }${
  294. names.root || names.commonjs
  295. ? `${getAuxiliaryComment(
  296. "commonjs"
  297. )} else if(typeof exports === 'object')\n` +
  298. ` exports[${libraryName(
  299. /** @type {Accessor} */
  300. (names.commonjs || names.root)
  301. )}] = factory(${externalsRequireArray(
  302. "commonjs"
  303. )});\n${getAuxiliaryComment("root")} else\n` +
  304. ` ${replaceKeys(
  305. accessorAccess(
  306. "root",
  307. /** @type {Accessor} */
  308. (names.root || names.commonjs)
  309. )
  310. )} = factory(${externalsRootArray(externals)});\n`
  311. : ` else {\n${
  312. externals.length > 0
  313. ? ` var a = typeof exports === 'object' ? factory(${externalsRequireArray(
  314. "commonjs"
  315. )}) : factory(${externalsRootArray(externals)});\n`
  316. : " var a = factory();\n"
  317. } for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];\n` +
  318. " }\n"
  319. }})(${runtimeTemplate.globalObject}, ${
  320. runtimeTemplate.supportsArrowFunction()
  321. ? `(${externalsArguments(externals)}) =>`
  322. : `function(${externalsArguments(externals)})`
  323. } {\nreturn `,
  324. "webpack/universalModuleDefinition"
  325. ),
  326. source,
  327. ";\n})"
  328. );
  329. }
  330. }
  331. module.exports = UmdLibraryPlugin;