@wuchale/svelte 0.19.2 → 0.19.3
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/transformer.d.ts +5 -2
- package/dist/transformer.js +25 -12
- package/package.json +2 -2
package/dist/transformer.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ export declare class SvelteTransformer extends Transformer<RuntimeCtxSv> {
|
|
|
11
11
|
currentElement?: string;
|
|
12
12
|
inCompoundText: boolean;
|
|
13
13
|
currentSnippet: number;
|
|
14
|
-
|
|
14
|
+
moduleExportExprs: AnyNode[];
|
|
15
15
|
mixedVisitor: MixedVisitor<MixedNodesTypes>;
|
|
16
16
|
constructor(content: string, filename: string, index: IndexTracker, heuristic: HeuristicFunc, patterns: CodePattern[], catalogExpr: CatalogExpr, rtConf: RuntimeConf<RuntimeCtxSv>, matchUrl: UrlMatcher);
|
|
17
17
|
visitExpressionTag: (node: AST.ExpressionTag) => Message[];
|
|
@@ -25,6 +25,9 @@ export declare class SvelteTransformer extends Transformer<RuntimeCtxSv> {
|
|
|
25
25
|
visitAttribute: (node: AST.Attribute) => Message[];
|
|
26
26
|
visitConstTag: (node: AST.ConstTag) => Message[];
|
|
27
27
|
visitRenderTag: (node: AST.RenderTag) => Message[];
|
|
28
|
+
visitHtmlTag: (node: AST.HtmlTag) => Message[];
|
|
29
|
+
visitOnDirective: (node: AST.OnDirective) => Message[];
|
|
30
|
+
hasIdentifier: (node: AnyNode | AnyNode[], name: string) => boolean;
|
|
28
31
|
visitSnippetBlock: (node: AST.SnippetBlock) => Message[];
|
|
29
32
|
visitIfBlock: (node: AST.IfBlock) => Message[];
|
|
30
33
|
visitEachBlock: (node: AST.EachBlock) => Message[];
|
|
@@ -40,7 +43,7 @@ export declare class SvelteTransformer extends Transformer<RuntimeCtxSv> {
|
|
|
40
43
|
visitRoot: (node: AST.Root) => Message[];
|
|
41
44
|
visitSv: (node: AST.SvelteNode | AnyNode) => Message[];
|
|
42
45
|
/** collects the ranges that will be checked if a snippet identifier is exported using RegExp test to simplify */
|
|
43
|
-
|
|
46
|
+
collectModuleExportExprs: (script: AST.Script) => void;
|
|
44
47
|
transformSv: () => Promise<TransformOutput>;
|
|
45
48
|
}
|
|
46
49
|
export {};
|
package/dist/transformer.js
CHANGED
|
@@ -20,7 +20,7 @@ export class SvelteTransformer extends Transformer {
|
|
|
20
20
|
currentElement;
|
|
21
21
|
inCompoundText = false;
|
|
22
22
|
currentSnippet = 0;
|
|
23
|
-
|
|
23
|
+
moduleExportExprs = []; // to choose which runtime var to use for snippets
|
|
24
24
|
mixedVisitor;
|
|
25
25
|
constructor(content, filename, index, heuristic, patterns, catalogExpr, rtConf, matchUrl) {
|
|
26
26
|
super(content, filename, index, heuristic, patterns, catalogExpr, rtConf, matchUrl, [varNames.rt, rtModuleVar]);
|
|
@@ -37,6 +37,9 @@ export class SvelteTransformer extends Transformer {
|
|
|
37
37
|
return msgs;
|
|
38
38
|
}
|
|
39
39
|
const needsWrapping = msgs.some(msg => {
|
|
40
|
+
if (msg.details.leftSide) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
40
43
|
const topCall = msg.details.topLevelCall ?? '';
|
|
41
44
|
if (noWrapTopCalls.includes(topCall) || noWrapTopCalls.some(c => topCall.startsWith(`${c}.`))) {
|
|
42
45
|
return false;
|
|
@@ -49,7 +52,7 @@ export class SvelteTransformer extends Transformer {
|
|
|
49
52
|
if (!needsWrapping) {
|
|
50
53
|
return msgs;
|
|
51
54
|
}
|
|
52
|
-
const isExported = this.
|
|
55
|
+
const isExported = this.moduleExportExprs.some(node => init.start >= node.start && init.end <= node.end);
|
|
53
56
|
if (!isExported) {
|
|
54
57
|
this.mstr.appendLeft(init.start, '$derived(');
|
|
55
58
|
this.mstr.appendRight(init.end, ')');
|
|
@@ -197,15 +200,25 @@ export class SvelteTransformer extends Transformer {
|
|
|
197
200
|
// @ts-expect-error
|
|
198
201
|
return this.visitVariableDeclaration(node.declaration);
|
|
199
202
|
};
|
|
200
|
-
visitRenderTag = (node) =>
|
|
201
|
-
|
|
202
|
-
|
|
203
|
+
visitRenderTag = (node) => this.visit(node.expression);
|
|
204
|
+
visitHtmlTag = (node) => this.visit(node.expression);
|
|
205
|
+
visitOnDirective = (node) => this.visit(node.expression);
|
|
206
|
+
hasIdentifier = (node, name) => {
|
|
207
|
+
if (!node || typeof node !== 'object') {
|
|
208
|
+
return false;
|
|
209
|
+
}
|
|
210
|
+
if (Array.isArray(node)) {
|
|
211
|
+
return node.some(child => this.hasIdentifier(child, name));
|
|
212
|
+
}
|
|
213
|
+
if (node.type === 'Identifier') {
|
|
214
|
+
return node.name === name;
|
|
215
|
+
}
|
|
216
|
+
return Object.values(node).some(value => this.hasIdentifier(value, name));
|
|
203
217
|
};
|
|
204
218
|
visitSnippetBlock = (node) => {
|
|
205
219
|
// use module runtime var because the snippet may be exported from the module
|
|
206
220
|
const prevRtVar = this.currentRtVar;
|
|
207
|
-
|
|
208
|
-
if (this.moduleExportRanges.some(([start, end]) => pattern.test(this.content.slice(start, end)))) {
|
|
221
|
+
if (this.hasIdentifier(this.moduleExportExprs, node.expression.name)) {
|
|
209
222
|
this.currentRtVar = rtModuleVar;
|
|
210
223
|
}
|
|
211
224
|
const msgs = this.visitFragment(node.body);
|
|
@@ -283,7 +296,7 @@ export class SvelteTransformer extends Transformer {
|
|
|
283
296
|
};
|
|
284
297
|
visitSv = (node) => this.visit(node);
|
|
285
298
|
/** collects the ranges that will be checked if a snippet identifier is exported using RegExp test to simplify */
|
|
286
|
-
|
|
299
|
+
collectModuleExportExprs = (script) => {
|
|
287
300
|
for (const stmt of script.content.body) {
|
|
288
301
|
if (stmt.type !== 'ExportNamedDeclaration') {
|
|
289
302
|
continue;
|
|
@@ -291,7 +304,7 @@ export class SvelteTransformer extends Transformer {
|
|
|
291
304
|
for (const spec of stmt.specifiers) {
|
|
292
305
|
if (spec.local.type === 'Identifier') {
|
|
293
306
|
const local = spec.local;
|
|
294
|
-
this.
|
|
307
|
+
this.moduleExportExprs.push(local);
|
|
295
308
|
}
|
|
296
309
|
}
|
|
297
310
|
const declaration = stmt.declaration;
|
|
@@ -299,14 +312,14 @@ export class SvelteTransformer extends Transformer {
|
|
|
299
312
|
continue;
|
|
300
313
|
}
|
|
301
314
|
if (declaration.type === 'FunctionDeclaration' || declaration.type === 'ClassDeclaration') {
|
|
302
|
-
this.
|
|
315
|
+
this.moduleExportExprs.push(declaration);
|
|
303
316
|
continue;
|
|
304
317
|
}
|
|
305
318
|
for (const decl of declaration?.declarations ?? []) {
|
|
306
319
|
if (!decl.init) {
|
|
307
320
|
continue;
|
|
308
321
|
}
|
|
309
|
-
this.
|
|
322
|
+
this.moduleExportExprs.push(decl.init);
|
|
310
323
|
}
|
|
311
324
|
}
|
|
312
325
|
};
|
|
@@ -324,7 +337,7 @@ export class SvelteTransformer extends Transformer {
|
|
|
324
337
|
this.mstr = new MagicString(this.content);
|
|
325
338
|
this.mixedVisitor = this.initMixedVisitor();
|
|
326
339
|
if (ast.type === 'Root' && ast.module) {
|
|
327
|
-
this.
|
|
340
|
+
this.collectModuleExportExprs(ast.module);
|
|
328
341
|
}
|
|
329
342
|
const msgs = this.visitSv(ast);
|
|
330
343
|
const initRuntime = this.initRuntime();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wuchale/svelte",
|
|
3
|
-
"version": "0.19.
|
|
3
|
+
"version": "0.19.3",
|
|
4
4
|
"description": "Protobuf-like i18n from plain code: Svelte adapter",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"dev": "tsc --watch",
|
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
"dependencies": {
|
|
54
54
|
"magic-string": "^0.30.21",
|
|
55
55
|
"svelte": "^5",
|
|
56
|
-
"wuchale": "^0.22.
|
|
56
|
+
"wuchale": "^0.22.1"
|
|
57
57
|
},
|
|
58
58
|
"devDependencies": {
|
|
59
59
|
"acorn": "^8.16.0",
|