@sentry/bundler-plugins 11.18.0 → 11.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. package/build/cjs/core/index.js +1 -2
  2. package/build/cjs/core/index.js.map +1 -1
  3. package/build/cjs/core/options-mapping.js.map +1 -1
  4. package/build/cjs/core/sentry/telemetry.js +1 -1
  5. package/build/cjs/core/sentry/telemetry.js.map +1 -1
  6. package/build/cjs/core/sourcemap-settings.js +26 -22
  7. package/build/cjs/core/sourcemap-settings.js.map +1 -1
  8. package/build/cjs/core/version.js +1 -1
  9. package/build/cjs/core/version.js.map +1 -1
  10. package/build/cjs/rewriter/arrays.js +25 -0
  11. package/build/cjs/rewriter/arrays.js.map +1 -0
  12. package/build/cjs/rewriter/ast.js +102 -0
  13. package/build/cjs/rewriter/ast.js.map +1 -0
  14. package/build/cjs/rewriter/class-extends.js +67 -0
  15. package/build/cjs/rewriter/class-extends.js.map +1 -0
  16. package/build/cjs/rewriter/exports.js +241 -0
  17. package/build/cjs/rewriter/exports.js.map +1 -0
  18. package/build/cjs/rewriter/imports.js +22 -0
  19. package/build/cjs/rewriter/imports.js.map +1 -0
  20. package/build/cjs/rewriter/index.js +45 -0
  21. package/build/cjs/rewriter/module-shape.js +120 -0
  22. package/build/cjs/rewriter/module-shape.js.map +1 -0
  23. package/build/cjs/rewriter/parse.js +64 -0
  24. package/build/cjs/rewriter/parse.js.map +1 -0
  25. package/build/cjs/rewriter/report.js +17 -0
  26. package/build/cjs/rewriter/report.js.map +1 -0
  27. package/build/cjs/rewriter/rewriter.js +68 -0
  28. package/build/cjs/rewriter/rewriter.js.map +1 -0
  29. package/build/cjs/vite/source-map-hooks.js +1 -1
  30. package/build/cjs/vite/source-map-hooks.js.map +1 -1
  31. package/build/esm/core/index.js +2 -2
  32. package/build/esm/core/index.js.map +1 -1
  33. package/build/esm/core/options-mapping.js.map +1 -1
  34. package/build/esm/core/sentry/telemetry.js +1 -1
  35. package/build/esm/core/sentry/telemetry.js.map +1 -1
  36. package/build/esm/core/sourcemap-settings.js +26 -21
  37. package/build/esm/core/sourcemap-settings.js.map +1 -1
  38. package/build/esm/core/version.js +1 -1
  39. package/build/esm/core/version.js.map +1 -1
  40. package/build/esm/package.json +1 -1
  41. package/build/esm/rewriter/arrays.js +25 -0
  42. package/build/esm/rewriter/arrays.js.map +1 -0
  43. package/build/esm/rewriter/ast.js +95 -0
  44. package/build/esm/rewriter/ast.js.map +1 -0
  45. package/build/esm/rewriter/class-extends.js +67 -0
  46. package/build/esm/rewriter/class-extends.js.map +1 -0
  47. package/build/esm/rewriter/exports.js +231 -0
  48. package/build/esm/rewriter/exports.js.map +1 -0
  49. package/build/esm/rewriter/imports.js +21 -0
  50. package/build/esm/rewriter/imports.js.map +1 -0
  51. package/build/esm/rewriter/index.js +11 -0
  52. package/build/esm/rewriter/module-shape.js +118 -0
  53. package/build/esm/rewriter/module-shape.js.map +1 -0
  54. package/build/esm/rewriter/parse.js +60 -0
  55. package/build/esm/rewriter/parse.js.map +1 -0
  56. package/build/esm/rewriter/report.js +16 -0
  57. package/build/esm/rewriter/report.js.map +1 -0
  58. package/build/esm/rewriter/rewriter.js +65 -0
  59. package/build/esm/rewriter/rewriter.js.map +1 -0
  60. package/build/esm/vite/source-map-hooks.js +2 -2
  61. package/build/esm/vite/source-map-hooks.js.map +1 -1
  62. package/build/types/core/index.d.ts +1 -1
  63. package/build/types/core/index.d.ts.map +1 -1
  64. package/build/types/core/options-mapping.d.ts +2 -2
  65. package/build/types/core/options-mapping.d.ts.map +1 -1
  66. package/build/types/core/sentry/telemetry.d.ts.map +1 -1
  67. package/build/types/core/sourcemap-settings.d.ts +8 -6
  68. package/build/types/core/sourcemap-settings.d.ts.map +1 -1
  69. package/build/types/core/types.d.ts +14 -11
  70. package/build/types/core/types.d.ts.map +1 -1
  71. package/build/types/core/version.d.ts +1 -1
  72. package/build/types/rewriter/arrays.d.ts +12 -0
  73. package/build/types/rewriter/arrays.d.ts.map +1 -0
  74. package/build/types/rewriter/ast.d.ts +120 -0
  75. package/build/types/rewriter/ast.d.ts.map +1 -0
  76. package/build/types/rewriter/class-extends.d.ts +16 -0
  77. package/build/types/rewriter/class-extends.d.ts.map +1 -0
  78. package/build/types/rewriter/exports.d.ts +88 -0
  79. package/build/types/rewriter/exports.d.ts.map +1 -0
  80. package/build/types/rewriter/imports.d.ts +7 -0
  81. package/build/types/rewriter/imports.d.ts.map +1 -0
  82. package/build/types/rewriter/index.d.ts +10 -0
  83. package/build/types/rewriter/index.d.ts.map +1 -0
  84. package/build/types/rewriter/module-shape.d.ts +47 -0
  85. package/build/types/rewriter/module-shape.d.ts.map +1 -0
  86. package/build/types/rewriter/parse.d.ts +27 -0
  87. package/build/types/rewriter/parse.d.ts.map +1 -0
  88. package/build/types/rewriter/report.d.ts +12 -0
  89. package/build/types/rewriter/report.d.ts.map +1 -0
  90. package/build/types/rewriter/rewriter.d.ts +64 -0
  91. package/build/types/rewriter/rewriter.d.ts.map +1 -0
  92. package/build/types/vite/source-map-hooks.d.ts.map +1 -1
  93. package/package.json +9 -4
