CommonJsImportsParserPlugin.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { fileURLToPath } = require("url");
  7. const CommentCompilationWarning = require("../CommentCompilationWarning");
  8. const RuntimeGlobals = require("../RuntimeGlobals");
  9. const UnsupportedFeatureWarning = require("../UnsupportedFeatureWarning");
  10. const WebpackError = require("../WebpackError");
  11. const BasicEvaluatedExpression = require("../javascript/BasicEvaluatedExpression");
  12. const {
  13. evaluateToIdentifier,
  14. evaluateToString,
  15. expressionIsUnsupported,
  16. toConstantDependency
  17. } = require("../javascript/JavascriptParserHelpers");
  18. const CommonJsFullRequireDependency = require("./CommonJsFullRequireDependency");
  19. const CommonJsRequireContextDependency = require("./CommonJsRequireContextDependency");
  20. const CommonJsRequireDependency = require("./CommonJsRequireDependency");
  21. const ConstDependency = require("./ConstDependency");
  22. const ContextDependencyHelpers = require("./ContextDependencyHelpers");
  23. const LocalModuleDependency = require("./LocalModuleDependency");
  24. const { getLocalModule } = require("./LocalModulesHelpers");
  25. const RequireHeaderDependency = require("./RequireHeaderDependency");
  26. const RequireResolveContextDependency = require("./RequireResolveContextDependency");
  27. const RequireResolveDependency = require("./RequireResolveDependency");
  28. const RequireResolveHeaderDependency = require("./RequireResolveHeaderDependency");
  29. /** @typedef {import("estree").CallExpression} CallExpression */
  30. /** @typedef {import("estree").Expression} Expression */
  31. /** @typedef {import("estree").NewExpression} NewExpression */
  32. /** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
  33. /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
  34. /** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
  35. /** @typedef {import("../javascript/JavascriptParser").ImportSource} ImportSource */
  36. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  37. const createRequireSpecifierTag = Symbol("createRequire");
  38. const createdRequireIdentifierTag = Symbol("createRequire()");
  39. class CommonJsImportsParserPlugin {
  40. /**
  41. * @param {JavascriptParserOptions} options parser options
  42. */
  43. constructor(options) {
  44. this.options = options;
  45. }
  46. /**
  47. * @param {JavascriptParser} parser the parser
  48. * @returns {void}
  49. */
  50. apply(parser) {
  51. const options = this.options;
  52. const getContext = () => {
  53. if (parser.currentTagData) {
  54. const { context } = parser.currentTagData;
  55. return context;
  56. }
  57. };
  58. //#region metadata
  59. /**
  60. * @param {string} expression expression
  61. * @param {() => string[]} getMembers get members
  62. */
  63. const tapRequireExpression = (expression, getMembers) => {
  64. parser.hooks.typeof
  65. .for(expression)
  66. .tap(
  67. "CommonJsImportsParserPlugin",
  68. toConstantDependency(parser, JSON.stringify("function"))
  69. );
  70. parser.hooks.evaluateTypeof
  71. .for(expression)
  72. .tap("CommonJsImportsParserPlugin", evaluateToString("function"));
  73. parser.hooks.evaluateIdentifier
  74. .for(expression)
  75. .tap(
  76. "CommonJsImportsParserPlugin",
  77. evaluateToIdentifier(expression, "require", getMembers, true)
  78. );
  79. };
  80. /**
  81. * @param {string | symbol} tag tag
  82. */
  83. const tapRequireExpressionTag = tag => {
  84. parser.hooks.typeof
  85. .for(tag)
  86. .tap(
  87. "CommonJsImportsParserPlugin",
  88. toConstantDependency(parser, JSON.stringify("function"))
  89. );
  90. parser.hooks.evaluateTypeof
  91. .for(tag)
  92. .tap("CommonJsImportsParserPlugin", evaluateToString("function"));
  93. };
  94. tapRequireExpression("require", () => []);
  95. tapRequireExpression("require.resolve", () => ["resolve"]);
  96. tapRequireExpression("require.resolveWeak", () => ["resolveWeak"]);
  97. //#endregion
  98. // Weird stuff //
  99. parser.hooks.assign
  100. .for("require")
  101. .tap("CommonJsImportsParserPlugin", expr => {
  102. // to not leak to global "require", we need to define a local require here.
  103. const dep = new ConstDependency("var require;", 0);
  104. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  105. parser.state.module.addPresentationalDependency(dep);
  106. return true;
  107. });
  108. //#region Unsupported
  109. parser.hooks.expression
  110. .for("require.main")
  111. .tap(
  112. "CommonJsImportsParserPlugin",
  113. expressionIsUnsupported(
  114. parser,
  115. "require.main is not supported by webpack."
  116. )
  117. );
  118. parser.hooks.call
  119. .for("require.main.require")
  120. .tap(
  121. "CommonJsImportsParserPlugin",
  122. expressionIsUnsupported(
  123. parser,
  124. "require.main.require is not supported by webpack."
  125. )
  126. );
  127. parser.hooks.expression
  128. .for("module.parent.require")
  129. .tap(
  130. "CommonJsImportsParserPlugin",
  131. expressionIsUnsupported(
  132. parser,
  133. "module.parent.require is not supported by webpack."
  134. )
  135. );
  136. parser.hooks.call
  137. .for("module.parent.require")
  138. .tap(
  139. "CommonJsImportsParserPlugin",
  140. expressionIsUnsupported(
  141. parser,
  142. "module.parent.require is not supported by webpack."
  143. )
  144. );
  145. //#endregion
  146. //#region Renaming
  147. /**
  148. * @param {Expression} expr expression
  149. * @returns {boolean} true when set undefined
  150. */
  151. const defineUndefined = expr => {
  152. // To avoid "not defined" error, replace the value with undefined
  153. const dep = new ConstDependency(
  154. "undefined",
  155. /** @type {Range} */ (expr.range)
  156. );
  157. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  158. parser.state.module.addPresentationalDependency(dep);
  159. return false;
  160. };
  161. parser.hooks.canRename
  162. .for("require")
  163. .tap("CommonJsImportsParserPlugin", () => true);
  164. parser.hooks.rename
  165. .for("require")
  166. .tap("CommonJsImportsParserPlugin", defineUndefined);
  167. //#endregion
  168. //#region Inspection
  169. const requireCache = toConstantDependency(
  170. parser,
  171. RuntimeGlobals.moduleCache,
  172. [
  173. RuntimeGlobals.moduleCache,
  174. RuntimeGlobals.moduleId,
  175. RuntimeGlobals.moduleLoaded
  176. ]
  177. );
  178. parser.hooks.expression
  179. .for("require.cache")
  180. .tap("CommonJsImportsParserPlugin", requireCache);
  181. //#endregion
  182. //#region Require as expression
  183. /**
  184. * @param {Expression} expr expression
  185. * @returns {boolean} true when handled
  186. */
  187. const requireAsExpressionHandler = expr => {
  188. const dep = new CommonJsRequireContextDependency(
  189. {
  190. request: options.unknownContextRequest,
  191. recursive: options.unknownContextRecursive,
  192. regExp: options.unknownContextRegExp,
  193. mode: "sync"
  194. },
  195. /** @type {Range} */ (expr.range),
  196. undefined,
  197. parser.scope.inShorthand,
  198. getContext()
  199. );
  200. dep.critical =
  201. options.unknownContextCritical &&
  202. "require function is used in a way in which dependencies cannot be statically extracted";
  203. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  204. dep.optional = !!parser.scope.inTry;
  205. parser.state.current.addDependency(dep);
  206. return true;
  207. };
  208. parser.hooks.expression
  209. .for("require")
  210. .tap("CommonJsImportsParserPlugin", requireAsExpressionHandler);
  211. //#endregion
  212. //#region Require
  213. /**
  214. * @param {CallExpression | NewExpression} expr expression
  215. * @param {BasicEvaluatedExpression} param param
  216. * @returns {boolean | void} true when handled
  217. */
  218. const processRequireItem = (expr, param) => {
  219. if (param.isString()) {
  220. const dep = new CommonJsRequireDependency(
  221. /** @type {string} */ (param.string),
  222. /** @type {Range} */ (param.range),
  223. getContext()
  224. );
  225. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  226. dep.optional = !!parser.scope.inTry;
  227. parser.state.current.addDependency(dep);
  228. return true;
  229. }
  230. };
  231. /**
  232. * @param {CallExpression | NewExpression} expr expression
  233. * @param {BasicEvaluatedExpression} param param
  234. * @returns {boolean | void} true when handled
  235. */
  236. const processRequireContext = (expr, param) => {
  237. const dep = ContextDependencyHelpers.create(
  238. CommonJsRequireContextDependency,
  239. /** @type {Range} */ (expr.range),
  240. param,
  241. expr,
  242. options,
  243. {
  244. category: "commonjs"
  245. },
  246. parser,
  247. undefined,
  248. getContext()
  249. );
  250. if (!dep) return;
  251. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  252. dep.optional = !!parser.scope.inTry;
  253. parser.state.current.addDependency(dep);
  254. return true;
  255. };
  256. /**
  257. * @param {boolean} callNew true, when require is called with new
  258. * @returns {(expr: CallExpression | NewExpression) => (boolean | void)} handler
  259. */
  260. const createRequireHandler = callNew => expr => {
  261. if (options.commonjsMagicComments) {
  262. const { options: requireOptions, errors: commentErrors } =
  263. parser.parseCommentOptions(/** @type {Range} */ (expr.range));
  264. if (commentErrors) {
  265. for (const e of commentErrors) {
  266. const { comment } = e;
  267. parser.state.module.addWarning(
  268. new CommentCompilationWarning(
  269. `Compilation error while processing magic comment(-s): /*${comment.value}*/: ${e.message}`,
  270. comment.loc
  271. )
  272. );
  273. }
  274. }
  275. if (requireOptions) {
  276. if (requireOptions.webpackIgnore !== undefined) {
  277. if (typeof requireOptions.webpackIgnore !== "boolean") {
  278. parser.state.module.addWarning(
  279. new UnsupportedFeatureWarning(
  280. `\`webpackIgnore\` expected a boolean, but received: ${requireOptions.webpackIgnore}.`,
  281. /** @type {DependencyLocation} */ (expr.loc)
  282. )
  283. );
  284. } else {
  285. // Do not instrument `require()` if `webpackIgnore` is `true`
  286. if (requireOptions.webpackIgnore) {
  287. return true;
  288. }
  289. }
  290. }
  291. }
  292. }
  293. if (expr.arguments.length !== 1) return;
  294. let localModule;
  295. const param = parser.evaluateExpression(expr.arguments[0]);
  296. if (param.isConditional()) {
  297. let isExpression = false;
  298. for (const p of /** @type {BasicEvaluatedExpression[]} */ (
  299. param.options
  300. )) {
  301. const result = processRequireItem(expr, p);
  302. if (result === undefined) {
  303. isExpression = true;
  304. }
  305. }
  306. if (!isExpression) {
  307. const dep = new RequireHeaderDependency(
  308. /** @type {Range} */ (expr.callee.range)
  309. );
  310. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  311. parser.state.module.addPresentationalDependency(dep);
  312. return true;
  313. }
  314. }
  315. if (
  316. param.isString() &&
  317. (localModule = getLocalModule(
  318. parser.state,
  319. /** @type {string} */ (param.string)
  320. ))
  321. ) {
  322. localModule.flagUsed();
  323. const dep = new LocalModuleDependency(
  324. localModule,
  325. /** @type {Range} */ (expr.range),
  326. callNew
  327. );
  328. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  329. parser.state.module.addPresentationalDependency(dep);
  330. } else {
  331. const result = processRequireItem(expr, param);
  332. if (result === undefined) {
  333. processRequireContext(expr, param);
  334. } else {
  335. const dep = new RequireHeaderDependency(
  336. /** @type {Range} */ (expr.callee.range)
  337. );
  338. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  339. parser.state.module.addPresentationalDependency(dep);
  340. }
  341. }
  342. return true;
  343. };
  344. parser.hooks.call
  345. .for("require")
  346. .tap("CommonJsImportsParserPlugin", createRequireHandler(false));
  347. parser.hooks.new
  348. .for("require")
  349. .tap("CommonJsImportsParserPlugin", createRequireHandler(true));
  350. parser.hooks.call
  351. .for("module.require")
  352. .tap("CommonJsImportsParserPlugin", createRequireHandler(false));
  353. parser.hooks.new
  354. .for("module.require")
  355. .tap("CommonJsImportsParserPlugin", createRequireHandler(true));
  356. //#endregion
  357. //#region Require with property access
  358. /**
  359. * @param {Expression} expr expression
  360. * @param {string[]} calleeMembers callee members
  361. * @param {CallExpression} callExpr call expression
  362. * @param {string[]} members members
  363. * @param {Range[]} memberRanges member ranges
  364. * @returns {boolean | void} true when handled
  365. */
  366. const chainHandler = (
  367. expr,
  368. calleeMembers,
  369. callExpr,
  370. members,
  371. memberRanges
  372. ) => {
  373. if (callExpr.arguments.length !== 1) return;
  374. const param = parser.evaluateExpression(callExpr.arguments[0]);
  375. if (
  376. param.isString() &&
  377. !getLocalModule(parser.state, /** @type {string} */ (param.string))
  378. ) {
  379. const dep = new CommonJsFullRequireDependency(
  380. /** @type {string} */ (param.string),
  381. /** @type {Range} */ (expr.range),
  382. members,
  383. /** @type {Range[]} */ memberRanges
  384. );
  385. dep.asiSafe = !parser.isAsiPosition(
  386. /** @type {Range} */ (expr.range)[0]
  387. );
  388. dep.optional = !!parser.scope.inTry;
  389. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  390. parser.state.current.addDependency(dep);
  391. return true;
  392. }
  393. };
  394. /**
  395. * @param {CallExpression} expr expression
  396. * @param {string[]} calleeMembers callee members
  397. * @param {CallExpression} callExpr call expression
  398. * @param {string[]} members members
  399. * @param {Range[]} memberRanges member ranges
  400. * @returns {boolean | void} true when handled
  401. */
  402. const callChainHandler = (
  403. expr,
  404. calleeMembers,
  405. callExpr,
  406. members,
  407. memberRanges
  408. ) => {
  409. if (callExpr.arguments.length !== 1) return;
  410. const param = parser.evaluateExpression(callExpr.arguments[0]);
  411. if (
  412. param.isString() &&
  413. !getLocalModule(parser.state, /** @type {string} */ (param.string))
  414. ) {
  415. const dep = new CommonJsFullRequireDependency(
  416. /** @type {string} */ (param.string),
  417. /** @type {Range} */ (expr.callee.range),
  418. members,
  419. /** @type {Range[]} */ memberRanges
  420. );
  421. dep.call = true;
  422. dep.asiSafe = !parser.isAsiPosition(
  423. /** @type {Range} */ (expr.range)[0]
  424. );
  425. dep.optional = !!parser.scope.inTry;
  426. dep.loc = /** @type {DependencyLocation} */ (expr.callee.loc);
  427. parser.state.current.addDependency(dep);
  428. parser.walkExpressions(expr.arguments);
  429. return true;
  430. }
  431. };
  432. parser.hooks.memberChainOfCallMemberChain
  433. .for("require")
  434. .tap("CommonJsImportsParserPlugin", chainHandler);
  435. parser.hooks.memberChainOfCallMemberChain
  436. .for("module.require")
  437. .tap("CommonJsImportsParserPlugin", chainHandler);
  438. parser.hooks.callMemberChainOfCallMemberChain
  439. .for("require")
  440. .tap("CommonJsImportsParserPlugin", callChainHandler);
  441. parser.hooks.callMemberChainOfCallMemberChain
  442. .for("module.require")
  443. .tap("CommonJsImportsParserPlugin", callChainHandler);
  444. //#endregion
  445. //#region Require.resolve
  446. /**
  447. * @param {CallExpression} expr call expression
  448. * @param {boolean} weak weak
  449. * @returns {boolean | void} true when handled
  450. */
  451. const processResolve = (expr, weak) => {
  452. if (expr.arguments.length !== 1) return;
  453. const param = parser.evaluateExpression(expr.arguments[0]);
  454. if (param.isConditional()) {
  455. for (const option of /** @type {BasicEvaluatedExpression[]} */ (
  456. param.options
  457. )) {
  458. const result = processResolveItem(expr, option, weak);
  459. if (result === undefined) {
  460. processResolveContext(expr, option, weak);
  461. }
  462. }
  463. const dep = new RequireResolveHeaderDependency(
  464. /** @type {Range} */ (expr.callee.range)
  465. );
  466. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  467. parser.state.module.addPresentationalDependency(dep);
  468. return true;
  469. } else {
  470. const result = processResolveItem(expr, param, weak);
  471. if (result === undefined) {
  472. processResolveContext(expr, param, weak);
  473. }
  474. const dep = new RequireResolveHeaderDependency(
  475. /** @type {Range} */ (expr.callee.range)
  476. );
  477. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  478. parser.state.module.addPresentationalDependency(dep);
  479. return true;
  480. }
  481. };
  482. /**
  483. * @param {CallExpression} expr call expression
  484. * @param {BasicEvaluatedExpression} param param
  485. * @param {boolean} weak weak
  486. * @returns {boolean | void} true when handled
  487. */
  488. const processResolveItem = (expr, param, weak) => {
  489. if (param.isString()) {
  490. const dep = new RequireResolveDependency(
  491. /** @type {string} */ (param.string),
  492. /** @type {Range} */ (param.range),
  493. getContext()
  494. );
  495. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  496. dep.optional = !!parser.scope.inTry;
  497. dep.weak = weak;
  498. parser.state.current.addDependency(dep);
  499. return true;
  500. }
  501. };
  502. /**
  503. * @param {CallExpression} expr call expression
  504. * @param {BasicEvaluatedExpression} param param
  505. * @param {boolean} weak weak
  506. * @returns {boolean | void} true when handled
  507. */
  508. const processResolveContext = (expr, param, weak) => {
  509. const dep = ContextDependencyHelpers.create(
  510. RequireResolveContextDependency,
  511. /** @type {Range} */ (param.range),
  512. param,
  513. expr,
  514. options,
  515. {
  516. category: "commonjs",
  517. mode: weak ? "weak" : "sync"
  518. },
  519. parser,
  520. getContext()
  521. );
  522. if (!dep) return;
  523. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  524. dep.optional = !!parser.scope.inTry;
  525. parser.state.current.addDependency(dep);
  526. return true;
  527. };
  528. parser.hooks.call
  529. .for("require.resolve")
  530. .tap("CommonJsImportsParserPlugin", expr => {
  531. return processResolve(expr, false);
  532. });
  533. parser.hooks.call
  534. .for("require.resolveWeak")
  535. .tap("CommonJsImportsParserPlugin", expr => {
  536. return processResolve(expr, true);
  537. });
  538. //#endregion
  539. //#region Create require
  540. if (!options.createRequire) return;
  541. /** @type {ImportSource[]} */
  542. let moduleName = [];
  543. /** @type {string | undefined} */
  544. let specifierName;
  545. if (options.createRequire === true) {
  546. moduleName = ["module", "node:module"];
  547. specifierName = "createRequire";
  548. } else {
  549. let moduleName;
  550. const match = /^(.*) from (.*)$/.exec(options.createRequire);
  551. if (match) {
  552. [, specifierName, moduleName] = match;
  553. }
  554. if (!specifierName || !moduleName) {
  555. const err = new WebpackError(
  556. `Parsing javascript parser option "createRequire" failed, got ${JSON.stringify(
  557. options.createRequire
  558. )}`
  559. );
  560. err.details =
  561. 'Expected string in format "createRequire from module", where "createRequire" is specifier name and "module" name of the module';
  562. throw err;
  563. }
  564. }
  565. tapRequireExpressionTag(createdRequireIdentifierTag);
  566. tapRequireExpressionTag(createRequireSpecifierTag);
  567. parser.hooks.evaluateCallExpression
  568. .for(createRequireSpecifierTag)
  569. .tap("CommonJsImportsParserPlugin", expr => {
  570. const context = parseCreateRequireArguments(expr);
  571. if (context === undefined) return;
  572. const ident = parser.evaluatedVariable({
  573. tag: createdRequireIdentifierTag,
  574. data: { context },
  575. next: undefined
  576. });
  577. return new BasicEvaluatedExpression()
  578. .setIdentifier(
  579. /** @type {TODO} */ (ident),
  580. /** @type {TODO} */ (ident),
  581. () => []
  582. )
  583. .setSideEffects(false)
  584. .setRange(/** @type {Range} */ (expr.range));
  585. });
  586. parser.hooks.unhandledExpressionMemberChain
  587. .for(createdRequireIdentifierTag)
  588. .tap("CommonJsImportsParserPlugin", (expr, members) => {
  589. return expressionIsUnsupported(
  590. parser,
  591. `createRequire().${members.join(".")} is not supported by webpack.`
  592. )(expr);
  593. });
  594. parser.hooks.canRename
  595. .for(createdRequireIdentifierTag)
  596. .tap("CommonJsImportsParserPlugin", () => true);
  597. parser.hooks.canRename
  598. .for(createRequireSpecifierTag)
  599. .tap("CommonJsImportsParserPlugin", () => true);
  600. parser.hooks.rename
  601. .for(createRequireSpecifierTag)
  602. .tap("CommonJsImportsParserPlugin", defineUndefined);
  603. parser.hooks.expression
  604. .for(createdRequireIdentifierTag)
  605. .tap("CommonJsImportsParserPlugin", requireAsExpressionHandler);
  606. parser.hooks.call
  607. .for(createdRequireIdentifierTag)
  608. .tap("CommonJsImportsParserPlugin", createRequireHandler(false));
  609. /**
  610. * @param {CallExpression} expr call expression
  611. * @returns {string | void} context
  612. */
  613. const parseCreateRequireArguments = expr => {
  614. const args = expr.arguments;
  615. if (args.length !== 1) {
  616. const err = new WebpackError(
  617. "module.createRequire supports only one argument."
  618. );
  619. err.loc = /** @type {DependencyLocation} */ (expr.loc);
  620. parser.state.module.addWarning(err);
  621. return;
  622. }
  623. const arg = args[0];
  624. const evaluated = parser.evaluateExpression(arg);
  625. if (!evaluated.isString()) {
  626. const err = new WebpackError(
  627. "module.createRequire failed parsing argument."
  628. );
  629. err.loc = /** @type {DependencyLocation} */ (arg.loc);
  630. parser.state.module.addWarning(err);
  631. return;
  632. }
  633. const ctx = /** @type {string} */ (evaluated.string).startsWith("file://")
  634. ? fileURLToPath(/** @type {string} */ (evaluated.string))
  635. : /** @type {string} */ (evaluated.string);
  636. // argument always should be a filename
  637. return ctx.slice(0, ctx.lastIndexOf(ctx.startsWith("/") ? "/" : "\\"));
  638. };
  639. parser.hooks.import.tap(
  640. {
  641. name: "CommonJsImportsParserPlugin",
  642. stage: -10
  643. },
  644. (statement, source) => {
  645. if (
  646. !moduleName.includes(source) ||
  647. statement.specifiers.length !== 1 ||
  648. statement.specifiers[0].type !== "ImportSpecifier" ||
  649. statement.specifiers[0].imported.type !== "Identifier" ||
  650. statement.specifiers[0].imported.name !== specifierName
  651. )
  652. return;
  653. // clear for 'import { createRequire as x } from "module"'
  654. // if any other specifier was used import module
  655. const clearDep = new ConstDependency(
  656. parser.isAsiPosition(/** @type {Range} */ (statement.range)[0])
  657. ? ";"
  658. : "",
  659. /** @type {Range} */ (statement.range)
  660. );
  661. clearDep.loc = /** @type {DependencyLocation} */ (statement.loc);
  662. parser.state.module.addPresentationalDependency(clearDep);
  663. parser.unsetAsiPosition(/** @type {Range} */ (statement.range)[1]);
  664. return true;
  665. }
  666. );
  667. parser.hooks.importSpecifier.tap(
  668. {
  669. name: "CommonJsImportsParserPlugin",
  670. stage: -10
  671. },
  672. (statement, source, id, name) => {
  673. if (!moduleName.includes(source) || id !== specifierName) return;
  674. parser.tagVariable(name, createRequireSpecifierTag);
  675. return true;
  676. }
  677. );
  678. parser.hooks.preDeclarator.tap(
  679. "CommonJsImportsParserPlugin",
  680. declarator => {
  681. if (
  682. declarator.id.type !== "Identifier" ||
  683. !declarator.init ||
  684. declarator.init.type !== "CallExpression" ||
  685. declarator.init.callee.type !== "Identifier"
  686. )
  687. return;
  688. const variableInfo =
  689. /** @type {TODO} */
  690. (parser.getVariableInfo(declarator.init.callee.name));
  691. if (
  692. variableInfo &&
  693. variableInfo.tagInfo &&
  694. variableInfo.tagInfo.tag === createRequireSpecifierTag
  695. ) {
  696. const context = parseCreateRequireArguments(declarator.init);
  697. if (context === undefined) return;
  698. parser.tagVariable(declarator.id.name, createdRequireIdentifierTag, {
  699. name: declarator.id.name,
  700. context
  701. });
  702. return true;
  703. }
  704. }
  705. );
  706. parser.hooks.memberChainOfCallMemberChain
  707. .for(createRequireSpecifierTag)
  708. .tap(
  709. "CommonJsImportsParserPlugin",
  710. (expr, calleeMembers, callExpr, members) => {
  711. if (
  712. calleeMembers.length !== 0 ||
  713. members.length !== 1 ||
  714. members[0] !== "cache"
  715. )
  716. return;
  717. // createRequire().cache
  718. const context = parseCreateRequireArguments(callExpr);
  719. if (context === undefined) return;
  720. return requireCache(expr);
  721. }
  722. );
  723. parser.hooks.callMemberChainOfCallMemberChain
  724. .for(createRequireSpecifierTag)
  725. .tap(
  726. "CommonJsImportsParserPlugin",
  727. (expr, calleeMembers, innerCallExpression, members) => {
  728. if (
  729. calleeMembers.length !== 0 ||
  730. members.length !== 1 ||
  731. members[0] !== "resolve"
  732. )
  733. return;
  734. // createRequire().resolve()
  735. return processResolve(expr, false);
  736. }
  737. );
  738. parser.hooks.expressionMemberChain
  739. .for(createdRequireIdentifierTag)
  740. .tap("CommonJsImportsParserPlugin", (expr, members) => {
  741. // require.cache
  742. if (members.length === 1 && members[0] === "cache") {
  743. return requireCache(expr);
  744. }
  745. });
  746. parser.hooks.callMemberChain
  747. .for(createdRequireIdentifierTag)
  748. .tap("CommonJsImportsParserPlugin", (expr, members) => {
  749. // require.resolve()
  750. if (members.length === 1 && members[0] === "resolve") {
  751. return processResolve(expr, false);
  752. }
  753. });
  754. parser.hooks.call
  755. .for(createRequireSpecifierTag)
  756. .tap("CommonJsImportsParserPlugin", expr => {
  757. const clearDep = new ConstDependency(
  758. "/* createRequire() */ undefined",
  759. /** @type {Range} */ (expr.range)
  760. );
  761. clearDep.loc = /** @type {DependencyLocation} */ (expr.loc);
  762. parser.state.module.addPresentationalDependency(clearDep);
  763. return true;
  764. });
  765. //#endregion
  766. }
  767. }
  768. module.exports = CommonJsImportsParserPlugin;