MainTemplate.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const {
  7. ConcatSource,
  8. OriginalSource,
  9. PrefixSource,
  10. RawSource
  11. } = require("webpack-sources");
  12. const {
  13. Tapable,
  14. SyncWaterfallHook,
  15. SyncHook,
  16. SyncBailHook
  17. } = require("tapable");
  18. const Template = require("./Template");
  19. /** @typedef {import("webpack-sources").ConcatSource} ConcatSource */
  20. /** @typedef {import("webpack-sources").Source} Source */
  21. /** @typedef {import("./ModuleTemplate")} ModuleTemplate */
  22. /** @typedef {import("./Chunk")} Chunk */
  23. /** @typedef {import("./Module")} Module} */
  24. /** @typedef {import("./util/createHash").Hash} Hash} */
  25. /** @typedef {import("./Dependency").DependencyTemplate} DependencyTemplate} */
  26. /**
  27. * @typedef {Object} RenderManifestOptions
  28. * @property {Chunk} chunk the chunk used to render
  29. * @property {string} hash
  30. * @property {string} fullHash
  31. * @property {TODO} outputOptions
  32. * @property {{javascript: ModuleTemplate, webassembly: ModuleTemplate}} moduleTemplates
  33. * @property {Map<TODO, TODO>} dependencyTemplates
  34. */
  35. // require function shortcuts:
  36. // __webpack_require__.s = the module id of the entry point
  37. // __webpack_require__.c = the module cache
  38. // __webpack_require__.m = the module functions
  39. // __webpack_require__.p = the bundle public path
  40. // __webpack_require__.i = the identity function used for harmony imports
  41. // __webpack_require__.e = the chunk ensure function
  42. // __webpack_require__.d = the exported property define getter function
  43. // __webpack_require__.o = Object.prototype.hasOwnProperty.call
  44. // __webpack_require__.r = define compatibility on export
  45. // __webpack_require__.t = create a fake namespace object
  46. // __webpack_require__.n = compatibility get default export
  47. // __webpack_require__.h = the webpack hash
  48. // __webpack_require__.w = an object containing all installed WebAssembly.Instance export objects keyed by module id
  49. // __webpack_require__.oe = the uncaught error handler for the webpack runtime
  50. // __webpack_require__.nc = the script nonce
  51. module.exports = class MainTemplate extends Tapable {
  52. /**
  53. *
  54. * @param {TODO=} outputOptions output options for the MainTemplate
  55. */
  56. constructor(outputOptions) {
  57. super();
  58. /** @type {TODO?} */
  59. this.outputOptions = outputOptions || {};
  60. this.hooks = {
  61. /** @type {SyncWaterfallHook<TODO[], RenderManifestOptions>} */
  62. renderManifest: new SyncWaterfallHook(["result", "options"]),
  63. modules: new SyncWaterfallHook([
  64. "modules",
  65. "chunk",
  66. "hash",
  67. "moduleTemplate",
  68. "dependencyTemplates"
  69. ]),
  70. moduleObj: new SyncWaterfallHook([
  71. "source",
  72. "chunk",
  73. "hash",
  74. "moduleIdExpression"
  75. ]),
  76. requireEnsure: new SyncWaterfallHook([
  77. "source",
  78. "chunk",
  79. "hash",
  80. "chunkIdExpression"
  81. ]),
  82. bootstrap: new SyncWaterfallHook([
  83. "source",
  84. "chunk",
  85. "hash",
  86. "moduleTemplate",
  87. "dependencyTemplates"
  88. ]),
  89. localVars: new SyncWaterfallHook(["source", "chunk", "hash"]),
  90. require: new SyncWaterfallHook(["source", "chunk", "hash"]),
  91. requireExtensions: new SyncWaterfallHook(["source", "chunk", "hash"]),
  92. /** @type {SyncWaterfallHook<string, Chunk, string>} */
  93. beforeStartup: new SyncWaterfallHook(["source", "chunk", "hash"]),
  94. /** @type {SyncWaterfallHook<string, Chunk, string>} */
  95. startup: new SyncWaterfallHook(["source", "chunk", "hash"]),
  96. render: new SyncWaterfallHook([
  97. "source",
  98. "chunk",
  99. "hash",
  100. "moduleTemplate",
  101. "dependencyTemplates"
  102. ]),
  103. renderWithEntry: new SyncWaterfallHook(["source", "chunk", "hash"]),
  104. moduleRequire: new SyncWaterfallHook([
  105. "source",
  106. "chunk",
  107. "hash",
  108. "moduleIdExpression"
  109. ]),
  110. addModule: new SyncWaterfallHook([
  111. "source",
  112. "chunk",
  113. "hash",
  114. "moduleIdExpression",
  115. "moduleExpression"
  116. ]),
  117. currentHash: new SyncWaterfallHook(["source", "requestedLength"]),
  118. assetPath: new SyncWaterfallHook(["path", "options"]),
  119. hash: new SyncHook(["hash"]),
  120. hashForChunk: new SyncHook(["hash", "chunk"]),
  121. globalHashPaths: new SyncWaterfallHook(["paths"]),
  122. globalHash: new SyncBailHook(["chunk", "paths"]),
  123. // TODO this should be moved somewhere else
  124. // It's weird here
  125. hotBootstrap: new SyncWaterfallHook(["source", "chunk", "hash"])
  126. };
  127. this.hooks.startup.tap("MainTemplate", (source, chunk, hash) => {
  128. /** @type {string[]} */
  129. const buf = [];
  130. if (chunk.entryModule) {
  131. buf.push("// Load entry module and return exports");
  132. buf.push(
  133. `return ${this.renderRequireFunctionForModule(
  134. hash,
  135. chunk,
  136. JSON.stringify(chunk.entryModule.id)
  137. )}(${this.requireFn}.s = ${JSON.stringify(chunk.entryModule.id)});`
  138. );
  139. }
  140. return Template.asString(buf);
  141. });
  142. this.hooks.render.tap(
  143. "MainTemplate",
  144. (bootstrapSource, chunk, hash, moduleTemplate, dependencyTemplates) => {
  145. const source = new ConcatSource();
  146. source.add("/******/ (function(modules) { // webpackBootstrap\n");
  147. source.add(new PrefixSource("/******/", bootstrapSource));
  148. source.add("/******/ })\n");
  149. source.add(
  150. "/************************************************************************/\n"
  151. );
  152. source.add("/******/ (");
  153. source.add(
  154. this.hooks.modules.call(
  155. new RawSource(""),
  156. chunk,
  157. hash,
  158. moduleTemplate,
  159. dependencyTemplates
  160. )
  161. );
  162. source.add(")");
  163. return source;
  164. }
  165. );
  166. this.hooks.localVars.tap("MainTemplate", (source, chunk, hash) => {
  167. return Template.asString([
  168. source,
  169. "// The module cache",
  170. "var installedModules = {};"
  171. ]);
  172. });
  173. this.hooks.require.tap("MainTemplate", (source, chunk, hash) => {
  174. return Template.asString([
  175. source,
  176. "// Check if module is in cache",
  177. "if(installedModules[moduleId]) {",
  178. Template.indent("return installedModules[moduleId].exports;"),
  179. "}",
  180. "// Create a new module (and put it into the cache)",
  181. "var module = installedModules[moduleId] = {",
  182. Template.indent(this.hooks.moduleObj.call("", chunk, hash, "moduleId")),
  183. "};",
  184. "",
  185. Template.asString(
  186. outputOptions.strictModuleExceptionHandling
  187. ? [
  188. "// Execute the module function",
  189. "var threw = true;",
  190. "try {",
  191. Template.indent([
  192. `modules[moduleId].call(module.exports, module, module.exports, ${this.renderRequireFunctionForModule(
  193. hash,
  194. chunk,
  195. "moduleId"
  196. )});`,
  197. "threw = false;"
  198. ]),
  199. "} finally {",
  200. Template.indent([
  201. "if(threw) delete installedModules[moduleId];"
  202. ]),
  203. "}"
  204. ]
  205. : [
  206. "// Execute the module function",
  207. `modules[moduleId].call(module.exports, module, module.exports, ${this.renderRequireFunctionForModule(
  208. hash,
  209. chunk,
  210. "moduleId"
  211. )});`
  212. ]
  213. ),
  214. "",
  215. "// Flag the module as loaded",
  216. "module.l = true;",
  217. "",
  218. "// Return the exports of the module",
  219. "return module.exports;"
  220. ]);
  221. });
  222. this.hooks.moduleObj.tap(
  223. "MainTemplate",
  224. (source, chunk, hash, varModuleId) => {
  225. return Template.asString(["i: moduleId,", "l: false,", "exports: {}"]);
  226. }
  227. );
  228. this.hooks.requireExtensions.tap("MainTemplate", (source, chunk, hash) => {
  229. const buf = [];
  230. const chunkMaps = chunk.getChunkMaps();
  231. // Check if there are non initial chunks which need to be imported using require-ensure
  232. if (Object.keys(chunkMaps.hash).length) {
  233. buf.push("// This file contains only the entry chunk.");
  234. buf.push("// The chunk loading function for additional chunks");
  235. buf.push(`${this.requireFn}.e = function requireEnsure(chunkId) {`);
  236. buf.push(Template.indent("var promises = [];"));
  237. buf.push(
  238. Template.indent(
  239. this.hooks.requireEnsure.call("", chunk, hash, "chunkId")
  240. )
  241. );
  242. buf.push(Template.indent("return Promise.all(promises);"));
  243. buf.push("};");
  244. } else if (
  245. chunk.hasModuleInGraph(m =>
  246. m.blocks.some(b => b.chunkGroup && b.chunkGroup.chunks.length > 0)
  247. )
  248. ) {
  249. // There async blocks in the graph, so we need to add an empty requireEnsure
  250. // function anyway. This can happen with multiple entrypoints.
  251. buf.push("// The chunk loading function for additional chunks");
  252. buf.push("// Since all referenced chunks are already included");
  253. buf.push("// in this file, this function is empty here.");
  254. buf.push(`${this.requireFn}.e = function requireEnsure() {`);
  255. buf.push(Template.indent("return Promise.resolve();"));
  256. buf.push("};");
  257. }
  258. buf.push("");
  259. buf.push("// expose the modules object (__webpack_modules__)");
  260. buf.push(`${this.requireFn}.m = modules;`);
  261. buf.push("");
  262. buf.push("// expose the module cache");
  263. buf.push(`${this.requireFn}.c = installedModules;`);
  264. buf.push("");
  265. buf.push("// define getter function for harmony exports");
  266. buf.push(`${this.requireFn}.d = function(exports, name, getter) {`);
  267. buf.push(
  268. Template.indent([
  269. `if(!${this.requireFn}.o(exports, name)) {`,
  270. Template.indent([
  271. "Object.defineProperty(exports, name, { enumerable: true, get: getter });"
  272. ]),
  273. "}"
  274. ])
  275. );
  276. buf.push("};");
  277. buf.push("");
  278. buf.push("// define __esModule on exports");
  279. buf.push(`${this.requireFn}.r = function(exports) {`);
  280. buf.push(
  281. Template.indent([
  282. "if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {",
  283. Template.indent([
  284. "Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });"
  285. ]),
  286. "}",
  287. "Object.defineProperty(exports, '__esModule', { value: true });"
  288. ])
  289. );
  290. buf.push("};");
  291. buf.push("");
  292. buf.push("// create a fake namespace object");
  293. buf.push("// mode & 1: value is a module id, require it");
  294. buf.push("// mode & 2: merge all properties of value into the ns");
  295. buf.push("// mode & 4: return value when already ns object");
  296. buf.push("// mode & 8|1: behave like require");
  297. buf.push(`${this.requireFn}.t = function(value, mode) {`);
  298. buf.push(
  299. Template.indent([
  300. `if(mode & 1) value = ${this.requireFn}(value);`,
  301. `if(mode & 8) return value;`,
  302. "if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;",
  303. "var ns = Object.create(null);",
  304. `${this.requireFn}.r(ns);`,
  305. "Object.defineProperty(ns, 'default', { enumerable: true, value: value });",
  306. "if(mode & 2 && typeof value != 'string') for(var key in value) " +
  307. `${this.requireFn}.d(ns, key, function(key) { ` +
  308. "return value[key]; " +
  309. "}.bind(null, key));",
  310. "return ns;"
  311. ])
  312. );
  313. buf.push("};");
  314. buf.push("");
  315. buf.push(
  316. "// getDefaultExport function for compatibility with non-harmony modules"
  317. );
  318. buf.push(this.requireFn + ".n = function(module) {");
  319. buf.push(
  320. Template.indent([
  321. "var getter = module && module.__esModule ?",
  322. Template.indent([
  323. "function getDefault() { return module['default']; } :",
  324. "function getModuleExports() { return module; };"
  325. ]),
  326. `${this.requireFn}.d(getter, 'a', getter);`,
  327. "return getter;"
  328. ])
  329. );
  330. buf.push("};");
  331. buf.push("");
  332. buf.push("// Object.prototype.hasOwnProperty.call");
  333. buf.push(
  334. `${
  335. this.requireFn
  336. }.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };`
  337. );
  338. const publicPath = this.getPublicPath({
  339. hash: hash
  340. });
  341. buf.push("");
  342. buf.push("// __webpack_public_path__");
  343. buf.push(`${this.requireFn}.p = ${JSON.stringify(publicPath)};`);
  344. return Template.asString(buf);
  345. });
  346. this.requireFn = "__webpack_require__";
  347. }
  348. /**
  349. *
  350. * @param {RenderManifestOptions} options render manifest options
  351. * @returns {TODO[]} returns render manifest
  352. */
  353. getRenderManifest(options) {
  354. const result = [];
  355. this.hooks.renderManifest.call(result, options);
  356. return result;
  357. }
  358. /**
  359. * TODO webpack 5: remove moduleTemplate and dependencyTemplates
  360. * @param {string} hash hash to be used for render call
  361. * @param {Chunk} chunk Chunk instance
  362. * @param {ModuleTemplate} moduleTemplate ModuleTemplate instance for render
  363. * @param {Map<Function, DependencyTemplate>} dependencyTemplates dependency templates
  364. * @returns {string[]} the generated source of the bootstrap code
  365. */
  366. renderBootstrap(hash, chunk, moduleTemplate, dependencyTemplates) {
  367. const buf = [];
  368. buf.push(
  369. this.hooks.bootstrap.call(
  370. "",
  371. chunk,
  372. hash,
  373. moduleTemplate,
  374. dependencyTemplates
  375. )
  376. );
  377. buf.push(this.hooks.localVars.call("", chunk, hash));
  378. buf.push("");
  379. buf.push("// The require function");
  380. buf.push(`function ${this.requireFn}(moduleId) {`);
  381. buf.push(Template.indent(this.hooks.require.call("", chunk, hash)));
  382. buf.push("}");
  383. buf.push("");
  384. buf.push(
  385. Template.asString(this.hooks.requireExtensions.call("", chunk, hash))
  386. );
  387. buf.push("");
  388. buf.push(Template.asString(this.hooks.beforeStartup.call("", chunk, hash)));
  389. buf.push(Template.asString(this.hooks.startup.call("", chunk, hash)));
  390. return buf;
  391. }
  392. /**
  393. * @param {string} hash hash to be used for render call
  394. * @param {Chunk} chunk Chunk instance
  395. * @param {ModuleTemplate} moduleTemplate ModuleTemplate instance for render
  396. * @param {Map<Function, DependencyTemplate>} dependencyTemplates dependency templates
  397. * @returns {ConcatSource} the newly generated source from rendering
  398. */
  399. render(hash, chunk, moduleTemplate, dependencyTemplates) {
  400. const buf = this.renderBootstrap(
  401. hash,
  402. chunk,
  403. moduleTemplate,
  404. dependencyTemplates
  405. );
  406. let source = this.hooks.render.call(
  407. new OriginalSource(
  408. Template.prefix(buf, " \t") + "\n",
  409. "webpack/bootstrap"
  410. ),
  411. chunk,
  412. hash,
  413. moduleTemplate,
  414. dependencyTemplates
  415. );
  416. if (chunk.hasEntryModule()) {
  417. source = this.hooks.renderWithEntry.call(source, chunk, hash);
  418. }
  419. if (!source) {
  420. throw new Error(
  421. "Compiler error: MainTemplate plugin 'render' should return something"
  422. );
  423. }
  424. chunk.rendered = true;
  425. return new ConcatSource(source, ";");
  426. }
  427. /**
  428. *
  429. * @param {string} hash hash for render fn
  430. * @param {Chunk} chunk Chunk instance for require
  431. * @param {(number|string)=} varModuleId module id
  432. * @returns {TODO} the moduleRequire hook call return signature
  433. */
  434. renderRequireFunctionForModule(hash, chunk, varModuleId) {
  435. return this.hooks.moduleRequire.call(
  436. this.requireFn,
  437. chunk,
  438. hash,
  439. varModuleId
  440. );
  441. }
  442. /**
  443. *
  444. * @param {string} hash hash for render add fn
  445. * @param {Chunk} chunk Chunk instance for require add fn
  446. * @param {(string|number)=} varModuleId module id
  447. * @param {Module} varModule Module instance
  448. * @returns {TODO} renderAddModule call
  449. */
  450. renderAddModule(hash, chunk, varModuleId, varModule) {
  451. return this.hooks.addModule.call(
  452. `modules[${varModuleId}] = ${varModule};`,
  453. chunk,
  454. hash,
  455. varModuleId,
  456. varModule
  457. );
  458. }
  459. /**
  460. *
  461. * @param {string} hash string hash
  462. * @param {number=} length length
  463. * @returns {string} call hook return
  464. */
  465. renderCurrentHashCode(hash, length) {
  466. length = length || Infinity;
  467. return this.hooks.currentHash.call(
  468. JSON.stringify(hash.substr(0, length)),
  469. length
  470. );
  471. }
  472. /**
  473. *
  474. * @param {object} options get public path options
  475. * @returns {string} hook call
  476. */
  477. getPublicPath(options) {
  478. return this.hooks.assetPath.call(
  479. this.outputOptions.publicPath || "",
  480. options
  481. );
  482. }
  483. getAssetPath(path, options) {
  484. return this.hooks.assetPath.call(path, options);
  485. }
  486. /**
  487. * Updates hash with information from this template
  488. * @param {Hash} hash the hash to update
  489. * @returns {void}
  490. */
  491. updateHash(hash) {
  492. hash.update("maintemplate");
  493. hash.update("3");
  494. this.hooks.hash.call(hash);
  495. }
  496. /**
  497. * TODO webpack 5: remove moduleTemplate and dependencyTemplates
  498. * Updates hash with chunk-specific information from this template
  499. * @param {Hash} hash the hash to update
  500. * @param {Chunk} chunk the chunk
  501. * @param {ModuleTemplate} moduleTemplate ModuleTemplate instance for render
  502. * @param {Map<Function, DependencyTemplate>} dependencyTemplates dependency templates
  503. * @returns {void}
  504. */
  505. updateHashForChunk(hash, chunk, moduleTemplate, dependencyTemplates) {
  506. this.updateHash(hash);
  507. this.hooks.hashForChunk.call(hash, chunk);
  508. for (const line of this.renderBootstrap(
  509. "0000",
  510. chunk,
  511. moduleTemplate,
  512. dependencyTemplates
  513. )) {
  514. hash.update(line);
  515. }
  516. }
  517. useChunkHash(chunk) {
  518. const paths = this.hooks.globalHashPaths.call([]);
  519. return !this.hooks.globalHash.call(chunk, paths);
  520. }
  521. };