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.

568 lines
17 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 {
  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. /** @type {SyncWaterfallHook<string, Chunk, string>} */
  97. afterStartup: new SyncWaterfallHook(["source", "chunk", "hash"]),
  98. render: new SyncWaterfallHook([
  99. "source",
  100. "chunk",
  101. "hash",
  102. "moduleTemplate",
  103. "dependencyTemplates"
  104. ]),
  105. renderWithEntry: new SyncWaterfallHook(["source", "chunk", "hash"]),
  106. moduleRequire: new SyncWaterfallHook([
  107. "source",
  108. "chunk",
  109. "hash",
  110. "moduleIdExpression"
  111. ]),
  112. addModule: new SyncWaterfallHook([
  113. "source",
  114. "chunk",
  115. "hash",
  116. "moduleIdExpression",
  117. "moduleExpression"
  118. ]),
  119. currentHash: new SyncWaterfallHook(["source", "requestedLength"]),
  120. assetPath: new SyncWaterfallHook(["path", "options", "assetInfo"]),
  121. hash: new SyncHook(["hash"]),
  122. hashForChunk: new SyncHook(["hash", "chunk"]),
  123. globalHashPaths: new SyncWaterfallHook(["paths"]),
  124. globalHash: new SyncBailHook(["chunk", "paths"]),
  125. // TODO this should be moved somewhere else
  126. // It's weird here
  127. hotBootstrap: new SyncWaterfallHook(["source", "chunk", "hash"])
  128. };
  129. this.hooks.startup.tap("MainTemplate", (source, chunk, hash) => {
  130. /** @type {string[]} */
  131. const buf = [];
  132. if (chunk.entryModule) {
  133. buf.push("// Load entry module and return exports");
  134. buf.push(
  135. `return ${this.renderRequireFunctionForModule(
  136. hash,
  137. chunk,
  138. JSON.stringify(chunk.entryModule.id)
  139. )}(${this.requireFn}.s = ${JSON.stringify(chunk.entryModule.id)});`
  140. );
  141. }
  142. return Template.asString(buf);
  143. });
  144. this.hooks.render.tap(
  145. "MainTemplate",
  146. (bootstrapSource, chunk, hash, moduleTemplate, dependencyTemplates) => {
  147. const source = new ConcatSource();
  148. source.add("/******/ (function(modules) { // webpackBootstrap\n");
  149. source.add(new PrefixSource("/******/", bootstrapSource));
  150. source.add("/******/ })\n");
  151. source.add(
  152. "/************************************************************************/\n"
  153. );
  154. source.add("/******/ (");
  155. source.add(
  156. this.hooks.modules.call(
  157. new RawSource(""),
  158. chunk,
  159. hash,
  160. moduleTemplate,
  161. dependencyTemplates
  162. )
  163. );
  164. source.add(")");
  165. return source;
  166. }
  167. );
  168. this.hooks.localVars.tap("MainTemplate", (source, chunk, hash) => {
  169. return Template.asString([
  170. source,
  171. "// The module cache",
  172. "var installedModules = {};"
  173. ]);
  174. });
  175. this.hooks.require.tap("MainTemplate", (source, chunk, hash) => {
  176. return Template.asString([
  177. source,
  178. "// Check if module is in cache",
  179. "if(installedModules[moduleId]) {",
  180. Template.indent("return installedModules[moduleId].exports;"),
  181. "}",
  182. "// Create a new module (and put it into the cache)",
  183. "var module = installedModules[moduleId] = {",
  184. Template.indent(this.hooks.moduleObj.call("", chunk, hash, "moduleId")),
  185. "};",
  186. "",
  187. Template.asString(
  188. outputOptions.strictModuleExceptionHandling
  189. ? [
  190. "// Execute the module function",
  191. "var threw = true;",
  192. "try {",
  193. Template.indent([
  194. `modules[moduleId].call(module.exports, module, module.exports, ${this.renderRequireFunctionForModule(
  195. hash,
  196. chunk,
  197. "moduleId"
  198. )});`,
  199. "threw = false;"
  200. ]),
  201. "} finally {",
  202. Template.indent([
  203. "if(threw) delete installedModules[moduleId];"
  204. ]),
  205. "}"
  206. ]
  207. : [
  208. "// Execute the module function",
  209. `modules[moduleId].call(module.exports, module, module.exports, ${this.renderRequireFunctionForModule(
  210. hash,
  211. chunk,
  212. "moduleId"
  213. )});`
  214. ]
  215. ),
  216. "",
  217. "// Flag the module as loaded",
  218. "module.l = true;",
  219. "",
  220. "// Return the exports of the module",
  221. "return module.exports;"
  222. ]);
  223. });
  224. this.hooks.moduleObj.tap(
  225. "MainTemplate",
  226. (source, chunk, hash, varModuleId) => {
  227. return Template.asString(["i: moduleId,", "l: false,", "exports: {}"]);
  228. }
  229. );
  230. this.hooks.requireExtensions.tap("MainTemplate", (source, chunk, hash) => {
  231. const buf = [];
  232. const chunkMaps = chunk.getChunkMaps();
  233. // Check if there are non initial chunks which need to be imported using require-ensure
  234. if (Object.keys(chunkMaps.hash).length) {
  235. buf.push("// This file contains only the entry chunk.");
  236. buf.push("// The chunk loading function for additional chunks");
  237. buf.push(`${this.requireFn}.e = function requireEnsure(chunkId) {`);
  238. buf.push(Template.indent("var promises = [];"));
  239. buf.push(
  240. Template.indent(
  241. this.hooks.requireEnsure.call("", chunk, hash, "chunkId")
  242. )
  243. );
  244. buf.push(Template.indent("return Promise.all(promises);"));
  245. buf.push("};");
  246. } else if (
  247. chunk.hasModuleInGraph(m =>
  248. m.blocks.some(b => b.chunkGroup && b.chunkGroup.chunks.length > 0)
  249. )
  250. ) {
  251. // There async blocks in the graph, so we need to add an empty requireEnsure
  252. // function anyway. This can happen with multiple entrypoints.
  253. buf.push("// The chunk loading function for additional chunks");
  254. buf.push("// Since all referenced chunks are already included");
  255. buf.push("// in this file, this function is empty here.");
  256. buf.push(`${this.requireFn}.e = function requireEnsure() {`);
  257. buf.push(Template.indent("return Promise.resolve();"));
  258. buf.push("};");
  259. }
  260. buf.push("");
  261. buf.push("// expose the modules object (__webpack_modules__)");
  262. buf.push(`${this.requireFn}.m = modules;`);
  263. buf.push("");
  264. buf.push("// expose the module cache");
  265. buf.push(`${this.requireFn}.c = installedModules;`);
  266. buf.push("");
  267. buf.push("// define getter function for harmony exports");
  268. buf.push(`${this.requireFn}.d = function(exports, name, getter) {`);
  269. buf.push(
  270. Template.indent([
  271. `if(!${this.requireFn}.o(exports, name)) {`,
  272. Template.indent([
  273. "Object.defineProperty(exports, name, { enumerable: true, get: getter });"
  274. ]),
  275. "}"
  276. ])
  277. );
  278. buf.push("};");
  279. buf.push("");
  280. buf.push("// define __esModule on exports");
  281. buf.push(`${this.requireFn}.r = function(exports) {`);
  282. buf.push(
  283. Template.indent([
  284. "if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {",
  285. Template.indent([
  286. "Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });"
  287. ]),
  288. "}",
  289. "Object.defineProperty(exports, '__esModule', { value: true });"
  290. ])
  291. );
  292. buf.push("};");
  293. buf.push("");
  294. buf.push("// create a fake namespace object");
  295. buf.push("// mode & 1: value is a module id, require it");
  296. buf.push("// mode & 2: merge all properties of value into the ns");
  297. buf.push("// mode & 4: return value when already ns object");
  298. buf.push("// mode & 8|1: behave like require");
  299. buf.push(`${this.requireFn}.t = function(value, mode) {`);
  300. buf.push(
  301. Template.indent([
  302. `if(mode & 1) value = ${this.requireFn}(value);`,
  303. `if(mode & 8) return value;`,
  304. "if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;",
  305. "var ns = Object.create(null);",
  306. `${this.requireFn}.r(ns);`,
  307. "Object.defineProperty(ns, 'default', { enumerable: true, value: value });",
  308. "if(mode & 2 && typeof value != 'string') for(var key in value) " +
  309. `${this.requireFn}.d(ns, key, function(key) { ` +
  310. "return value[key]; " +
  311. "}.bind(null, key));",
  312. "return ns;"
  313. ])
  314. );
  315. buf.push("};");
  316. buf.push("");
  317. buf.push(
  318. "// getDefaultExport function for compatibility with non-harmony modules"
  319. );
  320. buf.push(this.requireFn + ".n = function(module) {");
  321. buf.push(
  322. Template.indent([
  323. "var getter = module && module.__esModule ?",
  324. Template.indent([
  325. "function getDefault() { return module['default']; } :",
  326. "function getModuleExports() { return module; };"
  327. ]),
  328. `${this.requireFn}.d(getter, 'a', getter);`,
  329. "return getter;"
  330. ])
  331. );
  332. buf.push("};");
  333. buf.push("");
  334. buf.push("// Object.prototype.hasOwnProperty.call");
  335. buf.push(
  336. `${this.requireFn}.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. const afterStartupCode = Template.asString(
  390. this.hooks.afterStartup.call("", chunk, hash)
  391. );
  392. if (afterStartupCode) {
  393. // TODO webpack 5: this is a bit hacky to avoid a breaking change
  394. // change it to a better way
  395. buf.push("var startupResult = (function() {");
  396. }
  397. buf.push(Template.asString(this.hooks.startup.call("", chunk, hash)));
  398. if (afterStartupCode) {
  399. buf.push("})();");
  400. buf.push(afterStartupCode);
  401. buf.push("return startupResult;");
  402. }
  403. return buf;
  404. }
  405. /**
  406. * @param {string} hash hash to be used for render call
  407. * @param {Chunk} chunk Chunk instance
  408. * @param {ModuleTemplate} moduleTemplate ModuleTemplate instance for render
  409. * @param {Map<Function, DependencyTemplate>} dependencyTemplates dependency templates
  410. * @returns {ConcatSource} the newly generated source from rendering
  411. */
  412. render(hash, chunk, moduleTemplate, dependencyTemplates) {
  413. const buf = this.renderBootstrap(
  414. hash,
  415. chunk,
  416. moduleTemplate,
  417. dependencyTemplates
  418. );
  419. let source = this.hooks.render.call(
  420. new OriginalSource(
  421. Template.prefix(buf, " \t") + "\n",
  422. "webpack/bootstrap"
  423. ),
  424. chunk,
  425. hash,
  426. moduleTemplate,
  427. dependencyTemplates
  428. );
  429. if (chunk.hasEntryModule()) {
  430. source = this.hooks.renderWithEntry.call(source, chunk, hash);
  431. }
  432. if (!source) {
  433. throw new Error(
  434. "Compiler error: MainTemplate plugin 'render' should return something"
  435. );
  436. }
  437. chunk.rendered = true;
  438. return new ConcatSource(source, ";");
  439. }
  440. /**
  441. *
  442. * @param {string} hash hash for render fn
  443. * @param {Chunk} chunk Chunk instance for require
  444. * @param {(number|string)=} varModuleId module id
  445. * @returns {TODO} the moduleRequire hook call return signature
  446. */
  447. renderRequireFunctionForModule(hash, chunk, varModuleId) {
  448. return this.hooks.moduleRequire.call(
  449. this.requireFn,
  450. chunk,
  451. hash,
  452. varModuleId
  453. );
  454. }
  455. /**
  456. *
  457. * @param {string} hash hash for render add fn
  458. * @param {Chunk} chunk Chunk instance for require add fn
  459. * @param {(string|number)=} varModuleId module id
  460. * @param {Module} varModule Module instance
  461. * @returns {TODO} renderAddModule call
  462. */
  463. renderAddModule(hash, chunk, varModuleId, varModule) {
  464. return this.hooks.addModule.call(
  465. `modules[${varModuleId}] = ${varModule};`,
  466. chunk,
  467. hash,
  468. varModuleId,
  469. varModule
  470. );
  471. }
  472. /**
  473. *
  474. * @param {string} hash string hash
  475. * @param {number=} length length
  476. * @returns {string} call hook return
  477. */
  478. renderCurrentHashCode(hash, length) {
  479. length = length || Infinity;
  480. return this.hooks.currentHash.call(
  481. JSON.stringify(hash.substr(0, length)),
  482. length
  483. );
  484. }
  485. /**
  486. *
  487. * @param {object} options get public path options
  488. * @returns {string} hook call
  489. */
  490. getPublicPath(options) {
  491. return this.hooks.assetPath.call(
  492. this.outputOptions.publicPath || "",
  493. options
  494. );
  495. }
  496. getAssetPath(path, options) {
  497. return this.hooks.assetPath.call(path, options);
  498. }
  499. getAssetPathWithInfo(path, options) {
  500. const assetInfo = {};
  501. // TODO webpack 5: refactor assetPath hook to receive { path, info } object
  502. const newPath = this.hooks.assetPath.call(path, options, assetInfo);
  503. return { path: newPath, info: assetInfo };
  504. }
  505. /**
  506. * Updates hash with information from this template
  507. * @param {Hash} hash the hash to update
  508. * @returns {void}
  509. */
  510. updateHash(hash) {
  511. hash.update("maintemplate");
  512. hash.update("3");
  513. this.hooks.hash.call(hash);
  514. }
  515. /**
  516. * TODO webpack 5: remove moduleTemplate and dependencyTemplates
  517. * Updates hash with chunk-specific information from this template
  518. * @param {Hash} hash the hash to update
  519. * @param {Chunk} chunk the chunk
  520. * @param {ModuleTemplate} moduleTemplate ModuleTemplate instance for render
  521. * @param {Map<Function, DependencyTemplate>} dependencyTemplates dependency templates
  522. * @returns {void}
  523. */
  524. updateHashForChunk(hash, chunk, moduleTemplate, dependencyTemplates) {
  525. this.updateHash(hash);
  526. this.hooks.hashForChunk.call(hash, chunk);
  527. for (const line of this.renderBootstrap(
  528. "0000",
  529. chunk,
  530. moduleTemplate,
  531. dependencyTemplates
  532. )) {
  533. hash.update(line);
  534. }
  535. }
  536. useChunkHash(chunk) {
  537. const paths = this.hooks.globalHashPaths.call([]);
  538. return !this.hooks.globalHash.call(chunk, paths);
  539. }
  540. };