@tanstack/router-plugin 1.41.0 → 1.43.5

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.
@@ -1,148 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const t = require("@babel/types");
4
- function _interopNamespaceDefault(e) {
5
- const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
6
- if (e) {
7
- for (const k in e) {
8
- if (k !== "default") {
9
- const d = Object.getOwnPropertyDescriptor(e, k);
10
- Object.defineProperty(n, k, d.get ? d : {
11
- enumerable: true,
12
- get: () => e[k]
13
- });
14
- }
15
- }
16
- }
17
- n.default = e;
18
- return Object.freeze(n);
19
- }
20
- const t__namespace = /* @__PURE__ */ _interopNamespaceDefault(t);
21
- const eliminateUnreferencedIdentifiers = (programPath, refs) => {
22
- let referencesRemovedInThisPass;
23
- const shouldBeRemoved = (ident) => {
24
- if (isIdentifierReferenced(ident)) return false;
25
- return true;
26
- };
27
- const sweepFunction = (path) => {
28
- const identifier = getIdentifier(path);
29
- if ((identifier == null ? void 0 : identifier.node) && shouldBeRemoved(identifier)) {
30
- ++referencesRemovedInThisPass;
31
- if (t__namespace.isAssignmentExpression(path.parentPath.node) || t__namespace.isVariableDeclarator(path.parentPath.node)) {
32
- path.parentPath.remove();
33
- } else {
34
- path.remove();
35
- }
36
- }
37
- };
38
- const sweepImport = (path) => {
39
- const local = path.get("local");
40
- if (shouldBeRemoved(local)) {
41
- ++referencesRemovedInThisPass;
42
- path.remove();
43
- if (path.parent.specifiers.length === 0) {
44
- path.parentPath.remove();
45
- }
46
- }
47
- };
48
- const handleObjectPattern = (pattern) => {
49
- const properties = pattern.get("properties");
50
- properties.forEach((property) => {
51
- if (property.node.type === "ObjectProperty") {
52
- const value = property.get("value");
53
- if (t__namespace.isIdentifier(value)) {
54
- if (shouldBeRemoved(value)) {
55
- property.remove();
56
- }
57
- } else if (t__namespace.isObjectPattern(value)) {
58
- handleObjectPattern(value);
59
- }
60
- } else if (t__namespace.isRestElement(property.node)) {
61
- const argument = property.get("argument");
62
- if (t__namespace.isIdentifier(argument) && shouldBeRemoved(argument)) {
63
- property.remove();
64
- }
65
- }
66
- });
67
- };
68
- do {
69
- referencesRemovedInThisPass = 0;
70
- programPath.scope.crawl();
71
- programPath.traverse({
72
- VariableDeclarator(path) {
73
- if (path.node.id.type === "Identifier") {
74
- const local = path.get("id");
75
- if (shouldBeRemoved(local)) {
76
- ++referencesRemovedInThisPass;
77
- path.remove();
78
- }
79
- } else if (path.node.id.type === "ObjectPattern") {
80
- handleObjectPattern(
81
- path.get("id")
82
- );
83
- } else if (path.node.id.type === "ArrayPattern") {
84
- const pattern = path.get("id");
85
- let hasRemoved = false;
86
- pattern.get("elements").forEach((element, index) => {
87
- let identifierPath;
88
- if (t__namespace.isIdentifier(element.node)) {
89
- identifierPath = element;
90
- } else if (t__namespace.isRestElement(element.node)) {
91
- identifierPath = element.get(
92
- "argument"
93
- );
94
- } else {
95
- return;
96
- }
97
- if (shouldBeRemoved(identifierPath)) {
98
- hasRemoved = true;
99
- pattern.node.elements[index] = null;
100
- }
101
- });
102
- if (hasRemoved && pattern.node.elements.every((element) => element === null)) {
103
- path.remove();
104
- ++referencesRemovedInThisPass;
105
- }
106
- }
107
- },
108
- FunctionDeclaration: sweepFunction,
109
- FunctionExpression: sweepFunction,
110
- ArrowFunctionExpression: sweepFunction,
111
- ImportSpecifier: sweepImport,
112
- ImportDefaultSpecifier: sweepImport,
113
- ImportNamespaceSpecifier: sweepImport
114
- });
115
- } while (referencesRemovedInThisPass);
116
- };
117
- function getIdentifier(path) {
118
- const parentPath = path.parentPath;
119
- if (parentPath.type === "VariableDeclarator") {
120
- const variablePath = parentPath;
121
- const name = variablePath.get("id");
122
- return name.node.type === "Identifier" ? name : null;
123
- }
124
- if (parentPath.type === "AssignmentExpression") {
125
- const variablePath = parentPath;
126
- const name = variablePath.get("left");
127
- return name.node.type === "Identifier" ? name : null;
128
- }
129
- if (path.node.type === "ArrowFunctionExpression") {
130
- return null;
131
- }
132
- if (path.node.type === "FunctionExpression") {
133
- return null;
134
- }
135
- return path.node.id && path.node.id.type === "Identifier" ? path.get("id") : null;
136
- }
137
- function isIdentifierReferenced(ident) {
138
- const binding = ident.scope.getBinding(ident.node.name);
139
- if (binding == null ? void 0 : binding.referenced) {
140
- if (binding.path.type === "FunctionDeclaration") {
141
- return !binding.constantViolations.concat(binding.referencePaths).every((ref) => ref.findParent((parent) => parent === binding.path));
142
- }
143
- return true;
144
- }
145
- return false;
146
- }
147
- exports.eliminateUnreferencedIdentifiers = eliminateUnreferencedIdentifiers;
148
- //# sourceMappingURL=eliminateUnreferencedIdentifiers.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"eliminateUnreferencedIdentifiers.cjs","sources":["../../src/eliminateUnreferencedIdentifiers.ts"],"sourcesContent":["// Copied from https://github.com/pcattori/vite-env-only/blob/main/src/dce.ts\n// Adapted with some minor changes for the purpose of this project\n\nimport * as t from '@babel/types'\nimport type { types as BabelTypes } from '@babel/core'\nimport type { NodePath } from '@babel/traverse'\n\ntype IdentifierPath = NodePath<BabelTypes.Identifier>\n\n/**\n * @param refs - If provided, only these identifiers will be considered for removal.\n */\nexport const eliminateUnreferencedIdentifiers = (\n programPath: NodePath<BabelTypes.Program>,\n refs?: Set<IdentifierPath>,\n) => {\n let referencesRemovedInThisPass: number\n\n const shouldBeRemoved = (ident: IdentifierPath) => {\n if (isIdentifierReferenced(ident)) return false\n if (!refs) return true\n return refs.has(ident)\n }\n\n const sweepFunction = (\n path: NodePath<\n | BabelTypes.FunctionDeclaration\n | BabelTypes.FunctionExpression\n | BabelTypes.ArrowFunctionExpression\n >,\n ) => {\n const identifier = getIdentifier(path)\n if (identifier?.node && shouldBeRemoved(identifier)) {\n ++referencesRemovedInThisPass\n\n if (\n t.isAssignmentExpression(path.parentPath.node) ||\n t.isVariableDeclarator(path.parentPath.node)\n ) {\n path.parentPath.remove()\n } else {\n path.remove()\n }\n }\n }\n\n const sweepImport = (\n path: NodePath<\n | BabelTypes.ImportSpecifier\n | BabelTypes.ImportDefaultSpecifier\n | BabelTypes.ImportNamespaceSpecifier\n >,\n ) => {\n const local = path.get('local')\n if (shouldBeRemoved(local)) {\n ++referencesRemovedInThisPass\n path.remove()\n if (\n (path.parent as BabelTypes.ImportDeclaration).specifiers.length === 0\n ) {\n path.parentPath.remove()\n }\n }\n }\n\n const handleObjectPattern = (pattern: NodePath<BabelTypes.ObjectPattern>) => {\n const properties = pattern.get('properties')\n properties.forEach((property) => {\n if (property.node.type === 'ObjectProperty') {\n const value = property.get('value') as any\n if (t.isIdentifier(value)) {\n if (shouldBeRemoved(value as any)) {\n property.remove()\n }\n } else if (t.isObjectPattern(value)) {\n handleObjectPattern(value as any)\n }\n } else if (t.isRestElement(property.node)) {\n const argument = property.get('argument')\n if (\n t.isIdentifier(argument as any) &&\n shouldBeRemoved(argument as NodePath<BabelTypes.Identifier>)\n ) {\n property.remove()\n }\n }\n })\n }\n\n // Traverse again to remove unused references. This happens at least once,\n // then repeats until no more references are removed.\n do {\n referencesRemovedInThisPass = 0\n\n programPath.scope.crawl()\n\n programPath.traverse({\n VariableDeclarator(path) {\n if (path.node.id.type === 'Identifier') {\n const local = path.get('id') as NodePath<BabelTypes.Identifier>\n if (shouldBeRemoved(local)) {\n ++referencesRemovedInThisPass\n path.remove()\n }\n } else if (path.node.id.type === 'ObjectPattern') {\n handleObjectPattern(\n path.get('id') as NodePath<BabelTypes.ObjectPattern>,\n )\n } else if (path.node.id.type === 'ArrayPattern') {\n const pattern = path.get('id') as NodePath<BabelTypes.ArrayPattern>\n\n let hasRemoved = false as boolean\n\n pattern.get('elements').forEach((element, index) => {\n // if (!element) return // Skip holes in the pattern\n\n let identifierPath: NodePath<BabelTypes.Identifier>\n\n if (t.isIdentifier(element.node)) {\n identifierPath = element as NodePath<BabelTypes.Identifier>\n } else if (t.isRestElement(element.node)) {\n identifierPath = element.get(\n 'argument',\n ) as NodePath<BabelTypes.Identifier>\n } else {\n // For now, ignore other types like AssignmentPattern\n return\n }\n\n if (shouldBeRemoved(identifierPath)) {\n hasRemoved = true\n pattern.node.elements[index] = null // Remove the element by setting it to null\n }\n })\n\n // If any elements were removed and no elements are left, remove the entire declaration\n if (\n hasRemoved &&\n pattern.node.elements.every((element) => element === null)\n ) {\n path.remove()\n ++referencesRemovedInThisPass\n }\n }\n },\n FunctionDeclaration: sweepFunction,\n FunctionExpression: sweepFunction,\n ArrowFunctionExpression: sweepFunction,\n ImportSpecifier: sweepImport,\n ImportDefaultSpecifier: sweepImport,\n ImportNamespaceSpecifier: sweepImport,\n })\n } while (referencesRemovedInThisPass)\n}\n\nfunction getIdentifier(\n path: NodePath<\n | BabelTypes.FunctionDeclaration\n | BabelTypes.FunctionExpression\n | BabelTypes.ArrowFunctionExpression\n >,\n): NodePath<BabelTypes.Identifier> | null {\n const parentPath = path.parentPath\n if (parentPath.type === 'VariableDeclarator') {\n const variablePath = parentPath as NodePath<BabelTypes.VariableDeclarator>\n const name = variablePath.get('id')\n return name.node.type === 'Identifier'\n ? (name as NodePath<BabelTypes.Identifier>)\n : null\n }\n\n if (parentPath.type === 'AssignmentExpression') {\n const variablePath = parentPath as NodePath<BabelTypes.AssignmentExpression>\n const name = variablePath.get('left')\n return name.node.type === 'Identifier'\n ? (name as NodePath<BabelTypes.Identifier>)\n : null\n }\n\n if (path.node.type === 'ArrowFunctionExpression') {\n return null\n }\n\n if (path.node.type === 'FunctionExpression') {\n return null\n }\n\n // eslint-disable-next-line ts/no-unnecessary-condition\n return path.node.id && path.node.id.type === 'Identifier'\n ? (path.get('id') as NodePath<BabelTypes.Identifier>)\n : null\n}\n\nfunction isIdentifierReferenced(\n ident: NodePath<BabelTypes.Identifier>,\n): boolean {\n const binding = ident.scope.getBinding(ident.node.name)\n if (binding?.referenced) {\n // Functions can reference themselves, so we need to check if there's a\n // binding outside the function scope or not.\n if (binding.path.type === 'FunctionDeclaration') {\n return !binding.constantViolations\n .concat(binding.referencePaths)\n // Check that every reference is contained within the function:\n .every((ref) => ref.findParent((parent) => parent === binding.path))\n }\n\n return true\n }\n return false\n}\n"],"names":["t"],"mappings":";;;;;;;;;;;;;;;;;;;;AAYa,MAAA,mCAAmC,CAC9C,aACA,SACG;AACC,MAAA;AAEE,QAAA,kBAAkB,CAAC,UAA0B;AAC7C,QAAA,uBAAuB,KAAK,EAAU,QAAA;AACxB,WAAA;AAAA,EACG;AAGjB,QAAA,gBAAgB,CACpB,SAKG;AACG,UAAA,aAAa,cAAc,IAAI;AACrC,SAAI,yCAAY,SAAQ,gBAAgB,UAAU,GAAG;AACjD,QAAA;AAGA,UAAAA,aAAE,uBAAuB,KAAK,WAAW,IAAI,KAC7CA,aAAE,qBAAqB,KAAK,WAAW,IAAI,GAC3C;AACA,aAAK,WAAW;MAAO,OAClB;AACL,aAAK,OAAO;AAAA,MACd;AAAA,IACF;AAAA,EAAA;AAGI,QAAA,cAAc,CAClB,SAKG;AACG,UAAA,QAAQ,KAAK,IAAI,OAAO;AAC1B,QAAA,gBAAgB,KAAK,GAAG;AACxB,QAAA;AACF,WAAK,OAAO;AACZ,UACG,KAAK,OAAwC,WAAW,WAAW,GACpE;AACA,aAAK,WAAW;MAClB;AAAA,IACF;AAAA,EAAA;AAGI,QAAA,sBAAsB,CAAC,YAAgD;AACrE,UAAA,aAAa,QAAQ,IAAI,YAAY;AAChC,eAAA,QAAQ,CAAC,aAAa;AAC3B,UAAA,SAAS,KAAK,SAAS,kBAAkB;AACrC,cAAA,QAAQ,SAAS,IAAI,OAAO;AAC9B,YAAAA,aAAE,aAAa,KAAK,GAAG;AACrB,cAAA,gBAAgB,KAAY,GAAG;AACjC,qBAAS,OAAO;AAAA,UAClB;AAAA,QACS,WAAAA,aAAE,gBAAgB,KAAK,GAAG;AACnC,8BAAoB,KAAY;AAAA,QAClC;AAAA,MACS,WAAAA,aAAE,cAAc,SAAS,IAAI,GAAG;AACnC,cAAA,WAAW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A,IAAA,CAC3B;AAAA,EACM,SAAA;AACX;AAEA,SAAS,cACP,MAKwC;AACxC,QAAM,aAAa,KAAK;AACpB,MAAA,WAAW,SAAS,sBAAsB;AAC5C,UAAM,eAAe;AACf,UAAA,OAAO,aAAa,IAAI,IAAI;AAClC,WAAO,KAAK,KAAK,SAAS,eACrB,OACD;AAAA,EACN;AAEI,MAAA,WAAW,SAAS,wBAAwB;AAC9C,UAAM,eAAe;AACf,UAAA,OAAO,aAAa,IAAI,MAAM;AACpC,WAAO,KAAK,KAAK,SAAS,eACrB,OACD;AAAA,EACN;AAEI,MAAA,KAAK,KAAK,SAAS,2BAA2B;AACzC,WAAA;AAAA,EACT;AAEI,MAAA,KAAK,KAAK,SAAS,sBAAsB;AACpC,WAAA;AAAA,EACT;AAGO,SAAA,KAAK,KAAK,MAAM,KAAK,KAAK,GAAG,SAAS,eACxC,KAAK,IAAI,IAAI,IACd;AACN;AAEA,SAAS,uBACP,OACS;AACT,QAAM,UAAU,MAAM,MAAM,WAAW,MAAM,KAAK,IAAI;AACtD,MAAI,mCAAS,YAAY;AAGnB,QAAA,QAAQ,KAAK,SAAS,uBAAuB;AAC/C,aAAO,CAAC,QAAQ,mBACb,OAAO,QAAQ,cAAc,EAE7B,MAAM,CAAC,QAAQ,IAAI,WAAW,CAAC,WAAW,WAAW,QAAQ,IAAI,CAAC;AAAA,IACvE;AAEO,WAAA;AAAA,EACT;AACO,SAAA;AACT;;"}
@@ -1,9 +0,0 @@
1
- import { types as BabelTypes } from '@babel/core';
2
- import { NodePath } from '@babel/traverse';
3
-
4
- type IdentifierPath = NodePath<BabelTypes.Identifier>;
5
- /**
6
- * @param refs - If provided, only these identifiers will be considered for removal.
7
- */
8
- export declare const eliminateUnreferencedIdentifiers: (programPath: NodePath<BabelTypes.Program>, refs?: Set<IdentifierPath>) => void;
9
- export {};
@@ -1,9 +0,0 @@
1
- import { types as BabelTypes } from '@babel/core';
2
- import { NodePath } from '@babel/traverse';
3
-
4
- type IdentifierPath = NodePath<BabelTypes.Identifier>;
5
- /**
6
- * @param refs - If provided, only these identifiers will be considered for removal.
7
- */
8
- export declare const eliminateUnreferencedIdentifiers: (programPath: NodePath<BabelTypes.Program>, refs?: Set<IdentifierPath>) => void;
9
- export {};
@@ -1,131 +0,0 @@
1
- import * as t from "@babel/types";
2
- const eliminateUnreferencedIdentifiers = (programPath, refs) => {
3
- let referencesRemovedInThisPass;
4
- const shouldBeRemoved = (ident) => {
5
- if (isIdentifierReferenced(ident)) return false;
6
- return true;
7
- };
8
- const sweepFunction = (path) => {
9
- const identifier = getIdentifier(path);
10
- if ((identifier == null ? void 0 : identifier.node) && shouldBeRemoved(identifier)) {
11
- ++referencesRemovedInThisPass;
12
- if (t.isAssignmentExpression(path.parentPath.node) || t.isVariableDeclarator(path.parentPath.node)) {
13
- path.parentPath.remove();
14
- } else {
15
- path.remove();
16
- }
17
- }
18
- };
19
- const sweepImport = (path) => {
20
- const local = path.get("local");
21
- if (shouldBeRemoved(local)) {
22
- ++referencesRemovedInThisPass;
23
- path.remove();
24
- if (path.parent.specifiers.length === 0) {
25
- path.parentPath.remove();
26
- }
27
- }
28
- };
29
- const handleObjectPattern = (pattern) => {
30
- const properties = pattern.get("properties");
31
- properties.forEach((property) => {
32
- if (property.node.type === "ObjectProperty") {
33
- const value = property.get("value");
34
- if (t.isIdentifier(value)) {
35
- if (shouldBeRemoved(value)) {
36
- property.remove();
37
- }
38
- } else if (t.isObjectPattern(value)) {
39
- handleObjectPattern(value);
40
- }
41
- } else if (t.isRestElement(property.node)) {
42
- const argument = property.get("argument");
43
- if (t.isIdentifier(argument) && shouldBeRemoved(argument)) {
44
- property.remove();
45
- }
46
- }
47
- });
48
- };
49
- do {
50
- referencesRemovedInThisPass = 0;
51
- programPath.scope.crawl();
52
- programPath.traverse({
53
- VariableDeclarator(path) {
54
- if (path.node.id.type === "Identifier") {
55
- const local = path.get("id");
56
- if (shouldBeRemoved(local)) {
57
- ++referencesRemovedInThisPass;
58
- path.remove();
59
- }
60
- } else if (path.node.id.type === "ObjectPattern") {
61
- handleObjectPattern(
62
- path.get("id")
63
- );
64
- } else if (path.node.id.type === "ArrayPattern") {
65
- const pattern = path.get("id");
66
- let hasRemoved = false;
67
- pattern.get("elements").forEach((element, index) => {
68
- let identifierPath;
69
- if (t.isIdentifier(element.node)) {
70
- identifierPath = element;
71
- } else if (t.isRestElement(element.node)) {
72
- identifierPath = element.get(
73
- "argument"
74
- );
75
- } else {
76
- return;
77
- }
78
- if (shouldBeRemoved(identifierPath)) {
79
- hasRemoved = true;
80
- pattern.node.elements[index] = null;
81
- }
82
- });
83
- if (hasRemoved && pattern.node.elements.every((element) => element === null)) {
84
- path.remove();
85
- ++referencesRemovedInThisPass;
86
- }
87
- }
88
- },
89
- FunctionDeclaration: sweepFunction,
90
- FunctionExpression: sweepFunction,
91
- ArrowFunctionExpression: sweepFunction,
92
- ImportSpecifier: sweepImport,
93
- ImportDefaultSpecifier: sweepImport,
94
- ImportNamespaceSpecifier: sweepImport
95
- });
96
- } while (referencesRemovedInThisPass);
97
- };
98
- function getIdentifier(path) {
99
- const parentPath = path.parentPath;
100
- if (parentPath.type === "VariableDeclarator") {
101
- const variablePath = parentPath;
102
- const name = variablePath.get("id");
103
- return name.node.type === "Identifier" ? name : null;
104
- }
105
- if (parentPath.type === "AssignmentExpression") {
106
- const variablePath = parentPath;
107
- const name = variablePath.get("left");
108
- return name.node.type === "Identifier" ? name : null;
109
- }
110
- if (path.node.type === "ArrowFunctionExpression") {
111
- return null;
112
- }
113
- if (path.node.type === "FunctionExpression") {
114
- return null;
115
- }
116
- return path.node.id && path.node.id.type === "Identifier" ? path.get("id") : null;
117
- }
118
- function isIdentifierReferenced(ident) {
119
- const binding = ident.scope.getBinding(ident.node.name);
120
- if (binding == null ? void 0 : binding.referenced) {
121
- if (binding.path.type === "FunctionDeclaration") {
122
- return !binding.constantViolations.concat(binding.referencePaths).every((ref) => ref.findParent((parent) => parent === binding.path));
123
- }
124
- return true;
125
- }
126
- return false;
127
- }
128
- export {
129
- eliminateUnreferencedIdentifiers
130
- };
131
- //# sourceMappingURL=eliminateUnreferencedIdentifiers.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"eliminateUnreferencedIdentifiers.js","sources":["../../src/eliminateUnreferencedIdentifiers.ts"],"sourcesContent":["// Copied from https://github.com/pcattori/vite-env-only/blob/main/src/dce.ts\n// Adapted with some minor changes for the purpose of this project\n\nimport * as t from '@babel/types'\nimport type { types as BabelTypes } from '@babel/core'\nimport type { NodePath } from '@babel/traverse'\n\ntype IdentifierPath = NodePath<BabelTypes.Identifier>\n\n/**\n * @param refs - If provided, only these identifiers will be considered for removal.\n */\nexport const eliminateUnreferencedIdentifiers = (\n programPath: NodePath<BabelTypes.Program>,\n refs?: Set<IdentifierPath>,\n) => {\n let referencesRemovedInThisPass: number\n\n const shouldBeRemoved = (ident: IdentifierPath) => {\n if (isIdentifierReferenced(ident)) return false\n if (!refs) return true\n return refs.has(ident)\n }\n\n const sweepFunction = (\n path: NodePath<\n | BabelTypes.FunctionDeclaration\n | BabelTypes.FunctionExpression\n | BabelTypes.ArrowFunctionExpression\n >,\n ) => {\n const identifier = getIdentifier(path)\n if (identifier?.node && shouldBeRemoved(identifier)) {\n ++referencesRemovedInThisPass\n\n if (\n t.isAssignmentExpression(path.parentPath.node) ||\n t.isVariableDeclarator(path.parentPath.node)\n ) {\n path.parentPath.remove()\n } else {\n path.remove()\n }\n }\n }\n\n const sweepImport = (\n path: NodePath<\n | BabelTypes.ImportSpecifier\n | BabelTypes.ImportDefaultSpecifier\n | BabelTypes.ImportNamespaceSpecifier\n >,\n ) => {\n const local = path.get('local')\n if (shouldBeRemoved(local)) {\n ++referencesRemovedInThisPass\n path.remove()\n if (\n (path.parent as BabelTypes.ImportDeclaration).specifiers.length === 0\n ) {\n path.parentPath.remove()\n }\n }\n }\n\n const handleObjectPattern = (pattern: NodePath<BabelTypes.ObjectPattern>) => {\n const properties = pattern.get('properties')\n properties.forEach((property) => {\n if (property.node.type === 'ObjectProperty') {\n const value = property.get('value') as any\n if (t.isIdentifier(value)) {\n if (shouldBeRemoved(value as any)) {\n property.remove()\n }\n } else if (t.isObjectPattern(value)) {\n handleObjectPattern(value as any)\n }\n } else if (t.isRestElement(property.node)) {\n const argument = property.get('argument')\n if (\n t.isIdentifier(argument as any) &&\n shouldBeRemoved(argument as NodePath<BabelTypes.Identifier>)\n ) {\n property.remove()\n }\n }\n })\n }\n\n // Traverse again to remove unused references. This happens at least once,\n // then repeats until no more references are removed.\n do {\n referencesRemovedInThisPass = 0\n\n programPath.scope.crawl()\n\n programPath.traverse({\n VariableDeclarator(path) {\n if (path.node.id.type === 'Identifier') {\n const local = path.get('id') as NodePath<BabelTypes.Identifier>\n if (shouldBeRemoved(local)) {\n ++referencesRemovedInThisPass\n path.remove()\n }\n } else if (path.node.id.type === 'ObjectPattern') {\n handleObjectPattern(\n path.get('id') as NodePath<BabelTypes.ObjectPattern>,\n )\n } else if (path.node.id.type === 'ArrayPattern') {\n const pattern = path.get('id') as NodePath<BabelTypes.ArrayPattern>\n\n let hasRemoved = false as boolean\n\n pattern.get('elements').forEach((element, index) => {\n // if (!element) return // Skip holes in the pattern\n\n let identifierPath: NodePath<BabelTypes.Identifier>\n\n if (t.isIdentifier(element.node)) {\n identifierPath = element as NodePath<BabelTypes.Identifier>\n } else if (t.isRestElement(element.node)) {\n identifierPath = element.get(\n 'argument',\n ) as NodePath<BabelTypes.Identifier>\n } else {\n // For now, ignore other types like AssignmentPattern\n return\n }\n\n if (shouldBeRemoved(identifierPath)) {\n hasRemoved = true\n pattern.node.elements[index] = null // Remove the element by setting it to null\n }\n })\n\n // If any elements were removed and no elements are left, remove the entire declaration\n if (\n hasRemoved &&\n pattern.node.elements.every((element) => element === null)\n ) {\n path.remove()\n ++referencesRemovedInThisPass\n }\n }\n },\n FunctionDeclaration: sweepFunction,\n FunctionExpression: sweepFunction,\n ArrowFunctionExpression: sweepFunction,\n ImportSpecifier: sweepImport,\n ImportDefaultSpecifier: sweepImport,\n ImportNamespaceSpecifier: sweepImport,\n })\n } while (referencesRemovedInThisPass)\n}\n\nfunction getIdentifier(\n path: NodePath<\n | BabelTypes.FunctionDeclaration\n | BabelTypes.FunctionExpression\n | BabelTypes.ArrowFunctionExpression\n >,\n): NodePath<BabelTypes.Identifier> | null {\n const parentPath = path.parentPath\n if (parentPath.type === 'VariableDeclarator') {\n const variablePath = parentPath as NodePath<BabelTypes.VariableDeclarator>\n const name = variablePath.get('id')\n return name.node.type === 'Identifier'\n ? (name as NodePath<BabelTypes.Identifier>)\n : null\n }\n\n if (parentPath.type === 'AssignmentExpression') {\n const variablePath = parentPath as NodePath<BabelTypes.AssignmentExpression>\n const name = variablePath.get('left')\n return name.node.type === 'Identifier'\n ? (name as NodePath<BabelTypes.Identifier>)\n : null\n }\n\n if (path.node.type === 'ArrowFunctionExpression') {\n return null\n }\n\n if (path.node.type === 'FunctionExpression') {\n return null\n }\n\n // eslint-disable-next-line ts/no-unnecessary-condition\n return path.node.id && path.node.id.type === 'Identifier'\n ? (path.get('id') as NodePath<BabelTypes.Identifier>)\n : null\n}\n\nfunction isIdentifierReferenced(\n ident: NodePath<BabelTypes.Identifier>,\n): boolean {\n const binding = ident.scope.getBinding(ident.node.name)\n if (binding?.referenced) {\n // Functions can reference themselves, so we need to check if there's a\n // binding outside the function scope or not.\n if (binding.path.type === 'FunctionDeclaration') {\n return !binding.constantViolations\n .concat(binding.referencePaths)\n // Check that every reference is contained within the function:\n .every((ref) => ref.findParent((parent) => parent === binding.path))\n }\n\n return true\n }\n return false\n}\n"],"names":[],"mappings":";AAYa,MAAA,mCAAmC,CAC9C,aACA,SACG;AACC,MAAA;AAEE,QAAA,kBAAkB,CAAC,UAA0B;AAC7C,QAAA,uBAAuB,KAAK,EAAU,QAAA;AACxB,WAAA;AAAA,EACG;AAGjB,QAAA,gBAAgB,CACpB,SAKG;AACG,UAAA,aAAa,cAAc,IAAI;AACrC,SAAI,yCAAY,SAAQ,gB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B;AACzC,WAAA;AAAA,EACT;AAEI,MAAA,KAAK,KAAK,SAAS,sBAAsB;AACpC,WAAA;AAAA,EACT;AAGO,SAAA,KAAK,KAAK,MAAM,KAAK,KAAK,GAAG,SAAS,eACxC,KAAK,IAAI,IAAI,IACd;AACN;AAEA,SAAS,uBACP,OACS;AACT,QAAM,UAAU,MAAM,MAAM,WAAW,MAAM,KAAK,IAAI;AACtD,MAAI,mCAAS,YAAY;AAGnB,QAAA,QAAQ,KAAK,SAAS,uBAAuB;AAC/C,aAAO,CAAC,QAAQ,mBACb,OAAO,QAAQ,cAAc,EAE7B,MAAM,CAAC,QAAQ,IAAI,WAAW,CAAC,WAAW,WAAW,QAAQ,IAAI,CAAC;AAAA,IACvE;AAEO,WAAA;AAAA,EACT;AACO,SAAA;AACT;"}
@@ -1,211 +0,0 @@
1
- // Copied from https://github.com/pcattori/vite-env-only/blob/main/src/dce.ts
2
- // Adapted with some minor changes for the purpose of this project
3
-
4
- import * as t from '@babel/types'
5
- import type { types as BabelTypes } from '@babel/core'
6
- import type { NodePath } from '@babel/traverse'
7
-
8
- type IdentifierPath = NodePath<BabelTypes.Identifier>
9
-
10
- /**
11
- * @param refs - If provided, only these identifiers will be considered for removal.
12
- */
13
- export const eliminateUnreferencedIdentifiers = (
14
- programPath: NodePath<BabelTypes.Program>,
15
- refs?: Set<IdentifierPath>,
16
- ) => {
17
- let referencesRemovedInThisPass: number
18
-
19
- const shouldBeRemoved = (ident: IdentifierPath) => {
20
- if (isIdentifierReferenced(ident)) return false
21
- if (!refs) return true
22
- return refs.has(ident)
23
- }
24
-
25
- const sweepFunction = (
26
- path: NodePath<
27
- | BabelTypes.FunctionDeclaration
28
- | BabelTypes.FunctionExpression
29
- | BabelTypes.ArrowFunctionExpression
30
- >,
31
- ) => {
32
- const identifier = getIdentifier(path)
33
- if (identifier?.node && shouldBeRemoved(identifier)) {
34
- ++referencesRemovedInThisPass
35
-
36
- if (
37
- t.isAssignmentExpression(path.parentPath.node) ||
38
- t.isVariableDeclarator(path.parentPath.node)
39
- ) {
40
- path.parentPath.remove()
41
- } else {
42
- path.remove()
43
- }
44
- }
45
- }
46
-
47
- const sweepImport = (
48
- path: NodePath<
49
- | BabelTypes.ImportSpecifier
50
- | BabelTypes.ImportDefaultSpecifier
51
- | BabelTypes.ImportNamespaceSpecifier
52
- >,
53
- ) => {
54
- const local = path.get('local')
55
- if (shouldBeRemoved(local)) {
56
- ++referencesRemovedInThisPass
57
- path.remove()
58
- if (
59
- (path.parent as BabelTypes.ImportDeclaration).specifiers.length === 0
60
- ) {
61
- path.parentPath.remove()
62
- }
63
- }
64
- }
65
-
66
- const handleObjectPattern = (pattern: NodePath<BabelTypes.ObjectPattern>) => {
67
- const properties = pattern.get('properties')
68
- properties.forEach((property) => {
69
- if (property.node.type === 'ObjectProperty') {
70
- const value = property.get('value') as any
71
- if (t.isIdentifier(value)) {
72
- if (shouldBeRemoved(value as any)) {
73
- property.remove()
74
- }
75
- } else if (t.isObjectPattern(value)) {
76
- handleObjectPattern(value as any)
77
- }
78
- } else if (t.isRestElement(property.node)) {
79
- const argument = property.get('argument')
80
- if (
81
- t.isIdentifier(argument as any) &&
82
- shouldBeRemoved(argument as NodePath<BabelTypes.Identifier>)
83
- ) {
84
- property.remove()
85
- }
86
- }
87
- })
88
- }
89
-
90
- // Traverse again to remove unused references. This happens at least once,
91
- // then repeats until no more references are removed.
92
- do {
93
- referencesRemovedInThisPass = 0
94
-
95
- programPath.scope.crawl()
96
-
97
- programPath.traverse({
98
- VariableDeclarator(path) {
99
- if (path.node.id.type === 'Identifier') {
100
- const local = path.get('id') as NodePath<BabelTypes.Identifier>
101
- if (shouldBeRemoved(local)) {
102
- ++referencesRemovedInThisPass
103
- path.remove()
104
- }
105
- } else if (path.node.id.type === 'ObjectPattern') {
106
- handleObjectPattern(
107
- path.get('id') as NodePath<BabelTypes.ObjectPattern>,
108
- )
109
- } else if (path.node.id.type === 'ArrayPattern') {
110
- const pattern = path.get('id') as NodePath<BabelTypes.ArrayPattern>
111
-
112
- let hasRemoved = false as boolean
113
-
114
- pattern.get('elements').forEach((element, index) => {
115
- // if (!element) return // Skip holes in the pattern
116
-
117
- let identifierPath: NodePath<BabelTypes.Identifier>
118
-
119
- if (t.isIdentifier(element.node)) {
120
- identifierPath = element as NodePath<BabelTypes.Identifier>
121
- } else if (t.isRestElement(element.node)) {
122
- identifierPath = element.get(
123
- 'argument',
124
- ) as NodePath<BabelTypes.Identifier>
125
- } else {
126
- // For now, ignore other types like AssignmentPattern
127
- return
128
- }
129
-
130
- if (shouldBeRemoved(identifierPath)) {
131
- hasRemoved = true
132
- pattern.node.elements[index] = null // Remove the element by setting it to null
133
- }
134
- })
135
-
136
- // If any elements were removed and no elements are left, remove the entire declaration
137
- if (
138
- hasRemoved &&
139
- pattern.node.elements.every((element) => element === null)
140
- ) {
141
- path.remove()
142
- ++referencesRemovedInThisPass
143
- }
144
- }
145
- },
146
- FunctionDeclaration: sweepFunction,
147
- FunctionExpression: sweepFunction,
148
- ArrowFunctionExpression: sweepFunction,
149
- ImportSpecifier: sweepImport,
150
- ImportDefaultSpecifier: sweepImport,
151
- ImportNamespaceSpecifier: sweepImport,
152
- })
153
- } while (referencesRemovedInThisPass)
154
- }
155
-
156
- function getIdentifier(
157
- path: NodePath<
158
- | BabelTypes.FunctionDeclaration
159
- | BabelTypes.FunctionExpression
160
- | BabelTypes.ArrowFunctionExpression
161
- >,
162
- ): NodePath<BabelTypes.Identifier> | null {
163
- const parentPath = path.parentPath
164
- if (parentPath.type === 'VariableDeclarator') {
165
- const variablePath = parentPath as NodePath<BabelTypes.VariableDeclarator>
166
- const name = variablePath.get('id')
167
- return name.node.type === 'Identifier'
168
- ? (name as NodePath<BabelTypes.Identifier>)
169
- : null
170
- }
171
-
172
- if (parentPath.type === 'AssignmentExpression') {
173
- const variablePath = parentPath as NodePath<BabelTypes.AssignmentExpression>
174
- const name = variablePath.get('left')
175
- return name.node.type === 'Identifier'
176
- ? (name as NodePath<BabelTypes.Identifier>)
177
- : null
178
- }
179
-
180
- if (path.node.type === 'ArrowFunctionExpression') {
181
- return null
182
- }
183
-
184
- if (path.node.type === 'FunctionExpression') {
185
- return null
186
- }
187
-
188
- // eslint-disable-next-line ts/no-unnecessary-condition
189
- return path.node.id && path.node.id.type === 'Identifier'
190
- ? (path.get('id') as NodePath<BabelTypes.Identifier>)
191
- : null
192
- }
193
-
194
- function isIdentifierReferenced(
195
- ident: NodePath<BabelTypes.Identifier>,
196
- ): boolean {
197
- const binding = ident.scope.getBinding(ident.node.name)
198
- if (binding?.referenced) {
199
- // Functions can reference themselves, so we need to check if there's a
200
- // binding outside the function scope or not.
201
- if (binding.path.type === 'FunctionDeclaration') {
202
- return !binding.constantViolations
203
- .concat(binding.referencePaths)
204
- // Check that every reference is contained within the function:
205
- .every((ref) => ref.findParent((parent) => parent === binding.path))
206
- }
207
-
208
- return true
209
- }
210
- return false
211
- }