RuntimePlugin.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const RuntimeGlobals = require("./RuntimeGlobals");
  7. const { getChunkFilenameTemplate } = require("./css/CssModulesPlugin");
  8. const RuntimeRequirementsDependency = require("./dependencies/RuntimeRequirementsDependency");
  9. const JavascriptModulesPlugin = require("./javascript/JavascriptModulesPlugin");
  10. const AsyncModuleRuntimeModule = require("./runtime/AsyncModuleRuntimeModule");
  11. const AutoPublicPathRuntimeModule = require("./runtime/AutoPublicPathRuntimeModule");
  12. const BaseUriRuntimeModule = require("./runtime/BaseUriRuntimeModule");
  13. const CompatGetDefaultExportRuntimeModule = require("./runtime/CompatGetDefaultExportRuntimeModule");
  14. const CompatRuntimeModule = require("./runtime/CompatRuntimeModule");
  15. const CreateFakeNamespaceObjectRuntimeModule = require("./runtime/CreateFakeNamespaceObjectRuntimeModule");
  16. const CreateScriptRuntimeModule = require("./runtime/CreateScriptRuntimeModule");
  17. const CreateScriptUrlRuntimeModule = require("./runtime/CreateScriptUrlRuntimeModule");
  18. const DefinePropertyGettersRuntimeModule = require("./runtime/DefinePropertyGettersRuntimeModule");
  19. const EnsureChunkRuntimeModule = require("./runtime/EnsureChunkRuntimeModule");
  20. const GetChunkFilenameRuntimeModule = require("./runtime/GetChunkFilenameRuntimeModule");
  21. const GetMainFilenameRuntimeModule = require("./runtime/GetMainFilenameRuntimeModule");
  22. const GetTrustedTypesPolicyRuntimeModule = require("./runtime/GetTrustedTypesPolicyRuntimeModule");
  23. const GlobalRuntimeModule = require("./runtime/GlobalRuntimeModule");
  24. const HasOwnPropertyRuntimeModule = require("./runtime/HasOwnPropertyRuntimeModule");
  25. const LoadScriptRuntimeModule = require("./runtime/LoadScriptRuntimeModule");
  26. const MakeNamespaceObjectRuntimeModule = require("./runtime/MakeNamespaceObjectRuntimeModule");
  27. const NonceRuntimeModule = require("./runtime/NonceRuntimeModule");
  28. const OnChunksLoadedRuntimeModule = require("./runtime/OnChunksLoadedRuntimeModule");
  29. const PublicPathRuntimeModule = require("./runtime/PublicPathRuntimeModule");
  30. const RelativeUrlRuntimeModule = require("./runtime/RelativeUrlRuntimeModule");
  31. const RuntimeIdRuntimeModule = require("./runtime/RuntimeIdRuntimeModule");
  32. const SystemContextRuntimeModule = require("./runtime/SystemContextRuntimeModule");
  33. const ShareRuntimeModule = require("./sharing/ShareRuntimeModule");
  34. const StringXor = require("./util/StringXor");
  35. /** @typedef {import("./Chunk")} Chunk */
  36. /** @typedef {import("./Compiler")} Compiler */
  37. /** @typedef {import("./Module")} Module */
  38. const GLOBALS_ON_REQUIRE = [
  39. RuntimeGlobals.chunkName,
  40. RuntimeGlobals.runtimeId,
  41. RuntimeGlobals.compatGetDefaultExport,
  42. RuntimeGlobals.createFakeNamespaceObject,
  43. RuntimeGlobals.createScript,
  44. RuntimeGlobals.createScriptUrl,
  45. RuntimeGlobals.getTrustedTypesPolicy,
  46. RuntimeGlobals.definePropertyGetters,
  47. RuntimeGlobals.ensureChunk,
  48. RuntimeGlobals.entryModuleId,
  49. RuntimeGlobals.getFullHash,
  50. RuntimeGlobals.global,
  51. RuntimeGlobals.makeNamespaceObject,
  52. RuntimeGlobals.moduleCache,
  53. RuntimeGlobals.moduleFactories,
  54. RuntimeGlobals.moduleFactoriesAddOnly,
  55. RuntimeGlobals.interceptModuleExecution,
  56. RuntimeGlobals.publicPath,
  57. RuntimeGlobals.baseURI,
  58. RuntimeGlobals.relativeUrl,
  59. RuntimeGlobals.scriptNonce,
  60. RuntimeGlobals.uncaughtErrorHandler,
  61. RuntimeGlobals.asyncModule,
  62. RuntimeGlobals.wasmInstances,
  63. RuntimeGlobals.instantiateWasm,
  64. RuntimeGlobals.shareScopeMap,
  65. RuntimeGlobals.initializeSharing,
  66. RuntimeGlobals.loadScript,
  67. RuntimeGlobals.systemContext,
  68. RuntimeGlobals.onChunksLoaded
  69. ];
  70. const MODULE_DEPENDENCIES = {
  71. [RuntimeGlobals.moduleLoaded]: [RuntimeGlobals.module],
  72. [RuntimeGlobals.moduleId]: [RuntimeGlobals.module]
  73. };
  74. const TREE_DEPENDENCIES = {
  75. [RuntimeGlobals.definePropertyGetters]: [RuntimeGlobals.hasOwnProperty],
  76. [RuntimeGlobals.compatGetDefaultExport]: [
  77. RuntimeGlobals.definePropertyGetters
  78. ],
  79. [RuntimeGlobals.createFakeNamespaceObject]: [
  80. RuntimeGlobals.definePropertyGetters,
  81. RuntimeGlobals.makeNamespaceObject,
  82. RuntimeGlobals.require
  83. ],
  84. [RuntimeGlobals.initializeSharing]: [RuntimeGlobals.shareScopeMap],
  85. [RuntimeGlobals.shareScopeMap]: [RuntimeGlobals.hasOwnProperty]
  86. };
  87. class RuntimePlugin {
  88. /**
  89. * @param {Compiler} compiler the Compiler
  90. * @returns {void}
  91. */
  92. apply(compiler) {
  93. compiler.hooks.compilation.tap("RuntimePlugin", compilation => {
  94. const globalChunkLoading = compilation.outputOptions.chunkLoading;
  95. /**
  96. * @param {Chunk} chunk chunk
  97. * @returns {boolean} true, when chunk loading is disabled for the chunk
  98. */
  99. const isChunkLoadingDisabledForChunk = chunk => {
  100. const options = chunk.getEntryOptions();
  101. const chunkLoading =
  102. options && options.chunkLoading !== undefined
  103. ? options.chunkLoading
  104. : globalChunkLoading;
  105. return chunkLoading === false;
  106. };
  107. compilation.dependencyTemplates.set(
  108. RuntimeRequirementsDependency,
  109. new RuntimeRequirementsDependency.Template()
  110. );
  111. for (const req of GLOBALS_ON_REQUIRE) {
  112. compilation.hooks.runtimeRequirementInModule
  113. .for(req)
  114. .tap("RuntimePlugin", (module, set) => {
  115. set.add(RuntimeGlobals.requireScope);
  116. });
  117. compilation.hooks.runtimeRequirementInTree
  118. .for(req)
  119. .tap("RuntimePlugin", (module, set) => {
  120. set.add(RuntimeGlobals.requireScope);
  121. });
  122. }
  123. for (const req of Object.keys(TREE_DEPENDENCIES)) {
  124. const deps =
  125. TREE_DEPENDENCIES[/** @type {keyof TREE_DEPENDENCIES} */ (req)];
  126. compilation.hooks.runtimeRequirementInTree
  127. .for(req)
  128. .tap("RuntimePlugin", (chunk, set) => {
  129. for (const dep of deps) set.add(dep);
  130. });
  131. }
  132. for (const req of Object.keys(MODULE_DEPENDENCIES)) {
  133. const deps =
  134. MODULE_DEPENDENCIES[/** @type {keyof MODULE_DEPENDENCIES} */ (req)];
  135. compilation.hooks.runtimeRequirementInModule
  136. .for(req)
  137. .tap("RuntimePlugin", (chunk, set) => {
  138. for (const dep of deps) set.add(dep);
  139. });
  140. }
  141. compilation.hooks.runtimeRequirementInTree
  142. .for(RuntimeGlobals.definePropertyGetters)
  143. .tap("RuntimePlugin", chunk => {
  144. compilation.addRuntimeModule(
  145. chunk,
  146. new DefinePropertyGettersRuntimeModule()
  147. );
  148. return true;
  149. });
  150. compilation.hooks.runtimeRequirementInTree
  151. .for(RuntimeGlobals.makeNamespaceObject)
  152. .tap("RuntimePlugin", chunk => {
  153. compilation.addRuntimeModule(
  154. chunk,
  155. new MakeNamespaceObjectRuntimeModule()
  156. );
  157. return true;
  158. });
  159. compilation.hooks.runtimeRequirementInTree
  160. .for(RuntimeGlobals.createFakeNamespaceObject)
  161. .tap("RuntimePlugin", chunk => {
  162. compilation.addRuntimeModule(
  163. chunk,
  164. new CreateFakeNamespaceObjectRuntimeModule()
  165. );
  166. return true;
  167. });
  168. compilation.hooks.runtimeRequirementInTree
  169. .for(RuntimeGlobals.hasOwnProperty)
  170. .tap("RuntimePlugin", chunk => {
  171. compilation.addRuntimeModule(
  172. chunk,
  173. new HasOwnPropertyRuntimeModule()
  174. );
  175. return true;
  176. });
  177. compilation.hooks.runtimeRequirementInTree
  178. .for(RuntimeGlobals.compatGetDefaultExport)
  179. .tap("RuntimePlugin", chunk => {
  180. compilation.addRuntimeModule(
  181. chunk,
  182. new CompatGetDefaultExportRuntimeModule()
  183. );
  184. return true;
  185. });
  186. compilation.hooks.runtimeRequirementInTree
  187. .for(RuntimeGlobals.runtimeId)
  188. .tap("RuntimePlugin", chunk => {
  189. compilation.addRuntimeModule(chunk, new RuntimeIdRuntimeModule());
  190. return true;
  191. });
  192. compilation.hooks.runtimeRequirementInTree
  193. .for(RuntimeGlobals.publicPath)
  194. .tap("RuntimePlugin", (chunk, set) => {
  195. const { outputOptions } = compilation;
  196. const { publicPath: globalPublicPath, scriptType } = outputOptions;
  197. const entryOptions = chunk.getEntryOptions();
  198. const publicPath =
  199. entryOptions && entryOptions.publicPath !== undefined
  200. ? entryOptions.publicPath
  201. : globalPublicPath;
  202. if (publicPath === "auto") {
  203. const module = new AutoPublicPathRuntimeModule();
  204. if (scriptType !== "module") set.add(RuntimeGlobals.global);
  205. compilation.addRuntimeModule(chunk, module);
  206. } else {
  207. const module = new PublicPathRuntimeModule(publicPath);
  208. if (
  209. typeof publicPath !== "string" ||
  210. /\[(full)?hash\]/.test(publicPath)
  211. ) {
  212. module.fullHash = true;
  213. }
  214. compilation.addRuntimeModule(chunk, module);
  215. }
  216. return true;
  217. });
  218. compilation.hooks.runtimeRequirementInTree
  219. .for(RuntimeGlobals.global)
  220. .tap("RuntimePlugin", chunk => {
  221. compilation.addRuntimeModule(chunk, new GlobalRuntimeModule());
  222. return true;
  223. });
  224. compilation.hooks.runtimeRequirementInTree
  225. .for(RuntimeGlobals.asyncModule)
  226. .tap("RuntimePlugin", chunk => {
  227. compilation.addRuntimeModule(chunk, new AsyncModuleRuntimeModule());
  228. return true;
  229. });
  230. compilation.hooks.runtimeRequirementInTree
  231. .for(RuntimeGlobals.systemContext)
  232. .tap("RuntimePlugin", chunk => {
  233. const { outputOptions } = compilation;
  234. const { library: globalLibrary } = outputOptions;
  235. const entryOptions = chunk.getEntryOptions();
  236. const libraryType =
  237. entryOptions && entryOptions.library !== undefined
  238. ? entryOptions.library.type
  239. : globalLibrary.type;
  240. if (libraryType === "system") {
  241. compilation.addRuntimeModule(
  242. chunk,
  243. new SystemContextRuntimeModule()
  244. );
  245. }
  246. return true;
  247. });
  248. compilation.hooks.runtimeRequirementInTree
  249. .for(RuntimeGlobals.getChunkScriptFilename)
  250. .tap("RuntimePlugin", (chunk, set) => {
  251. if (
  252. typeof compilation.outputOptions.chunkFilename === "string" &&
  253. /\[(full)?hash(:\d+)?\]/.test(
  254. compilation.outputOptions.chunkFilename
  255. )
  256. ) {
  257. set.add(RuntimeGlobals.getFullHash);
  258. }
  259. compilation.addRuntimeModule(
  260. chunk,
  261. new GetChunkFilenameRuntimeModule(
  262. "javascript",
  263. "javascript",
  264. RuntimeGlobals.getChunkScriptFilename,
  265. chunk =>
  266. chunk.filenameTemplate ||
  267. (chunk.canBeInitial()
  268. ? compilation.outputOptions.filename
  269. : compilation.outputOptions.chunkFilename),
  270. false
  271. )
  272. );
  273. return true;
  274. });
  275. compilation.hooks.runtimeRequirementInTree
  276. .for(RuntimeGlobals.getChunkCssFilename)
  277. .tap("RuntimePlugin", (chunk, set) => {
  278. if (
  279. typeof compilation.outputOptions.cssChunkFilename === "string" &&
  280. /\[(full)?hash(:\d+)?\]/.test(
  281. compilation.outputOptions.cssChunkFilename
  282. )
  283. ) {
  284. set.add(RuntimeGlobals.getFullHash);
  285. }
  286. compilation.addRuntimeModule(
  287. chunk,
  288. new GetChunkFilenameRuntimeModule(
  289. "css",
  290. "css",
  291. RuntimeGlobals.getChunkCssFilename,
  292. chunk =>
  293. getChunkFilenameTemplate(chunk, compilation.outputOptions),
  294. set.has(RuntimeGlobals.hmrDownloadUpdateHandlers)
  295. )
  296. );
  297. return true;
  298. });
  299. compilation.hooks.runtimeRequirementInTree
  300. .for(RuntimeGlobals.getChunkUpdateScriptFilename)
  301. .tap("RuntimePlugin", (chunk, set) => {
  302. if (
  303. /\[(full)?hash(:\d+)?\]/.test(
  304. compilation.outputOptions.hotUpdateChunkFilename
  305. )
  306. )
  307. set.add(RuntimeGlobals.getFullHash);
  308. compilation.addRuntimeModule(
  309. chunk,
  310. new GetChunkFilenameRuntimeModule(
  311. "javascript",
  312. "javascript update",
  313. RuntimeGlobals.getChunkUpdateScriptFilename,
  314. c => compilation.outputOptions.hotUpdateChunkFilename,
  315. true
  316. )
  317. );
  318. return true;
  319. });
  320. compilation.hooks.runtimeRequirementInTree
  321. .for(RuntimeGlobals.getUpdateManifestFilename)
  322. .tap("RuntimePlugin", (chunk, set) => {
  323. if (
  324. /\[(full)?hash(:\d+)?\]/.test(
  325. compilation.outputOptions.hotUpdateMainFilename
  326. )
  327. ) {
  328. set.add(RuntimeGlobals.getFullHash);
  329. }
  330. compilation.addRuntimeModule(
  331. chunk,
  332. new GetMainFilenameRuntimeModule(
  333. "update manifest",
  334. RuntimeGlobals.getUpdateManifestFilename,
  335. compilation.outputOptions.hotUpdateMainFilename
  336. )
  337. );
  338. return true;
  339. });
  340. compilation.hooks.runtimeRequirementInTree
  341. .for(RuntimeGlobals.ensureChunk)
  342. .tap("RuntimePlugin", (chunk, set) => {
  343. const hasAsyncChunks = chunk.hasAsyncChunks();
  344. if (hasAsyncChunks) {
  345. set.add(RuntimeGlobals.ensureChunkHandlers);
  346. }
  347. compilation.addRuntimeModule(
  348. chunk,
  349. new EnsureChunkRuntimeModule(set)
  350. );
  351. return true;
  352. });
  353. compilation.hooks.runtimeRequirementInTree
  354. .for(RuntimeGlobals.ensureChunkIncludeEntries)
  355. .tap("RuntimePlugin", (chunk, set) => {
  356. set.add(RuntimeGlobals.ensureChunkHandlers);
  357. });
  358. compilation.hooks.runtimeRequirementInTree
  359. .for(RuntimeGlobals.shareScopeMap)
  360. .tap("RuntimePlugin", (chunk, set) => {
  361. compilation.addRuntimeModule(chunk, new ShareRuntimeModule());
  362. return true;
  363. });
  364. compilation.hooks.runtimeRequirementInTree
  365. .for(RuntimeGlobals.loadScript)
  366. .tap("RuntimePlugin", (chunk, set) => {
  367. const withCreateScriptUrl = !!compilation.outputOptions.trustedTypes;
  368. if (withCreateScriptUrl) {
  369. set.add(RuntimeGlobals.createScriptUrl);
  370. }
  371. const withFetchPriority = set.has(RuntimeGlobals.hasFetchPriority);
  372. compilation.addRuntimeModule(
  373. chunk,
  374. new LoadScriptRuntimeModule(withCreateScriptUrl, withFetchPriority)
  375. );
  376. return true;
  377. });
  378. compilation.hooks.runtimeRequirementInTree
  379. .for(RuntimeGlobals.createScript)
  380. .tap("RuntimePlugin", (chunk, set) => {
  381. if (compilation.outputOptions.trustedTypes) {
  382. set.add(RuntimeGlobals.getTrustedTypesPolicy);
  383. }
  384. compilation.addRuntimeModule(chunk, new CreateScriptRuntimeModule());
  385. return true;
  386. });
  387. compilation.hooks.runtimeRequirementInTree
  388. .for(RuntimeGlobals.createScriptUrl)
  389. .tap("RuntimePlugin", (chunk, set) => {
  390. if (compilation.outputOptions.trustedTypes) {
  391. set.add(RuntimeGlobals.getTrustedTypesPolicy);
  392. }
  393. compilation.addRuntimeModule(
  394. chunk,
  395. new CreateScriptUrlRuntimeModule()
  396. );
  397. return true;
  398. });
  399. compilation.hooks.runtimeRequirementInTree
  400. .for(RuntimeGlobals.getTrustedTypesPolicy)
  401. .tap("RuntimePlugin", (chunk, set) => {
  402. compilation.addRuntimeModule(
  403. chunk,
  404. new GetTrustedTypesPolicyRuntimeModule(set)
  405. );
  406. return true;
  407. });
  408. compilation.hooks.runtimeRequirementInTree
  409. .for(RuntimeGlobals.relativeUrl)
  410. .tap("RuntimePlugin", (chunk, set) => {
  411. compilation.addRuntimeModule(chunk, new RelativeUrlRuntimeModule());
  412. return true;
  413. });
  414. compilation.hooks.runtimeRequirementInTree
  415. .for(RuntimeGlobals.onChunksLoaded)
  416. .tap("RuntimePlugin", (chunk, set) => {
  417. compilation.addRuntimeModule(
  418. chunk,
  419. new OnChunksLoadedRuntimeModule()
  420. );
  421. return true;
  422. });
  423. compilation.hooks.runtimeRequirementInTree
  424. .for(RuntimeGlobals.baseURI)
  425. .tap("RuntimePlugin", chunk => {
  426. if (isChunkLoadingDisabledForChunk(chunk)) {
  427. compilation.addRuntimeModule(chunk, new BaseUriRuntimeModule());
  428. return true;
  429. }
  430. });
  431. compilation.hooks.runtimeRequirementInTree
  432. .for(RuntimeGlobals.scriptNonce)
  433. .tap("RuntimePlugin", chunk => {
  434. compilation.addRuntimeModule(chunk, new NonceRuntimeModule());
  435. return true;
  436. });
  437. // TODO webpack 6: remove CompatRuntimeModule
  438. compilation.hooks.additionalTreeRuntimeRequirements.tap(
  439. "RuntimePlugin",
  440. (chunk, set) => {
  441. const { mainTemplate } = compilation;
  442. if (
  443. mainTemplate.hooks.bootstrap.isUsed() ||
  444. mainTemplate.hooks.localVars.isUsed() ||
  445. mainTemplate.hooks.requireEnsure.isUsed() ||
  446. mainTemplate.hooks.requireExtensions.isUsed()
  447. ) {
  448. compilation.addRuntimeModule(chunk, new CompatRuntimeModule());
  449. }
  450. }
  451. );
  452. JavascriptModulesPlugin.getCompilationHooks(compilation).chunkHash.tap(
  453. "RuntimePlugin",
  454. (chunk, hash, { chunkGraph }) => {
  455. const xor = new StringXor();
  456. for (const m of chunkGraph.getChunkRuntimeModulesIterable(chunk)) {
  457. xor.add(chunkGraph.getModuleHash(m, chunk.runtime));
  458. }
  459. xor.updateHash(hash);
  460. }
  461. );
  462. });
  463. }
  464. }
  465. module.exports = RuntimePlugin;