You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1504 lines
38 KiB

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