@@ -0,0 +1,95 @@
1
+ // src/rewriter/ast.ts
2
+ /** The declaration a top-level statement makes, looking through `export` (`export const x = ...`). */
3
+ function declarationOf(node) {
4
+ return node.type === "ExportNamedDeclaration" ? node.declaration : node;
5
+ }
6
+ /** Whether `node` calls `methodName`, either bare (`fn()`) or as a member (`ns.fn()`). */
7
+ function isCallToMethod(node, methodName) {
8
+ if (node.type !== "CallExpression") return false;
9
+ const callee = node.callee;
10
+ if (!callee) return false;
11
+ if (callee.type === "Identifier" && callee.name === methodName) return true;
12
+ return callee.type === "MemberExpression" && callee.property?.type === "Identifier" && callee.property.name === methodName;
13
+ }
14
+ function isAnonymousFunctionOrClass(node) {
15
+ if (node.type === "ArrowFunctionExpression") return true;
16
+ return [
17
+ "FunctionExpression",
18
+ "FunctionDeclaration",
19
+ "ClassExpression",
20
+ "ClassDeclaration"
21
+ ].includes(node.type) && !node.id;
22
+ }
23
+ /** `export type { X }` or `export { type X }`, which TypeScript erases. */
24
+ function isTypeOnlyExport(node) {
25
+ return node.exportKind === "type";
26
+ }
27
+ /** The name an export or import specifier refers to, for both `x` and `"x"`. */
28
+ function exportedName(node) {
29
+ if (node?.type === "Identifier") return node.name;
30
+ if (node?.type === "Literal" && typeof node.value === "string") return node.value;
31
+ }
32
+ /**
33
+ * The offset after the hashbang and the directive prologue (`'use server'`),
34
+ * where code may be inserted without changing what the module means.
35
+ */
36
+ function directivesEnd(ast) {
37
+ let end = ast.hashbang?.end ?? 0;
38
+ for (const node of ast.body) {
39
+ if (node.type !== "ExpressionStatement" || typeof node.directive !== "string") break;
40
+ end = node.end;
41
+ }
42
+ return end;
43
+ }
44
+ /**
45
+ * The binding names a declaration introduces, including names inside
46
+ * destructuring patterns. `undefined` for a declaration that binds no runtime
47
+ * value (`interface`, `type`).
48
+ */
49
+ function declaredNames(declaration) {
50
+ switch (declaration.type) {
51
+ case "ClassDeclaration":
52
+ case "FunctionDeclaration": {
53
+ const name = declaration.id?.name;
54
+ return name ? [name] : void 0;
55
+ }
56
+ case "VariableDeclaration": {
57
+ const names = [];
58
+ for (const declarator of declaration.declarations ?? []) collectPatternNames(declarator.id, names);
59
+ return names;
60
+ }
61
+ default: return;
62
+ }
63
+ }
64
+ function collectPatternNames(pattern, names) {
65
+ if (!pattern) return;
66
+ const node = pattern;
67
+ switch (node.type) {
68
+ case "Identifier":
69
+ if (node.name) names.push(node.name);
70
+ break;
71
+ case "ObjectPattern":
72
+ for (const property of node.properties ?? []) collectPatternNames(property.type === "RestElement" ? property.argument : property.value, names);
73
+ break;
74
+ case "ArrayPattern":
75
+ for (const element of node.elements ?? []) collectPatternNames(element, names);
76
+ break;
77
+ case "AssignmentPattern":
78
+ collectPatternNames(node.left, names);
79
+ break;
80
+ case "RestElement": collectPatternNames(node.argument, names);
81
+ }
82
+ }
83
+ /** Visit `node` and every node below it, parents first. */
84
+ function walk(node, visit) {
85
+ if (Array.isArray(node)) {
86
+ for (const child of node) walk(child, visit);
87
+ return;
88
+ }
89
+ if (!node || typeof node !== "object" || typeof node.type !== "string") return;
90
+ visit(node);
91
+ for (const value of Object.values(node)) if (value && typeof value === "object") walk(value, visit);
92
+ }
93
+ export { declarationOf, declaredNames, directivesEnd, exportedName, isAnonymousFunctionOrClass, isCallToMethod, isTypeOnlyExport, walk };
94
+
95
+ //# sourceMappingURL=ast.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ast.js","names":[],"sources":["../../../src/rewriter/ast.ts"],"sourcesContent":["// Minimal ESTree node shapes. Any parser that puts `start`/`end` offsets on\n// every node fits (oxc, acorn, Rollup's `this.parse`).\n\nexport interface BaseNode {\n type: string;\n start: number;\n end: number;\n}\n\nexport interface ProgramBody {\n body: BaseNode[];\n hashbang?: BaseNode | null;\n}\n\nexport interface IdentifierNode extends BaseNode {\n name: string;\n}\n\n/** An `Identifier` or, for `export { x as \"name\" }`, a string `Literal`. */\nexport interface NameNode {\n type: string;\n name?: string;\n value?: unknown;\n}\n\nexport interface CallExpressionNode extends BaseNode {\n callee?: { type: string; name?: string; property?: { type: string; name?: string } };\n arguments?: BaseNode[];\n}\n\nexport interface MemberExpressionNode extends BaseNode {\n object?: { type: string; name?: string };\n property?: { type: string; name?: string };\n}\n\nexport interface ClassDeclarationNode extends BaseNode {\n id?: IdentifierNode | null;\n superClass?: BaseNode | null;\n decorators?: BaseNode[] | null;\n}\n\nexport interface ExportDefaultNode extends BaseNode {\n declaration: BaseNode;\n}\n\nexport interface ExportSpecifierNode extends BaseNode {\n local?: NameNode;\n exported?: NameNode;\n exportKind?: string;\n}\n\nexport interface ExportNamedNode extends BaseNode {\n declaration?: BaseNode | null;\n source?: (BaseNode & { value?: unknown }) | null;\n specifiers?: ExportSpecifierNode[];\n exportKind?: string;\n}\n\nexport interface ExportAllNode extends BaseNode {\n source?: { value?: unknown };\n exported?: NameNode | null;\n exportKind?: string;\n}\n\nexport interface VariableDeclaratorNode {\n id?: BaseNode | null;\n init?: BaseNode | null;\n}\n\nexport interface VariableDeclarationNode extends BaseNode {\n declarations?: VariableDeclaratorNode[];\n}\n\nexport interface ImportSpecifierNode {\n type: string;\n imported?: NameNode;\n local?: NameNode;\n importKind?: string;\n}\n\nexport interface ImportDeclarationNode extends BaseNode {\n source?: { value?: unknown };\n specifiers?: ImportSpecifierNode[];\n importKind?: string;\n}\n\nexport interface ArrayExpressionNode extends BaseNode {\n elements?: Array<BaseNode | null>;\n}\n\nexport interface PropertyNode extends BaseNode {\n key?: NameNode;\n value?: BaseNode;\n computed?: boolean;\n}\n\n/** The declaration a top-level statement makes, looking through `export` (`export const x = ...`). */\nexport function declarationOf(node: BaseNode): BaseNode | null | undefined {\n return node.type === 'ExportNamedDeclaration' ? (node as ExportNamedNode).declaration : node;\n}\n\n/** Whether `node` calls `methodName`, either bare (`fn()`) or as a member (`ns.fn()`). */\nexport function isCallToMethod(node: BaseNode, methodName: string): boolean {\n if (node.type !== 'CallExpression') return false;\n const callee = (node as CallExpressionNode).callee;\n if (!callee) return false;\n if (callee.type === 'Identifier' && callee.name === methodName) return true;\n return (\n callee.type === 'MemberExpression' && callee.property?.type === 'Identifier' && callee.property.name === methodName\n );\n}\n\nexport function isAnonymousFunctionOrClass(node: BaseNode): boolean {\n if (node.type === 'ArrowFunctionExpression') return true;\n const isFunctionOrClass = [\n 'FunctionExpression',\n 'FunctionDeclaration',\n 'ClassExpression',\n 'ClassDeclaration',\n ].includes(node.type);\n return isFunctionOrClass && !(node as ClassDeclarationNode).id;\n}\n\n/** `export type { X }` or `export { type X }`, which TypeScript erases. */\nexport function isTypeOnlyExport(node: { exportKind?: string }): boolean {\n return node.exportKind === 'type';\n}\n\n/** The name an export or import specifier refers to, for both `x` and `\"x\"`. */\nexport function exportedName(node: NameNode | undefined): string | undefined {\n if (node?.type === 'Identifier') return node.name;\n if (node?.type === 'Literal' && typeof node.value === 'string') return node.value;\n return undefined;\n}\n\n/**\n * The offset after the hashbang and the directive prologue (`'use server'`),\n * where code may be inserted without changing what the module means.\n */\nexport function directivesEnd(ast: ProgramBody): number {\n let end = ast.hashbang?.end ?? 0;\n for (const node of ast.body) {\n if (node.type !== 'ExpressionStatement' || typeof (node as { directive?: unknown }).directive !== 'string') break;\n end = node.end;\n }\n return end;\n}\n\n/**\n * The binding names a declaration introduces, including names inside\n * destructuring patterns. `undefined` for a declaration that binds no runtime\n * value (`interface`, `type`).\n */\nexport function declaredNames(declaration: BaseNode): string[] | undefined {\n switch (declaration.type) {\n case 'ClassDeclaration':\n case 'FunctionDeclaration': {\n const name = (declaration as ClassDeclarationNode).id?.name;\n return name ? [name] : undefined;\n }\n case 'VariableDeclaration': {\n const names: string[] = [];\n for (const declarator of (declaration as VariableDeclarationNode).declarations ?? []) {\n collectPatternNames(declarator.id, names);\n }\n return names;\n }\n default:\n return undefined;\n }\n}\n\nfunction collectPatternNames(pattern: BaseNode | null | undefined, names: string[]): void {\n if (!pattern) return;\n const node = pattern as BaseNode & {\n name?: string;\n properties?: Array<BaseNode & { value?: BaseNode; argument?: BaseNode }>;\n elements?: Array<BaseNode | null>;\n left?: BaseNode;\n argument?: BaseNode;\n };\n switch (node.type) {\n case 'Identifier':\n if (node.name) names.push(node.name);\n break;\n case 'ObjectPattern':\n for (const property of node.properties ?? []) {\n collectPatternNames(property.type === 'RestElement' ? property.argument : property.value, names);\n }\n break;\n case 'ArrayPattern':\n for (const element of node.elements ?? []) collectPatternNames(element, names);\n break;\n case 'AssignmentPattern':\n collectPatternNames(node.left, names);\n break;\n case 'RestElement':\n collectPatternNames(node.argument, names);\n break;\n }\n}\n\n/** Visit `node` and every node below it, parents first. */\nexport function walk(node: unknown, visit: (node: BaseNode) => void): void {\n if (Array.isArray(node)) {\n for (const child of node) walk(child, visit);\n return;\n }\n if (!node || typeof node !== 'object' || typeof (node as BaseNode).type !== 'string') return;\n\n visit(node as BaseNode);\n for (const value of Object.values(node)) {\n if (value && typeof value === 'object') walk(value, visit);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AAI,KAAK;GAE1C,OAAO;EACT;EACA,SACE;CACJ;AACF;AAEA,SAAS,oBAAoB,SAAsC,OAAuB;CACxF,IAAI,CAAC,SAAS;CACd,MAAM,OAAO;CAOb,QAAQ,KAAK,MAAb;EACE,KAAK;GACH,IAAI,KAAK,MAAM,MAAM,KAAK,KAAK,IAAI;GACnC;EACF,KAAK;GACH,KAAK,MAAM,YAAY,KAAK,cAAc,CAAC,GACzC,oBAAoB,SAAS,SAAS,gBAAgB,SAAS,WAAW,SAAS,OAAO,KAAK;GAEjG;EACF,KAAK;GACH,KAAK,MAAM,WAAW,KAAK,YAAY,CAAC,GAAG,oBAAoB,SAAS,KAAK;GAC7E;EACF,KAAK;GACH,oBAAoB,KAAK,MAAM,KAAK;GACpC;EACF,KAAK,eACH,oBAAoB,KAAK,UAAU,KAAK;CAE5C;AACF;;AAGA,SAAgB,KAAK,MAAe,OAAuC;CACzE,IAAI,MAAM,QAAQ,IAAI,GAAG;EACvB,KAAK,MAAM,SAAS,MAAM,KAAK,OAAO,KAAK;EAC3C;CACF;CACA,IAAI,CAAC,QAAQ,OAAO,SAAS,YAAY,OAAQ,KAAkB,SAAS,UAAU;CAEtF,MAAM,IAAgB;CACtB,KAAK,MAAM,SAAS,OAAO,OAAO,IAAI,GACpC,IAAI,SAAS,OAAO,UAAU,UAAU,KAAK,OAAO,KAAK;AAE7D"}
@@ -0,0 +1,67 @@
1
+ import { declarationOf } from "./ast.js";
2
+ // src/rewriter/class-extends.ts
3
+ /**
4
+ * Find top-level classes that (transitively, within this module) extend the
5
+ * given base class. A superclass must be a plain identifier (`extends Base`)
6
+ * or member access (`extends ns.Base`).
7
+ *
8
+ * Only same-file base chains are resolvable here; a base class imported from
9
+ * another module under a different name is invisible.
10
+ */
11
+ function findClassesExtending(ast, base) {
12
+ const bases = collectBaseImports(ast, base);
13
+ if (bases.named.size === 0 && bases.namespaces.size === 0) return /* @__PURE__ */ new Set();
14
+ const classes = /* @__PURE__ */ new Map();
15
+ for (const node of ast.body) {
16
+ const declaration = declarationOf(node);
17
+ if (declaration?.type !== "ClassDeclaration") continue;
18
+ const classNode = declaration;
19
+ if (classNode.id?.name) classes.set(classNode.id.name, classNode);
20
+ }
21
+ const found = /* @__PURE__ */ new Set();
22
+ let changed = true;
23
+ while (changed) {
24
+ changed = false;
25
+ for (const [name, classNode] of classes) {
26
+ if (found.has(name)) continue;
27
+ if (extendsBase(classNode.superClass, base, bases, found)) {
28
+ found.add(name);
29
+ changed = true;
30
+ }
31
+ }
32
+ }
33
+ return found;
34
+ }
35
+ function collectBaseImports(ast, base) {
36
+ const named = /* @__PURE__ */ new Set();
37
+ const namespaces = /* @__PURE__ */ new Set();
38
+ for (const node of ast.body) {
39
+ if (node.type !== "ImportDeclaration") continue;
40
+ const importNode = node;
41
+ if (importNode.source?.value !== base.module || importNode.importKind === "type") continue;
42
+ for (const specifier of importNode.specifiers ?? []) {
43
+ if (specifier.importKind === "type") continue;
44
+ if (specifier.type === "ImportSpecifier" && specifier.imported?.name === base.name && specifier.local?.name) named.add(specifier.local.name);
45
+ else if (specifier.type === "ImportNamespaceSpecifier" && specifier.local?.name) namespaces.add(specifier.local.name);
46
+ }
47
+ }
48
+ return {
49
+ named,
50
+ namespaces
51
+ };
52
+ }
53
+ function extendsBase(superClass, base, bases, detected) {
54
+ if (!superClass) return false;
55
+ if (superClass.type === "Identifier") {
56
+ const name = superClass.name;
57
+ return bases.named.has(name) || detected.has(name);
58
+ }
59
+ if (superClass.type === "MemberExpression") {
60
+ const member = superClass;
61
+ return member.object?.type === "Identifier" && !!member.object.name && bases.namespaces.has(member.object.name) && member.property?.name === base.name;
62
+ }
63
+ return false;
64
+ }
65
+ export { findClassesExtending };
66
+
67
+ //# sourceMappingURL=class-extends.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"class-extends.js","names":[],"sources":["../../../src/rewriter/class-extends.ts"],"sourcesContent":["import type {\n BaseNode,\n ClassDeclarationNode,\n IdentifierNode,\n ImportDeclarationNode,\n MemberExpressionNode,\n ProgramBody,\n} from './ast';\nimport { declarationOf } from './ast';\n\n/** A base class, named by the module it is imported from and its export name. */\nexport interface BaseClassRef {\n module: string;\n name: string;\n}\n\ninterface BaseBindings {\n /** Local identifiers bound to the named import of the base class. */\n named: Set<string>;\n /** Local identifiers bound to a `* as ns` import of the base module. */\n namespaces: Set<string>;\n}\n\n/**\n * Find top-level classes that (transitively, within this module) extend the\n * given base class. A superclass must be a plain identifier (`extends Base`)\n * or member access (`extends ns.Base`).\n *\n * Only same-file base chains are resolvable here; a base class imported from\n * another module under a different name is invisible.\n */\nexport function findClassesExtending(ast: ProgramBody, base: BaseClassRef): Set<string> {\n const bases = collectBaseImports(ast, base);\n if (bases.named.size === 0 && bases.namespaces.size === 0) {\n return new Set<string>();\n }\n\n // Every top-level class, including the `export class Foo {}` form.\n const classes = new Map<string, ClassDeclarationNode>();\n for (const node of ast.body) {\n const declaration = declarationOf(node);\n if (declaration?.type !== 'ClassDeclaration') continue;\n const classNode = declaration as ClassDeclarationNode;\n if (classNode.id?.name) classes.set(classNode.id.name, classNode);\n }\n\n const found = new Set<string>();\n // Iterate to a fixed point so an indirect chain (A extends B extends Base) is\n // fully resolved regardless of declaration order.\n let changed = true;\n while (changed) {\n changed = false;\n for (const [name, classNode] of classes) {\n if (found.has(name)) continue;\n if (extendsBase(classNode.superClass, base, bases, found)) {\n found.add(name);\n changed = true;\n }\n }\n }\n return found;\n}\n\nfunction collectBaseImports(ast: ProgramBody, base: BaseClassRef): BaseBindings {\n const named = new Set<string>();\n const namespaces = new Set<string>();\n for (const node of ast.body) {\n if (node.type !== 'ImportDeclaration') continue;\n const importNode = node as ImportDeclarationNode;\n if (importNode.source?.value !== base.module || importNode.importKind === 'type') continue;\n for (const specifier of importNode.specifiers ?? []) {\n if (specifier.importKind === 'type') continue;\n if (specifier.type === 'ImportSpecifier' && specifier.imported?.name === base.name && specifier.local?.name) {\n named.add(specifier.local.name);\n } else if (specifier.type === 'ImportNamespaceSpecifier' && specifier.local?.name) {\n namespaces.add(specifier.local.name);\n }\n }\n }\n return { named, namespaces };\n}\n\nfunction extendsBase(\n superClass: BaseNode | null | undefined,\n base: BaseClassRef,\n bases: BaseBindings,\n detected: Set<string>,\n): boolean {\n if (!superClass) return false;\n if (superClass.type === 'Identifier') {\n const name = (superClass as IdentifierNode).name;\n return bases.named.has(name) || detected.has(name);\n }\n if (superClass.type === 'MemberExpression') {\n const member = superClass as MemberExpressionNode;\n return (\n member.object?.type === 'Identifier' &&\n !!member.object.name &&\n bases.namespaces.has(member.object.name) &&\n member.property?.name === base.name\n );\n }\n return false;\n}\n"],"mappings":";;;;;;;;;;AA+BA,SAAgB,qBAAqB,KAAkB,MAAiC;CACtF,MAAM,QAAQ,mBAAmB,KAAK,IAAI;CAC1C,IAAI,MAAM,MAAM,SAAS,KAAK,MAAM,WAAW,SAAS,GACtD,uBAAO,IAAI,IAAY;CAIzB,MAAM,0BAAU,IAAI,IAAkC;CACtD,KAAK,MAAM,QAAQ,IAAI,MAAM;EAC3B,MAAM,cAAc,cAAc,IAAI;EACtC,IAAI,aAAa,SAAS,oBAAoB;EAC9C,MAAM,YAAY;EAClB,IAAI,UAAU,IAAI,MAAM,QAAQ,IAAI,UAAU,GAAG,MAAM,SAAS;CAClE;CAEA,MAAM,wBAAQ,IAAI,IAAY;CAG9B,IAAI,UAAU;CACd,OAAO,SAAS;EACd,UAAU;EACV,KAAK,MAAM,CAAC,MAAM,cAAc,SAAS;GACvC,IAAI,MAAM,IAAI,IAAI,GAAG;GACrB,IAAI,YAAY,UAAU,YAAY,MAAM,OAAO,KAAK,GAAG;IACzD,MAAM,IAAI,IAAI;IACd,UAAU;GACZ;EACF;CACF;CACA,OAAO;AACT;AAEA,SAAS,mBAAmB,KAAkB,MAAkC;CAC9E,MAAM,wBAAQ,IAAI,IAAY;CAC9B,MAAM,6BAAa,IAAI,IAAY;CACnC,KAAK,MAAM,QAAQ,IAAI,MAAM;EAC3B,IAAI,KAAK,SAAS,qBAAqB;EACvC,MAAM,aAAa;EACnB,IAAI,WAAW,QAAQ,UAAU,KAAK,UAAU,WAAW,eAAe,QAAQ;EAClF,KAAK,MAAM,aAAa,WAAW,cAAc,CAAC,GAAG;GACnD,IAAI,UAAU,eAAe,QAAQ;GACrC,IAAI,UAAU,SAAS,qBAAqB,UAAU,UAAU,SAAS,KAAK,QAAQ,UAAU,OAAO,MACrG,MAAM,IAAI,UAAU,MAAM,IAAI;QACzB,IAAI,UAAU,SAAS,8BAA8B,UAAU,OAAO,MAC3E,WAAW,IAAI,UAAU,MAAM,IAAI;EAEvC;CACF;CACA,OAAO;EAAE;EAAO;CAAW;AAC7B;AAEA,SAAS,YACP,YACA,MACA,OACA,UACS;CACT,IAAI,CAAC,YAAY,OAAO;CACxB,IAAI,WAAW,SAAS,cAAc;EACpC,MAAM,OAAQ,WAA8B;EAC5C,OAAO,MAAM,MAAM,IAAI,IAAI,KAAK,SAAS,IAAI,IAAI;CACnD;CACA,IAAI,WAAW,SAAS,oBAAoB;EAC1C,MAAM,SAAS;EACf,OACE,OAAO,QAAQ,SAAS,gBACxB,CAAC,CAAC,OAAO,OAAO,QAChB,MAAM,WAAW,IAAI,OAAO,OAAO,IAAI,KACvC,OAAO,UAAU,SAAS,KAAK;CAEnC;CACA,OAAO;AACT"}
@@ -0,0 +1,231 @@
1
+ import { declarationOf, declaredNames, exportedName, isAnonymousFunctionOrClass, isCallToMethod, isTypeOnlyExport } from "./ast.js";
2
+ // src/rewriter/exports.ts
3
+ /** Specifier name a `export { default as X } from '...'` re-export uses for the source's default. */
4
+ const DEFAULT_IMPORT = "default";
5
+ /** Top-level class declarations, exported or not, by name. */
6
+ function collectTopLevelClasses(ast) {
7
+ const classes = /* @__PURE__ */ new Map();
8
+ for (const node of ast.body) {
9
+ const declaration = declarationOf(node);
10
+ if (declaration?.type !== "ClassDeclaration") continue;
11
+ const classNode = declaration;
12
+ if (classNode.id?.name) classes.set(classNode.id.name, classNode);
13
+ }
14
+ return classes;
15
+ }
16
+ /** Top-level value binding name → the statement that declares it. */
17
+ function collectDeclaringStatements(ast) {
18
+ const statements = /* @__PURE__ */ new Map();
19
+ for (const node of ast.body) {
20
+ const declaration = declarationOf(node);
21
+ if (!declaration) continue;
22
+ const names = declaration.type === "ImportDeclaration" ? valueImportNames(declaration) : declaredNames(declaration) ?? [];
23
+ for (const name of names) if (name) statements.set(name, node);
24
+ }
25
+ return statements;
26
+ }
27
+ function valueImportNames(node) {
28
+ if (node.importKind === "type") return [];
29
+ return (node.specifiers ?? []).filter((specifier) => specifier.importKind !== "type").map((specifier) => specifier.local?.name);
30
+ }
31
+ /**
32
+ * Top-level bindings already assigned the result of one of `wrapperNames`
33
+ * (`const X = wrap(...)`), so exporting one must not wrap it a second time.
34
+ */
35
+ function collectManuallyWrappedLocals(ast, wrapperNames) {
36
+ const locals = /* @__PURE__ */ new Set();
37
+ for (const node of ast.body) {
38
+ const declaration = declarationOf(node);
39
+ if (declaration?.type !== "VariableDeclaration") continue;
40
+ for (const declarator of declaration.declarations ?? []) {
41
+ const name = declarator.id?.type === "Identifier" ? declarator.id.name : void 0;
42
+ const init = declarator.init;
43
+ if (name && init && wrapperNames.some((wrapper) => isCallToMethod(init, wrapper))) locals.add(name);
44
+ }
45
+ }
46
+ return locals;
47
+ }
48
+ /** The value export statements (`export const`, `export { }`) that export `name`. */
49
+ function findNamedExports(ast, name) {
50
+ const found = [];
51
+ for (const node of ast.body) {
52
+ if (node.type !== "ExportNamedDeclaration" || isTypeOnlyExport(node)) continue;
53
+ const exportNode = node;
54
+ if (exportNode.declaration ? !!declaredNames(exportNode.declaration)?.includes(name) : (exportNode.specifiers ?? []).some((specifier) => !isTypeOnlyExport(specifier) && exportedName(specifier.exported) === name)) found.push(exportNode);
55
+ }
56
+ return found;
57
+ }
58
+ /**
59
+ * Wrap a binding declared in this module and return the binding that holds the wrapper.
60
+ *
61
+ * The declaration is left exactly as written, so a class or function keeps its
62
+ * `Function.prototype.name`. Only the *export* has to become the wrapper, so the wrapper gets its
63
+ * own binding and the export is aliased to it.
64
+ */
65
+ function wrapLocalBinding(state, localName, declaringNode, wrap) {
66
+ const existing = state.wrappedLocals.get(localName);
67
+ if (existing) return existing;
68
+ const wrappedName = `__SENTRY_WRAPPED_${localName}__`;
69
+ state.wrappedLocals.set(localName, wrappedName);
70
+ state.ms.appendLeft(declaringNode.end, `\nconst ${wrappedName} = ${wrap(localName)};\n`);
71
+ state.needsImport = true;
72
+ return wrappedName;
73
+ }
74
+ /**
75
+ * Wrap `localName` where an inline export declares it (`export class X {}`,
76
+ * `export const x = ...`, `export function x() {}`). The `export` keyword is
77
+ * replaced by an export clause that aliases the name to the wrapper. Other
78
+ * names the same declaration exports are exported again, through their
79
+ * wrapper when a later call wraps them too.
80
+ *
81
+ * @returns whether the declaration exports `localName`
82
+ */
83
+ function wrapInlineExport(state, exportNode, localName, wrap) {
84
+ const declaration = exportNode.declaration;
85
+ const names = declaration ? declaredNames(declaration) : void 0;
86
+ if (!declaration || !names?.includes(localName)) return false;
87
+ wrapLocalBinding(state, localName, declaration, wrap);
88
+ const specifiers = names.map((name) => {
89
+ const wrappedName = state.wrappedLocals.get(name);
90
+ return wrappedName ? `${wrappedName} as ${name}` : name;
91
+ });
92
+ const decoratorStart = declaration.decorators?.[0]?.start ?? exportNode.start;
93
+ const start = Math.min(decoratorStart, exportNode.start);
94
+ const decorators = state.ms.original.slice(start, exportNode.start);
95
+ state.ms.overwrite(start, declaration.start, `export { ${specifiers.join(", ")} };\n${decorators}`);
96
+ return true;
97
+ }
98
+ /**
99
+ * Wrap the specifiers of an `export { ... }` statement that `wrapSpecifier` picks.
100
+ *
101
+ * Each such specifier is re-pointed at a binding holding the wrapper, which means rebuilding the
102
+ * statement; the other specifiers are carried over verbatim. A binding that lives in **another**
103
+ * module is first bound under a private name (see {@link wrapForeignExport}), since an import
104
+ * binding cannot be reassigned and the `from` form has no local binding at all.
105
+ */
106
+ function wrapSpecifierExports(state, node, wrapSpecifier) {
107
+ const specifiers = node.specifiers ?? [];
108
+ if (specifiers.length === 0 || isTypeOnlyExport(node)) return;
109
+ const sourceLiteral = node.source ? state.ms.original.slice(node.source.start, node.source.end) : void 0;
110
+ const prelude = [];
111
+ const wrappedPairs = [];
112
+ const kept = [];
113
+ for (const specifier of specifiers) {
114
+ const pair = isTypeOnlyExport(specifier) ? void 0 : wrapSpecifier(specifier, sourceLiteral, prelude);
115
+ if (pair) wrappedPairs.push(pair);
116
+ else kept.push(state.ms.original.slice(specifier.start, specifier.end));
117
+ }
118
+ if (wrappedPairs.length === 0) return;
119
+ const statements = [...prelude, `export { ${wrappedPairs.join(", ")} };`];
120
+ if (kept.length > 0) {
121
+ const clause = `export { ${kept.join(", ")} }`;
122
+ statements.push(sourceLiteral ? `${clause} from ${sourceLiteral};` : `${clause};`);
123
+ }
124
+ state.ms.overwrite(node.start, node.end, statements.join("\n"));
125
+ }
126
+ /**
127
+ * Wrap a binding that comes from another module: imported and re-exported
128
+ * (`sourceLiteral` undefined), or re-exported directly (`export { x } from`).
129
+ * Pushes the statements that bind the wrapper onto `prelude` and returns the
130
+ * pair to export.
131
+ */
132
+ function wrapForeignExport(state, localName, exportedAs, sourceLiteral, prelude, wrap) {
133
+ let target = localName;
134
+ if (sourceLiteral) {
135
+ target = `__SENTRY_REEXPORT_${exportedAs}__`;
136
+ prelude.push(importAs(localName, target, sourceLiteral));
137
+ }
138
+ const wrappedName = `__SENTRY_WRAPPED_${exportedAs}__`;
139
+ prelude.push(`const ${wrappedName} = ${wrap(target)};`);
140
+ state.needsImport = true;
141
+ return `${wrappedName} as ${exportedAs}`;
142
+ }
143
+ /**
144
+ * Wrap `export default <declaration or expression>`.
145
+ *
146
+ * `export default Foo`, where `Foo` already has a wrapper binding (a class
147
+ * also exported by name), is re-pointed at that binding instead of wrapping
148
+ * twice.
149
+ */
150
+ function wrapDefaultExport(state, node, wrap) {
151
+ const decl = node.declaration;
152
+ if (decl.type === "Identifier") {
153
+ const wrappedName = state.wrappedLocals.get(decl.name);
154
+ if (wrappedName) {
155
+ state.ms.overwrite(decl.start, decl.end, wrappedName);
156
+ return;
157
+ }
158
+ }
159
+ const source = state.ms.original;
160
+ const keywordsEnd = exportDefaultKeywordsEnd(source, node.start);
161
+ const declaredName = decl.type === "ClassDeclaration" || decl.type === "FunctionDeclaration" ? decl.id?.name : void 0;
162
+ let wrapped = "__SENTRY_DEFAULT_EXPORT__";
163
+ if (declaredName) {
164
+ state.ms.remove(node.start, decl.start);
165
+ state.ms.appendLeft(node.end, `\nvar __SENTRY_DEFAULT_EXPORT__ = ${declaredName};`);
166
+ wrapped = declaredName;
167
+ } else if (isAnonymousFunctionOrClass(decl)) {
168
+ const hasSemicolon = source[node.end - 1] === ";";
169
+ state.ms.overwrite(node.start, keywordsEnd, "var __SENTRY_DEFAULT_EXPORT__ = { default:");
170
+ state.ms.appendLeft(hasSemicolon ? node.end - 1 : node.end, hasSemicolon ? " }.default" : " }.default;");
171
+ } else state.ms.overwrite(node.start, keywordsEnd, "var __SENTRY_DEFAULT_EXPORT__ =");
172
+ state.ms.appendLeft(node.end, "\nexport { __SENTRY_DEFAULT_EXPORT__ as default };");
173
+ state.ms.append(`\n__SENTRY_DEFAULT_EXPORT__ = ${wrap(wrapped)};`);
174
+ state.needsImport = true;
175
+ }
176
+ /**
177
+ * Offset just past the `default` keyword of the `export default` statement at `start`. Comments can
178
+ * sit between `export`, `default` and the expression, and may themselves contain the word `default`.
179
+ */
180
+ function exportDefaultKeywordsEnd(source, start) {
181
+ let index = start + 6;
182
+ while (index < source.length && !source.startsWith("default", index)) if (source.startsWith("//", index)) {
183
+ const lineEnd = source.indexOf("\n", index);
184
+ index = lineEnd === -1 ? source.length : lineEnd;
185
+ } else if (source.startsWith("/*", index)) {
186
+ const commentEnd = source.indexOf("*/", index + 2);
187
+ index = commentEnd === -1 ? source.length : commentEnd + 2;
188
+ } else index++;
189
+ return index + 7;
190
+ }
191
+ /**
192
+ * `export { handler as default }`, the form bundlers emit for a default export. The export is set
193
+ * in two steps. Right after `handler` is declared, it is bound to `handler` as-is, so a circular
194
+ * import that reads it during module evaluation gets the handler, as before the transform. At the
195
+ * end of the module, it is rebound to the wrapper, so a handler assigned or given methods later is
196
+ * wrapped as it ends up. The binding is a `var`: a read before `handler` is declared gets
197
+ * `undefined`, as it would from the bundler's own `var`, where a `const` would throw and stop the
198
+ * Worker from starting.
199
+ *
200
+ * @returns the pair to export in place of the specifier
201
+ */
202
+ function wrapDefaultSpecifier(state, localName, declaringStatement, wrap) {
203
+ const wrappedName = state.wrappedLocals.get(localName);
204
+ if (wrappedName) return `${wrappedName} as default`;
205
+ if (declaringStatement) state.ms.appendLeft(declaringStatement.end, `\nvar __SENTRY_DEFAULT_EXPORT__ = ${localName};`);
206
+ state.ms.append(`\n${declaringStatement ? "" : "var "}__SENTRY_DEFAULT_EXPORT__ = ${wrap(localName)};`);
207
+ state.needsImport = true;
208
+ return "__SENTRY_DEFAULT_EXPORT__ as default";
209
+ }
210
+ /**
211
+ * `export { default } from './handler'` and `export { x as default } from './handler'`.
212
+ *
213
+ * The original re-export is a live binding to the handler module's export, but the wrapper is not
214
+ * set until this module's body runs. A circular import that reads the default export before then
215
+ * gets `undefined` instead of the handler. The binding is a `var` so that the read does not throw,
216
+ * as a `const` would, and stop the Worker from starting. See `wrapDefaultSpecifier`.
217
+ *
218
+ * @returns the pair to export in place of the specifier
219
+ */
220
+ function wrapDefaultReexport(state, localName, sourceLiteral, prelude, wrap) {
221
+ const target = "__SENTRY_REEXPORT_default__";
222
+ prelude.push(importAs(localName, target, sourceLiteral), `var __SENTRY_DEFAULT_EXPORT__ = ${wrap(target)};`);
223
+ state.needsImport = true;
224
+ return "__SENTRY_DEFAULT_EXPORT__ as default";
225
+ }
226
+ function importAs(importedName, localName, sourceLiteral) {
227
+ return importedName === DEFAULT_IMPORT ? `import ${localName} from ${sourceLiteral};` : `import { ${importedName} as ${localName} } from ${sourceLiteral};`;
228
+ }
229
+ export { collectDeclaringStatements, collectManuallyWrappedLocals, collectTopLevelClasses, findNamedExports, wrapDefaultExport, wrapDefaultReexport, wrapDefaultSpecifier, wrapForeignExport, wrapInlineExport, wrapLocalBinding, wrapSpecifierExports };
230
+
231
+ //# sourceMappingURL=exports.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"exports.js","names":[],"sources":["../../../src/rewriter/exports.ts"],"sourcesContent":["import type {\n BaseNode,\n ClassDeclarationNode,\n ExportDefaultNode,\n ExportNamedNode,\n ExportSpecifierNode,\n IdentifierNode,\n ImportDeclarationNode,\n ProgramBody,\n VariableDeclarationNode,\n} from './ast';\nimport {\n declarationOf,\n declaredNames,\n exportedName,\n isAnonymousFunctionOrClass,\n isCallToMethod,\n isTypeOnlyExport,\n} from './ast';\nimport type { RewriteState } from './rewriter';\n\n/** Builds the expression that wraps `target`, e.g. `target => \\`wrap(${target})\\``. */\nexport type WrapExpression = (target: string) => string;\n\n/**\n * Handles one export specifier of a statement passed to {@link wrapSpecifierExports}.\n * Returns the `Wrapped as Exported` pair to emit in its place, or `undefined`\n * to keep the specifier as written. Statements the pair depends on go onto\n * `prelude`, which is emitted before the rebuilt export.\n */\nexport type SpecifierWrapper = (\n specifier: ExportSpecifierNode,\n sourceLiteral: string | undefined,\n prelude: string[],\n) => string | undefined;\n\n/** Specifier name a `export { default as X } from '...'` re-export uses for the source's default. */\nconst DEFAULT_IMPORT = 'default';\n\n/** Top-level class declarations, exported or not, by name. */\nexport function collectTopLevelClasses(ast: ProgramBody): Map<string, ClassDeclarationNode> {\n const classes = new Map<string, ClassDeclarationNode>();\n for (const node of ast.body) {\n const declaration = declarationOf(node);\n if (declaration?.type !== 'ClassDeclaration') continue;\n const classNode = declaration as ClassDeclarationNode;\n if (classNode.id?.name) classes.set(classNode.id.name, classNode);\n }\n return classes;\n}\n\n/** Top-level value binding name → the statement that declares it. */\nexport function collectDeclaringStatements(ast: ProgramBody): Map<string, BaseNode> {\n const statements = new Map<string, BaseNode>();\n for (const node of ast.body) {\n const declaration = declarationOf(node);\n if (!declaration) continue;\n const names =\n declaration.type === 'ImportDeclaration' ? valueImportNames(declaration) : (declaredNames(declaration) ?? []);\n for (const name of names) {\n if (name) statements.set(name, node);\n }\n }\n return statements;\n}\n\nfunction valueImportNames(node: ImportDeclarationNode): (string | undefined)[] {\n if (node.importKind === 'type') return [];\n return (node.specifiers ?? [])\n .filter(specifier => specifier.importKind !== 'type')\n .map(specifier => specifier.local?.name);\n}\n\n/**\n * Top-level bindings already assigned the result of one of `wrapperNames`\n * (`const X = wrap(...)`), so exporting one must not wrap it a second time.\n */\nexport function collectManuallyWrappedLocals(ast: ProgramBody, wrapperNames: readonly string[]): Set<string> {\n const locals = new Set<string>();\n\n for (const node of ast.body) {\n const declaration = declarationOf(node);\n if (declaration?.type !== 'VariableDeclaration') continue;\n for (const declarator of (declaration as VariableDeclarationNode).declarations ?? []) {\n const name = declarator.id?.type === 'Identifier' ? (declarator.id as IdentifierNode).name : undefined;\n const init = declarator.init;\n if (name && init && wrapperNames.some(wrapper => isCallToMethod(init, wrapper))) {\n locals.add(name);\n }\n }\n }\n\n return locals;\n}\n\n/** The value export statements (`export const`, `export { }`) that export `name`. */\nexport function findNamedExports(ast: ProgramBody, name: string): ExportNamedNode[] {\n const found: ExportNamedNode[] = [];\n for (const node of ast.body) {\n if (node.type !== 'ExportNamedDeclaration' || isTypeOnlyExport(node as ExportNamedNode)) continue;\n const exportNode = node as ExportNamedNode;\n const exportsName = exportNode.declaration\n ? !!declaredNames(exportNode.declaration)?.includes(name)\n : (exportNode.specifiers ?? []).some(\n specifier => !isTypeOnlyExport(specifier) && exportedName(specifier.exported) === name,\n );\n if (exportsName) found.push(exportNode);\n }\n return found;\n}\n\n/**\n * Wrap a binding declared in this module and return the binding that holds the wrapper.\n *\n * The declaration is left exactly as written, so a class or function keeps its\n * `Function.prototype.name`. Only the *export* has to become the wrapper, so the wrapper gets its\n * own binding and the export is aliased to it.\n */\nexport function wrapLocalBinding(\n state: RewriteState,\n localName: string,\n declaringNode: BaseNode,\n wrap: WrapExpression,\n): string {\n const existing = state.wrappedLocals.get(localName);\n if (existing) return existing;\n\n const wrappedName = `__SENTRY_WRAPPED_${localName}__`;\n state.wrappedLocals.set(localName, wrappedName);\n state.ms.appendLeft(declaringNode.end, `\\nconst ${wrappedName} = ${wrap(localName)};\\n`);\n state.needsImport = true;\n return wrappedName;\n}\n\n/**\n * Wrap `localName` where an inline export declares it (`export class X {}`,\n * `export const x = ...`, `export function x() {}`). The `export` keyword is\n * replaced by an export clause that aliases the name to the wrapper. Other\n * names the same declaration exports are exported again, through their\n * wrapper when a later call wraps them too.\n *\n * @returns whether the declaration exports `localName`\n */\nexport function wrapInlineExport(\n state: RewriteState,\n exportNode: ExportNamedNode,\n localName: string,\n wrap: WrapExpression,\n): boolean {\n const declaration = exportNode.declaration;\n const names = declaration ? declaredNames(declaration) : undefined;\n if (!declaration || !names?.includes(localName)) return false;\n\n wrapLocalBinding(state, localName, declaration, wrap);\n const specifiers = names.map(name => {\n const wrappedName = state.wrappedLocals.get(name);\n return wrappedName ? `${wrappedName} as ${name}` : name;\n });\n // In `@dec export class A {}` the decorators come before `export`, so the\n // clause has to go before them.\n const decoratorStart = (declaration as ClassDeclarationNode).decorators?.[0]?.start ?? exportNode.start;\n const start = Math.min(decoratorStart, exportNode.start);\n const decorators = state.ms.original.slice(start, exportNode.start);\n // A second call for another name of this declaration overwrites the same range again.\n state.ms.overwrite(start, declaration.start, `export { ${specifiers.join(', ')} };\\n${decorators}`);\n return true;\n}\n\n/**\n * Wrap the specifiers of an `export { ... }` statement that `wrapSpecifier` picks.\n *\n * Each such specifier is re-pointed at a binding holding the wrapper, which means rebuilding the\n * statement; the other specifiers are carried over verbatim. A binding that lives in **another**\n * module is first bound under a private name (see {@link wrapForeignExport}), since an import\n * binding cannot be reassigned and the `from` form has no local binding at all.\n */\nexport function wrapSpecifierExports(\n state: RewriteState,\n node: ExportNamedNode,\n wrapSpecifier: SpecifierWrapper,\n): void {\n const specifiers = node.specifiers ?? [];\n if (specifiers.length === 0 || isTypeOnlyExport(node)) return;\n\n const sourceLiteral = node.source ? state.ms.original.slice(node.source.start, node.source.end) : undefined;\n\n const prelude: string[] = [];\n const wrappedPairs: string[] = [];\n const kept: string[] = [];\n\n for (const specifier of specifiers) {\n const pair = isTypeOnlyExport(specifier) ? undefined : wrapSpecifier(specifier, sourceLiteral, prelude);\n if (pair) {\n wrappedPairs.push(pair);\n } else {\n kept.push(state.ms.original.slice(specifier.start, specifier.end));\n }\n }\n\n if (wrappedPairs.length === 0) return;\n\n const statements = [...prelude, `export { ${wrappedPairs.join(', ')} };`];\n if (kept.length > 0) {\n const clause = `export { ${kept.join(', ')} }`;\n statements.push(sourceLiteral ? `${clause} from ${sourceLiteral};` : `${clause};`);\n }\n state.ms.overwrite(node.start, node.end, statements.join('\\n'));\n}\n\n/**\n * Wrap a binding that comes from another module: imported and re-exported\n * (`sourceLiteral` undefined), or re-exported directly (`export { x } from`).\n * Pushes the statements that bind the wrapper onto `prelude` and returns the\n * pair to export.\n */\nexport function wrapForeignExport(\n state: RewriteState,\n localName: string,\n exportedAs: string,\n sourceLiteral: string | undefined,\n prelude: string[],\n wrap: WrapExpression,\n): string {\n let target = localName;\n\n if (sourceLiteral) {\n target = `__SENTRY_REEXPORT_${exportedAs}__`;\n prelude.push(importAs(localName, target, sourceLiteral));\n }\n\n const wrappedName = `__SENTRY_WRAPPED_${exportedAs}__`;\n prelude.push(`const ${wrappedName} = ${wrap(target)};`);\n state.needsImport = true;\n return `${wrappedName} as ${exportedAs}`;\n}\n\n/**\n * Wrap `export default <declaration or expression>`.\n *\n * `export default Foo`, where `Foo` already has a wrapper binding (a class\n * also exported by name), is re-pointed at that binding instead of wrapping\n * twice.\n */\nexport function wrapDefaultExport(state: RewriteState, node: ExportDefaultNode, wrap: WrapExpression): void {\n const decl = node.declaration;\n\n if (decl.type === 'Identifier') {\n const wrappedName = state.wrappedLocals.get((decl as IdentifierNode).name);\n if (wrappedName) {\n state.ms.overwrite(decl.start, decl.end, wrappedName);\n return;\n }\n }\n\n // Only the `export default` keywords are replaced, so parentheses around the expression stay.\n const source = state.ms.original;\n const keywordsEnd = exportDefaultKeywordsEnd(source, node.start);\n const declaredName =\n decl.type === 'ClassDeclaration' || decl.type === 'FunctionDeclaration'\n ? (decl as ClassDeclarationNode).id?.name\n : undefined;\n\n // As with `export { handler as default }`, the export is set in two steps: bound as-is where the\n // original statement set it, then rebound at the end of the module to the wrapper, which sees\n // methods added in between. See `wrapDefaultSpecifier`.\n let wrapped = '__SENTRY_DEFAULT_EXPORT__';\n if (declaredName) {\n // `export default class Handler {}`: the declaration stays, so `Handler` stays bound in the module.\n state.ms.remove(node.start, decl.start);\n state.ms.appendLeft(node.end, `\\nvar __SENTRY_DEFAULT_EXPORT__ = ${declaredName};`);\n wrapped = declaredName;\n } else if (isAnonymousFunctionOrClass(decl)) {\n // As a property value, the class or function gets the name `default`, as `export default` gives it.\n const hasSemicolon = source[node.end - 1] === ';';\n state.ms.overwrite(node.start, keywordsEnd, 'var __SENTRY_DEFAULT_EXPORT__ = { default:');\n state.ms.appendLeft(hasSemicolon ? node.end - 1 : node.end, hasSemicolon ? ' }.default' : ' }.default;');\n } else {\n state.ms.overwrite(node.start, keywordsEnd, 'var __SENTRY_DEFAULT_EXPORT__ =');\n }\n state.ms.appendLeft(node.end, '\\nexport { __SENTRY_DEFAULT_EXPORT__ as default };');\n state.ms.append(`\\n__SENTRY_DEFAULT_EXPORT__ = ${wrap(wrapped)};`);\n state.needsImport = true;\n}\n\n/**\n * Offset just past the `default` keyword of the `export default` statement at `start`. Comments can\n * sit between `export`, `default` and the expression, and may themselves contain the word `default`.\n */\nfunction exportDefaultKeywordsEnd(source: string, start: number): number {\n let index = start + 'export'.length;\n while (index < source.length && !source.startsWith('default', index)) {\n if (source.startsWith('//', index)) {\n const lineEnd = source.indexOf('\\n', index);\n index = lineEnd === -1 ? source.length : lineEnd;\n } else if (source.startsWith('/*', index)) {\n const commentEnd = source.indexOf('*/', index + 2);\n index = commentEnd === -1 ? source.length : commentEnd + 2;\n } else {\n index++;\n }\n }\n return index + 'default'.length;\n}\n\n/**\n * `export { handler as default }`, the form bundlers emit for a default export. The export is set\n * in two steps. Right after `handler` is declared, it is bound to `handler` as-is, so a circular\n * import that reads it during module evaluation gets the handler, as before the transform. At the\n * end of the module, it is rebound to the wrapper, so a handler assigned or given methods later is\n * wrapped as it ends up. The binding is a `var`: a read before `handler` is declared gets\n * `undefined`, as it would from the bundler's own `var`, where a `const` would throw and stop the\n * Worker from starting.\n *\n * @returns the pair to export in place of the specifier\n */\nexport function wrapDefaultSpecifier(\n state: RewriteState,\n localName: string,\n declaringStatement: BaseNode | undefined,\n wrap: WrapExpression,\n): string {\n // Same as `export default Foo`: re-point at the wrapper a named export already made.\n const wrappedName = state.wrappedLocals.get(localName);\n if (wrappedName) return `${wrappedName} as default`;\n\n if (declaringStatement) {\n state.ms.appendLeft(declaringStatement.end, `\\nvar __SENTRY_DEFAULT_EXPORT__ = ${localName};`);\n }\n state.ms.append(`\\n${declaringStatement ? '' : 'var '}__SENTRY_DEFAULT_EXPORT__ = ${wrap(localName)};`);\n state.needsImport = true;\n return '__SENTRY_DEFAULT_EXPORT__ as default';\n}\n\n/**\n * `export { default } from './handler'` and `export { x as default } from './handler'`.\n *\n * The original re-export is a live binding to the handler module's export, but the wrapper is not\n * set until this module's body runs. A circular import that reads the default export before then\n * gets `undefined` instead of the handler. The binding is a `var` so that the read does not throw,\n * as a `const` would, and stop the Worker from starting. See `wrapDefaultSpecifier`.\n *\n * @returns the pair to export in place of the specifier\n */\nexport function wrapDefaultReexport(\n state: RewriteState,\n localName: string,\n sourceLiteral: string,\n prelude: string[],\n wrap: WrapExpression,\n): string {\n const target = '__SENTRY_REEXPORT_default__';\n prelude.push(importAs(localName, target, sourceLiteral), `var __SENTRY_DEFAULT_EXPORT__ = ${wrap(target)};`);\n state.needsImport = true;\n return '__SENTRY_DEFAULT_EXPORT__ as default';\n}\n\nfunction importAs(importedName: string, localName: string, sourceLiteral: string): string {\n return importedName === DEFAULT_IMPORT\n ? `import ${localName} from ${sourceLiteral};`\n : `import { ${importedName} as ${localName} } from ${sourceLiteral};`;\n}\n"],"mappings":";;;AAqCA,MAAM,iBAAiB;;AAGvB,SAAgB,uBAAuB,KAAqD;CAC1F,MAAM,0BAAU,IAAI,IAAkC;CACtD,KAAK,MAAM,QAAQ,IAAI,MAAM;EAC3B,MAAM,cAAc,cAAc,IAAI;EACtC,IAAI,aAAa,SAAS,oBAAoB;EAC9C,MAAM,YAAY;EAClB,IAAI,UAAU,IAAI,MAAM,QAAQ,IAAI,UAAU,GAAG,MAAM,SAAS;CAClE;CACA,OAAO;AACT;;AAGA,SAAgB,2BAA2B,KAAyC;CAClF,MAAM,6BAAa,IAAI,IAAsB;CAC7C,KAAK,MAAM,QAAQ,IAAI,MAAM;EAC3B,MAAM,cAAc,cAAc,IAAI;EACtC,IAAI,CAAC,aAAa;EAClB,MAAM,QACJ,YAAY,SAAS,sBAAsB,iBAAiB,WAAW,IAAK,cAAc,WAAW,KAAK,CAAC;EAC7G,KAAK,MAAM,QAAQ,OACjB,IAAI,MAAM,WAAW,IAAI,MAAM,IAAI;CAEvC;CACA,OAAO;AACT;AAEA,SAAS,iBAAiB,MAAqD;CAC7E,IAAI,KAAK,eAAe,QAAQ,OAAO,CAAC;CACxC,QAAQ,KAAK,cAAc,CAAC,EAAA,CACzB,QAAO,cAAa,UAAU,eAAe,MAAM,CAAC,CACpD,KAAI,cAAa,UAAU,OAAO,IAAI;AAC3C;;;;;AAMA,SAAgB,6BAA6B,KAAkB,cAA8C;CAC3G,MAAM,yBAAS,IAAI,IAAY;CAE/B,KAAK,MAAM,QAAQ,IAAI,MAAM;EAC3B,MAAM,cAAc,cAAc,IAAI;EACtC,IAAI,aAAa,SAAS,uBAAuB;EACjD,KAAK,MAAM,cAAe,YAAwC,gBAAgB,CAAC,GAAG;GACpF,MAAM,OAAO,WAAW,IAAI,SAAS,eAAgB,WAAW,GAAsB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Q;CACpB,OAAO,QAAQ,OAAO,UAAU,CAAC,OAAO,WAAW,WAAW,KAAK,GACjE,IAAI,OAAO,WAAW,MAAM,KAAK,GAAG;EAClC,MAAM,UAAU,OAAO,QAAQ,MAAM,KAAK;EAC1C,QAAQ,YAAY,KAAK,OAAO,SAAS;CAC3C,OAAO,IAAI,OAAO,WAAW,MAAM,KAAK,GAAG;EACzC,MAAM,aAAa,OAAO,QAAQ,MAAM,QAAQ,CAAC;EACjD,QAAQ,eAAe,KAAK,OAAO,SAAS,aAAa;CAC3D,OACE;CAGJ,OAAO,QAAQ;AACjB;;;;;;;;;;;;AAaA,SAAgB,qBACd,OACA,WACA,oBACA,MACQ;CAER,MAAM,cAAc,MAAM,cAAc,IAAI,SAAS;CACrD,IAAI,aAAa,OAAO,GAAG,YAAY;CAEvC,IAAI,oBACF,MAAM,GAAG,WAAW,mBAAmB,KAAK,qCAAqC,UAAU,EAAE;CAE/F,MAAM,GAAG,OAAO,KAAK,qBAAqB,KAAK,OAAO,8BAA8B,KAAK,SAAS,EAAE,EAAE;CACtG,MAAM,cAAc;CACpB,OAAO;AACT;;;;;;;;;;;AAYA,SAAgB,oBACd,OACA,WACA,eACA,SACA,MACQ;CACR,MAAM,SAAS;CACf,QAAQ,KAAK,SAAS,WAAW,QAAQ,aAAa,GAAG,mCAAmC,KAAK,MAAM,EAAE,EAAE;CAC3G,MAAM,cAAc;CACpB,OAAO;AACT;AAEA,SAAS,SAAS,cAAsB,WAAmB,eAA+B;CACxF,OAAO,iBAAiB,iBACpB,UAAU,UAAU,QAAQ,cAAc,KAC1C,YAAY,aAAa,MAAM,UAAU,UAAU,cAAc;AACvE"}
@@ -0,0 +1,21 @@
1
+ import { directivesEnd, exportedName } from "./ast.js";
2
+ // src/rewriter/imports.ts
3
+ /** Whether the module already binds `name` to the export of the same name from `module`. */
4
+ function hasImport(ast, module, name) {
5
+ return ast.body.some((node) => {
6
+ if (node.type !== "ImportDeclaration") return false;
7
+ const importNode = node;
8
+ if (importNode.source?.value !== module || importNode.importKind === "type") return false;
9
+ return (importNode.specifiers ?? []).some((specifier) => specifier.type === "ImportSpecifier" && specifier.importKind !== "type" && exportedName(specifier.imported) === name && specifier.local?.name === name);
10
+ });
11
+ }
12
+ /** Adds an import statement after the hashbang and directives, once per module. */
13
+ function addImport(state, ast, statement) {
14
+ if (state.imports.has(statement)) return;
15
+ state.imports.add(statement);
16
+ const position = directivesEnd(ast);
17
+ state.ms.appendLeft(position, position === 0 ? `${statement}\n` : `\n${statement}`);
18
+ }
19
+ export { addImport, hasImport };
20
+
21
+ //# sourceMappingURL=imports.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"imports.js","names":[],"sources":["../../../src/rewriter/imports.ts"],"sourcesContent":["import type { ImportDeclarationNode, ProgramBody } from './ast';\nimport { directivesEnd, exportedName } from './ast';\nimport type { RewriteState } from './rewriter';\n\n/** Whether the module already binds `name` to the export of the same name from `module`. */\nexport function hasImport(ast: ProgramBody, module: string, name: string): boolean {\n return ast.body.some(node => {\n if (node.type !== 'ImportDeclaration') return false;\n const importNode = node as ImportDeclarationNode;\n if (importNode.source?.value !== module || importNode.importKind === 'type') return false;\n return (importNode.specifiers ?? []).some(\n specifier =>\n specifier.type === 'ImportSpecifier' &&\n specifier.importKind !== 'type' &&\n exportedName(specifier.imported) === name &&\n specifier.local?.name === name,\n );\n });\n}\n\n/** Adds an import statement after the hashbang and directives, once per module. */\nexport function addImport(state: RewriteState, ast: ProgramBody, statement: string): void {\n if (state.imports.has(statement)) return;\n state.imports.add(statement);\n\n const position = directivesEnd(ast);\n state.ms.appendLeft(position, position === 0 ? `${statement}\\n` : `\\n${statement}`);\n}\n"],"mappings":";;;AAKA,SAAgB,UAAU,KAAkB,QAAgB,MAAuB;CACjF,OAAO,IAAI,KAAK,MAAK,SAAQ;EAC3B,IAAI,KAAK,SAAS,qBAAqB,OAAO;EAC9C,MAAM,aAAa;EACnB,IAAI,WAAW,QAAQ,UAAU,UAAU,WAAW,eAAe,QAAQ,OAAO;EACpF,QAAQ,WAAW,cAAc,CAAC,EAAA,CAAG,MACnC,cACE,UAAU,SAAS,qBACnB,UAAU,eAAe,UACzB,aAAa,UAAU,QAAQ,MAAM,QACrC,UAAU,OAAO,SAAS,IAC9B;CACF,CAAC;AACH;;AAGA,SAAgB,UAAU,OAAqB,KAAkB,WAAyB;CACxF,IAAI,MAAM,QAAQ,IAAI,SAAS,GAAG;CAClC,MAAM,QAAQ,IAAI,SAAS;CAE3B,MAAM,WAAW,cAAc,GAAG;CAClC,MAAM,GAAG,WAAW,UAAU,aAAa,IAAI,GAAG,UAAU,MAAM,KAAK,WAAW;AACpF"}
@@ -0,0 +1,11 @@
1
+ import "../_virtual/_rolldown/runtime.js";
2
+ import { declarationOf, declaredNames, directivesEnd, exportedName, isAnonymousFunctionOrClass, isCallToMethod, isTypeOnlyExport, walk } from "./ast.js";
3
+ import { wrapArrayLiteral } from "./arrays.js";
4
+ import { findClassesExtending } from "./class-extends.js";
5
+ import { collectDeclaringStatements, collectManuallyWrappedLocals, collectTopLevelClasses, findNamedExports, wrapDefaultExport, wrapDefaultReexport, wrapDefaultSpecifier, wrapForeignExport, wrapInlineExport, wrapLocalBinding, wrapSpecifierExports } from "./exports.js";
6
+ import { addImport, hasImport } from "./imports.js";
7
+ import { DEFAULT_EXPORT, shapeFromAst, shapeFromSource } from "./module-shape.js";
8
+ import { bundlerParser, isParserLoading, langFromId, loadParser, parseModule } from "./parse.js";
9
+ import { createReport, formatReport } from "./report.js";
10
+ import { createCallSiteRewriter, createRewriteState } from "./rewriter.js";
11
+ export { DEFAULT_EXPORT, addImport, bundlerParser, collectDeclaringStatements, collectManuallyWrappedLocals, collectTopLevelClasses, createCallSiteRewriter, createReport, createRewriteState, declarationOf, declaredNames, directivesEnd, exportedName, findClassesExtending, findNamedExports, formatReport, hasImport, isAnonymousFunctionOrClass, isCallToMethod, isParserLoading, isTypeOnlyExport, langFromId, loadParser, parseModule, shapeFromAst, shapeFromSource, walk, wrapArrayLiteral, wrapDefaultExport, wrapDefaultReexport, wrapDefaultSpecifier, wrapForeignExport, wrapInlineExport, wrapLocalBinding, wrapSpecifierExports };
@@ -0,0 +1,118 @@
1
+ import { exportedName, isTypeOnlyExport } from "./ast.js";
2
+ import { langFromId, parseModule } from "./parse.js";
3
+ // src/rewriter/module-shape.ts
4
+ /** Binding name used for a module's default export. */
5
+ const DEFAULT_EXPORT = "default";
6
+ function emptyShape() {
7
+ return {
8
+ classes: /* @__PURE__ */ new Map(),
9
+ imports: /* @__PURE__ */ new Map(),
10
+ namespaces: /* @__PURE__ */ new Map(),
11
+ reexports: /* @__PURE__ */ new Map(),
12
+ localExports: /* @__PURE__ */ new Map(),
13
+ starExports: []
14
+ };
15
+ }
16
+ /** Collect the shape of a module from its parsed AST. */
17
+ function shapeFromAst(ast) {
18
+ const shape = emptyShape();
19
+ for (const node of ast.body) switch (node.type) {
20
+ case "ClassDeclaration":
21
+ addAstClass(shape, node);
22
+ break;
23
+ case "ImportDeclaration":
24
+ addAstImports(shape, node);
25
+ break;
26
+ case "ExportNamedDeclaration":
27
+ addAstNamedExport(shape, node);
28
+ break;
29
+ case "ExportDefaultDeclaration": {
30
+ const decl = node.declaration;
31
+ if (decl.type === "ClassDeclaration" || decl.type === "ClassExpression") {
32
+ shape.defaultExportIsClass = true;
33
+ shape.defaultExportSuper = superRefFromNode(decl.superClass);
34
+ addAstClass(shape, decl);
35
+ } else if (decl.type === "Identifier") shape.defaultExportName = decl.name;
36
+ break;
37
+ }
38
+ case "ExportAllDeclaration": {
39
+ const starNode = node;
40
+ const source = starNode.exported || isTypeOnlyExport(starNode) ? void 0 : asString(starNode.source?.value);
41
+ if (source) shape.starExports.push(source);
42
+ break;
43
+ }
44
+ }
45
+ return shape;
46
+ }
47
+ /**
48
+ * Extract the module shape from unprocessed source, such as a sibling module
49
+ * read off disk. Returns `undefined` when the source does not parse.
50
+ */
51
+ function shapeFromSource(code, id) {
52
+ const lang = langFromId(id);
53
+ for (const candidate of lang === "tsx" ? [lang] : [lang, "tsx"]) try {
54
+ return shapeFromAst(parseModule(code, id, candidate));
55
+ } catch {}
56
+ }
57
+ function superRefFromNode(superClass) {
58
+ if (!superClass) return void 0;
59
+ if (superClass.type === "Identifier") return {
60
+ kind: "identifier",
61
+ name: superClass.name
62
+ };
63
+ if (superClass.type === "MemberExpression") {
64
+ const member = superClass;
65
+ const object = member.object?.type === "Identifier" ? member.object.name : void 0;
66
+ const property = member.property?.name;
67
+ if (object && property) return {
68
+ kind: "member",
69
+ object,
70
+ property
71
+ };
72
+ }
73
+ }
74
+ function addAstClass(shape, classNode) {
75
+ if (classNode.id?.name) shape.classes.set(classNode.id.name, superRefFromNode(classNode.superClass));
76
+ }
77
+ function addAstImports(shape, node) {
78
+ const source = asString(node.source?.value);
79
+ if (!source || node.importKind === "type") return;
80
+ for (const specifier of node.specifiers ?? []) {
81
+ const local = specifier.local?.name;
82
+ if (!local || specifier.importKind === "type") continue;
83
+ const imported = exportedName(specifier.imported);
84
+ if (specifier.type === "ImportSpecifier" && imported) shape.imports.set(local, {
85
+ source,
86
+ imported
87
+ });
88
+ else if (specifier.type === "ImportDefaultSpecifier") shape.imports.set(local, {
89
+ source,
90
+ imported: DEFAULT_EXPORT
91
+ });
92
+ else if (specifier.type === "ImportNamespaceSpecifier") shape.namespaces.set(local, source);
93
+ }
94
+ }
95
+ function addAstNamedExport(shape, node) {
96
+ if (node.declaration?.type === "ClassDeclaration") {
97
+ addAstClass(shape, node.declaration);
98
+ return;
99
+ }
100
+ if (isTypeOnlyExport(node)) return;
101
+ const source = asString(node.source?.value);
102
+ for (const specifier of node.specifiers ?? []) {
103
+ const exported = exportedName(specifier.exported);
104
+ const local = exportedName(specifier.local);
105
+ if (!exported || !local || isTypeOnlyExport(specifier)) continue;
106
+ if (source) shape.reexports.set(exported, {
107
+ source,
108
+ imported: local
109
+ });
110
+ else shape.localExports.set(exported, local);
111
+ }
112
+ }
113
+ function asString(value) {
114
+ return typeof value === "string" && value ? value : void 0;
115
+ }
116
+ export { DEFAULT_EXPORT, shapeFromAst, shapeFromSource };
117
+
118
+ //# sourceMappingURL=module-shape.js.map