CommonJsImportsParserPlugin.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  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 {TODO} 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. return true;
  331. } else {
  332. const result = processRequireItem(expr, param);
  333. if (result === undefined) {
  334. processRequireContext(expr, param);
  335. } else {
  336. const dep = new RequireHeaderDependency(
  337. /** @type {Range} */ (expr.callee.range)
  338. );
  339. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  340. parser.state.module.addPresentationalDependency(dep);
  341. }
  342. return true;
  343. }
  344. };
  345. parser.hooks.call
  346. .for("require")
  347. .tap("CommonJsImportsParserPlugin", createRequireHandler(false));
  348. parser.hooks.new
  349. .for("require")
  350. .tap("CommonJsImportsParserPlugin", createRequireHandler(true));
  351. parser.hooks.call
  352. .for("module.require")
  353. .tap("CommonJsImportsParserPlugin", createRequireHandler(false));
  354. parser.hooks.new
  355. .for("module.require")
  356. .tap("CommonJsImportsParserPlugin", createRequireHandler(true));
  357. //#endregion
  358. //#region Require with property access
  359. /**
  360. * @param {Expression} expr expression
  361. * @param {string[]} calleeMembers callee members
  362. * @param {CallExpression} callExpr call expression
  363. * @param {string[]} members members
  364. * @returns {boolean | void} true when handled
  365. */
  366. const chainHandler = (expr, calleeMembers, callExpr, members) => {
  367. if (callExpr.arguments.length !== 1) return;
  368. const param = parser.evaluateExpression(callExpr.arguments[0]);
  369. if (
  370. param.isString() &&
  371. !getLocalModule(parser.state, /** @type {string} */ (param.string))
  372. ) {
  373. const dep = new CommonJsFullRequireDependency(
  374. /** @type {string} */ (param.string),
  375. /** @type {Range} */ (expr.range),
  376. members
  377. );
  378. dep.asiSafe = !parser.isAsiPosition(
  379. /** @type {Range} */ (expr.range)[0]
  380. );
  381. dep.optional = !!parser.scope.inTry;
  382. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  383. parser.state.current.addDependency(dep);
  384. return true;
  385. }
  386. };
  387. /**
  388. * @param {CallExpression} expr expression
  389. * @param {string[]} calleeMembers callee members
  390. * @param {CallExpression} callExpr call expression
  391. * @param {string[]} members members
  392. * @returns {boolean | void} true when handled
  393. */
  394. const callChainHandler = (expr, calleeMembers, callExpr, members) => {
  395. if (callExpr.arguments.length !== 1) return;
  396. const param = parser.evaluateExpression(callExpr.arguments[0]);
  397. if (
  398. param.isString() &&
  399. !getLocalModule(parser.state, /** @type {string} */ (param.string))
  400. ) {
  401. const dep = new CommonJsFullRequireDependency(
  402. /** @type {string} */ (param.string),
  403. /** @type {Range} */ (expr.callee.range),
  404. members
  405. );
  406. dep.call = true;
  407. dep.asiSafe = !parser.isAsiPosition(
  408. /** @type {Range} */ (expr.range)[0]
  409. );
  410. dep.optional = !!parser.scope.inTry;
  411. dep.loc = /** @type {DependencyLocation} */ (expr.callee.loc);
  412. parser.state.current.addDependency(dep);
  413. parser.walkExpressions(expr.arguments);
  414. return true;
  415. }
  416. };
  417. parser.hooks.memberChainOfCallMemberChain
  418. .for("require")
  419. .tap("CommonJsImportsParserPlugin", chainHandler);
  420. parser.hooks.memberChainOfCallMemberChain
  421. .for("module.require")
  422. .tap("CommonJsImportsParserPlugin", chainHandler);
  423. parser.hooks.callMemberChainOfCallMemberChain
  424. .for("require")
  425. .tap("CommonJsImportsParserPlugin", callChainHandler);
  426. parser.hooks.callMemberChainOfCallMemberChain
  427. .for("module.require")
  428. .tap("CommonJsImportsParserPlugin", callChainHandler);
  429. //#endregion
  430. //#region Require.resolve
  431. /**
  432. * @param {CallExpression} expr call expression
  433. * @param {boolean} weak weak
  434. * @returns {boolean | void} true when handled
  435. */
  436. const processResolve = (expr, weak) => {
  437. if (expr.arguments.length !== 1) return;
  438. const param = parser.evaluateExpression(expr.arguments[0]);
  439. if (param.isConditional()) {
  440. for (const option of /** @type {BasicEvaluatedExpression[]} */ (
  441. param.options
  442. )) {
  443. const result = processResolveItem(expr, option, weak);
  444. if (result === undefined) {
  445. processResolveContext(expr, option, weak);
  446. }
  447. }
  448. const dep = new RequireResolveHeaderDependency(
  449. /** @type {Range} */ (expr.callee.range)
  450. );
  451. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  452. parser.state.module.addPresentationalDependency(dep);
  453. return true;
  454. } else {
  455. const result = processResolveItem(expr, param, weak);
  456. if (result === undefined) {
  457. processResolveContext(expr, param, weak);
  458. }
  459. const dep = new RequireResolveHeaderDependency(
  460. /** @type {Range} */ (expr.callee.range)
  461. );
  462. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  463. parser.state.module.addPresentationalDependency(dep);
  464. return true;
  465. }
  466. };
  467. /**
  468. * @param {CallExpression} expr call expression
  469. * @param {BasicEvaluatedExpression} param param
  470. * @param {boolean} weak weak
  471. * @returns {boolean | void} true when handled
  472. */
  473. const processResolveItem = (expr, param, weak) => {
  474. if (param.isString()) {
  475. const dep = new RequireResolveDependency(
  476. /** @type {string} */ (param.string),
  477. /** @type {Range} */ (param.range),
  478. getContext()
  479. );
  480. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  481. dep.optional = !!parser.scope.inTry;
  482. dep.weak = weak;
  483. parser.state.current.addDependency(dep);
  484. return true;
  485. }
  486. };
  487. /**
  488. * @param {CallExpression} expr call expression
  489. * @param {BasicEvaluatedExpression} param param
  490. * @param {boolean} weak weak
  491. * @returns {boolean | void} true when handled
  492. */
  493. const processResolveContext = (expr, param, weak) => {
  494. const dep = ContextDependencyHelpers.create(
  495. RequireResolveContextDependency,
  496. /** @type {Range} */ (param.range),
  497. param,
  498. expr,
  499. options,
  500. {
  501. category: "commonjs",
  502. mode: weak ? "weak" : "sync"
  503. },
  504. parser,
  505. getContext()
  506. );
  507. if (!dep) return;
  508. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  509. dep.optional = !!parser.scope.inTry;
  510. parser.state.current.addDependency(dep);
  511. return true;
  512. };
  513. parser.hooks.call
  514. .for("require.resolve")
  515. .tap("CommonJsImportsParserPlugin", expr => {
  516. return processResolve(expr, false);
  517. });
  518. parser.hooks.call
  519. .for("require.resolveWeak")
  520. .tap("CommonJsImportsParserPlugin", expr => {
  521. return processResolve(expr, true);
  522. });
  523. //#endregion
  524. //#region Create require
  525. if (!options.createRequire) return;
  526. /** @type {ImportSource[]} */
  527. let moduleName = [];
  528. /** @type {string | undefined} */
  529. let specifierName;
  530. if (options.createRequire === true) {
  531. moduleName = ["module", "node:module"];
  532. specifierName = "createRequire";
  533. } else {
  534. let moduleName;
  535. const match = /^(.*) from (.*)$/.exec(options.createRequire);
  536. if (match) {
  537. [, specifierName, moduleName] = match;
  538. }
  539. if (!specifierName || !moduleName) {
  540. const err = new WebpackError(
  541. `Parsing javascript parser option "createRequire" failed, got ${JSON.stringify(
  542. options.createRequire
  543. )}`
  544. );
  545. err.details =
  546. 'Expected string in format "createRequire from module", where "createRequire" is specifier name and "module" name of the module';
  547. throw err;
  548. }
  549. }
  550. tapRequireExpressionTag(createdRequireIdentifierTag);
  551. tapRequireExpressionTag(createRequireSpecifierTag);
  552. parser.hooks.evaluateCallExpression
  553. .for(createRequireSpecifierTag)
  554. .tap("CommonJsImportsParserPlugin", expr => {
  555. const context = parseCreateRequireArguments(expr);
  556. if (context === undefined) return;
  557. const ident = parser.evaluatedVariable({
  558. tag: createdRequireIdentifierTag,
  559. data: { context },
  560. next: undefined
  561. });
  562. return new BasicEvaluatedExpression()
  563. .setIdentifier(
  564. /** @type {TODO} */ (ident),
  565. /** @type {TODO} */ (ident),
  566. () => []
  567. )
  568. .setSideEffects(false)
  569. .setRange(/** @type {Range} */ (expr.range));
  570. });
  571. parser.hooks.unhandledExpressionMemberChain
  572. .for(createdRequireIdentifierTag)
  573. .tap("CommonJsImportsParserPlugin", (expr, members) => {
  574. return expressionIsUnsupported(
  575. parser,
  576. `createRequire().${members.join(".")} is not supported by webpack.`
  577. )(expr);
  578. });
  579. parser.hooks.canRename
  580. .for(createdRequireIdentifierTag)
  581. .tap("CommonJsImportsParserPlugin", () => true);
  582. parser.hooks.canRename
  583. .for(createRequireSpecifierTag)
  584. .tap("CommonJsImportsParserPlugin", () => true);
  585. parser.hooks.rename
  586. .for(createRequireSpecifierTag)
  587. .tap("CommonJsImportsParserPlugin", defineUndefined);
  588. parser.hooks.expression
  589. .for(createdRequireIdentifierTag)
  590. .tap("CommonJsImportsParserPlugin", requireAsExpressionHandler);
  591. parser.hooks.call
  592. .for(createdRequireIdentifierTag)
  593. .tap("CommonJsImportsParserPlugin", createRequireHandler(false));
  594. /**
  595. * @param {CallExpression} expr call expression
  596. * @returns {string | void} context
  597. */
  598. const parseCreateRequireArguments = expr => {
  599. const args = expr.arguments;
  600. if (args.length !== 1) {
  601. const err = new WebpackError(
  602. "module.createRequire supports only one argument."
  603. );
  604. err.loc = /** @type {DependencyLocation} */ (expr.loc);
  605. parser.state.module.addWarning(err);
  606. return;
  607. }
  608. const arg = args[0];
  609. const evaluated = parser.evaluateExpression(arg);
  610. if (!evaluated.isString()) {
  611. const err = new WebpackError(
  612. "module.createRequire failed parsing argument."
  613. );
  614. err.loc = /** @type {DependencyLocation} */ (arg.loc);
  615. parser.state.module.addWarning(err);
  616. return;
  617. }
  618. const ctx = /** @type {string} */ (evaluated.string).startsWith("file://")
  619. ? fileURLToPath(/** @type {string} */ (evaluated.string))
  620. : /** @type {string} */ (evaluated.string);
  621. // argument always should be a filename
  622. return ctx.slice(0, ctx.lastIndexOf(ctx.startsWith("/") ? "/" : "\\"));
  623. };
  624. parser.hooks.import.tap(
  625. {
  626. name: "CommonJsImportsParserPlugin",
  627. stage: -10
  628. },
  629. (statement, source) => {
  630. if (
  631. !moduleName.includes(source) ||
  632. statement.specifiers.length !== 1 ||
  633. statement.specifiers[0].type !== "ImportSpecifier" ||
  634. statement.specifiers[0].imported.type !== "Identifier" ||
  635. statement.specifiers[0].imported.name !== specifierName
  636. )
  637. return;
  638. // clear for 'import { createRequire as x } from "module"'
  639. // if any other specifier was used import module
  640. const clearDep = new ConstDependency(
  641. parser.isAsiPosition(/** @type {Range} */ (statement.range)[0])
  642. ? ";"
  643. : "",
  644. /** @type {Range} */ (statement.range)
  645. );
  646. clearDep.loc = /** @type {DependencyLocation} */ (statement.loc);
  647. parser.state.module.addPresentationalDependency(clearDep);
  648. parser.unsetAsiPosition(/** @type {Range} */ (statement.range)[1]);
  649. return true;
  650. }
  651. );
  652. parser.hooks.importSpecifier.tap(
  653. {
  654. name: "CommonJsImportsParserPlugin",
  655. stage: -10
  656. },
  657. (statement, source, id, name) => {
  658. if (!moduleName.includes(source) || id !== specifierName) return;
  659. parser.tagVariable(name, createRequireSpecifierTag);
  660. return true;
  661. }
  662. );
  663. parser.hooks.preDeclarator.tap(
  664. "CommonJsImportsParserPlugin",
  665. declarator => {
  666. if (
  667. declarator.id.type !== "Identifier" ||
  668. !declarator.init ||
  669. declarator.init.type !== "CallExpression" ||
  670. declarator.init.callee.type !== "Identifier"
  671. )
  672. return;
  673. const variableInfo =
  674. /** @type {TODO} */
  675. (parser.getVariableInfo(declarator.init.callee.name));
  676. if (
  677. variableInfo &&
  678. variableInfo.tagInfo &&
  679. variableInfo.tagInfo.tag === createRequireSpecifierTag
  680. ) {
  681. const context = parseCreateRequireArguments(declarator.init);
  682. if (context === undefined) return;
  683. parser.tagVariable(declarator.id.name, createdRequireIdentifierTag, {
  684. name: declarator.id.name,
  685. context
  686. });
  687. return true;
  688. }
  689. }
  690. );
  691. parser.hooks.memberChainOfCallMemberChain
  692. .for(createRequireSpecifierTag)
  693. .tap(
  694. "CommonJsImportsParserPlugin",
  695. (expr, calleeMembers, callExpr, members) => {
  696. if (
  697. calleeMembers.length !== 0 ||
  698. members.length !== 1 ||
  699. members[0] !== "cache"
  700. )
  701. return;
  702. // createRequire().cache
  703. const context = parseCreateRequireArguments(callExpr);
  704. if (context === undefined) return;
  705. return requireCache(expr);
  706. }
  707. );
  708. parser.hooks.callMemberChainOfCallMemberChain
  709. .for(createRequireSpecifierTag)
  710. .tap(
  711. "CommonJsImportsParserPlugin",
  712. (expr, calleeMembers, innerCallExpression, members) => {
  713. if (
  714. calleeMembers.length !== 0 ||
  715. members.length !== 1 ||
  716. members[0] !== "resolve"
  717. )
  718. return;
  719. // createRequire().resolve()
  720. return processResolve(expr, false);
  721. }
  722. );
  723. parser.hooks.expressionMemberChain
  724. .for(createdRequireIdentifierTag)
  725. .tap("CommonJsImportsParserPlugin", (expr, members) => {
  726. // require.cache
  727. if (members.length === 1 && members[0] === "cache") {
  728. return requireCache(expr);
  729. }
  730. });
  731. parser.hooks.callMemberChain
  732. .for(createdRequireIdentifierTag)
  733. .tap("CommonJsImportsParserPlugin", (expr, members) => {
  734. // require.resolve()
  735. if (members.length === 1 && members[0] === "resolve") {
  736. return processResolve(expr, false);
  737. }
  738. });
  739. parser.hooks.call
  740. .for(createRequireSpecifierTag)
  741. .tap("CommonJsImportsParserPlugin", expr => {
  742. const clearDep = new ConstDependency(
  743. "/* createRequire() */ undefined",
  744. /** @type {Range} */ (expr.range)
  745. );
  746. clearDep.loc = /** @type {DependencyLocation} */ (expr.loc);
  747. parser.state.module.addPresentationalDependency(clearDep);
  748. return true;
  749. });
  750. //#endregion
  751. }
  752. }
  753. module.exports = CommonJsImportsParserPlugin;