RuntimePlugin.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. const isChunkLoadingDisabledForChunk = chunk => {
  96. const options = chunk.getEntryOptions();
  97. const chunkLoading =
  98. options && options.chunkLoading !== undefined
  99. ? options.chunkLoading
  100. : globalChunkLoading;
  101. return chunkLoading === false;
  102. };
  103. compilation.dependencyTemplates.set(
  104. RuntimeRequirementsDependency,
  105. new RuntimeRequirementsDependency.Template()
  106. );
  107. for (const req of GLOBALS_ON_REQUIRE) {
  108. compilation.hooks.runtimeRequirementInModule
  109. .for(req)
  110. .tap("RuntimePlugin", (module, set) => {
  111. set.add(RuntimeGlobals.requireScope);
  112. });
  113. compilation.hooks.runtimeRequirementInTree
  114. .for(req)
  115. .tap("RuntimePlugin", (module, set) => {
  116. set.add(RuntimeGlobals.requireScope);
  117. });
  118. }
  119. for (const req of Object.keys(TREE_DEPENDENCIES)) {
  120. const deps = TREE_DEPENDENCIES[req];
  121. compilation.hooks.runtimeRequirementInTree
  122. .for(req)
  123. .tap("RuntimePlugin", (chunk, set) => {
  124. for (const dep of deps) set.add(dep);
  125. });
  126. }
  127. for (const req of Object.keys(MODULE_DEPENDENCIES)) {
  128. const deps = MODULE_DEPENDENCIES[req];
  129. compilation.hooks.runtimeRequirementInModule
  130. .for(req)
  131. .tap("RuntimePlugin", (chunk, set) => {
  132. for (const dep of deps) set.add(dep);
  133. });
  134. }
  135. compilation.hooks.runtimeRequirementInTree
  136. .for(RuntimeGlobals.definePropertyGetters)
  137. .tap("RuntimePlugin", chunk => {
  138. compilation.addRuntimeModule(
  139. chunk,
  140. new DefinePropertyGettersRuntimeModule()
  141. );
  142. return true;
  143. });
  144. compilation.hooks.runtimeRequirementInTree
  145. .for(RuntimeGlobals.makeNamespaceObject)
  146. .tap("RuntimePlugin", chunk => {
  147. compilation.addRuntimeModule(
  148. chunk,
  149. new MakeNamespaceObjectRuntimeModule()
  150. );
  151. return true;
  152. });
  153. compilation.hooks.runtimeRequirementInTree
  154. .for(RuntimeGlobals.createFakeNamespaceObject)
  155. .tap("RuntimePlugin", chunk => {
  156. compilation.addRuntimeModule(
  157. chunk,
  158. new CreateFakeNamespaceObjectRuntimeModule()
  159. );
  160. return true;
  161. });
  162. compilation.hooks.runtimeRequirementInTree
  163. .for(RuntimeGlobals.hasOwnProperty)
  164. .tap("RuntimePlugin", chunk => {
  165. compilation.addRuntimeModule(
  166. chunk,
  167. new HasOwnPropertyRuntimeModule()
  168. );
  169. return true;
  170. });
  171. compilation.hooks.runtimeRequirementInTree
  172. .for(RuntimeGlobals.compatGetDefaultExport)
  173. .tap("RuntimePlugin", chunk => {
  174. compilation.addRuntimeModule(
  175. chunk,
  176. new CompatGetDefaultExportRuntimeModule()
  177. );
  178. return true;
  179. });
  180. compilation.hooks.runtimeRequirementInTree
  181. .for(RuntimeGlobals.runtimeId)
  182. .tap("RuntimePlugin", chunk => {
  183. compilation.addRuntimeModule(chunk, new RuntimeIdRuntimeModule());
  184. return true;
  185. });
  186. compilation.hooks.runtimeRequirementInTree
  187. .for(RuntimeGlobals.publicPath)
  188. .tap("RuntimePlugin", (chunk, set) => {
  189. const { outputOptions } = compilation;
  190. const { publicPath: globalPublicPath, scriptType } = outputOptions;
  191. const entryOptions = chunk.getEntryOptions();
  192. const publicPath =
  193. entryOptions && entryOptions.publicPath !== undefined
  194. ? entryOptions.publicPath
  195. : globalPublicPath;
  196. if (publicPath === "auto") {
  197. const module = new AutoPublicPathRuntimeModule();
  198. if (scriptType !== "module") set.add(RuntimeGlobals.global);
  199. compilation.addRuntimeModule(chunk, module);
  200. } else {
  201. const module = new PublicPathRuntimeModule(publicPath);
  202. if (
  203. typeof publicPath !== "string" ||
  204. /\[(full)?hash\]/.test(publicPath)
  205. ) {
  206. module.fullHash = true;
  207. }
  208. compilation.addRuntimeModule(chunk, module);
  209. }
  210. return true;
  211. });
  212. compilation.hooks.runtimeRequirementInTree
  213. .for(RuntimeGlobals.global)
  214. .tap("RuntimePlugin", chunk => {
  215. compilation.addRuntimeModule(chunk, new GlobalRuntimeModule());
  216. return true;
  217. });
  218. compilation.hooks.runtimeRequirementInTree
  219. .for(RuntimeGlobals.asyncModule)
  220. .tap("RuntimePlugin", chunk => {
  221. compilation.addRuntimeModule(chunk, new AsyncModuleRuntimeModule());
  222. return true;
  223. });
  224. compilation.hooks.runtimeRequirementInTree
  225. .for(RuntimeGlobals.systemContext)
  226. .tap("RuntimePlugin", chunk => {
  227. const { outputOptions } = compilation;
  228. const { library: globalLibrary } = outputOptions;
  229. const entryOptions = chunk.getEntryOptions();
  230. const libraryType =
  231. entryOptions && entryOptions.library !== undefined
  232. ? entryOptions.library.type
  233. : globalLibrary.type;
  234. if (libraryType === "system") {
  235. compilation.addRuntimeModule(
  236. chunk,
  237. new SystemContextRuntimeModule()
  238. );
  239. }
  240. return true;
  241. });
  242. compilation.hooks.runtimeRequirementInTree
  243. .for(RuntimeGlobals.getChunkScriptFilename)
  244. .tap("RuntimePlugin", (chunk, set) => {
  245. if (
  246. typeof compilation.outputOptions.chunkFilename === "string" &&
  247. /\[(full)?hash(:\d+)?\]/.test(
  248. compilation.outputOptions.chunkFilename
  249. )
  250. ) {
  251. set.add(RuntimeGlobals.getFullHash);
  252. }
  253. compilation.addRuntimeModule(
  254. chunk,
  255. new GetChunkFilenameRuntimeModule(
  256. "javascript",
  257. "javascript",
  258. RuntimeGlobals.getChunkScriptFilename,
  259. chunk =>
  260. chunk.filenameTemplate ||
  261. (chunk.canBeInitial()
  262. ? compilation.outputOptions.filename
  263. : compilation.outputOptions.chunkFilename),
  264. false
  265. )
  266. );
  267. return true;
  268. });
  269. compilation.hooks.runtimeRequirementInTree
  270. .for(RuntimeGlobals.getChunkCssFilename)
  271. .tap("RuntimePlugin", (chunk, set) => {
  272. if (
  273. typeof compilation.outputOptions.cssChunkFilename === "string" &&
  274. /\[(full)?hash(:\d+)?\]/.test(
  275. compilation.outputOptions.cssChunkFilename
  276. )
  277. ) {
  278. set.add(RuntimeGlobals.getFullHash);
  279. }
  280. compilation.addRuntimeModule(
  281. chunk,
  282. new GetChunkFilenameRuntimeModule(
  283. "css",
  284. "css",
  285. RuntimeGlobals.getChunkCssFilename,
  286. chunk =>
  287. getChunkFilenameTemplate(chunk, compilation.outputOptions),
  288. set.has(RuntimeGlobals.hmrDownloadUpdateHandlers)
  289. )
  290. );
  291. return true;
  292. });
  293. compilation.hooks.runtimeRequirementInTree
  294. .for(RuntimeGlobals.getChunkUpdateScriptFilename)
  295. .tap("RuntimePlugin", (chunk, set) => {
  296. if (
  297. /\[(full)?hash(:\d+)?\]/.test(
  298. compilation.outputOptions.hotUpdateChunkFilename
  299. )
  300. )
  301. set.add(RuntimeGlobals.getFullHash);
  302. compilation.addRuntimeModule(
  303. chunk,
  304. new GetChunkFilenameRuntimeModule(
  305. "javascript",
  306. "javascript update",
  307. RuntimeGlobals.getChunkUpdateScriptFilename,
  308. c => compilation.outputOptions.hotUpdateChunkFilename,
  309. true
  310. )
  311. );
  312. return true;
  313. });
  314. compilation.hooks.runtimeRequirementInTree
  315. .for(RuntimeGlobals.getUpdateManifestFilename)
  316. .tap("RuntimePlugin", (chunk, set) => {
  317. if (
  318. /\[(full)?hash(:\d+)?\]/.test(
  319. compilation.outputOptions.hotUpdateMainFilename
  320. )
  321. ) {
  322. set.add(RuntimeGlobals.getFullHash);
  323. }
  324. compilation.addRuntimeModule(
  325. chunk,
  326. new GetMainFilenameRuntimeModule(
  327. "update manifest",
  328. RuntimeGlobals.getUpdateManifestFilename,
  329. compilation.outputOptions.hotUpdateMainFilename
  330. )
  331. );
  332. return true;
  333. });
  334. compilation.hooks.runtimeRequirementInTree
  335. .for(RuntimeGlobals.ensureChunk)
  336. .tap("RuntimePlugin", (chunk, set) => {
  337. const hasAsyncChunks = chunk.hasAsyncChunks();
  338. if (hasAsyncChunks) {
  339. set.add(RuntimeGlobals.ensureChunkHandlers);
  340. }
  341. compilation.addRuntimeModule(
  342. chunk,
  343. new EnsureChunkRuntimeModule(set)
  344. );
  345. return true;
  346. });
  347. compilation.hooks.runtimeRequirementInTree
  348. .for(RuntimeGlobals.ensureChunkIncludeEntries)
  349. .tap("RuntimePlugin", (chunk, set) => {
  350. set.add(RuntimeGlobals.ensureChunkHandlers);
  351. });
  352. compilation.hooks.runtimeRequirementInTree
  353. .for(RuntimeGlobals.shareScopeMap)
  354. .tap("RuntimePlugin", (chunk, set) => {
  355. compilation.addRuntimeModule(chunk, new ShareRuntimeModule());
  356. return true;
  357. });
  358. compilation.hooks.runtimeRequirementInTree
  359. .for(RuntimeGlobals.loadScript)
  360. .tap("RuntimePlugin", (chunk, set) => {
  361. const withCreateScriptUrl = !!compilation.outputOptions.trustedTypes;
  362. if (withCreateScriptUrl) {
  363. set.add(RuntimeGlobals.createScriptUrl);
  364. }
  365. compilation.addRuntimeModule(
  366. chunk,
  367. new LoadScriptRuntimeModule(withCreateScriptUrl)
  368. );
  369. return true;
  370. });
  371. compilation.hooks.runtimeRequirementInTree
  372. .for(RuntimeGlobals.createScript)
  373. .tap("RuntimePlugin", (chunk, set) => {
  374. if (compilation.outputOptions.trustedTypes) {
  375. set.add(RuntimeGlobals.getTrustedTypesPolicy);
  376. }
  377. compilation.addRuntimeModule(chunk, new CreateScriptRuntimeModule());
  378. return true;
  379. });
  380. compilation.hooks.runtimeRequirementInTree
  381. .for(RuntimeGlobals.createScriptUrl)
  382. .tap("RuntimePlugin", (chunk, set) => {
  383. if (compilation.outputOptions.trustedTypes) {
  384. set.add(RuntimeGlobals.getTrustedTypesPolicy);
  385. }
  386. compilation.addRuntimeModule(
  387. chunk,
  388. new CreateScriptUrlRuntimeModule()
  389. );
  390. return true;
  391. });
  392. compilation.hooks.runtimeRequirementInTree
  393. .for(RuntimeGlobals.getTrustedTypesPolicy)
  394. .tap("RuntimePlugin", (chunk, set) => {
  395. compilation.addRuntimeModule(
  396. chunk,
  397. new GetTrustedTypesPolicyRuntimeModule(set)
  398. );
  399. return true;
  400. });
  401. compilation.hooks.runtimeRequirementInTree
  402. .for(RuntimeGlobals.relativeUrl)
  403. .tap("RuntimePlugin", (chunk, set) => {
  404. compilation.addRuntimeModule(chunk, new RelativeUrlRuntimeModule());
  405. return true;
  406. });
  407. compilation.hooks.runtimeRequirementInTree
  408. .for(RuntimeGlobals.onChunksLoaded)
  409. .tap("RuntimePlugin", (chunk, set) => {
  410. compilation.addRuntimeModule(
  411. chunk,
  412. new OnChunksLoadedRuntimeModule()
  413. );
  414. return true;
  415. });
  416. compilation.hooks.runtimeRequirementInTree
  417. .for(RuntimeGlobals.baseURI)
  418. .tap("RuntimePlugin", chunk => {
  419. if (isChunkLoadingDisabledForChunk(chunk)) {
  420. compilation.addRuntimeModule(chunk, new BaseUriRuntimeModule());
  421. return true;
  422. }
  423. });
  424. compilation.hooks.runtimeRequirementInTree
  425. .for(RuntimeGlobals.scriptNonce)
  426. .tap("RuntimePlugin", chunk => {
  427. compilation.addRuntimeModule(chunk, new NonceRuntimeModule());
  428. return true;
  429. });
  430. // TODO webpack 6: remove CompatRuntimeModule
  431. compilation.hooks.additionalTreeRuntimeRequirements.tap(
  432. "RuntimePlugin",
  433. (chunk, set) => {
  434. const { mainTemplate } = compilation;
  435. if (
  436. mainTemplate.hooks.bootstrap.isUsed() ||
  437. mainTemplate.hooks.localVars.isUsed() ||
  438. mainTemplate.hooks.requireEnsure.isUsed() ||
  439. mainTemplate.hooks.requireExtensions.isUsed()
  440. ) {
  441. compilation.addRuntimeModule(chunk, new CompatRuntimeModule());
  442. }
  443. }
  444. );
  445. JavascriptModulesPlugin.getCompilationHooks(compilation).chunkHash.tap(
  446. "RuntimePlugin",
  447. (chunk, hash, { chunkGraph }) => {
  448. const xor = new StringXor();
  449. for (const m of chunkGraph.getChunkRuntimeModulesIterable(chunk)) {
  450. xor.add(chunkGraph.getModuleHash(m, chunk.runtime));
  451. }
  452. xor.updateHash(hash);
  453. }
  454. );
  455. });
  456. }
  457. }
  458. module.exports = RuntimePlugin;