CommonJsExportsParserPlugin.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const RuntimeGlobals = require("../RuntimeGlobals");
  7. const formatLocation = require("../formatLocation");
  8. const { evaluateToString } = require("../javascript/JavascriptParserHelpers");
  9. const propertyAccess = require("../util/propertyAccess");
  10. const CommonJsExportRequireDependency = require("./CommonJsExportRequireDependency");
  11. const CommonJsExportsDependency = require("./CommonJsExportsDependency");
  12. const CommonJsSelfReferenceDependency = require("./CommonJsSelfReferenceDependency");
  13. const DynamicExports = require("./DynamicExports");
  14. const HarmonyExports = require("./HarmonyExports");
  15. const ModuleDecoratorDependency = require("./ModuleDecoratorDependency");
  16. /** @typedef {import("estree").Expression} ExpressionNode */
  17. /** @typedef {import("../NormalModule")} NormalModule */
  18. /** @typedef {import("../javascript/BasicEvaluatedExpression")} BasicEvaluatedExpression */
  19. /** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
  20. const getValueOfPropertyDescription = expr => {
  21. if (expr.type !== "ObjectExpression") return;
  22. for (const property of expr.properties) {
  23. if (property.computed) continue;
  24. const key = property.key;
  25. if (key.type !== "Identifier" || key.name !== "value") continue;
  26. return property.value;
  27. }
  28. };
  29. const isTruthyLiteral = expr => {
  30. switch (expr.type) {
  31. case "Literal":
  32. return !!expr.value;
  33. case "UnaryExpression":
  34. if (expr.operator === "!") return isFalsyLiteral(expr.argument);
  35. }
  36. return false;
  37. };
  38. const isFalsyLiteral = expr => {
  39. switch (expr.type) {
  40. case "Literal":
  41. return !expr.value;
  42. case "UnaryExpression":
  43. if (expr.operator === "!") return isTruthyLiteral(expr.argument);
  44. }
  45. return false;
  46. };
  47. /**
  48. * @param {JavascriptParser} parser the parser
  49. * @param {ExpressionNode} expr expression
  50. * @returns {{ argument: BasicEvaluatedExpression, ids: string[] } | undefined} parsed call
  51. */
  52. const parseRequireCall = (parser, expr) => {
  53. const ids = [];
  54. while (expr.type === "MemberExpression") {
  55. if (expr.object.type === "Super") return;
  56. if (!expr.property) return;
  57. const prop = expr.property;
  58. if (expr.computed) {
  59. if (prop.type !== "Literal") return;
  60. ids.push(`${prop.value}`);
  61. } else {
  62. if (prop.type !== "Identifier") return;
  63. ids.push(prop.name);
  64. }
  65. expr = expr.object;
  66. }
  67. if (expr.type !== "CallExpression" || expr.arguments.length !== 1) return;
  68. const callee = expr.callee;
  69. if (
  70. callee.type !== "Identifier" ||
  71. parser.getVariableInfo(callee.name) !== "require"
  72. ) {
  73. return;
  74. }
  75. const arg = expr.arguments[0];
  76. if (arg.type === "SpreadElement") return;
  77. const argValue = parser.evaluateExpression(arg);
  78. return { argument: argValue, ids: ids.reverse() };
  79. };
  80. class CommonJsExportsParserPlugin {
  81. constructor(moduleGraph) {
  82. this.moduleGraph = moduleGraph;
  83. }
  84. /**
  85. * @param {JavascriptParser} parser the parser
  86. */
  87. apply(parser) {
  88. const enableStructuredExports = () => {
  89. DynamicExports.enable(parser.state);
  90. };
  91. const checkNamespace = (topLevel, members, valueExpr) => {
  92. if (!DynamicExports.isEnabled(parser.state)) return;
  93. if (members.length > 0 && members[0] === "__esModule") {
  94. if (valueExpr && isTruthyLiteral(valueExpr) && topLevel) {
  95. DynamicExports.setFlagged(parser.state);
  96. } else {
  97. DynamicExports.setDynamic(parser.state);
  98. }
  99. }
  100. };
  101. const bailout = reason => {
  102. DynamicExports.bailout(parser.state);
  103. if (reason) bailoutHint(reason);
  104. };
  105. const bailoutHint = reason => {
  106. this.moduleGraph
  107. .getOptimizationBailout(parser.state.module)
  108. .push(`CommonJS bailout: ${reason}`);
  109. };
  110. // metadata //
  111. parser.hooks.evaluateTypeof
  112. .for("module")
  113. .tap("CommonJsExportsParserPlugin", evaluateToString("object"));
  114. parser.hooks.evaluateTypeof
  115. .for("exports")
  116. .tap("CommonJsPlugin", evaluateToString("object"));
  117. // exporting //
  118. const handleAssignExport = (expr, base, members) => {
  119. if (HarmonyExports.isEnabled(parser.state)) return;
  120. // Handle reexporting
  121. const requireCall = parseRequireCall(parser, expr.right);
  122. if (
  123. requireCall &&
  124. requireCall.argument.isString() &&
  125. (members.length === 0 || members[0] !== "__esModule")
  126. ) {
  127. enableStructuredExports();
  128. // It's possible to reexport __esModule, so we must convert to a dynamic module
  129. if (members.length === 0) DynamicExports.setDynamic(parser.state);
  130. const dep = new CommonJsExportRequireDependency(
  131. expr.range,
  132. null,
  133. base,
  134. members,
  135. requireCall.argument.string,
  136. requireCall.ids,
  137. !parser.isStatementLevelExpression(expr)
  138. );
  139. dep.loc = expr.loc;
  140. dep.optional = !!parser.scope.inTry;
  141. parser.state.module.addDependency(dep);
  142. return true;
  143. }
  144. if (members.length === 0) return;
  145. enableStructuredExports();
  146. const remainingMembers = members;
  147. checkNamespace(
  148. parser.statementPath.length === 1 &&
  149. parser.isStatementLevelExpression(expr),
  150. remainingMembers,
  151. expr.right
  152. );
  153. const dep = new CommonJsExportsDependency(
  154. expr.left.range,
  155. null,
  156. base,
  157. remainingMembers
  158. );
  159. dep.loc = expr.loc;
  160. parser.state.module.addDependency(dep);
  161. parser.walkExpression(expr.right);
  162. return true;
  163. };
  164. parser.hooks.assignMemberChain
  165. .for("exports")
  166. .tap("CommonJsExportsParserPlugin", (expr, members) => {
  167. return handleAssignExport(expr, "exports", members);
  168. });
  169. parser.hooks.assignMemberChain
  170. .for("this")
  171. .tap("CommonJsExportsParserPlugin", (expr, members) => {
  172. if (!parser.scope.topLevelScope) return;
  173. return handleAssignExport(expr, "this", members);
  174. });
  175. parser.hooks.assignMemberChain
  176. .for("module")
  177. .tap("CommonJsExportsParserPlugin", (expr, members) => {
  178. if (members[0] !== "exports") return;
  179. return handleAssignExport(expr, "module.exports", members.slice(1));
  180. });
  181. parser.hooks.call
  182. .for("Object.defineProperty")
  183. .tap("CommonJsExportsParserPlugin", expression => {
  184. const expr = /** @type {import("estree").CallExpression} */ (
  185. expression
  186. );
  187. if (!parser.isStatementLevelExpression(expr)) return;
  188. if (expr.arguments.length !== 3) return;
  189. if (expr.arguments[0].type === "SpreadElement") return;
  190. if (expr.arguments[1].type === "SpreadElement") return;
  191. if (expr.arguments[2].type === "SpreadElement") return;
  192. const exportsArg = parser.evaluateExpression(expr.arguments[0]);
  193. if (!exportsArg.isIdentifier()) return;
  194. if (
  195. exportsArg.identifier !== "exports" &&
  196. exportsArg.identifier !== "module.exports" &&
  197. (exportsArg.identifier !== "this" || !parser.scope.topLevelScope)
  198. ) {
  199. return;
  200. }
  201. const propertyArg = parser.evaluateExpression(expr.arguments[1]);
  202. const property = propertyArg.asString();
  203. if (typeof property !== "string") return;
  204. enableStructuredExports();
  205. const descArg = expr.arguments[2];
  206. checkNamespace(
  207. parser.statementPath.length === 1,
  208. [property],
  209. getValueOfPropertyDescription(descArg)
  210. );
  211. const dep = new CommonJsExportsDependency(
  212. expr.range,
  213. expr.arguments[2].range,
  214. `Object.defineProperty(${exportsArg.identifier})`,
  215. [property]
  216. );
  217. dep.loc = expr.loc;
  218. parser.state.module.addDependency(dep);
  219. parser.walkExpression(expr.arguments[2]);
  220. return true;
  221. });
  222. // Self reference //
  223. const handleAccessExport = (expr, base, members, call = undefined) => {
  224. if (HarmonyExports.isEnabled(parser.state)) return;
  225. if (members.length === 0) {
  226. bailout(`${base} is used directly at ${formatLocation(expr.loc)}`);
  227. }
  228. if (call && members.length === 1) {
  229. bailoutHint(
  230. `${base}${propertyAccess(
  231. members
  232. )}(...) prevents optimization as ${base} is passed as call context at ${formatLocation(
  233. expr.loc
  234. )}`
  235. );
  236. }
  237. const dep = new CommonJsSelfReferenceDependency(
  238. expr.range,
  239. base,
  240. members,
  241. !!call
  242. );
  243. dep.loc = expr.loc;
  244. parser.state.module.addDependency(dep);
  245. if (call) {
  246. parser.walkExpressions(call.arguments);
  247. }
  248. return true;
  249. };
  250. parser.hooks.callMemberChain
  251. .for("exports")
  252. .tap("CommonJsExportsParserPlugin", (expr, members) => {
  253. return handleAccessExport(expr.callee, "exports", members, expr);
  254. });
  255. parser.hooks.expressionMemberChain
  256. .for("exports")
  257. .tap("CommonJsExportsParserPlugin", (expr, members) => {
  258. return handleAccessExport(expr, "exports", members);
  259. });
  260. parser.hooks.expression
  261. .for("exports")
  262. .tap("CommonJsExportsParserPlugin", expr => {
  263. return handleAccessExport(expr, "exports", []);
  264. });
  265. parser.hooks.callMemberChain
  266. .for("module")
  267. .tap("CommonJsExportsParserPlugin", (expr, members) => {
  268. if (members[0] !== "exports") return;
  269. return handleAccessExport(
  270. expr.callee,
  271. "module.exports",
  272. members.slice(1),
  273. expr
  274. );
  275. });
  276. parser.hooks.expressionMemberChain
  277. .for("module")
  278. .tap("CommonJsExportsParserPlugin", (expr, members) => {
  279. if (members[0] !== "exports") return;
  280. return handleAccessExport(expr, "module.exports", members.slice(1));
  281. });
  282. parser.hooks.expression
  283. .for("module.exports")
  284. .tap("CommonJsExportsParserPlugin", expr => {
  285. return handleAccessExport(expr, "module.exports", []);
  286. });
  287. parser.hooks.callMemberChain
  288. .for("this")
  289. .tap("CommonJsExportsParserPlugin", (expr, members) => {
  290. if (!parser.scope.topLevelScope) return;
  291. return handleAccessExport(expr.callee, "this", members, expr);
  292. });
  293. parser.hooks.expressionMemberChain
  294. .for("this")
  295. .tap("CommonJsExportsParserPlugin", (expr, members) => {
  296. if (!parser.scope.topLevelScope) return;
  297. return handleAccessExport(expr, "this", members);
  298. });
  299. parser.hooks.expression
  300. .for("this")
  301. .tap("CommonJsExportsParserPlugin", expr => {
  302. if (!parser.scope.topLevelScope) return;
  303. return handleAccessExport(expr, "this", []);
  304. });
  305. // Bailouts //
  306. parser.hooks.expression.for("module").tap("CommonJsPlugin", expr => {
  307. bailout();
  308. const isHarmony = HarmonyExports.isEnabled(parser.state);
  309. const dep = new ModuleDecoratorDependency(
  310. isHarmony
  311. ? RuntimeGlobals.harmonyModuleDecorator
  312. : RuntimeGlobals.nodeModuleDecorator,
  313. !isHarmony
  314. );
  315. dep.loc = expr.loc;
  316. parser.state.module.addDependency(dep);
  317. return true;
  318. });
  319. }
  320. }
  321. module.exports = CommonJsExportsParserPlugin;