MainTemplate.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. }
  245. buf.push("");
  246. buf.push("// expose the modules object (__webpack_modules__)");
  247. buf.push(`${this.requireFn}.m = modules;`);
  248. buf.push("");
  249. buf.push("// expose the module cache");
  250. buf.push(`${this.requireFn}.c = installedModules;`);
  251. buf.push("");
  252. buf.push("// define getter function for harmony exports");
  253. buf.push(`${this.requireFn}.d = function(exports, name, getter) {`);
  254. buf.push(
  255. Template.indent([
  256. `if(!${this.requireFn}.o(exports, name)) {`,
  257. Template.indent([
  258. "Object.defineProperty(exports, name, { enumerable: true, get: getter });"
  259. ]),
  260. "}"
  261. ])
  262. );
  263. buf.push("};");
  264. buf.push("");
  265. buf.push("// define __esModule on exports");
  266. buf.push(`${this.requireFn}.r = function(exports) {`);
  267. buf.push(
  268. Template.indent([
  269. "if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {",
  270. Template.indent([
  271. "Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });"
  272. ]),
  273. "}",
  274. "Object.defineProperty(exports, '__esModule', { value: true });"
  275. ])
  276. );
  277. buf.push("};");
  278. buf.push("");
  279. buf.push("// create a fake namespace object");
  280. buf.push("// mode & 1: value is a module id, require it");
  281. buf.push("// mode & 2: merge all properties of value into the ns");
  282. buf.push("// mode & 4: return value when already ns object");
  283. buf.push("// mode & 8|1: behave like require");
  284. buf.push(`${this.requireFn}.t = function(value, mode) {`);
  285. buf.push(
  286. Template.indent([
  287. `if(mode & 1) value = ${this.requireFn}(value);`,
  288. `if(mode & 8) return value;`,
  289. "if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;",
  290. "var ns = Object.create(null);",
  291. `${this.requireFn}.r(ns);`,
  292. "Object.defineProperty(ns, 'default', { enumerable: true, value: value });",
  293. "if(mode & 2 && typeof value != 'string') for(var key in value) " +
  294. `${this.requireFn}.d(ns, key, function(key) { ` +
  295. "return value[key]; " +
  296. "}.bind(null, key));",
  297. "return ns;"
  298. ])
  299. );
  300. buf.push("};");
  301. buf.push("");
  302. buf.push(
  303. "// getDefaultExport function for compatibility with non-harmony modules"
  304. );
  305. buf.push(this.requireFn + ".n = function(module) {");
  306. buf.push(
  307. Template.indent([
  308. "var getter = module && module.__esModule ?",
  309. Template.indent([
  310. "function getDefault() { return module['default']; } :",
  311. "function getModuleExports() { return module; };"
  312. ]),
  313. `${this.requireFn}.d(getter, 'a', getter);`,
  314. "return getter;"
  315. ])
  316. );
  317. buf.push("};");
  318. buf.push("");
  319. buf.push("// Object.prototype.hasOwnProperty.call");
  320. buf.push(
  321. `${
  322. this.requireFn
  323. }.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };`
  324. );
  325. const publicPath = this.getPublicPath({
  326. hash: hash
  327. });
  328. buf.push("");
  329. buf.push("// __webpack_public_path__");
  330. buf.push(`${this.requireFn}.p = ${JSON.stringify(publicPath)};`);
  331. return Template.asString(buf);
  332. });
  333. this.requireFn = "__webpack_require__";
  334. }
  335. /**
  336. *
  337. * @param {RenderManifestOptions} options render manifest options
  338. * @returns {TODO[]} returns render manifest
  339. */
  340. getRenderManifest(options) {
  341. const result = [];
  342. this.hooks.renderManifest.call(result, options);
  343. return result;
  344. }
  345. /**
  346. *
  347. * @param {string} hash hash to be used for render call
  348. * @param {Chunk} chunk Chunk instance
  349. * @param {ModuleTemplate} moduleTemplate ModuleTemplate instance for render
  350. * @param {Map<Function, DependencyTemplate>} dependencyTemplates dependency templates
  351. * @returns {ConcatSource} the newly generated source from rendering
  352. */
  353. render(hash, chunk, moduleTemplate, dependencyTemplates) {
  354. const buf = [];
  355. buf.push(
  356. this.hooks.bootstrap.call(
  357. "",
  358. chunk,
  359. hash,
  360. moduleTemplate,
  361. dependencyTemplates
  362. )
  363. );
  364. buf.push(this.hooks.localVars.call("", chunk, hash));
  365. buf.push("");
  366. buf.push("// The require function");
  367. buf.push(`function ${this.requireFn}(moduleId) {`);
  368. buf.push(Template.indent(this.hooks.require.call("", chunk, hash)));
  369. buf.push("}");
  370. buf.push("");
  371. buf.push(
  372. Template.asString(this.hooks.requireExtensions.call("", chunk, hash))
  373. );
  374. buf.push("");
  375. buf.push(Template.asString(this.hooks.beforeStartup.call("", chunk, hash)));
  376. buf.push(Template.asString(this.hooks.startup.call("", chunk, hash)));
  377. let source = this.hooks.render.call(
  378. new OriginalSource(
  379. Template.prefix(buf, " \t") + "\n",
  380. "webpack/bootstrap"
  381. ),
  382. chunk,
  383. hash,
  384. moduleTemplate,
  385. dependencyTemplates
  386. );
  387. if (chunk.hasEntryModule()) {
  388. source = this.hooks.renderWithEntry.call(source, chunk, hash);
  389. }
  390. if (!source) {
  391. throw new Error(
  392. "Compiler error: MainTemplate plugin 'render' should return something"
  393. );
  394. }
  395. chunk.rendered = true;
  396. return new ConcatSource(source, ";");
  397. }
  398. /**
  399. *
  400. * @param {string} hash hash for render fn
  401. * @param {Chunk} chunk Chunk instance for require
  402. * @param {(number|string)=} varModuleId module id
  403. * @returns {TODO} the moduleRequire hook call return signature
  404. */
  405. renderRequireFunctionForModule(hash, chunk, varModuleId) {
  406. return this.hooks.moduleRequire.call(
  407. this.requireFn,
  408. chunk,
  409. hash,
  410. varModuleId
  411. );
  412. }
  413. /**
  414. *
  415. * @param {string} hash hash for render add fn
  416. * @param {Chunk} chunk Chunk instance for require add fn
  417. * @param {(string|number)=} varModuleId module id
  418. * @param {Module} varModule Module instance
  419. * @returns {TODO} renderAddModule call
  420. */
  421. renderAddModule(hash, chunk, varModuleId, varModule) {
  422. return this.hooks.addModule.call(
  423. `modules[${varModuleId}] = ${varModule};`,
  424. chunk,
  425. hash,
  426. varModuleId,
  427. varModule
  428. );
  429. }
  430. /**
  431. *
  432. * @param {string} hash string hash
  433. * @param {number=} length length
  434. * @returns {string} call hook return
  435. */
  436. renderCurrentHashCode(hash, length) {
  437. length = length || Infinity;
  438. return this.hooks.currentHash.call(
  439. JSON.stringify(hash.substr(0, length)),
  440. length
  441. );
  442. }
  443. /**
  444. *
  445. * @param {object} options get public path options
  446. * @returns {string} hook call
  447. */
  448. getPublicPath(options) {
  449. return this.hooks.assetPath.call(
  450. this.outputOptions.publicPath || "",
  451. options
  452. );
  453. }
  454. getAssetPath(path, options) {
  455. return this.hooks.assetPath.call(path, options);
  456. }
  457. /**
  458. * Updates hash with information from this template
  459. * @param {Hash} hash the hash to update
  460. * @returns {void}
  461. */
  462. updateHash(hash) {
  463. hash.update("maintemplate");
  464. hash.update("3");
  465. hash.update(this.outputOptions.publicPath + "");
  466. this.hooks.hash.call(hash);
  467. }
  468. /**
  469. * Updates hash with chunk-specific information from this template
  470. * @param {Hash} hash the hash to update
  471. * @param {Chunk} chunk the chunk
  472. * @returns {void}
  473. */
  474. updateHashForChunk(hash, chunk) {
  475. this.updateHash(hash);
  476. this.hooks.hashForChunk.call(hash, chunk);
  477. }
  478. useChunkHash(chunk) {
  479. const paths = this.hooks.globalHashPaths.call([]);
  480. return !this.hooks.globalHash.call(chunk, paths);
  481. }
  482. };