TransformBasicDependencyPluginLegacy.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. const cachePrefix = require('./util').cachePrefix;
  2. const LoggerFactory = require('./loggerFactory');
  3. const pluginCompat = require('./util/plugin-compat');
  4. const relateContext = require('./util/relate-context');
  5. let LocalModule;
  6. try {
  7. LocalModule = require('webpack/lib/dependencies/LocalModule');
  8. } catch (_) {}
  9. function flattenPrototype(obj) {
  10. if (typeof obj === 'string') {
  11. return obj;
  12. }
  13. const copy = {};
  14. for (const key in obj) {
  15. copy[key] = obj[key];
  16. }
  17. return copy;
  18. }
  19. let AMDDefineDependency;
  20. let AMDRequireArrayDependency;
  21. let AMDRequireContextDependency;
  22. let AMDRequireDependency;
  23. let AMDRequireItemDependency;
  24. let CommonJsRequireContextDependency;
  25. let CommonJsRequireDependency;
  26. let ConstDependency;
  27. let ContextDependency;
  28. let ContextElementDependency;
  29. let CriticalDependencyWarning;
  30. let DelegatedExportsDependency;
  31. let DelegatedSourceDependency;
  32. let DllEntryDependency;
  33. let HarmonyAcceptDependency;
  34. let HarmonyAcceptImportDependency;
  35. let HarmonyCompatibilityDependency;
  36. let HarmonyExportExpressionDependency;
  37. let HarmonyExportHeaderDependency;
  38. let HarmonyExportImportedSpecifierDependency;
  39. let HarmonyExportSpecifierDependency;
  40. let HarmonyImportDependency;
  41. let HarmonyImportSpecifierDependency;
  42. let ImportContextDependency;
  43. let ImportDependency;
  44. let ImportEagerContextDependency;
  45. let ImportEagerDependency;
  46. let ImportLazyContextDependency;
  47. let ImportLazyOnceContextDependency;
  48. let ImportWeakContextDependency;
  49. let ImportWeakDependency;
  50. let LoaderDependency;
  51. let LocalModuleDependency;
  52. let ModuleDependency;
  53. let ModuleHotAcceptDependency;
  54. let ModuleHotDeclineDependency;
  55. let MultiEntryDependency;
  56. let NullDependency;
  57. let PrefetchDependency;
  58. let RequireContextDependency;
  59. let RequireEnsureDependency;
  60. let RequireEnsureItemDependency;
  61. let RequireHeaderDependency;
  62. let RequireIncludeDependency;
  63. let RequireResolveContextDependency;
  64. let RequireResolveDependency;
  65. let RequireResolveHeaderDependency;
  66. let SingleEntryDependency;
  67. let UnsupportedDependency;
  68. const DependencySchemas2 = [
  69. [
  70. 'AMDDefineDependency',
  71. 'range',
  72. 'arrayRange',
  73. 'functionRange',
  74. 'objectRange',
  75. 'namedModule',
  76. ],
  77. ['AMDRequireArrayDependency', 'depsArray', 'range'],
  78. [
  79. 'AMDRequireContextDependency',
  80. 'request',
  81. 'recursive',
  82. 'regExp',
  83. 'range',
  84. 'valueRange',
  85. ],
  86. ['AMDRequireDependency', 'block'],
  87. ['AMDRequireItemDependency', 'request', 'range'],
  88. [
  89. 'CommonJsRequireContextDependency',
  90. 'request',
  91. 'recursive',
  92. 'regExp',
  93. 'range',
  94. 'valueRange',
  95. ],
  96. ['CommonJsRequireDependency', 'request', 'range'],
  97. ['ConstDependency', 'expression', 'range'],
  98. ['ContextDependency', 'request', 'recursive', 'regExp'],
  99. ['ContextElementDependency', 'request', 'userRequest'],
  100. ['DelegatedSourceDependency', 'request'],
  101. ['DllEntryDependency', 'dependencies', 'name'],
  102. ['HarmonyAcceptDependency', 'range', 'dependencies', 'hasCallback'],
  103. ['HarmonyAcceptImportDependency', 'request', 'importedVar', 'range'],
  104. ['HarmonyCompatibilityDependency', 'originModule'],
  105. [
  106. 'HarmonyExportExpressionDependency',
  107. 'originModule',
  108. 'range',
  109. 'rangeStatement',
  110. 'prefix',
  111. ],
  112. ['HarmonyExportHeaderDependency', 'range', 'rangeStatement'],
  113. [
  114. 'HarmonyExportImportedSpecifierDependency',
  115. 'originModule',
  116. 'importDependency',
  117. 'importedVar',
  118. 'id',
  119. 'name',
  120. ],
  121. [
  122. 'HarmonyExportSpecifierDependency',
  123. 'originModule',
  124. 'id',
  125. 'name',
  126. 'position',
  127. 'immutable',
  128. ],
  129. ['HarmonyImportDependency', 'request', 'importedVar', 'range'],
  130. [
  131. 'HarmonyImportSpecifierDependency',
  132. 'importDependency',
  133. 'importedVar',
  134. 'id',
  135. 'name',
  136. 'range',
  137. 'strictExportPresence',
  138. ],
  139. [
  140. 'ImportContextDependency',
  141. 'request',
  142. 'recursive',
  143. 'regExp',
  144. 'range',
  145. 'valueRange',
  146. 'chunkName',
  147. ],
  148. ['ImportDependency', 'request', 'block'],
  149. [
  150. 'ImportEagerContextDependency',
  151. 'request',
  152. 'recursive',
  153. 'regExp',
  154. 'range',
  155. 'valueRange',
  156. 'chunkName',
  157. ],
  158. ['ImportEagerDependency', 'request', 'range'],
  159. [
  160. 'ImportLazyContextDependency',
  161. 'request',
  162. 'recursive',
  163. 'regExp',
  164. 'range',
  165. 'valueRange',
  166. 'chunkName',
  167. ],
  168. [
  169. 'ImportLazyOnceContextDependency',
  170. 'request',
  171. 'recursive',
  172. 'regExp',
  173. 'range',
  174. 'valueRange',
  175. 'chunkName',
  176. ],
  177. ['LoaderDependency', 'request'],
  178. ['LocalModuleDependency', 'localModule', 'range'],
  179. ['ModuleDependency', 'request'],
  180. ['ModuleHotAcceptDependency', 'request', 'range'],
  181. ['ModuleHotDeclineDependency', 'request', 'range'],
  182. ['MultiEntryDependency', 'dependencies', 'name'],
  183. ['NullDependency'],
  184. ['PrefetchDependency', 'request'],
  185. ['RequireContextDependency', 'request', 'recursive', 'regExp', 'range'],
  186. ['RequireEnsureDependency', 'block'],
  187. ['RequireEnsureItemDependency', 'request'],
  188. ['RequireHeaderDependency', 'range'],
  189. ['RequireIncludeDependency', 'request', 'range'],
  190. [
  191. 'RequireResolveContextDependency',
  192. 'request',
  193. 'recursive',
  194. 'regExp',
  195. 'range',
  196. 'valueRange',
  197. ],
  198. ['RequireResolveDependency', 'request', 'range'],
  199. ['RequireResolveHeaderDependency', 'range'],
  200. ['SingleEntryDependency', 'request'],
  201. ['UnsupportedDependency', 'request', 'range'],
  202. ];
  203. const DependencySchemas3 = [
  204. [
  205. 'AMDDefineDependency',
  206. 'range',
  207. 'arrayRange',
  208. 'functionRange',
  209. 'objectRange',
  210. 'namedModule',
  211. ],
  212. ['AMDRequireArrayDependency', 'depsArray', 'range'],
  213. [
  214. 'AMDRequireContextDependency',
  215. 'request',
  216. 'recursive',
  217. 'regExp',
  218. 'range',
  219. 'valueRange',
  220. ],
  221. ['AMDRequireDependency', 'block'],
  222. ['AMDRequireItemDependency', 'request', 'range'],
  223. [
  224. 'CommonJsRequireContextDependency',
  225. 'request',
  226. 'recursive',
  227. 'regExp',
  228. 'range',
  229. 'valueRange',
  230. ],
  231. ['CommonJsRequireDependency', 'request', 'range'],
  232. ['ConstDependency', 'expression', 'range'],
  233. ['ContextDependency', 'request', 'recursive', 'regExp'],
  234. ['ContextElementDependency', 'request', 'userRequest'],
  235. ['CriticalDependencyWarning', 'message'],
  236. ['DelegatedExportsDependency', 'originModule', 'exports'],
  237. ['DelegatedSourceDependency', 'request'],
  238. ['DllEntryDependency', 'dependencies', 'name'],
  239. ['HarmonyAcceptDependency', 'range', 'dependencies', 'hasCallback'],
  240. ['HarmonyAcceptImportDependency', 'request', 'importedVar', 'range'],
  241. ['HarmonyCompatibilityDependency', 'originModule'],
  242. [
  243. 'HarmonyExportExpressionDependency',
  244. 'originModule',
  245. 'range',
  246. 'rangeStatement',
  247. 'prefix',
  248. ],
  249. ['HarmonyExportHeaderDependency', 'range', 'rangeStatement'],
  250. [
  251. 'HarmonyExportImportedSpecifierDependency',
  252. 'originModule',
  253. 'importDependency',
  254. 'importedVar',
  255. 'id',
  256. 'name',
  257. 'activeExports',
  258. 'otherStarExports',
  259. ],
  260. [
  261. 'HarmonyExportSpecifierDependency',
  262. 'originModule',
  263. 'id',
  264. 'name',
  265. 'position',
  266. 'immutable',
  267. ],
  268. ['HarmonyImportDependency', 'request', 'importedVar', 'range'],
  269. [
  270. 'HarmonyImportSpecifierDependency',
  271. 'importDependency',
  272. 'importedVar',
  273. 'id',
  274. 'name',
  275. 'range',
  276. 'strictExportPresence',
  277. ],
  278. [
  279. 'ImportContextDependency',
  280. 'request',
  281. 'recursive',
  282. 'regExp',
  283. 'range',
  284. 'valueRange',
  285. 'chunkName',
  286. ],
  287. ['ImportDependency', 'request', 'block'],
  288. [
  289. 'ImportEagerContextDependency',
  290. 'request',
  291. 'recursive',
  292. 'regExp',
  293. 'range',
  294. 'valueRange',
  295. 'chunkName',
  296. ],
  297. ['ImportEagerDependency', 'request', 'range'],
  298. [
  299. 'ImportLazyContextDependency',
  300. 'request',
  301. 'recursive',
  302. 'regExp',
  303. 'range',
  304. 'valueRange',
  305. 'chunkName',
  306. ],
  307. [
  308. 'ImportLazyOnceContextDependency',
  309. 'request',
  310. 'recursive',
  311. 'regExp',
  312. 'range',
  313. 'valueRange',
  314. 'chunkName',
  315. ],
  316. [
  317. 'ImportWeakContextDependency',
  318. 'request',
  319. 'recursive',
  320. 'regExp',
  321. 'range',
  322. 'valueRange',
  323. 'chunkName',
  324. ],
  325. ['ImportWeakDependency', 'request', 'range'],
  326. ['LoaderDependency', 'request'],
  327. ['LocalModuleDependency', 'localModule', 'range'],
  328. ['ModuleDependency', 'request'],
  329. ['ModuleHotAcceptDependency', 'request', 'range'],
  330. ['ModuleHotDeclineDependency', 'request', 'range'],
  331. ['MultiEntryDependency', 'dependencies', 'name'],
  332. ['NullDependency'],
  333. ['PrefetchDependency', 'request'],
  334. [
  335. 'RequireContextDependency',
  336. 'request',
  337. 'recursive',
  338. 'regExp',
  339. 'asyncMode',
  340. 'range',
  341. ],
  342. ['RequireEnsureDependency', 'block'],
  343. ['RequireEnsureItemDependency', 'request'],
  344. ['RequireHeaderDependency', 'range'],
  345. ['RequireIncludeDependency', 'request', 'range'],
  346. [
  347. 'RequireResolveContextDependency',
  348. 'request',
  349. 'recursive',
  350. 'regExp',
  351. 'range',
  352. 'valueRange',
  353. ],
  354. ['RequireResolveDependency', 'request', 'range'],
  355. ['RequireResolveHeaderDependency', 'range'],
  356. ['SingleEntryDependency', 'request'],
  357. ['UnsupportedDependency', 'request', 'range'],
  358. ];
  359. const freezeArgument = {
  360. dependencies(arg, dependency, extra, methods) {
  361. return methods.mapFreeze('Dependency', null, arg, extra);
  362. },
  363. depsArray(arg, dependency, extra, methods) {
  364. return methods.mapFreeze('Dependency', null, arg, extra);
  365. },
  366. localModule({ name, idx }, dependency, extra, methods) {
  367. return {
  368. name: name,
  369. idx: idx,
  370. };
  371. },
  372. regExp(arg, dependency, extra, methods) {
  373. return arg ? arg.source : false;
  374. },
  375. request(arg, dependency, extra, methods) {
  376. return relateContext.relateAbsoluteRequest(extra.module.context, arg);
  377. },
  378. userRequest(arg, dependency, extra, methods) {
  379. return relateContext.relateAbsoluteRequest(extra.module.context, arg);
  380. },
  381. block(arg, dependency, extra, methods) {
  382. // Dependency nested in a parent. Freezing the block is a loop.
  383. if (arg.dependencies.includes(dependency)) {
  384. return;
  385. }
  386. return methods.freeze('DependencyBlock', null, arg, extra);
  387. },
  388. importDependency(arg, dependency, extra, methods) {
  389. return methods.freeze('Dependency', null, arg, extra);
  390. },
  391. originModule(arg, dependency, extra, methods) {
  392. // This will be in extra, generated or found during the process of thawing.
  393. },
  394. activeExports(arg, dependency, extra, methods) {
  395. return null;
  396. },
  397. otherStarExports(arg, dependency, extra, methods) {
  398. if (arg) {
  399. // This will be in extra, generated during the process of thawing.
  400. return 'star';
  401. }
  402. return null;
  403. },
  404. options(arg, dependency, extra, methods) {
  405. if (arg.regExp) {
  406. return Object.assign({}, arg, {
  407. regExp: arg.regExp.source,
  408. });
  409. }
  410. return arg;
  411. },
  412. parserScope(arg, dependencies, extra, methods) {
  413. return;
  414. },
  415. };
  416. const thawArgument = {
  417. dependencies(arg, frozen, extra, methods) {
  418. return methods.mapThaw('Dependency', null, arg, extra);
  419. },
  420. depsArray(arg, frozen, extra, methods) {
  421. return methods.mapThaw('Dependency', null, arg, extra);
  422. },
  423. localModule({ idx, name, used }, frozen, extra, methods) {
  424. const state = extra.state;
  425. if (!state.localModules) {
  426. state.localModules = [];
  427. }
  428. if (!state.localModules[idx]) {
  429. state.localModules[idx] = new LocalModule(extra.module, name, idx);
  430. state.localModules[idx].used = used;
  431. }
  432. return state.localModules[idx];
  433. },
  434. regExp(arg, frozen, extra, methods) {
  435. return arg ? new RegExp(arg) : arg;
  436. },
  437. // request: function(arg, dependency, extra, methods) {
  438. // return relateContext.contextNormalRequest(extra.compilation.compiler, arg);
  439. // },
  440. block(arg, frozen, extra, methods) {
  441. // Not having a block, means it needs to create a cycle and refer to its
  442. // parent.
  443. if (!arg) {
  444. return extra.parent;
  445. }
  446. return methods.thaw('DependencyBlock', null, arg, extra);
  447. },
  448. importDependency(arg, frozen, extra, methods) {
  449. return methods.thaw('Dependency', null, arg, extra);
  450. },
  451. originModule(arg, frozen, extra, methods) {
  452. return extra.module;
  453. },
  454. activeExports(arg, { name }, { state }, methods) {
  455. state.activeExports = state.activeExports || new Set();
  456. if (name) {
  457. state.activeExports.add(name);
  458. }
  459. return state.activeExports;
  460. },
  461. otherStarExports(arg, frozen, { state }, methods) {
  462. if (arg === 'star') {
  463. return state.otherStarExports || [];
  464. }
  465. return null;
  466. },
  467. options(arg, frozen, extra, methods) {
  468. if (arg.regExp) {
  469. return Object.assign({}, arg, {
  470. regExp: new RegExp(arg.regExp),
  471. });
  472. }
  473. return arg;
  474. },
  475. parserScope(arg, frozen, { state }, methods) {
  476. state.harmonyParserScope = state.harmonyParserScope || {};
  477. return state.harmonyParserScope;
  478. },
  479. };
  480. function freezeDependency(dependency, extra, methods) {
  481. const schemas = extra.schemas;
  482. for (let i = 0; i < schemas.length; i++) {
  483. if (dependency.constructor === schemas[i].Dependency) {
  484. const frozen = {
  485. type: schemas[i][0],
  486. };
  487. for (let j = 1; j < schemas[i].length; j++) {
  488. let arg = dependency[schemas[i][j]];
  489. if (freezeArgument[schemas[i][j]]) {
  490. arg = freezeArgument[schemas[i][j]](arg, dependency, extra, methods);
  491. }
  492. frozen[schemas[i][j]] = arg;
  493. }
  494. return frozen;
  495. }
  496. }
  497. }
  498. function thawDependency(frozen, extra, methods) {
  499. const schemas = extra.schemas;
  500. schemas.map = schemas.map || {};
  501. if (schemas.map[frozen.type]) {
  502. const depSchema = schemas.map[frozen.type];
  503. const Dependency = depSchema.Dependency;
  504. try {
  505. return new Dependency(...depSchema.args(frozen, extra, methods));
  506. } catch (_) {
  507. return new (Function.prototype.bind.apply(
  508. Dependency,
  509. [null].concat(depSchema.args(frozen, extra, methods)),
  510. ))();
  511. }
  512. }
  513. for (const depSchema of schemas) {
  514. if (frozen.type === depSchema[0]) {
  515. schemas.map[frozen.type] = depSchema;
  516. const Dependency = depSchema.Dependency;
  517. const lines = [];
  518. for (let j = 1; j < depSchema.length; j++) {
  519. const argName = depSchema[j];
  520. if (thawArgument[argName]) {
  521. lines.push(
  522. ` thawArgument.${argName}(frozen.${argName}, frozen, extra, methods)`,
  523. );
  524. } else {
  525. lines.push(` frozen.${argName}`);
  526. }
  527. }
  528. depSchema.args = new Function(
  529. 'thawArgument',
  530. `
  531. return function(frozen, extra, methods) {
  532. return [
  533. ${lines.join(',\n')}
  534. ];
  535. };
  536. `,
  537. )(thawArgument);
  538. try {
  539. return new Dependency(...depSchema.args(frozen, extra, methods));
  540. } catch (_) {
  541. return new (Function.prototype.bind.apply(
  542. Dependency,
  543. [null].concat(depSchema.args(frozen, extra, methods)),
  544. ))();
  545. }
  546. }
  547. }
  548. }
  549. class TransformBasicDependencyPluginLegacy {
  550. constructor(options) {
  551. this.options = options;
  552. }
  553. apply(compiler) {
  554. let schemas = DependencySchemas3;
  555. if (this.options.schema < 3) {
  556. schemas = DependencySchemas2;
  557. }
  558. pluginCompat.tap(
  559. compiler,
  560. 'afterPlugins',
  561. 'TransformBasicDependencyPlugin scan Dependency types',
  562. () => {
  563. pluginCompat.tap(
  564. compiler,
  565. 'compilation',
  566. 'TransformBasicDependencyPlugin scan Dependencies types',
  567. ({ dependencyFactories }) => {
  568. const Dependencies = dependencyFactories.keys();
  569. for (const Dep of Dependencies) {
  570. for (let i = 0; i < schemas.length; i++) {
  571. if (Dep.name === schemas[i][0]) {
  572. schemas[i].Dependency = Dep;
  573. }
  574. }
  575. }
  576. for (let i = 0; i < schemas.length; i++) {
  577. if (!schemas[i].Dependency) {
  578. if (this.options.schema < 4) {
  579. } else {
  580. if (schemas[i][0] === 'JsonExportsDependency') {
  581. try {
  582. schemas[
  583. i
  584. ].Dependency = require('webpack/lib/dependencies/JsonExportsDependency');
  585. } catch (_) {}
  586. } else if (schemas[i][0] === 'DelegatedExportsDependency') {
  587. try {
  588. schemas[
  589. i
  590. ].Dependency = require('webpack/lib/dependencies/DelegatedExportsDependency');
  591. } catch (_) {}
  592. } else if (schemas[i][0] === 'DelegatedSourceDependency') {
  593. try {
  594. schemas[
  595. i
  596. ].Dependency = require('webpack/lib/dependencies/DelegatedSourceDependency');
  597. } catch (_) {}
  598. }
  599. }
  600. }
  601. }
  602. },
  603. );
  604. },
  605. );
  606. let methods;
  607. pluginCompat.tap(
  608. compiler,
  609. '_hardSourceMethods',
  610. 'TransformBasicDependencyPlugin methods',
  611. _methods => {
  612. methods = _methods;
  613. },
  614. );
  615. pluginCompat.tap(
  616. compiler,
  617. '_hardSourceFreezeDependency',
  618. 'TransformBasicDependencyPlugin freeze',
  619. (frozen, dependency, extra) => {
  620. extra.schemas = schemas;
  621. const _frozen = freezeDependency(dependency, extra, methods);
  622. if (_frozen) {
  623. if (dependency.prepend) {
  624. _frozen.prepend = dependency.prepend;
  625. }
  626. if (dependency.replaces) {
  627. _frozen.replaces = dependency.replaces;
  628. }
  629. if (dependency.critical) {
  630. _frozen.critical = dependency.critical;
  631. }
  632. if (typeof dependency.namespaceObjectAsContext !== 'undefined') {
  633. _frozen.namespaceObjectAsContext =
  634. dependency.namespaceObjectAsContext;
  635. }
  636. if (typeof dependency.callArgs !== 'undefined') {
  637. _frozen.callArgs = dependency.callArgs;
  638. }
  639. if (typeof dependency.call !== 'undefined') {
  640. _frozen.call = dependency.call;
  641. }
  642. if (typeof dependency.directImport !== 'undefined') {
  643. _frozen.directImport = dependency.directImport;
  644. }
  645. if (typeof dependency.shorthand !== 'undefined') {
  646. _frozen.shorthand = dependency.shorthand;
  647. }
  648. if (
  649. typeof dependency.localModule === 'object' &&
  650. dependency.localModule !== null
  651. ) {
  652. _frozen.localModule = {
  653. name: dependency.localModule.name,
  654. idx: dependency.localModule.idx,
  655. used: dependency.localModule.used,
  656. };
  657. }
  658. return _frozen;
  659. }
  660. return frozen;
  661. },
  662. );
  663. pluginCompat.tap(
  664. compiler,
  665. '_hardSourceAfterFreezeDependency',
  666. 'TransformBasicDependencyPlugin after freeze',
  667. (frozen, dependency, extra) => {
  668. if (frozen && dependency.loc) {
  669. frozen.loc = flattenPrototype(dependency.loc);
  670. }
  671. if (frozen && dependency.optional) {
  672. frozen.optional = dependency.optional;
  673. }
  674. if (frozen && dependency.getWarnings) {
  675. const warnings = dependency.getWarnings();
  676. if (warnings && warnings.length) {
  677. frozen.warnings = warnings.map(({ stack }) =>
  678. stack.includes('\n at pluginCompat.tap')
  679. ? stack.split('\n at pluginCompat.tap')[0]
  680. : stack.split('\n at Compiler.pluginCompat.tap')[0],
  681. );
  682. }
  683. }
  684. return frozen;
  685. },
  686. );
  687. pluginCompat.tap(
  688. compiler,
  689. '_hardSourceThawDependency',
  690. 'TransformBasicDependencyPlugin',
  691. (dependency, frozen, extra) => {
  692. extra.schemas = schemas;
  693. const _thawed = thawDependency(frozen, extra, methods);
  694. if (_thawed) {
  695. const state = extra.state;
  696. // console.log('Thawed', frozen.type);
  697. if (frozen.prepend) {
  698. _thawed.prepend = frozen.prepend;
  699. }
  700. if (frozen.replaces) {
  701. _thawed.replaces = frozen.replaced;
  702. }
  703. if (frozen.critical) {
  704. _thawed.critical = frozen.critical;
  705. }
  706. if (typeof frozen.namespaceObjectAsContext !== 'undefined') {
  707. _thawed.namespaceObjectAsContext = frozen.namespaceObjectAsContext;
  708. }
  709. if (typeof frozen.callArgs !== 'undefined') {
  710. _thawed.callArgs = frozen.callArgs;
  711. }
  712. if (typeof frozen.call !== 'undefined') {
  713. _thawed.call = frozen.call;
  714. }
  715. if (typeof frozen.directImport !== 'undefined') {
  716. _thawed.directImport = frozen.directImport;
  717. }
  718. if (typeof frozen.shorthand !== 'undefined') {
  719. _thawed.shorthand = frozen.shorthand;
  720. }
  721. if (
  722. typeof frozen.localModule === 'object' &&
  723. frozen.localModule !== null
  724. ) {
  725. if (!state.localModules) {
  726. state.localModules = [];
  727. }
  728. if (!state.localModules[frozen.localModule.idx]) {
  729. state.localModules[frozen.localModule.idx] = new LocalModule(
  730. extra.module,
  731. frozen.localModule.name,
  732. frozen.localModule.idx,
  733. );
  734. state.localModules[frozen.localModule.idx].used =
  735. frozen.localModule.used;
  736. }
  737. _thawed.localModule = state.localModules[frozen.localModule.idx];
  738. }
  739. if (frozen.type === 'HarmonyImportDependency') {
  740. const ref = frozen.range.toString();
  741. if (state.imports[ref]) {
  742. return state.imports[ref];
  743. }
  744. state.imports[ref] = _thawed;
  745. } else if (
  746. frozen.type === 'HarmonyExportImportedSpecifierDependency'
  747. ) {
  748. if (_thawed.otherStarExports) {
  749. extra.state.otherStarExports = (
  750. extra.state.otherStarExports || []
  751. ).concat(_thawed);
  752. }
  753. }
  754. return _thawed;
  755. }
  756. return dependency;
  757. },
  758. );
  759. pluginCompat.tap(
  760. compiler,
  761. '_hardSourceAfterThawDependency',
  762. 'TransformBasicDependencyPlugin',
  763. (dependency, { loc, optional, warnings }, extra) => {
  764. if (dependency && loc) {
  765. dependency.loc = loc;
  766. }
  767. if (dependency && optional) {
  768. dependency.optional = true;
  769. }
  770. if (dependency && warnings && dependency.getWarnings) {
  771. const frozenWarnings = warnings;
  772. const _getWarnings = dependency.getWarnings;
  773. dependency.getWarnings = function() {
  774. const warnings = _getWarnings.call(this);
  775. if (warnings && warnings.length) {
  776. return warnings.map((warning, i) => {
  777. const stack = warning.stack.split(
  778. '\n at Compilation.reportDependencyErrorsAndWarnings',
  779. )[1];
  780. warning.stack = `${
  781. frozenWarnings[i]
  782. }\n at Compilation.reportDependencyErrorsAndWarnings${stack}`;
  783. return warning;
  784. });
  785. }
  786. return warnings;
  787. };
  788. }
  789. return dependency;
  790. },
  791. );
  792. }
  793. }
  794. module.exports = TransformBasicDependencyPluginLegacy;