unplugin-zed-gpui 0.0.3 → 0.0.4

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.
package/dist/index.cjs CHANGED
@@ -85,6 +85,7 @@ const zedGpuiPlugin = (0, unplugin.createUnplugin)((options = {}) => {
85
85
  return {
86
86
  name: "unplugin-zed-gpui",
87
87
  transform(code, id) {
88
+ if (shouldProcessFile(id)) analyzeUsage(code, usedMethods, opts);
88
89
  if (hasElementPrototypeAssign(code)) {
89
90
  if (opts.debug) {
90
91
  console.log(`[zed-gpui] Processing element file: ${id}`);
@@ -92,7 +93,6 @@ const zedGpuiPlugin = (0, unplugin.createUnplugin)((options = {}) => {
92
93
  }
93
94
  return transformElementFile(code, usedMethods, opts);
94
95
  }
95
- if (shouldProcessFile(id)) analyzeUsage(code, usedMethods, opts);
96
96
  return null;
97
97
  },
98
98
  buildEnd() {
@@ -203,15 +203,20 @@ function addUsedMethod(usedMethods, elementType, methodName) {
203
203
  function inferElementType(node, bindings) {
204
204
  if (_babel_types.isIdentifier(node)) return bindings.get(node.name);
205
205
  if (_babel_types.isTSAsExpression(node) || _babel_types.isTSTypeAssertion(node)) return getElementTypeFromTsType(node.typeAnnotation) ?? inferElementType(node.expression, bindings);
206
+ if (_babel_types.isTSNonNullExpression(node)) return inferElementType(node.expression, bindings);
206
207
  if (_babel_types.isCallExpression(node)) {
208
+ if (_babel_types.isMemberExpression(node.callee)) {
209
+ if (_babel_types.isIdentifier(node.callee.object, { name: "document" }) && (_babel_types.isIdentifier(node.callee.property, { name: "querySelector" }) || _babel_types.isIdentifier(node.callee.property, { name: "getElementById" }))) {
210
+ const typeArg = node.typeArguments?.params[0];
211
+ return typeArg && _babel_types.isTSType(typeArg) ? getElementTypeFromTsType(typeArg) : "HTMLElement";
212
+ }
213
+ if (_babel_types.isIdentifier(node.callee.property, { name: "createElement" }) && _babel_types.isIdentifier(node.callee.object, { name: "document" }) && _babel_types.isStringLiteral(node.arguments[0])) return tagElementTypes[node.arguments[0].value] ?? "HTMLElement";
214
+ return inferElementType(node.callee.object, bindings);
215
+ }
207
216
  if (_babel_types.isIdentifier(node.callee)) {
208
217
  if (node.callee.name === "h" && _babel_types.isStringLiteral(node.arguments[0])) return tagElementTypes[node.arguments[0].value] ?? "HTMLElement";
209
218
  return factoryElementTypes[node.callee.name];
210
219
  }
211
- if (_babel_types.isMemberExpression(node.callee)) {
212
- if (_babel_types.isMemberExpression(node.callee) && _babel_types.isIdentifier(node.callee.property, { name: "createElement" }) && _babel_types.isIdentifier(node.callee.object, { name: "document" }) && _babel_types.isStringLiteral(node.arguments[0])) return tagElementTypes[node.arguments[0].value] ?? "HTMLElement";
213
- return inferElementType(node.callee.object, bindings);
214
- }
215
220
  }
216
221
  }
217
222
  function getElementTypeFromTypeAnnotation(node) {
@@ -234,20 +239,23 @@ function transformElementFile(code, usedMethods, options) {
234
239
  });
235
240
  let modified = false;
236
241
  const prototypeMethods = /* @__PURE__ */ new Map();
242
+ const assignments = [];
237
243
  for (const node of ast.program.body) {
238
- if (!_babel_types.isExpressionStatement(node) || !_babel_types.isCallExpression(node.expression)) continue;
239
- const targetPrototype = getAssignedPrototype(node.expression.arguments[0]);
240
- const sourceArg = unwrapExpression(node.expression.arguments[1]);
241
- if (!targetPrototype || !_babel_types.isObjectExpression(sourceArg)) continue;
242
- prototypeMethods.set(targetPrototype, new Set(sourceArg.properties.map((prop) => _babel_types.isObjectProperty(prop) || _babel_types.isObjectMethod(prop) ? getPropertyName(prop.key) : void 0).filter((methodName) => !!methodName)));
244
+ if (!_babel_types.isExpressionStatement(node)) continue;
245
+ for (const expression of _babel_types.isSequenceExpression(node.expression) ? node.expression.expressions : [node.expression]) {
246
+ if (!_babel_types.isCallExpression(expression) || !_babel_types.isMemberExpression(expression.callee) || !_babel_types.isIdentifier(expression.callee.object, { name: "Object" }) || !_babel_types.isIdentifier(expression.callee.property, { name: "assign" }) || expression.arguments.length < 2) continue;
247
+ const targetPrototype = getAssignedPrototype(expression.arguments[0]);
248
+ const sourceArg = unwrapExpression(expression.arguments[1]);
249
+ if (targetPrototype && _babel_types.isObjectExpression(sourceArg)) {
250
+ assignments.push({
251
+ targetPrototype,
252
+ sourceArg
253
+ });
254
+ prototypeMethods.set(targetPrototype, new Set(sourceArg.properties.map((prop) => _babel_types.isObjectProperty(prop) || _babel_types.isObjectMethod(prop) ? getPropertyName(prop.key) : void 0).filter((methodName) => !!methodName)));
255
+ }
256
+ }
243
257
  }
244
- for (const node of ast.program.body) {
245
- if (!_babel_types.isExpressionStatement(node) || !_babel_types.isCallExpression(node.expression)) continue;
246
- const callExpr = node.expression;
247
- if (!_babel_types.isMemberExpression(callExpr.callee) || !_babel_types.isIdentifier(callExpr.callee.object, { name: "Object" }) || !_babel_types.isIdentifier(callExpr.callee.property, { name: "assign" }) || callExpr.arguments.length < 2) continue;
248
- const targetPrototype = getAssignedPrototype(callExpr.arguments[0]);
249
- const sourceArg = unwrapExpression(callExpr.arguments[1]);
250
- if (!targetPrototype || !_babel_types.isObjectExpression(sourceArg)) continue;
258
+ for (const { targetPrototype, sourceArg } of assignments) {
251
259
  const originalProperties = sourceArg.properties.length;
252
260
  sourceArg.properties = sourceArg.properties.filter((prop) => {
253
261
  if (!_babel_types.isObjectProperty(prop) && !_babel_types.isObjectMethod(prop)) return true;
package/dist/index.mjs CHANGED
@@ -57,6 +57,7 @@ const zedGpuiPlugin = createUnplugin((options = {}) => {
57
57
  return {
58
58
  name: "unplugin-zed-gpui",
59
59
  transform(code, id) {
60
+ if (shouldProcessFile(id)) analyzeUsage(code, usedMethods, opts);
60
61
  if (hasElementPrototypeAssign(code)) {
61
62
  if (opts.debug) {
62
63
  console.log(`[zed-gpui] Processing element file: ${id}`);
@@ -64,7 +65,6 @@ const zedGpuiPlugin = createUnplugin((options = {}) => {
64
65
  }
65
66
  return transformElementFile(code, usedMethods, opts);
66
67
  }
67
- if (shouldProcessFile(id)) analyzeUsage(code, usedMethods, opts);
68
68
  return null;
69
69
  },
70
70
  buildEnd() {
@@ -175,15 +175,20 @@ function addUsedMethod(usedMethods, elementType, methodName) {
175
175
  function inferElementType(node, bindings) {
176
176
  if (t.isIdentifier(node)) return bindings.get(node.name);
177
177
  if (t.isTSAsExpression(node) || t.isTSTypeAssertion(node)) return getElementTypeFromTsType(node.typeAnnotation) ?? inferElementType(node.expression, bindings);
178
+ if (t.isTSNonNullExpression(node)) return inferElementType(node.expression, bindings);
178
179
  if (t.isCallExpression(node)) {
180
+ if (t.isMemberExpression(node.callee)) {
181
+ if (t.isIdentifier(node.callee.object, { name: "document" }) && (t.isIdentifier(node.callee.property, { name: "querySelector" }) || t.isIdentifier(node.callee.property, { name: "getElementById" }))) {
182
+ const typeArg = node.typeArguments?.params[0];
183
+ return typeArg && t.isTSType(typeArg) ? getElementTypeFromTsType(typeArg) : "HTMLElement";
184
+ }
185
+ if (t.isIdentifier(node.callee.property, { name: "createElement" }) && t.isIdentifier(node.callee.object, { name: "document" }) && t.isStringLiteral(node.arguments[0])) return tagElementTypes[node.arguments[0].value] ?? "HTMLElement";
186
+ return inferElementType(node.callee.object, bindings);
187
+ }
179
188
  if (t.isIdentifier(node.callee)) {
180
189
  if (node.callee.name === "h" && t.isStringLiteral(node.arguments[0])) return tagElementTypes[node.arguments[0].value] ?? "HTMLElement";
181
190
  return factoryElementTypes[node.callee.name];
182
191
  }
183
- if (t.isMemberExpression(node.callee)) {
184
- if (t.isMemberExpression(node.callee) && t.isIdentifier(node.callee.property, { name: "createElement" }) && t.isIdentifier(node.callee.object, { name: "document" }) && t.isStringLiteral(node.arguments[0])) return tagElementTypes[node.arguments[0].value] ?? "HTMLElement";
185
- return inferElementType(node.callee.object, bindings);
186
- }
187
192
  }
188
193
  }
189
194
  function getElementTypeFromTypeAnnotation(node) {
@@ -206,20 +211,23 @@ function transformElementFile(code, usedMethods, options) {
206
211
  });
207
212
  let modified = false;
208
213
  const prototypeMethods = /* @__PURE__ */ new Map();
214
+ const assignments = [];
209
215
  for (const node of ast.program.body) {
210
- if (!t.isExpressionStatement(node) || !t.isCallExpression(node.expression)) continue;
211
- const targetPrototype = getAssignedPrototype(node.expression.arguments[0]);
212
- const sourceArg = unwrapExpression(node.expression.arguments[1]);
213
- if (!targetPrototype || !t.isObjectExpression(sourceArg)) continue;
214
- prototypeMethods.set(targetPrototype, new Set(sourceArg.properties.map((prop) => t.isObjectProperty(prop) || t.isObjectMethod(prop) ? getPropertyName(prop.key) : void 0).filter((methodName) => !!methodName)));
216
+ if (!t.isExpressionStatement(node)) continue;
217
+ for (const expression of t.isSequenceExpression(node.expression) ? node.expression.expressions : [node.expression]) {
218
+ if (!t.isCallExpression(expression) || !t.isMemberExpression(expression.callee) || !t.isIdentifier(expression.callee.object, { name: "Object" }) || !t.isIdentifier(expression.callee.property, { name: "assign" }) || expression.arguments.length < 2) continue;
219
+ const targetPrototype = getAssignedPrototype(expression.arguments[0]);
220
+ const sourceArg = unwrapExpression(expression.arguments[1]);
221
+ if (targetPrototype && t.isObjectExpression(sourceArg)) {
222
+ assignments.push({
223
+ targetPrototype,
224
+ sourceArg
225
+ });
226
+ prototypeMethods.set(targetPrototype, new Set(sourceArg.properties.map((prop) => t.isObjectProperty(prop) || t.isObjectMethod(prop) ? getPropertyName(prop.key) : void 0).filter((methodName) => !!methodName)));
227
+ }
228
+ }
215
229
  }
216
- for (const node of ast.program.body) {
217
- if (!t.isExpressionStatement(node) || !t.isCallExpression(node.expression)) continue;
218
- const callExpr = node.expression;
219
- if (!t.isMemberExpression(callExpr.callee) || !t.isIdentifier(callExpr.callee.object, { name: "Object" }) || !t.isIdentifier(callExpr.callee.property, { name: "assign" }) || callExpr.arguments.length < 2) continue;
220
- const targetPrototype = getAssignedPrototype(callExpr.arguments[0]);
221
- const sourceArg = unwrapExpression(callExpr.arguments[1]);
222
- if (!targetPrototype || !t.isObjectExpression(sourceArg)) continue;
230
+ for (const { targetPrototype, sourceArg } of assignments) {
223
231
  const originalProperties = sourceArg.properties.length;
224
232
  sourceArg.properties = sourceArg.properties.filter((prop) => {
225
233
  if (!t.isObjectProperty(prop) && !t.isObjectMethod(prop)) return true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unplugin-zed-gpui",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "Unplugin for zed-gpui tree-shaking optimization - removes unused zed-gpui methods from bundle",
5
5
  "author": {
6
6
  "name": "Kasukabe Tsumugi",