Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

ConcatenatedModule.js 38KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Module = require("../Module");
  7. const Template = require("../Template");
  8. const Parser = require("../Parser");
  9. const eslintScope = require("eslint-scope");
  10. const { ConcatSource, ReplaceSource } = require("webpack-sources");
  11. const DependencyReference = require("../dependencies/DependencyReference");
  12. const HarmonyImportDependency = require("../dependencies/HarmonyImportDependency");
  13. const HarmonyImportSideEffectDependency = require("../dependencies/HarmonyImportSideEffectDependency");
  14. const HarmonyImportSpecifierDependency = require("../dependencies/HarmonyImportSpecifierDependency");
  15. const HarmonyExportSpecifierDependency = require("../dependencies/HarmonyExportSpecifierDependency");
  16. const HarmonyExportExpressionDependency = require("../dependencies/HarmonyExportExpressionDependency");
  17. const HarmonyExportImportedSpecifierDependency = require("../dependencies/HarmonyExportImportedSpecifierDependency");
  18. const HarmonyCompatibilityDependency = require("../dependencies/HarmonyCompatibilityDependency");
  19. const createHash = require("../util/createHash");
  20. /** @typedef {import("../Dependency")} Dependency */
  21. /** @typedef {import("../Compilation")} Compilation */
  22. /** @typedef {import("../util/createHash").Hash} Hash */
  23. /**
  24. * @typedef {Object} ConcatenationEntry
  25. * @property {"concatenated" | "external"} type
  26. * @property {Module} module
  27. */
  28. const ensureNsObjSource = (
  29. info,
  30. moduleToInfoMap,
  31. requestShortener,
  32. strictHarmonyModule
  33. ) => {
  34. if (!info.hasNamespaceObject) {
  35. info.hasNamespaceObject = true;
  36. const name = info.exportMap.get(true);
  37. const nsObj = [`var ${name} = {};`, `__webpack_require__.r(${name});`];
  38. for (const exportName of info.module.buildMeta.providedExports) {
  39. const finalName = getFinalName(
  40. info,
  41. exportName,
  42. moduleToInfoMap,
  43. requestShortener,
  44. false,
  45. strictHarmonyModule
  46. );
  47. nsObj.push(
  48. `__webpack_require__.d(${name}, ${JSON.stringify(
  49. exportName
  50. )}, function() { return ${finalName}; });`
  51. );
  52. }
  53. info.namespaceObjectSource = nsObj.join("\n") + "\n";
  54. }
  55. };
  56. const getExternalImport = (
  57. importedModule,
  58. info,
  59. exportName,
  60. asCall,
  61. strictHarmonyModule
  62. ) => {
  63. const used = importedModule.isUsed(exportName);
  64. if (!used) return "/* unused reexport */undefined";
  65. const comment =
  66. used !== exportName ? ` ${Template.toNormalComment(exportName)}` : "";
  67. switch (importedModule.buildMeta.exportsType) {
  68. case "named":
  69. if (exportName === "default") {
  70. return info.name;
  71. } else if (exportName === true) {
  72. info.interopNamespaceObjectUsed = true;
  73. return info.interopNamespaceObjectName;
  74. } else {
  75. break;
  76. }
  77. case "namespace":
  78. if (exportName === true) {
  79. return info.name;
  80. } else {
  81. break;
  82. }
  83. default:
  84. if (strictHarmonyModule) {
  85. if (exportName === "default") {
  86. return info.name;
  87. } else if (exportName === true) {
  88. info.interopNamespaceObjectUsed = true;
  89. return info.interopNamespaceObjectName;
  90. } else {
  91. return "/* non-default import from non-esm module */undefined";
  92. }
  93. } else {
  94. if (exportName === "default") {
  95. info.interopDefaultAccessUsed = true;
  96. return asCall
  97. ? `${info.interopDefaultAccessName}()`
  98. : `${info.interopDefaultAccessName}.a`;
  99. } else if (exportName === true) {
  100. return info.name;
  101. } else {
  102. break;
  103. }
  104. }
  105. }
  106. const reference = `${info.name}[${JSON.stringify(used)}${comment}]`;
  107. if (asCall) return `Object(${reference})`;
  108. return reference;
  109. };
  110. const getFinalName = (
  111. info,
  112. exportName,
  113. moduleToInfoMap,
  114. requestShortener,
  115. asCall,
  116. strictHarmonyModule,
  117. alreadyVisited = new Set()
  118. ) => {
  119. switch (info.type) {
  120. case "concatenated": {
  121. const directExport = info.exportMap.get(exportName);
  122. if (directExport) {
  123. if (exportName === true) {
  124. ensureNsObjSource(
  125. info,
  126. moduleToInfoMap,
  127. requestShortener,
  128. strictHarmonyModule
  129. );
  130. } else if (!info.module.isUsed(exportName)) {
  131. return "/* unused export */ undefined";
  132. }
  133. if (info.globalExports.has(directExport)) {
  134. return directExport;
  135. }
  136. const name = info.internalNames.get(directExport);
  137. if (!name) {
  138. throw new Error(
  139. `The export "${directExport}" in "${info.module.readableIdentifier(
  140. requestShortener
  141. )}" has no internal name`
  142. );
  143. }
  144. return name;
  145. }
  146. const reexport = info.reexportMap.get(exportName);
  147. if (reexport) {
  148. if (alreadyVisited.has(reexport)) {
  149. throw new Error(
  150. `Circular reexports ${Array.from(
  151. alreadyVisited,
  152. e =>
  153. `"${e.module.readableIdentifier(requestShortener)}".${
  154. e.exportName
  155. }`
  156. ).join(
  157. " --> "
  158. )} -(circular)-> "${reexport.module.readableIdentifier(
  159. requestShortener
  160. )}".${reexport.exportName}`
  161. );
  162. }
  163. alreadyVisited.add(reexport);
  164. const refInfo = moduleToInfoMap.get(reexport.module);
  165. if (refInfo) {
  166. // module is in the concatenation
  167. return getFinalName(
  168. refInfo,
  169. reexport.exportName,
  170. moduleToInfoMap,
  171. requestShortener,
  172. asCall,
  173. strictHarmonyModule,
  174. alreadyVisited
  175. );
  176. }
  177. }
  178. const problem =
  179. `Cannot get final name for export "${exportName}" in "${info.module.readableIdentifier(
  180. requestShortener
  181. )}"` +
  182. ` (known exports: ${Array.from(info.exportMap.keys())
  183. .filter(name => name !== true)
  184. .join(" ")}, ` +
  185. `known reexports: ${Array.from(info.reexportMap.keys()).join(" ")})`;
  186. return `${Template.toNormalComment(problem)} undefined`;
  187. }
  188. case "external": {
  189. const importedModule = info.module;
  190. return getExternalImport(
  191. importedModule,
  192. info,
  193. exportName,
  194. asCall,
  195. strictHarmonyModule
  196. );
  197. }
  198. }
  199. };
  200. const addScopeSymbols1 = (s, nameSet, scopeSet) => {
  201. let scope = s;
  202. while (scope) {
  203. if (scopeSet.has(scope)) break;
  204. scopeSet.add(scope);
  205. for (const variable of scope.variables) {
  206. nameSet.add(variable.name);
  207. }
  208. scope = scope.upper;
  209. }
  210. };
  211. const addScopeSymbols2 = (s, nameSet, scopeSet1, scopeSet2) => {
  212. let scope = s;
  213. while (scope) {
  214. if (scopeSet1.has(scope)) break;
  215. if (scopeSet2.has(scope)) break;
  216. scopeSet1.add(scope);
  217. for (const variable of scope.variables) {
  218. nameSet.add(variable.name);
  219. }
  220. scope = scope.upper;
  221. }
  222. };
  223. const getAllReferences = variable => {
  224. let set = variable.references;
  225. // Look for inner scope variables too (like in class Foo { t() { Foo } })
  226. const identifiers = new Set(variable.identifiers);
  227. for (const scope of variable.scope.childScopes) {
  228. for (const innerVar of scope.variables) {
  229. if (innerVar.identifiers.some(id => identifiers.has(id))) {
  230. set = set.concat(innerVar.references);
  231. break;
  232. }
  233. }
  234. }
  235. return set;
  236. };
  237. const getPathInAst = (ast, node) => {
  238. if (ast === node) {
  239. return [];
  240. }
  241. const nr = node.range;
  242. const enterNode = n => {
  243. if (!n) return undefined;
  244. const r = n.range;
  245. if (r) {
  246. if (r[0] <= nr[0] && r[1] >= nr[1]) {
  247. const path = getPathInAst(n, node);
  248. if (path) {
  249. path.push(n);
  250. return path;
  251. }
  252. }
  253. }
  254. return undefined;
  255. };
  256. var i;
  257. if (Array.isArray(ast)) {
  258. for (i = 0; i < ast.length; i++) {
  259. const enterResult = enterNode(ast[i]);
  260. if (enterResult !== undefined) return enterResult;
  261. }
  262. } else if (ast && typeof ast === "object") {
  263. const keys = Object.keys(ast);
  264. for (i = 0; i < keys.length; i++) {
  265. const value = ast[keys[i]];
  266. if (Array.isArray(value)) {
  267. const pathResult = getPathInAst(value, node);
  268. if (pathResult !== undefined) return pathResult;
  269. } else if (value && typeof value === "object") {
  270. const enterResult = enterNode(value);
  271. if (enterResult !== undefined) return enterResult;
  272. }
  273. }
  274. }
  275. };
  276. class ConcatenatedModule extends Module {
  277. constructor(rootModule, modules, concatenationList) {
  278. super("javascript/esm", null);
  279. super.setChunks(rootModule._chunks);
  280. // Info from Factory
  281. this.rootModule = rootModule;
  282. this.factoryMeta = rootModule.factoryMeta;
  283. // Info from Compilation
  284. this.index = rootModule.index;
  285. this.index2 = rootModule.index2;
  286. this.depth = rootModule.depth;
  287. // Info from Optimization
  288. this.used = rootModule.used;
  289. this.usedExports = rootModule.usedExports;
  290. // Info from Build
  291. this.buildInfo = {
  292. strict: true,
  293. cacheable: modules.every(m => m.buildInfo.cacheable),
  294. moduleArgument: rootModule.buildInfo.moduleArgument,
  295. exportsArgument: rootModule.buildInfo.exportsArgument,
  296. fileDependencies: new Set(),
  297. contextDependencies: new Set(),
  298. assets: undefined
  299. };
  300. this.built = modules.some(m => m.built);
  301. this.buildMeta = rootModule.buildMeta;
  302. // Caching
  303. this._numberOfConcatenatedModules = modules.length;
  304. // Graph
  305. const modulesSet = new Set(modules);
  306. this.reasons = rootModule.reasons.filter(
  307. reason =>
  308. !(reason.dependency instanceof HarmonyImportDependency) ||
  309. !modulesSet.has(reason.module)
  310. );
  311. this.dependencies = [];
  312. this.warnings = [];
  313. this.errors = [];
  314. this._orderedConcatenationList =
  315. concatenationList ||
  316. ConcatenatedModule.createConcatenationList(rootModule, modulesSet, null);
  317. for (const info of this._orderedConcatenationList) {
  318. if (info.type === "concatenated") {
  319. const m = info.module;
  320. // populate dependencies
  321. for (const d of m.dependencies.filter(
  322. dep =>
  323. !(dep instanceof HarmonyImportDependency) ||
  324. !modulesSet.has(dep._module)
  325. )) {
  326. this.dependencies.push(d);
  327. }
  328. // populate file dependencies
  329. if (m.buildInfo.fileDependencies) {
  330. for (const file of m.buildInfo.fileDependencies) {
  331. this.buildInfo.fileDependencies.add(file);
  332. }
  333. }
  334. // populate context dependencies
  335. if (m.buildInfo.contextDependencies) {
  336. for (const context of m.buildInfo.contextDependencies) {
  337. this.buildInfo.contextDependencies.add(context);
  338. }
  339. }
  340. // populate warnings
  341. for (const warning of m.warnings) {
  342. this.warnings.push(warning);
  343. }
  344. // populate errors
  345. for (const error of m.errors) {
  346. this.errors.push(error);
  347. }
  348. if (m.buildInfo.assets) {
  349. if (this.buildInfo.assets === undefined) {
  350. this.buildInfo.assets = Object.create(null);
  351. }
  352. Object.assign(this.buildInfo.assets, m.buildInfo.assets);
  353. }
  354. if (m.buildInfo.assetsInfo) {
  355. if (this.buildInfo.assetsInfo === undefined) {
  356. this.buildInfo.assetsInfo = new Map();
  357. }
  358. for (const [key, value] of m.buildInfo.assetsInfo) {
  359. this.buildInfo.assetsInfo.set(key, value);
  360. }
  361. }
  362. }
  363. }
  364. this._identifier = this._createIdentifier();
  365. }
  366. get modules() {
  367. return this._orderedConcatenationList
  368. .filter(info => info.type === "concatenated")
  369. .map(info => info.module);
  370. }
  371. identifier() {
  372. return this._identifier;
  373. }
  374. readableIdentifier(requestShortener) {
  375. return (
  376. this.rootModule.readableIdentifier(requestShortener) +
  377. ` + ${this._numberOfConcatenatedModules - 1} modules`
  378. );
  379. }
  380. libIdent(options) {
  381. return this.rootModule.libIdent(options);
  382. }
  383. nameForCondition() {
  384. return this.rootModule.nameForCondition();
  385. }
  386. build(options, compilation, resolver, fs, callback) {
  387. throw new Error("Cannot build this module. It should be already built.");
  388. }
  389. size() {
  390. // Guess size from embedded modules
  391. return this._orderedConcatenationList.reduce((sum, info) => {
  392. switch (info.type) {
  393. case "concatenated":
  394. return sum + info.module.size();
  395. case "external":
  396. return sum + 5;
  397. }
  398. return sum;
  399. }, 0);
  400. }
  401. /**
  402. * @param {Module} rootModule the root of the concatenation
  403. * @param {Set<Module>} modulesSet a set of modules which should be concatenated
  404. * @param {Compilation} compilation the compilation context
  405. * @returns {ConcatenationEntry[]} concatenation list
  406. */
  407. static createConcatenationList(rootModule, modulesSet, compilation) {
  408. const list = [];
  409. const set = new Set();
  410. /**
  411. * @param {Module} module a module
  412. * @returns {(function(): Module)[]} imported modules in order
  413. */
  414. const getConcatenatedImports = module => {
  415. /** @type {WeakMap<DependencyReference, Dependency>} */
  416. const map = new WeakMap();
  417. const references = module.dependencies
  418. .filter(dep => dep instanceof HarmonyImportDependency)
  419. .map(dep => {
  420. const ref = compilation.getDependencyReference(module, dep);
  421. if (ref) map.set(ref, dep);
  422. return ref;
  423. })
  424. .filter(ref => ref);
  425. DependencyReference.sort(references);
  426. // TODO webpack 5: remove this hack, see also DependencyReference
  427. return references.map(ref => {
  428. const dep = map.get(ref);
  429. return () => compilation.getDependencyReference(module, dep).module;
  430. });
  431. };
  432. const enterModule = getModule => {
  433. const module = getModule();
  434. if (!module) return;
  435. if (set.has(module)) return;
  436. set.add(module);
  437. if (modulesSet.has(module)) {
  438. const imports = getConcatenatedImports(module);
  439. imports.forEach(enterModule);
  440. list.push({
  441. type: "concatenated",
  442. module
  443. });
  444. } else {
  445. list.push({
  446. type: "external",
  447. get module() {
  448. // We need to use a getter here, because the module in the dependency
  449. // could be replaced by some other process (i. e. also replaced with a
  450. // concatenated module)
  451. return getModule();
  452. }
  453. });
  454. }
  455. };
  456. enterModule(() => rootModule);
  457. return list;
  458. }
  459. _createIdentifier() {
  460. let orderedConcatenationListIdentifiers = "";
  461. for (let i = 0; i < this._orderedConcatenationList.length; i++) {
  462. if (this._orderedConcatenationList[i].type === "concatenated") {
  463. orderedConcatenationListIdentifiers += this._orderedConcatenationList[
  464. i
  465. ].module.identifier();
  466. orderedConcatenationListIdentifiers += " ";
  467. }
  468. }
  469. const hash = createHash("md4");
  470. hash.update(orderedConcatenationListIdentifiers);
  471. return this.rootModule.identifier() + " " + hash.digest("hex");
  472. }
  473. source(dependencyTemplates, runtimeTemplate) {
  474. const requestShortener = runtimeTemplate.requestShortener;
  475. // Metainfo for each module
  476. const modulesWithInfo = this._orderedConcatenationList.map((info, idx) => {
  477. switch (info.type) {
  478. case "concatenated": {
  479. const exportMap = new Map();
  480. const reexportMap = new Map();
  481. for (const dep of info.module.dependencies) {
  482. if (dep instanceof HarmonyExportSpecifierDependency) {
  483. if (!exportMap.has(dep.name)) {
  484. exportMap.set(dep.name, dep.id);
  485. }
  486. } else if (dep instanceof HarmonyExportExpressionDependency) {
  487. if (!exportMap.has("default")) {
  488. exportMap.set("default", "__WEBPACK_MODULE_DEFAULT_EXPORT__");
  489. }
  490. } else if (
  491. dep instanceof HarmonyExportImportedSpecifierDependency
  492. ) {
  493. const exportName = dep.name;
  494. const importName = dep._id;
  495. const importedModule = dep._module;
  496. if (exportName && importName) {
  497. if (!reexportMap.has(exportName)) {
  498. reexportMap.set(exportName, {
  499. module: importedModule,
  500. exportName: importName,
  501. dependency: dep
  502. });
  503. }
  504. } else if (exportName) {
  505. if (!reexportMap.has(exportName)) {
  506. reexportMap.set(exportName, {
  507. module: importedModule,
  508. exportName: true,
  509. dependency: dep
  510. });
  511. }
  512. } else if (importedModule) {
  513. for (const name of importedModule.buildMeta.providedExports) {
  514. if (dep.activeExports.has(name) || name === "default") {
  515. continue;
  516. }
  517. if (!reexportMap.has(name)) {
  518. reexportMap.set(name, {
  519. module: importedModule,
  520. exportName: name,
  521. dependency: dep
  522. });
  523. }
  524. }
  525. }
  526. }
  527. }
  528. return {
  529. type: "concatenated",
  530. module: info.module,
  531. index: idx,
  532. ast: undefined,
  533. internalSource: undefined,
  534. source: undefined,
  535. globalScope: undefined,
  536. moduleScope: undefined,
  537. internalNames: new Map(),
  538. globalExports: new Set(),
  539. exportMap: exportMap,
  540. reexportMap: reexportMap,
  541. hasNamespaceObject: false,
  542. namespaceObjectSource: null
  543. };
  544. }
  545. case "external":
  546. return {
  547. type: "external",
  548. module: info.module,
  549. index: idx,
  550. name: undefined,
  551. interopNamespaceObjectUsed: false,
  552. interopNamespaceObjectName: undefined,
  553. interopDefaultAccessUsed: false,
  554. interopDefaultAccessName: undefined
  555. };
  556. default:
  557. throw new Error(`Unsupported concatenation entry type ${info.type}`);
  558. }
  559. });
  560. // Create mapping from module to info
  561. const moduleToInfoMap = new Map();
  562. for (const m of modulesWithInfo) {
  563. moduleToInfoMap.set(m.module, m);
  564. }
  565. // Configure template decorators for dependencies
  566. const innerDependencyTemplates = new Map(dependencyTemplates);
  567. innerDependencyTemplates.set(
  568. HarmonyImportSpecifierDependency,
  569. new HarmonyImportSpecifierDependencyConcatenatedTemplate(
  570. dependencyTemplates.get(HarmonyImportSpecifierDependency),
  571. moduleToInfoMap
  572. )
  573. );
  574. innerDependencyTemplates.set(
  575. HarmonyImportSideEffectDependency,
  576. new HarmonyImportSideEffectDependencyConcatenatedTemplate(
  577. dependencyTemplates.get(HarmonyImportSideEffectDependency),
  578. moduleToInfoMap
  579. )
  580. );
  581. innerDependencyTemplates.set(
  582. HarmonyExportSpecifierDependency,
  583. new HarmonyExportSpecifierDependencyConcatenatedTemplate(
  584. dependencyTemplates.get(HarmonyExportSpecifierDependency),
  585. this.rootModule
  586. )
  587. );
  588. innerDependencyTemplates.set(
  589. HarmonyExportExpressionDependency,
  590. new HarmonyExportExpressionDependencyConcatenatedTemplate(
  591. dependencyTemplates.get(HarmonyExportExpressionDependency),
  592. this.rootModule
  593. )
  594. );
  595. innerDependencyTemplates.set(
  596. HarmonyExportImportedSpecifierDependency,
  597. new HarmonyExportImportedSpecifierDependencyConcatenatedTemplate(
  598. dependencyTemplates.get(HarmonyExportImportedSpecifierDependency),
  599. this.rootModule,
  600. moduleToInfoMap
  601. )
  602. );
  603. innerDependencyTemplates.set(
  604. HarmonyCompatibilityDependency,
  605. new HarmonyCompatibilityDependencyConcatenatedTemplate(
  606. dependencyTemplates.get(HarmonyCompatibilityDependency),
  607. this.rootModule,
  608. moduleToInfoMap
  609. )
  610. );
  611. // Must use full identifier in our cache here to ensure that the source
  612. // is updated should our dependencies list change.
  613. // TODO webpack 5 refactor
  614. innerDependencyTemplates.set(
  615. "hash",
  616. innerDependencyTemplates.get("hash") + this.identifier()
  617. );
  618. // Generate source code and analyse scopes
  619. // Prepare a ReplaceSource for the final source
  620. for (const info of modulesWithInfo) {
  621. if (info.type === "concatenated") {
  622. const m = info.module;
  623. const source = m.source(innerDependencyTemplates, runtimeTemplate);
  624. const code = source.source();
  625. let ast;
  626. try {
  627. ast = Parser.parse(code, {
  628. sourceType: "module"
  629. });
  630. } catch (err) {
  631. if (
  632. err.loc &&
  633. typeof err.loc === "object" &&
  634. typeof err.loc.line === "number"
  635. ) {
  636. const lineNumber = err.loc.line;
  637. const lines = code.split("\n");
  638. err.message +=
  639. "\n| " +
  640. lines
  641. .slice(Math.max(0, lineNumber - 3), lineNumber + 2)
  642. .join("\n| ");
  643. }
  644. throw err;
  645. }
  646. const scopeManager = eslintScope.analyze(ast, {
  647. ecmaVersion: 6,
  648. sourceType: "module",
  649. optimistic: true,
  650. ignoreEval: true,
  651. impliedStrict: true
  652. });
  653. const globalScope = scopeManager.acquire(ast);
  654. const moduleScope = globalScope.childScopes[0];
  655. const resultSource = new ReplaceSource(source);
  656. info.ast = ast;
  657. info.internalSource = source;
  658. info.source = resultSource;
  659. info.globalScope = globalScope;
  660. info.moduleScope = moduleScope;
  661. }
  662. }
  663. // List of all used names to avoid conflicts
  664. const allUsedNames = new Set([
  665. "__WEBPACK_MODULE_DEFAULT_EXPORT__", // avoid using this internal name
  666. "abstract",
  667. "arguments",
  668. "async",
  669. "await",
  670. "boolean",
  671. "break",
  672. "byte",
  673. "case",
  674. "catch",
  675. "char",
  676. "class",
  677. "const",
  678. "continue",
  679. "debugger",
  680. "default",
  681. "delete",
  682. "do",
  683. "double",
  684. "else",
  685. "enum",
  686. "eval",
  687. "export",
  688. "extends",
  689. "false",
  690. "final",
  691. "finally",
  692. "float",
  693. "for",
  694. "function",
  695. "goto",
  696. "if",
  697. "implements",
  698. "import",
  699. "in",
  700. "instanceof",
  701. "int",
  702. "interface",
  703. "let",
  704. "long",
  705. "native",
  706. "new",
  707. "null",
  708. "package",
  709. "private",
  710. "protected",
  711. "public",
  712. "return",
  713. "short",
  714. "static",
  715. "super",
  716. "switch",
  717. "synchronized",
  718. "this",
  719. "throw",
  720. "throws",
  721. "transient",
  722. "true",
  723. "try",
  724. "typeof",
  725. "var",
  726. "void",
  727. "volatile",
  728. "while",
  729. "with",
  730. "yield",
  731. "module",
  732. "__dirname",
  733. "__filename",
  734. "exports",
  735. "Array",
  736. "Date",
  737. "eval",
  738. "function",
  739. "hasOwnProperty",
  740. "Infinity",
  741. "isFinite",
  742. "isNaN",
  743. "isPrototypeOf",
  744. "length",
  745. "Math",
  746. "NaN",
  747. "name",
  748. "Number",
  749. "Object",
  750. "prototype",
  751. "String",
  752. "toString",
  753. "undefined",
  754. "valueOf",
  755. "alert",
  756. "all",
  757. "anchor",
  758. "anchors",
  759. "area",
  760. "assign",
  761. "blur",
  762. "button",
  763. "checkbox",
  764. "clearInterval",
  765. "clearTimeout",
  766. "clientInformation",
  767. "close",
  768. "closed",
  769. "confirm",
  770. "constructor",
  771. "crypto",
  772. "decodeURI",
  773. "decodeURIComponent",
  774. "defaultStatus",
  775. "document",
  776. "element",
  777. "elements",
  778. "embed",
  779. "embeds",
  780. "encodeURI",
  781. "encodeURIComponent",
  782. "escape",
  783. "event",
  784. "fileUpload",
  785. "focus",
  786. "form",
  787. "forms",
  788. "frame",
  789. "innerHeight",
  790. "innerWidth",
  791. "layer",
  792. "layers",
  793. "link",
  794. "location",
  795. "mimeTypes",
  796. "navigate",
  797. "navigator",
  798. "frames",
  799. "frameRate",
  800. "hidden",
  801. "history",
  802. "image",
  803. "images",
  804. "offscreenBuffering",
  805. "open",
  806. "opener",
  807. "option",
  808. "outerHeight",
  809. "outerWidth",
  810. "packages",
  811. "pageXOffset",
  812. "pageYOffset",
  813. "parent",
  814. "parseFloat",
  815. "parseInt",
  816. "password",
  817. "pkcs11",
  818. "plugin",
  819. "prompt",
  820. "propertyIsEnum",
  821. "radio",
  822. "reset",
  823. "screenX",
  824. "screenY",
  825. "scroll",
  826. "secure",
  827. "select",
  828. "self",
  829. "setInterval",
  830. "setTimeout",
  831. "status",
  832. "submit",
  833. "taint",
  834. "text",
  835. "textarea",
  836. "top",
  837. "unescape",
  838. "untaint",
  839. "window",
  840. "onblur",
  841. "onclick",
  842. "onerror",
  843. "onfocus",
  844. "onkeydown",
  845. "onkeypress",
  846. "onkeyup",
  847. "onmouseover",
  848. "onload",
  849. "onmouseup",
  850. "onmousedown",
  851. "onsubmit"
  852. ]);
  853. // Set of already checked scopes
  854. const alreadyCheckedScopes = new Set();
  855. // get all global names
  856. for (const info of modulesWithInfo) {
  857. const superClassExpressions = [];
  858. // ignore symbols from moduleScope
  859. if (info.moduleScope) {
  860. alreadyCheckedScopes.add(info.moduleScope);
  861. // The super class expression in class scopes behaves weird
  862. // We store ranges of all super class expressions to make
  863. // renaming to work correctly
  864. for (const childScope of info.moduleScope.childScopes) {
  865. if (childScope.type !== "class") continue;
  866. if (!childScope.block.superClass) continue;
  867. superClassExpressions.push({
  868. range: childScope.block.superClass.range,
  869. variables: childScope.variables
  870. });
  871. }
  872. }
  873. // add global symbols
  874. if (info.globalScope) {
  875. for (const reference of info.globalScope.through) {
  876. const name = reference.identifier.name;
  877. if (
  878. /^__WEBPACK_MODULE_REFERENCE__\d+_([\da-f]+|ns)(_call)?(_strict)?__$/.test(
  879. name
  880. )
  881. ) {
  882. for (const expr of superClassExpressions) {
  883. if (
  884. expr.range[0] <= reference.identifier.range[0] &&
  885. expr.range[1] >= reference.identifier.range[1]
  886. ) {
  887. for (const variable of expr.variables) {
  888. allUsedNames.add(variable.name);
  889. }
  890. }
  891. }
  892. addScopeSymbols1(
  893. reference.from,
  894. allUsedNames,
  895. alreadyCheckedScopes
  896. );
  897. } else {
  898. allUsedNames.add(name);
  899. }
  900. }
  901. }
  902. // add exported globals
  903. if (info.type === "concatenated") {
  904. const variables = new Set();
  905. for (const variable of info.moduleScope.variables) {
  906. variables.add(variable.name);
  907. }
  908. for (const [, variable] of info.exportMap) {
  909. if (!variables.has(variable)) {
  910. info.globalExports.add(variable);
  911. }
  912. }
  913. }
  914. }
  915. // generate names for symbols
  916. for (const info of modulesWithInfo) {
  917. switch (info.type) {
  918. case "concatenated": {
  919. const namespaceObjectName = this.findNewName(
  920. "namespaceObject",
  921. allUsedNames,
  922. null,
  923. info.module.readableIdentifier(requestShortener)
  924. );
  925. allUsedNames.add(namespaceObjectName);
  926. info.internalNames.set(namespaceObjectName, namespaceObjectName);
  927. info.exportMap.set(true, namespaceObjectName);
  928. for (const variable of info.moduleScope.variables) {
  929. const name = variable.name;
  930. if (allUsedNames.has(name)) {
  931. const references = getAllReferences(variable);
  932. const symbolsInReferences = new Set();
  933. const alreadyCheckedInnerScopes = new Set();
  934. for (const ref of references) {
  935. addScopeSymbols2(
  936. ref.from,
  937. symbolsInReferences,
  938. alreadyCheckedInnerScopes,
  939. alreadyCheckedScopes
  940. );
  941. }
  942. const newName = this.findNewName(
  943. name,
  944. allUsedNames,
  945. symbolsInReferences,
  946. info.module.readableIdentifier(requestShortener)
  947. );
  948. allUsedNames.add(newName);
  949. info.internalNames.set(name, newName);
  950. const source = info.source;
  951. const allIdentifiers = new Set(
  952. references.map(r => r.identifier).concat(variable.identifiers)
  953. );
  954. for (const identifier of allIdentifiers) {
  955. const r = identifier.range;
  956. const path = getPathInAst(info.ast, identifier);
  957. if (
  958. path &&
  959. path.length > 1 &&
  960. path[1].type === "Property" &&
  961. path[1].shorthand
  962. ) {
  963. source.insert(r[1], `: ${newName}`);
  964. } else {
  965. source.replace(r[0], r[1] - 1, newName);
  966. }
  967. }
  968. } else {
  969. allUsedNames.add(name);
  970. info.internalNames.set(name, name);
  971. }
  972. }
  973. break;
  974. }
  975. case "external": {
  976. const externalName = this.findNewName(
  977. "",
  978. allUsedNames,
  979. null,
  980. info.module.readableIdentifier(requestShortener)
  981. );
  982. allUsedNames.add(externalName);
  983. info.name = externalName;
  984. if (
  985. info.module.buildMeta.exportsType === "named" ||
  986. !info.module.buildMeta.exportsType
  987. ) {
  988. const externalNameInterop = this.findNewName(
  989. "namespaceObject",
  990. allUsedNames,
  991. null,
  992. info.module.readableIdentifier(requestShortener)
  993. );
  994. allUsedNames.add(externalNameInterop);
  995. info.interopNamespaceObjectName = externalNameInterop;
  996. }
  997. if (!info.module.buildMeta.exportsType) {
  998. const externalNameInterop = this.findNewName(
  999. "default",
  1000. allUsedNames,
  1001. null,
  1002. info.module.readableIdentifier(requestShortener)
  1003. );
  1004. allUsedNames.add(externalNameInterop);
  1005. info.interopDefaultAccessName = externalNameInterop;
  1006. }
  1007. break;
  1008. }
  1009. }
  1010. }
  1011. // Find and replace referenced to modules
  1012. for (const info of modulesWithInfo) {
  1013. if (info.type === "concatenated") {
  1014. for (const reference of info.globalScope.through) {
  1015. const name = reference.identifier.name;
  1016. const match = /^__WEBPACK_MODULE_REFERENCE__(\d+)_([\da-f]+|ns)(_call)?(_strict)?__$/.exec(
  1017. name
  1018. );
  1019. if (match) {
  1020. const referencedModule = modulesWithInfo[+match[1]];
  1021. let exportName;
  1022. if (match[2] === "ns") {
  1023. exportName = true;
  1024. } else {
  1025. const exportData = match[2];
  1026. exportName = Buffer.from(exportData, "hex").toString("utf-8");
  1027. }
  1028. const asCall = !!match[3];
  1029. const strictHarmonyModule = !!match[4];
  1030. const finalName = getFinalName(
  1031. referencedModule,
  1032. exportName,
  1033. moduleToInfoMap,
  1034. requestShortener,
  1035. asCall,
  1036. strictHarmonyModule
  1037. );
  1038. const r = reference.identifier.range;
  1039. const source = info.source;
  1040. source.replace(r[0], r[1] - 1, finalName);
  1041. }
  1042. }
  1043. }
  1044. }
  1045. const result = new ConcatSource();
  1046. // add harmony compatibility flag (must be first because of possible circular dependencies)
  1047. const usedExports = this.rootModule.usedExports;
  1048. if (usedExports === true || usedExports === null) {
  1049. result.add(
  1050. runtimeTemplate.defineEsModuleFlagStatement({
  1051. exportsArgument: this.exportsArgument
  1052. })
  1053. );
  1054. }
  1055. // define required namespace objects (must be before evaluation modules)
  1056. for (const info of modulesWithInfo) {
  1057. if (info.namespaceObjectSource) {
  1058. result.add(info.namespaceObjectSource);
  1059. }
  1060. }
  1061. // evaluate modules in order
  1062. for (const info of modulesWithInfo) {
  1063. switch (info.type) {
  1064. case "concatenated":
  1065. result.add(
  1066. `\n// CONCATENATED MODULE: ${info.module.readableIdentifier(
  1067. requestShortener
  1068. )}\n`
  1069. );
  1070. result.add(info.source);
  1071. break;
  1072. case "external":
  1073. result.add(
  1074. `\n// EXTERNAL MODULE: ${info.module.readableIdentifier(
  1075. requestShortener
  1076. )}\n`
  1077. );
  1078. result.add(
  1079. `var ${info.name} = __webpack_require__(${JSON.stringify(
  1080. info.module.id
  1081. )});\n`
  1082. );
  1083. if (info.interopNamespaceObjectUsed) {
  1084. if (info.module.buildMeta.exportsType === "named") {
  1085. result.add(
  1086. `var ${info.interopNamespaceObjectName} = /*#__PURE__*/__webpack_require__.t(${info.name}, 2);\n`
  1087. );
  1088. } else if (!info.module.buildMeta.exportsType) {
  1089. result.add(
  1090. `var ${info.interopNamespaceObjectName} = /*#__PURE__*/__webpack_require__.t(${info.name});\n`
  1091. );
  1092. }
  1093. }
  1094. if (info.interopDefaultAccessUsed) {
  1095. result.add(
  1096. `var ${info.interopDefaultAccessName} = /*#__PURE__*/__webpack_require__.n(${info.name});\n`
  1097. );
  1098. }
  1099. break;
  1100. default:
  1101. throw new Error(`Unsupported concatenation entry type ${info.type}`);
  1102. }
  1103. }
  1104. return result;
  1105. }
  1106. findNewName(oldName, usedNamed1, usedNamed2, extraInfo) {
  1107. let name = oldName;
  1108. if (name === "__WEBPACK_MODULE_DEFAULT_EXPORT__") name = "";
  1109. // Remove uncool stuff
  1110. extraInfo = extraInfo.replace(
  1111. /\.+\/|(\/index)?\.([a-zA-Z0-9]{1,4})($|\s|\?)|\s*\+\s*\d+\s*modules/g,
  1112. ""
  1113. );
  1114. const splittedInfo = extraInfo.split("/");
  1115. while (splittedInfo.length) {
  1116. name = splittedInfo.pop() + (name ? "_" + name : "");
  1117. const nameIdent = Template.toIdentifier(name);
  1118. if (
  1119. !usedNamed1.has(nameIdent) &&
  1120. (!usedNamed2 || !usedNamed2.has(nameIdent))
  1121. )
  1122. return nameIdent;
  1123. }
  1124. let i = 0;
  1125. let nameWithNumber = Template.toIdentifier(`${name}_${i}`);
  1126. while (
  1127. usedNamed1.has(nameWithNumber) ||
  1128. (usedNamed2 && usedNamed2.has(nameWithNumber))
  1129. ) {
  1130. i++;
  1131. nameWithNumber = Template.toIdentifier(`${name}_${i}`);
  1132. }
  1133. return nameWithNumber;
  1134. }
  1135. /**
  1136. * @param {Hash} hash the hash used to track dependencies
  1137. * @returns {void}
  1138. */
  1139. updateHash(hash) {
  1140. for (const info of this._orderedConcatenationList) {
  1141. switch (info.type) {
  1142. case "concatenated":
  1143. info.module.updateHash(hash);
  1144. break;
  1145. case "external":
  1146. hash.update(`${info.module.id}`);
  1147. break;
  1148. }
  1149. }
  1150. super.updateHash(hash);
  1151. }
  1152. }
  1153. class HarmonyImportSpecifierDependencyConcatenatedTemplate {
  1154. constructor(originalTemplate, modulesMap) {
  1155. this.originalTemplate = originalTemplate;
  1156. this.modulesMap = modulesMap;
  1157. }
  1158. getHarmonyInitOrder(dep) {
  1159. const module = dep._module;
  1160. const info = this.modulesMap.get(module);
  1161. if (!info) {
  1162. return this.originalTemplate.getHarmonyInitOrder(dep);
  1163. }
  1164. return NaN;
  1165. }
  1166. harmonyInit(dep, source, runtimeTemplate, dependencyTemplates) {
  1167. const module = dep._module;
  1168. const info = this.modulesMap.get(module);
  1169. if (!info) {
  1170. this.originalTemplate.harmonyInit(
  1171. dep,
  1172. source,
  1173. runtimeTemplate,
  1174. dependencyTemplates
  1175. );
  1176. return;
  1177. }
  1178. }
  1179. apply(dep, source, runtime, dependencyTemplates) {
  1180. const module = dep._module;
  1181. const info = this.modulesMap.get(module);
  1182. if (!info) {
  1183. this.originalTemplate.apply(dep, source, runtime, dependencyTemplates);
  1184. return;
  1185. }
  1186. let content;
  1187. const callFlag = dep.call ? "_call" : "";
  1188. const strictFlag = dep.originModule.buildMeta.strictHarmonyModule
  1189. ? "_strict"
  1190. : "";
  1191. if (dep._id === null) {
  1192. content = `__WEBPACK_MODULE_REFERENCE__${info.index}_ns${strictFlag}__`;
  1193. } else if (dep.namespaceObjectAsContext) {
  1194. content = `__WEBPACK_MODULE_REFERENCE__${
  1195. info.index
  1196. }_ns${strictFlag}__[${JSON.stringify(dep._id)}]`;
  1197. } else {
  1198. const exportData = Buffer.from(dep._id, "utf-8").toString("hex");
  1199. content = `__WEBPACK_MODULE_REFERENCE__${info.index}_${exportData}${callFlag}${strictFlag}__`;
  1200. }
  1201. if (dep.shorthand) {
  1202. content = dep.name + ": " + content;
  1203. }
  1204. source.replace(dep.range[0], dep.range[1] - 1, content);
  1205. }
  1206. }
  1207. class HarmonyImportSideEffectDependencyConcatenatedTemplate {
  1208. constructor(originalTemplate, modulesMap) {
  1209. this.originalTemplate = originalTemplate;
  1210. this.modulesMap = modulesMap;
  1211. }
  1212. getHarmonyInitOrder(dep) {
  1213. const module = dep._module;
  1214. const info = this.modulesMap.get(module);
  1215. if (!info) {
  1216. return this.originalTemplate.getHarmonyInitOrder(dep);
  1217. }
  1218. return NaN;
  1219. }
  1220. harmonyInit(dep, source, runtime, dependencyTemplates) {
  1221. const module = dep._module;
  1222. const info = this.modulesMap.get(module);
  1223. if (!info) {
  1224. this.originalTemplate.harmonyInit(
  1225. dep,
  1226. source,
  1227. runtime,
  1228. dependencyTemplates
  1229. );
  1230. return;
  1231. }
  1232. }
  1233. apply(dep, source, runtime, dependencyTemplates) {
  1234. const module = dep._module;
  1235. const info = this.modulesMap.get(module);
  1236. if (!info) {
  1237. this.originalTemplate.apply(dep, source, runtime, dependencyTemplates);
  1238. return;
  1239. }
  1240. }
  1241. }
  1242. class HarmonyExportSpecifierDependencyConcatenatedTemplate {
  1243. constructor(originalTemplate, rootModule) {
  1244. this.originalTemplate = originalTemplate;
  1245. this.rootModule = rootModule;
  1246. }
  1247. getHarmonyInitOrder(dep) {
  1248. if (dep.originModule === this.rootModule) {
  1249. return this.originalTemplate.getHarmonyInitOrder(dep);
  1250. }
  1251. return NaN;
  1252. }
  1253. harmonyInit(dep, source, runtime, dependencyTemplates) {
  1254. if (dep.originModule === this.rootModule) {
  1255. this.originalTemplate.harmonyInit(
  1256. dep,
  1257. source,
  1258. runtime,
  1259. dependencyTemplates
  1260. );
  1261. return;
  1262. }
  1263. }
  1264. apply(dep, source, runtime, dependencyTemplates) {
  1265. if (dep.originModule === this.rootModule) {
  1266. this.originalTemplate.apply(dep, source, runtime, dependencyTemplates);
  1267. }
  1268. }
  1269. }
  1270. class HarmonyExportExpressionDependencyConcatenatedTemplate {
  1271. constructor(originalTemplate, rootModule) {
  1272. this.originalTemplate = originalTemplate;
  1273. this.rootModule = rootModule;
  1274. }
  1275. apply(dep, source, runtime, dependencyTemplates) {
  1276. let content =
  1277. "/* harmony default export */ var __WEBPACK_MODULE_DEFAULT_EXPORT__ = ";
  1278. if (dep.originModule === this.rootModule) {
  1279. const used = dep.originModule.isUsed("default");
  1280. const exportsName = dep.originModule.exportsArgument;
  1281. if (used) content += `${exportsName}[${JSON.stringify(used)}] = `;
  1282. }
  1283. if (dep.range) {
  1284. source.replace(
  1285. dep.rangeStatement[0],
  1286. dep.range[0] - 1,
  1287. content + "(" + dep.prefix
  1288. );
  1289. source.replace(dep.range[1], dep.rangeStatement[1] - 1, ");");
  1290. return;
  1291. }
  1292. source.replace(
  1293. dep.rangeStatement[0],
  1294. dep.rangeStatement[1] - 1,
  1295. content + dep.prefix
  1296. );
  1297. }
  1298. }
  1299. class HarmonyExportImportedSpecifierDependencyConcatenatedTemplate {
  1300. constructor(originalTemplate, rootModule, modulesMap) {
  1301. this.originalTemplate = originalTemplate;
  1302. this.rootModule = rootModule;
  1303. this.modulesMap = modulesMap;
  1304. }
  1305. getExports(dep) {
  1306. const importModule = dep._module;
  1307. if (dep._id) {
  1308. // export { named } from "module"
  1309. return [
  1310. {
  1311. name: dep.name,
  1312. id: dep._id,
  1313. module: importModule
  1314. }
  1315. ];
  1316. }
  1317. if (dep.name) {
  1318. // export * as abc from "module"
  1319. return [
  1320. {
  1321. name: dep.name,
  1322. id: true,
  1323. module: importModule
  1324. }
  1325. ];
  1326. }
  1327. // export * from "module"
  1328. return importModule.buildMeta.providedExports
  1329. .filter(exp => exp !== "default" && !dep.activeExports.has(exp))
  1330. .map(exp => {
  1331. return {
  1332. name: exp,
  1333. id: exp,
  1334. module: importModule
  1335. };
  1336. });
  1337. }
  1338. getHarmonyInitOrder(dep) {
  1339. const module = dep._module;
  1340. const info = this.modulesMap.get(module);
  1341. if (!info) {
  1342. return this.originalTemplate.getHarmonyInitOrder(dep);
  1343. }
  1344. return NaN;
  1345. }
  1346. harmonyInit(dep, source, runtime, dependencyTemplates) {
  1347. const module = dep._module;
  1348. const info = this.modulesMap.get(module);
  1349. if (!info) {
  1350. this.originalTemplate.harmonyInit(
  1351. dep,
  1352. source,
  1353. runtime,
  1354. dependencyTemplates
  1355. );
  1356. return;
  1357. }
  1358. }
  1359. apply(dep, source, runtime, dependencyTemplates) {
  1360. if (dep.originModule === this.rootModule) {
  1361. if (this.modulesMap.get(dep._module)) {
  1362. const exportDefs = this.getExports(dep);
  1363. for (const def of exportDefs) {
  1364. const info = this.modulesMap.get(def.module);
  1365. const used = dep.originModule.isUsed(def.name);
  1366. if (!used) {
  1367. source.insert(
  1368. -1,
  1369. `/* unused concated harmony import ${def.name} */\n`
  1370. );
  1371. continue;
  1372. }
  1373. let finalName;
  1374. const strictFlag = dep.originModule.buildMeta.strictHarmonyModule
  1375. ? "_strict"
  1376. : "";
  1377. if (def.id === true) {
  1378. finalName = `__WEBPACK_MODULE_REFERENCE__${info.index}_ns${strictFlag}__`;
  1379. } else {
  1380. const exportData = Buffer.from(def.id, "utf-8").toString("hex");
  1381. finalName = `__WEBPACK_MODULE_REFERENCE__${info.index}_${exportData}${strictFlag}__`;
  1382. }
  1383. const exportsName = this.rootModule.exportsArgument;
  1384. const content =
  1385. `/* concated harmony reexport ${def.name} */` +
  1386. `__webpack_require__.d(${exportsName}, ` +
  1387. `${JSON.stringify(used)}, ` +
  1388. `function() { return ${finalName}; });\n`;
  1389. source.insert(-1, content);
  1390. }
  1391. } else {
  1392. this.originalTemplate.apply(dep, source, runtime, dependencyTemplates);
  1393. }
  1394. }
  1395. }
  1396. }
  1397. class HarmonyCompatibilityDependencyConcatenatedTemplate {
  1398. constructor(originalTemplate, rootModule, modulesMap) {
  1399. this.originalTemplate = originalTemplate;
  1400. this.rootModule = rootModule;
  1401. this.modulesMap = modulesMap;
  1402. }
  1403. apply(dep, source, runtime, dependencyTemplates) {
  1404. // do nothing
  1405. }
  1406. }
  1407. module.exports = ConcatenatedModule;