unplugin-zed-gpui 0.0.4 → 0.0.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.
- package/dist/index.cjs +22 -8
- package/dist/index.d.ts +1 -1
- package/dist/index.mjs +22 -8
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -86,7 +86,7 @@ const zedGpuiPlugin = (0, unplugin.createUnplugin)((options = {}) => {
|
|
|
86
86
|
name: "unplugin-zed-gpui",
|
|
87
87
|
transform(code, id) {
|
|
88
88
|
if (shouldProcessFile(id)) analyzeUsage(code, usedMethods, opts);
|
|
89
|
-
if (
|
|
89
|
+
if (hasPrototypeEnhancement(code)) {
|
|
90
90
|
if (opts.debug) {
|
|
91
91
|
console.log(`[zed-gpui] Processing element file: ${id}`);
|
|
92
92
|
console.log(`[zed-gpui] Used methods:`, formatUsedMethods(usedMethods));
|
|
@@ -139,8 +139,8 @@ function analyzeUsage(code, usedMethods, options) {
|
|
|
139
139
|
function hasZedGpuiUsage(code) {
|
|
140
140
|
return /\.\s*([a-z_][a-zA-Z0-9_]*)\s*\(/.test(code);
|
|
141
141
|
}
|
|
142
|
-
function
|
|
143
|
-
return code.includes("
|
|
142
|
+
function hasPrototypeEnhancement(code) {
|
|
143
|
+
return code.includes("$_(") && /HTML[A-Za-z]*Element/.test(code);
|
|
144
144
|
}
|
|
145
145
|
/**
|
|
146
146
|
* Recursively scan AST nodes for method calls
|
|
@@ -243,13 +243,14 @@ function transformElementFile(code, usedMethods, options) {
|
|
|
243
243
|
for (const node of ast.program.body) {
|
|
244
244
|
if (!_babel_types.isExpressionStatement(node)) continue;
|
|
245
245
|
for (const expression of _babel_types.isSequenceExpression(node.expression) ? node.expression.expressions : [node.expression]) {
|
|
246
|
-
if (!_babel_types.isCallExpression(expression) || !_babel_types.
|
|
247
|
-
const targetPrototype =
|
|
246
|
+
if (!_babel_types.isCallExpression(expression) || !_babel_types.isIdentifier(expression.callee, { name: "$_" }) || expression.arguments.length < 2) continue;
|
|
247
|
+
const targetPrototype = getEnhancedPrototype(expression.arguments[0]);
|
|
248
248
|
const sourceArg = unwrapExpression(expression.arguments[1]);
|
|
249
249
|
if (targetPrototype && _babel_types.isObjectExpression(sourceArg)) {
|
|
250
250
|
assignments.push({
|
|
251
251
|
targetPrototype,
|
|
252
|
-
sourceArg
|
|
252
|
+
sourceArg,
|
|
253
|
+
call: expression
|
|
253
254
|
});
|
|
254
255
|
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
|
}
|
|
@@ -269,6 +270,19 @@ function transformElementFile(code, usedMethods, options) {
|
|
|
269
270
|
if (options.debug) console.log(`[zed-gpui] Removed ${originalProperties - sourceArg.properties.length} unused methods from ${targetPrototype}`);
|
|
270
271
|
}
|
|
271
272
|
}
|
|
273
|
+
ast.program.body = ast.program.body.filter((node) => {
|
|
274
|
+
if (!_babel_types.isExpressionStatement(node)) return true;
|
|
275
|
+
if (_babel_types.isSequenceExpression(node.expression)) {
|
|
276
|
+
const expressions = node.expression.expressions.filter((expression) => !assignments.some(({ call, sourceArg }) => call === expression && sourceArg.properties.length === 0));
|
|
277
|
+
if (expressions.length !== node.expression.expressions.length) modified = true;
|
|
278
|
+
if (expressions.length === 0) return false;
|
|
279
|
+
node.expression = expressions.length === 1 ? expressions[0] : _babel_types.sequenceExpression(expressions);
|
|
280
|
+
return true;
|
|
281
|
+
}
|
|
282
|
+
const shouldRemove = assignments.some(({ call, sourceArg }) => call === node.expression && sourceArg.properties.length === 0);
|
|
283
|
+
if (shouldRemove) modified = true;
|
|
284
|
+
return !shouldRemove;
|
|
285
|
+
});
|
|
272
286
|
if (modified) return { code: (0, _babel_generator.default)(ast, {}, code).code };
|
|
273
287
|
return null;
|
|
274
288
|
} catch (error) {
|
|
@@ -276,8 +290,8 @@ function transformElementFile(code, usedMethods, options) {
|
|
|
276
290
|
return null;
|
|
277
291
|
}
|
|
278
292
|
}
|
|
279
|
-
function
|
|
280
|
-
return _babel_types.
|
|
293
|
+
function getEnhancedPrototype(node) {
|
|
294
|
+
return _babel_types.isIdentifier(node) && isElementType(node.name) ? node.name : void 0;
|
|
281
295
|
}
|
|
282
296
|
function unwrapExpression(node) {
|
|
283
297
|
while (_babel_types.isTSAsExpression(node) || _babel_types.isTSTypeAssertion(node) || _babel_types.isTSNonNullExpression(node)) node = node.expression;
|
package/dist/index.d.ts
CHANGED
|
@@ -20,6 +20,6 @@ interface PluginOptions {
|
|
|
20
20
|
* Unplugin for zed-gpui tree-shaking optimization
|
|
21
21
|
* Removes unused zed-gpui methods from the bundle
|
|
22
22
|
*/
|
|
23
|
-
declare const zedGpuiPlugin: import("unplugin").UnpluginInstance<PluginOptions, boolean>;
|
|
23
|
+
declare const zedGpuiPlugin: import("unplugin").UnpluginInstance<PluginOptions | undefined, boolean>;
|
|
24
24
|
//#endregion
|
|
25
25
|
export { PluginOptions, zedGpuiPlugin as default, zedGpuiPlugin };
|
package/dist/index.mjs
CHANGED
|
@@ -58,7 +58,7 @@ const zedGpuiPlugin = createUnplugin((options = {}) => {
|
|
|
58
58
|
name: "unplugin-zed-gpui",
|
|
59
59
|
transform(code, id) {
|
|
60
60
|
if (shouldProcessFile(id)) analyzeUsage(code, usedMethods, opts);
|
|
61
|
-
if (
|
|
61
|
+
if (hasPrototypeEnhancement(code)) {
|
|
62
62
|
if (opts.debug) {
|
|
63
63
|
console.log(`[zed-gpui] Processing element file: ${id}`);
|
|
64
64
|
console.log(`[zed-gpui] Used methods:`, formatUsedMethods(usedMethods));
|
|
@@ -111,8 +111,8 @@ function analyzeUsage(code, usedMethods, options) {
|
|
|
111
111
|
function hasZedGpuiUsage(code) {
|
|
112
112
|
return /\.\s*([a-z_][a-zA-Z0-9_]*)\s*\(/.test(code);
|
|
113
113
|
}
|
|
114
|
-
function
|
|
115
|
-
return code.includes("
|
|
114
|
+
function hasPrototypeEnhancement(code) {
|
|
115
|
+
return code.includes("$_(") && /HTML[A-Za-z]*Element/.test(code);
|
|
116
116
|
}
|
|
117
117
|
/**
|
|
118
118
|
* Recursively scan AST nodes for method calls
|
|
@@ -215,13 +215,14 @@ function transformElementFile(code, usedMethods, options) {
|
|
|
215
215
|
for (const node of ast.program.body) {
|
|
216
216
|
if (!t.isExpressionStatement(node)) continue;
|
|
217
217
|
for (const expression of t.isSequenceExpression(node.expression) ? node.expression.expressions : [node.expression]) {
|
|
218
|
-
if (!t.isCallExpression(expression) || !t.
|
|
219
|
-
const targetPrototype =
|
|
218
|
+
if (!t.isCallExpression(expression) || !t.isIdentifier(expression.callee, { name: "$_" }) || expression.arguments.length < 2) continue;
|
|
219
|
+
const targetPrototype = getEnhancedPrototype(expression.arguments[0]);
|
|
220
220
|
const sourceArg = unwrapExpression(expression.arguments[1]);
|
|
221
221
|
if (targetPrototype && t.isObjectExpression(sourceArg)) {
|
|
222
222
|
assignments.push({
|
|
223
223
|
targetPrototype,
|
|
224
|
-
sourceArg
|
|
224
|
+
sourceArg,
|
|
225
|
+
call: expression
|
|
225
226
|
});
|
|
226
227
|
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
|
}
|
|
@@ -241,6 +242,19 @@ function transformElementFile(code, usedMethods, options) {
|
|
|
241
242
|
if (options.debug) console.log(`[zed-gpui] Removed ${originalProperties - sourceArg.properties.length} unused methods from ${targetPrototype}`);
|
|
242
243
|
}
|
|
243
244
|
}
|
|
245
|
+
ast.program.body = ast.program.body.filter((node) => {
|
|
246
|
+
if (!t.isExpressionStatement(node)) return true;
|
|
247
|
+
if (t.isSequenceExpression(node.expression)) {
|
|
248
|
+
const expressions = node.expression.expressions.filter((expression) => !assignments.some(({ call, sourceArg }) => call === expression && sourceArg.properties.length === 0));
|
|
249
|
+
if (expressions.length !== node.expression.expressions.length) modified = true;
|
|
250
|
+
if (expressions.length === 0) return false;
|
|
251
|
+
node.expression = expressions.length === 1 ? expressions[0] : t.sequenceExpression(expressions);
|
|
252
|
+
return true;
|
|
253
|
+
}
|
|
254
|
+
const shouldRemove = assignments.some(({ call, sourceArg }) => call === node.expression && sourceArg.properties.length === 0);
|
|
255
|
+
if (shouldRemove) modified = true;
|
|
256
|
+
return !shouldRemove;
|
|
257
|
+
});
|
|
244
258
|
if (modified) return { code: generate(ast, {}, code).code };
|
|
245
259
|
return null;
|
|
246
260
|
} catch (error) {
|
|
@@ -248,8 +262,8 @@ function transformElementFile(code, usedMethods, options) {
|
|
|
248
262
|
return null;
|
|
249
263
|
}
|
|
250
264
|
}
|
|
251
|
-
function
|
|
252
|
-
return t.
|
|
265
|
+
function getEnhancedPrototype(node) {
|
|
266
|
+
return t.isIdentifier(node) && isElementType(node.name) ? node.name : void 0;
|
|
253
267
|
}
|
|
254
268
|
function unwrapExpression(node) {
|
|
255
269
|
while (t.isTSAsExpression(node) || t.isTSTypeAssertion(node) || t.isTSNonNullExpression(node)) node = node.expression;
|