index.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. import {createRequire as __cjsCompatRequire} from 'module';
  2. const require = __cjsCompatRequire(import.meta.url);
  3. import {
  4. FatalLinkerError,
  5. FileLinker,
  6. LinkerEnvironment,
  7. assert,
  8. isFatalLinkerError
  9. } from "../../chunk-RNO4EXM5.js";
  10. import "../../chunk-KOIBHR3X.js";
  11. import {
  12. ConsoleLogger,
  13. LogLevel
  14. } from "../../chunk-GBKXY6BH.js";
  15. import "../../chunk-5UDS2TPQ.js";
  16. import {
  17. NodeJSFileSystem
  18. } from "../../chunk-STORTTKY.js";
  19. import "../../chunk-KPQ72R34.js";
  20. // bazel-out/darwin_arm64-fastbuild/bin/packages/compiler-cli/linker/babel/src/es2015_linker_plugin.mjs
  21. import { types as t4 } from "@babel/core";
  22. // bazel-out/darwin_arm64-fastbuild/bin/packages/compiler-cli/linker/babel/src/ast/babel_ast_factory.mjs
  23. import { types as t } from "@babel/core";
  24. var BabelAstFactory = class {
  25. sourceUrl;
  26. constructor(sourceUrl) {
  27. this.sourceUrl = sourceUrl;
  28. }
  29. attachComments(statement, leadingComments) {
  30. for (let i = leadingComments.length - 1; i >= 0; i--) {
  31. const comment = leadingComments[i];
  32. t.addComment(statement, "leading", comment.toString(), !comment.multiline);
  33. }
  34. }
  35. createArrayLiteral = t.arrayExpression;
  36. createAssignment(target, value) {
  37. assert(target, isLExpression, "must be a left hand side expression");
  38. return t.assignmentExpression("=", target, value);
  39. }
  40. createBinaryExpression(leftOperand, operator, rightOperand) {
  41. switch (operator) {
  42. case "&&":
  43. case "||":
  44. case "??":
  45. return t.logicalExpression(operator, leftOperand, rightOperand);
  46. default:
  47. return t.binaryExpression(operator, leftOperand, rightOperand);
  48. }
  49. }
  50. createBlock = t.blockStatement;
  51. createCallExpression(callee, args, pure) {
  52. const call = t.callExpression(callee, args);
  53. if (pure) {
  54. t.addComment(call, "leading", " @__PURE__ ", false);
  55. }
  56. return call;
  57. }
  58. createConditional = t.conditionalExpression;
  59. createElementAccess(expression, element) {
  60. return t.memberExpression(expression, element, true);
  61. }
  62. createExpressionStatement = t.expressionStatement;
  63. createFunctionDeclaration(functionName, parameters, body) {
  64. assert(body, t.isBlockStatement, "a block");
  65. return t.functionDeclaration(t.identifier(functionName), parameters.map((param) => t.identifier(param)), body);
  66. }
  67. createArrowFunctionExpression(parameters, body) {
  68. if (t.isStatement(body)) {
  69. assert(body, t.isBlockStatement, "a block");
  70. }
  71. return t.arrowFunctionExpression(parameters.map((param) => t.identifier(param)), body);
  72. }
  73. createFunctionExpression(functionName, parameters, body) {
  74. assert(body, t.isBlockStatement, "a block");
  75. const name = functionName !== null ? t.identifier(functionName) : null;
  76. return t.functionExpression(name, parameters.map((param) => t.identifier(param)), body);
  77. }
  78. createIdentifier = t.identifier;
  79. createIfStatement = t.ifStatement;
  80. createDynamicImport(url) {
  81. return this.createCallExpression(t.import(), [typeof url === "string" ? t.stringLiteral(url) : url], false);
  82. }
  83. createLiteral(value) {
  84. if (typeof value === "string") {
  85. return t.stringLiteral(value);
  86. } else if (typeof value === "number") {
  87. return t.numericLiteral(value);
  88. } else if (typeof value === "boolean") {
  89. return t.booleanLiteral(value);
  90. } else if (value === void 0) {
  91. return t.identifier("undefined");
  92. } else if (value === null) {
  93. return t.nullLiteral();
  94. } else {
  95. throw new Error(`Invalid literal: ${value} (${typeof value})`);
  96. }
  97. }
  98. createNewExpression = t.newExpression;
  99. createObjectLiteral(properties) {
  100. return t.objectExpression(properties.map((prop) => {
  101. const key = prop.quoted ? t.stringLiteral(prop.propertyName) : t.identifier(prop.propertyName);
  102. return t.objectProperty(key, prop.value);
  103. }));
  104. }
  105. createParenthesizedExpression = t.parenthesizedExpression;
  106. createPropertyAccess(expression, propertyName) {
  107. return t.memberExpression(expression, t.identifier(propertyName), false);
  108. }
  109. createReturnStatement = t.returnStatement;
  110. createTaggedTemplate(tag, template) {
  111. return t.taggedTemplateExpression(tag, this.createTemplateLiteral(template));
  112. }
  113. createTemplateLiteral(template) {
  114. const elements = template.elements.map((element, i) => this.setSourceMapRange(t.templateElement(element, i === template.elements.length - 1), element.range));
  115. return t.templateLiteral(elements, template.expressions);
  116. }
  117. createThrowStatement = t.throwStatement;
  118. createTypeOfExpression(expression) {
  119. return t.unaryExpression("typeof", expression);
  120. }
  121. createUnaryExpression = t.unaryExpression;
  122. createVariableDeclaration(variableName, initializer, type) {
  123. return t.variableDeclaration(type, [
  124. t.variableDeclarator(t.identifier(variableName), initializer)
  125. ]);
  126. }
  127. setSourceMapRange(node, sourceMapRange) {
  128. if (sourceMapRange === null) {
  129. return node;
  130. }
  131. node.loc = {
  132. filename: sourceMapRange.url !== this.sourceUrl ? sourceMapRange.url : void 0,
  133. start: {
  134. line: sourceMapRange.start.line + 1,
  135. column: sourceMapRange.start.column
  136. },
  137. end: {
  138. line: sourceMapRange.end.line + 1,
  139. column: sourceMapRange.end.column
  140. }
  141. };
  142. node.start = sourceMapRange.start.offset;
  143. node.end = sourceMapRange.end.offset;
  144. return node;
  145. }
  146. };
  147. function isLExpression(expr) {
  148. return t.isLVal(expr);
  149. }
  150. // bazel-out/darwin_arm64-fastbuild/bin/packages/compiler-cli/linker/babel/src/ast/babel_ast_host.mjs
  151. import { types as t2 } from "@babel/core";
  152. var BabelAstHost = class {
  153. getSymbolName(node) {
  154. if (t2.isIdentifier(node)) {
  155. return node.name;
  156. } else if (t2.isMemberExpression(node) && t2.isIdentifier(node.property)) {
  157. return node.property.name;
  158. } else {
  159. return null;
  160. }
  161. }
  162. isStringLiteral = t2.isStringLiteral;
  163. parseStringLiteral(str) {
  164. assert(str, t2.isStringLiteral, "a string literal");
  165. return str.value;
  166. }
  167. isNumericLiteral = t2.isNumericLiteral;
  168. parseNumericLiteral(num) {
  169. assert(num, t2.isNumericLiteral, "a numeric literal");
  170. return num.value;
  171. }
  172. isBooleanLiteral(bool) {
  173. return t2.isBooleanLiteral(bool) || isMinifiedBooleanLiteral(bool);
  174. }
  175. parseBooleanLiteral(bool) {
  176. if (t2.isBooleanLiteral(bool)) {
  177. return bool.value;
  178. } else if (isMinifiedBooleanLiteral(bool)) {
  179. return !bool.argument.value;
  180. } else {
  181. throw new FatalLinkerError(bool, "Unsupported syntax, expected a boolean literal.");
  182. }
  183. }
  184. isNull(node) {
  185. return t2.isNullLiteral(node);
  186. }
  187. isArrayLiteral = t2.isArrayExpression;
  188. parseArrayLiteral(array) {
  189. assert(array, t2.isArrayExpression, "an array literal");
  190. return array.elements.map((element) => {
  191. assert(element, isNotEmptyElement, "element in array not to be empty");
  192. assert(element, isNotSpreadElement, "element in array not to use spread syntax");
  193. return element;
  194. });
  195. }
  196. isObjectLiteral = t2.isObjectExpression;
  197. parseObjectLiteral(obj) {
  198. assert(obj, t2.isObjectExpression, "an object literal");
  199. const result = /* @__PURE__ */ new Map();
  200. for (const property of obj.properties) {
  201. assert(property, t2.isObjectProperty, "a property assignment");
  202. assert(property.value, t2.isExpression, "an expression");
  203. assert(property.key, isObjectExpressionPropertyName, "a property name");
  204. const key = t2.isIdentifier(property.key) ? property.key.name : property.key.value;
  205. result.set(`${key}`, property.value);
  206. }
  207. return result;
  208. }
  209. isFunctionExpression(node) {
  210. return t2.isFunction(node) || t2.isArrowFunctionExpression(node);
  211. }
  212. parseReturnValue(fn) {
  213. assert(fn, this.isFunctionExpression, "a function");
  214. if (!t2.isBlockStatement(fn.body)) {
  215. return fn.body;
  216. }
  217. if (fn.body.body.length !== 1) {
  218. throw new FatalLinkerError(fn.body, "Unsupported syntax, expected a function body with a single return statement.");
  219. }
  220. const stmt = fn.body.body[0];
  221. assert(stmt, t2.isReturnStatement, "a function body with a single return statement");
  222. if (stmt.argument === null || stmt.argument === void 0) {
  223. throw new FatalLinkerError(stmt, "Unsupported syntax, expected function to return a value.");
  224. }
  225. return stmt.argument;
  226. }
  227. parseParameters(fn) {
  228. assert(fn, this.isFunctionExpression, "a function");
  229. return fn.params.map((param) => {
  230. assert(param, t2.isIdentifier, "an identifier");
  231. return param;
  232. });
  233. }
  234. isCallExpression = t2.isCallExpression;
  235. parseCallee(call) {
  236. assert(call, t2.isCallExpression, "a call expression");
  237. assert(call.callee, t2.isExpression, "an expression");
  238. return call.callee;
  239. }
  240. parseArguments(call) {
  241. assert(call, t2.isCallExpression, "a call expression");
  242. return call.arguments.map((arg) => {
  243. assert(arg, isNotSpreadArgument, "argument not to use spread syntax");
  244. assert(arg, t2.isExpression, "argument to be an expression");
  245. return arg;
  246. });
  247. }
  248. getRange(node) {
  249. if (node.loc == null || node.start == null || node.end == null) {
  250. throw new FatalLinkerError(node, "Unable to read range for node - it is missing location information.");
  251. }
  252. return {
  253. startLine: node.loc.start.line - 1,
  254. startCol: node.loc.start.column,
  255. startPos: node.start,
  256. endPos: node.end
  257. };
  258. }
  259. };
  260. function isNotEmptyElement(e) {
  261. return e !== null;
  262. }
  263. function isNotSpreadElement(e) {
  264. return !t2.isSpreadElement(e);
  265. }
  266. function isObjectExpressionPropertyName(n) {
  267. return t2.isIdentifier(n) || t2.isStringLiteral(n) || t2.isNumericLiteral(n);
  268. }
  269. function isNotSpreadArgument(arg) {
  270. return !t2.isSpreadElement(arg);
  271. }
  272. function isMinifiedBooleanLiteral(node) {
  273. return t2.isUnaryExpression(node) && node.prefix && node.operator === "!" && t2.isNumericLiteral(node.argument) && (node.argument.value === 0 || node.argument.value === 1);
  274. }
  275. // bazel-out/darwin_arm64-fastbuild/bin/packages/compiler-cli/linker/babel/src/babel_declaration_scope.mjs
  276. import { types as t3 } from "@babel/core";
  277. var BabelDeclarationScope = class {
  278. declarationScope;
  279. constructor(declarationScope) {
  280. this.declarationScope = declarationScope;
  281. }
  282. getConstantScopeRef(expression) {
  283. let bindingExpression = expression;
  284. while (t3.isMemberExpression(bindingExpression)) {
  285. bindingExpression = bindingExpression.object;
  286. }
  287. if (!t3.isIdentifier(bindingExpression)) {
  288. return null;
  289. }
  290. const binding = this.declarationScope.getBinding(bindingExpression.name);
  291. if (binding === void 0) {
  292. return null;
  293. }
  294. const path = binding.scope.path;
  295. if (!path.isFunctionDeclaration() && !path.isFunctionExpression() && !(path.isProgram() && path.node.sourceType === "module")) {
  296. return null;
  297. }
  298. return path;
  299. }
  300. };
  301. // bazel-out/darwin_arm64-fastbuild/bin/packages/compiler-cli/linker/babel/src/es2015_linker_plugin.mjs
  302. function createEs2015LinkerPlugin({ fileSystem, logger, ...options }) {
  303. let fileLinker = null;
  304. return {
  305. visitor: {
  306. Program: {
  307. enter(_, state) {
  308. var _a, _b;
  309. assertNull(fileLinker);
  310. const file = state.file;
  311. const filename = (_a = file.opts.filename) != null ? _a : file.opts.filenameRelative;
  312. if (!filename) {
  313. throw new Error("No filename (nor filenameRelative) provided by Babel. This is required for the linking of partially compiled directives and components.");
  314. }
  315. const sourceUrl = fileSystem.resolve((_b = file.opts.cwd) != null ? _b : ".", filename);
  316. const linkerEnvironment = LinkerEnvironment.create(fileSystem, logger, new BabelAstHost(), new BabelAstFactory(sourceUrl), options);
  317. fileLinker = new FileLinker(linkerEnvironment, sourceUrl, file.code);
  318. },
  319. exit() {
  320. assertNotNull(fileLinker);
  321. for (const { constantScope, statements } of fileLinker.getConstantStatements()) {
  322. insertStatements(constantScope, statements);
  323. }
  324. fileLinker = null;
  325. }
  326. },
  327. CallExpression(call, state) {
  328. if (fileLinker === null) {
  329. return;
  330. }
  331. try {
  332. const calleeName = getCalleeName(call);
  333. if (calleeName === null) {
  334. return;
  335. }
  336. const args = call.node.arguments;
  337. if (!fileLinker.isPartialDeclaration(calleeName) || !isExpressionArray(args)) {
  338. return;
  339. }
  340. const declarationScope = new BabelDeclarationScope(call.scope);
  341. const replacement = fileLinker.linkPartialDeclaration(calleeName, args, declarationScope);
  342. call.replaceWith(replacement);
  343. } catch (e) {
  344. const node = isFatalLinkerError(e) ? e.node : call.node;
  345. throw buildCodeFrameError(state.file, e.message, node);
  346. }
  347. }
  348. }
  349. };
  350. }
  351. function insertStatements(path, statements) {
  352. if (path.isProgram()) {
  353. insertIntoProgram(path, statements);
  354. } else {
  355. insertIntoFunction(path, statements);
  356. }
  357. }
  358. function insertIntoFunction(fn, statements) {
  359. const body = fn.get("body");
  360. body.unshiftContainer("body", statements);
  361. }
  362. function insertIntoProgram(program, statements) {
  363. const body = program.get("body");
  364. const insertBeforeIndex = body.findIndex((statement) => !statement.isImportDeclaration());
  365. if (insertBeforeIndex === -1) {
  366. program.unshiftContainer("body", statements);
  367. } else {
  368. body[insertBeforeIndex].insertBefore(statements);
  369. }
  370. }
  371. function getCalleeName(call) {
  372. const callee = call.node.callee;
  373. if (t4.isIdentifier(callee)) {
  374. return callee.name;
  375. } else if (t4.isMemberExpression(callee) && t4.isIdentifier(callee.property)) {
  376. return callee.property.name;
  377. } else if (t4.isMemberExpression(callee) && t4.isStringLiteral(callee.property)) {
  378. return callee.property.value;
  379. } else {
  380. return null;
  381. }
  382. }
  383. function isExpressionArray(nodes) {
  384. return nodes.every((node) => t4.isExpression(node));
  385. }
  386. function assertNull(obj) {
  387. if (obj !== null) {
  388. throw new Error("BUG - expected `obj` to be null");
  389. }
  390. }
  391. function assertNotNull(obj) {
  392. if (obj === null) {
  393. throw new Error("BUG - expected `obj` not to be null");
  394. }
  395. }
  396. function buildCodeFrameError(file, message, node) {
  397. const filename = file.opts.filename || "(unknown file)";
  398. const error = file.hub.buildError(node, message);
  399. return `${filename}: ${error.message}`;
  400. }
  401. // bazel-out/darwin_arm64-fastbuild/bin/packages/compiler-cli/linker/babel/src/babel_plugin.mjs
  402. function defaultLinkerPlugin(api, options) {
  403. api.assertVersion(7);
  404. return createEs2015LinkerPlugin({
  405. ...options,
  406. fileSystem: new NodeJSFileSystem(),
  407. logger: new ConsoleLogger(LogLevel.info)
  408. });
  409. }
  410. // bazel-out/darwin_arm64-fastbuild/bin/packages/compiler-cli/linker/babel/index.mjs
  411. var babel_default = defaultLinkerPlugin;
  412. export {
  413. createEs2015LinkerPlugin,
  414. babel_default as default
  415. };
  416. /**
  417. * @license
  418. * Copyright Google LLC All Rights Reserved.
  419. *
  420. * Use of this source code is governed by an MIT-style license that can be
  421. * found in the LICENSE file at https://angular.dev/license
  422. */
  423. //# sourceMappingURL=index.js.map