@wuchale/svelte 0.20.0 → 0.20.1
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 +2 -7
- package/dist/transformer.js +18 -36
- package/package.json +4 -4
package/dist/transformer.d.ts
CHANGED
|
@@ -4,17 +4,13 @@ import type { CodePattern, HeuristicFunc, Message, RuntimeConf, TransformCtx, Tr
|
|
|
4
4
|
import { MixedVisitor } from 'wuchale/adapter-utils';
|
|
5
5
|
import { Transformer } from 'wuchale/adapter-vanilla';
|
|
6
6
|
type MixedNodesTypes = AST.Text | AST.Tag | AST.ElementLike | AST.SvelteElement | AST.Block | AST.Comment;
|
|
7
|
-
type
|
|
8
|
-
currentSnippet: number;
|
|
9
|
-
};
|
|
10
|
-
type MixedVisitorSvelte = MixedVisitor<MixedNodesTypes, AddCtx, AST.Text, AST.Comment, AST.ExpressionTag>;
|
|
7
|
+
type MixedVisitorSvelte = MixedVisitor<MixedNodesTypes, AST.Text, AST.Comment, AST.ExpressionTag>;
|
|
11
8
|
export type RuntimeCtxSv = {
|
|
12
9
|
module: boolean;
|
|
13
10
|
};
|
|
14
11
|
export declare class SvelteTransformer extends Transformer {
|
|
15
12
|
currentElement?: string | undefined;
|
|
16
|
-
|
|
17
|
-
addCtx: AddCtx;
|
|
13
|
+
currentSnippet: number;
|
|
18
14
|
moduleExportExprs: AnyNode[];
|
|
19
15
|
mixedVisitor: MixedVisitorSvelte;
|
|
20
16
|
constructor(ctx: TransformCtx, heuristic: HeuristicFunc, patterns: CodePattern[], rtConf: RuntimeConf);
|
|
@@ -24,7 +20,6 @@ export declare class SvelteTransformer extends Transformer {
|
|
|
24
20
|
visitFragment(node: AST.Fragment): Message[];
|
|
25
21
|
visitRegularElement(node: AST.ElementLike): Message[];
|
|
26
22
|
visitComponent(node: AST.Component): Message[];
|
|
27
|
-
visitText(node: AST.Text): Message[];
|
|
28
23
|
visitSpreadAttribute(node: AST.SpreadAttribute): Message[];
|
|
29
24
|
visitAttribute(node: AST.Attribute): Message[];
|
|
30
25
|
visitConstTag(node: AST.ConstTag): Message[];
|
package/dist/transformer.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { parse, preprocess } from 'svelte/compiler';
|
|
2
2
|
import { getKey } from 'wuchale';
|
|
3
|
-
import { MixedVisitor,
|
|
3
|
+
import { MixedVisitor, varNames } from 'wuchale/adapter-utils';
|
|
4
4
|
import { parseScript, Transformer } from 'wuchale/adapter-vanilla';
|
|
5
|
-
const nodesWithChildren = ['RegularElement', 'Component', 'SvelteElement'];
|
|
6
5
|
const noWrapTopCalls = ['$props', '$state', '$derived', '$effect'];
|
|
7
6
|
const rtComponent = 'W_tx_';
|
|
8
7
|
const headerAdd = `\nimport ${rtComponent} from "@wuchale/svelte/runtime.svelte"`;
|
|
@@ -17,8 +16,7 @@ const removeCSS = ({ content }) => ({
|
|
|
17
16
|
export class SvelteTransformer extends Transformer {
|
|
18
17
|
// state
|
|
19
18
|
currentElement;
|
|
20
|
-
|
|
21
|
-
addCtx = { currentSnippet: 0 };
|
|
19
|
+
currentSnippet = 0;
|
|
22
20
|
moduleExportExprs = []; // to choose which runtime var to use for snippets
|
|
23
21
|
mixedVisitor;
|
|
24
22
|
constructor(ctx, heuristic, patterns, rtConf) {
|
|
@@ -64,8 +62,10 @@ export class SvelteTransformer extends Transformer {
|
|
|
64
62
|
}
|
|
65
63
|
initMixedVisitor() {
|
|
66
64
|
return new MixedVisitor({
|
|
67
|
-
|
|
65
|
+
mstr: this.mstr,
|
|
66
|
+
index: this.index,
|
|
68
67
|
content: this.content,
|
|
68
|
+
vars: this.vars.bind(this),
|
|
69
69
|
getRange: node => ({ start: node.start, end: node.end }),
|
|
70
70
|
isText: node => node.type === 'Text',
|
|
71
71
|
isComment: node => node.type === 'Comment',
|
|
@@ -73,18 +73,18 @@ export class SvelteTransformer extends Transformer {
|
|
|
73
73
|
isExpression: node => node.type === 'ExpressionTag',
|
|
74
74
|
getTextContent: node => node.data,
|
|
75
75
|
getCommentData: node => node.data.trim(),
|
|
76
|
-
|
|
77
|
-
visitFunc: MixedVisitor.withCtxRestore(this, this.visitSv.bind(this)),
|
|
76
|
+
visitFunc: this.visitSv.bind(this),
|
|
78
77
|
fullHeuristicDetails: this.fullHeuristicDetails.bind(this),
|
|
79
78
|
checkHeuristic: this.getHeuristicMessageType.bind(this),
|
|
80
|
-
wrapNested: (msgInfo, hasExprs, nestedRanges, lastChildEnd) => {
|
|
79
|
+
wrapNested: (inNested, msgInfo, hasExprs, nestedRanges, lastChildEnd) => {
|
|
81
80
|
const snippets = [];
|
|
81
|
+
const vars = this.vars();
|
|
82
82
|
// create and reference snippets
|
|
83
83
|
for (const [childStart, childEnd, haveCtx] of nestedRanges) {
|
|
84
|
-
const snippetName = `${snipPrefix}${this.
|
|
84
|
+
const snippetName = `${snipPrefix}${this.currentSnippet}`;
|
|
85
85
|
snippets.push(snippetName);
|
|
86
|
-
this.
|
|
87
|
-
const snippetBegin = `\n{#snippet ${snippetName}(${haveCtx ?
|
|
86
|
+
this.currentSnippet++;
|
|
87
|
+
const snippetBegin = `\n{#snippet ${snippetName}(${haveCtx ? vars.nestCtx : ''})}\n`;
|
|
88
88
|
this.mstr.appendRight(childStart, snippetBegin);
|
|
89
89
|
this.mstr.prependLeft(childEnd, '\n{/snippet}\n');
|
|
90
90
|
}
|
|
@@ -93,12 +93,12 @@ export class SvelteTransformer extends Transformer {
|
|
|
93
93
|
begin += ` t={[${snippets.join(', ')}]}`;
|
|
94
94
|
}
|
|
95
95
|
begin += ' x=';
|
|
96
|
-
if (
|
|
97
|
-
begin += `{${
|
|
96
|
+
if (inNested) {
|
|
97
|
+
begin += `{${vars.nestCtx}} n`;
|
|
98
98
|
}
|
|
99
99
|
else {
|
|
100
100
|
const index = this.index.get(getKey(msgInfo.msgStr, msgInfo.context));
|
|
101
|
-
begin += `{${
|
|
101
|
+
begin += `{${vars.rtCtx}(${index})}`;
|
|
102
102
|
}
|
|
103
103
|
let end = ' />\n';
|
|
104
104
|
if (hasExprs) {
|
|
@@ -112,12 +112,8 @@ export class SvelteTransformer extends Transformer {
|
|
|
112
112
|
}
|
|
113
113
|
visitFragment(node) {
|
|
114
114
|
return this.mixedVisitor.visit({
|
|
115
|
-
mstr: this.mstr,
|
|
116
|
-
index: this.index,
|
|
117
|
-
addCtx: this.addCtx,
|
|
118
115
|
children: node.nodes,
|
|
119
116
|
commentDirectives: this.commentDirectives,
|
|
120
|
-
inCompoundText: this.inCompoundText,
|
|
121
117
|
scope: 'markup',
|
|
122
118
|
element: this.currentElement,
|
|
123
119
|
useComponent: this.currentElement !== 'title',
|
|
@@ -137,18 +133,6 @@ export class SvelteTransformer extends Transformer {
|
|
|
137
133
|
visitComponent(node) {
|
|
138
134
|
return this.visitRegularElement(node);
|
|
139
135
|
}
|
|
140
|
-
visitText(node) {
|
|
141
|
-
const [startWh, trimmed, endWh] = nonWhitespaceText(node.data);
|
|
142
|
-
const [pass, msgInfo] = this.checkHeuristic(trimmed, {
|
|
143
|
-
scope: 'markup',
|
|
144
|
-
element: this.currentElement,
|
|
145
|
-
});
|
|
146
|
-
if (!pass) {
|
|
147
|
-
return [];
|
|
148
|
-
}
|
|
149
|
-
this.mstr.update(node.start + startWh, node.end - endWh, `{${this.literalRepl(msgInfo)}}`);
|
|
150
|
-
return [msgInfo];
|
|
151
|
-
}
|
|
152
136
|
visitSpreadAttribute(node) {
|
|
153
137
|
return this.visit(node.expression);
|
|
154
138
|
}
|
|
@@ -164,17 +148,15 @@ export class SvelteTransformer extends Transformer {
|
|
|
164
148
|
values = [node.value];
|
|
165
149
|
}
|
|
166
150
|
if (values.length > 1) {
|
|
167
|
-
|
|
168
|
-
mstr: this.mstr,
|
|
169
|
-
index: this.index,
|
|
170
|
-
addCtx: this.addCtx,
|
|
151
|
+
const msgs = this.mixedVisitor.visit({
|
|
171
152
|
children: values,
|
|
172
153
|
commentDirectives: this.commentDirectives,
|
|
173
|
-
inCompoundText: false,
|
|
174
154
|
scope: 'attribute',
|
|
175
155
|
element: this.currentElement,
|
|
176
156
|
attribute: node.name,
|
|
177
157
|
});
|
|
158
|
+
msgs.push(...this.mixedVisitor.applyMod('attribute'));
|
|
159
|
+
return msgs;
|
|
178
160
|
}
|
|
179
161
|
const value = values[0];
|
|
180
162
|
const heuDetails = {
|
|
@@ -334,7 +316,7 @@ export class SvelteTransformer extends Transformer {
|
|
|
334
316
|
this.commentDirectives = {}; // reset
|
|
335
317
|
msgs.push(...this.visitProgram(node.instance.content));
|
|
336
318
|
}
|
|
337
|
-
msgs.push(...this.visitFragment(node.fragment));
|
|
319
|
+
msgs.push(...this.visitFragment(node.fragment), ...this.mixedVisitor.applyMod());
|
|
338
320
|
return msgs;
|
|
339
321
|
}
|
|
340
322
|
visitSv(node) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wuchale/svelte",
|
|
3
|
-
"version": "0.20.
|
|
3
|
+
"version": "0.20.1",
|
|
4
4
|
"description": "Protobuf-like i18n from plain code: Svelte adapter",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"dev": "tsc --watch",
|
|
@@ -54,12 +54,12 @@
|
|
|
54
54
|
"svelte": "^5"
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
|
-
"wuchale": "^0.
|
|
57
|
+
"wuchale": "^0.25.0"
|
|
58
58
|
},
|
|
59
59
|
"devDependencies": {
|
|
60
|
-
"@types/node": "~24.13.
|
|
60
|
+
"@types/node": "~24.13.2",
|
|
61
61
|
"svelte": "^5.56.3",
|
|
62
|
-
"acorn": "^8.
|
|
62
|
+
"acorn": "^8.17.0",
|
|
63
63
|
"typescript": "^6.0.3"
|
|
64
64
|
},
|
|
65
65
|
"type": "module"
|