@wuchale/svelte 0.14.0 → 0.14.2
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.js +3 -2
- package/dist/transformer.d.ts +2 -1
- package/dist/transformer.js +35 -28
- package/package.json +2 -2
- package/src/runtime.svelte +5 -5
package/dist/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// $$ cd .. && npm run test
|
|
1
2
|
import { defaultGenerateLoadID, defaultHeuristic, deepMergeObjects } from 'wuchale';
|
|
2
3
|
import { SvelteTransformer } from "./transformer.js";
|
|
3
4
|
import { getDependencies } from 'wuchale/adapter-utils';
|
|
@@ -54,8 +55,8 @@ const defaultArgs = {
|
|
|
54
55
|
export const adapter = (args = defaultArgs) => {
|
|
55
56
|
const { heuristic, pluralsFunc, runtime, ...rest } = deepMergeObjects(args, defaultArgs);
|
|
56
57
|
return {
|
|
57
|
-
transform: ({ content, filename, index,
|
|
58
|
-
return new SvelteTransformer(content, filename, index, heuristic, pluralsFunc,
|
|
58
|
+
transform: ({ content, filename, index, expr }) => {
|
|
59
|
+
return new SvelteTransformer(content, filename, index, heuristic, pluralsFunc, expr, runtime).transformSv();
|
|
59
60
|
},
|
|
60
61
|
loaderExts: ['.svelte.js', '.svelte.ts', '.js', '.ts'],
|
|
61
62
|
defaultLoaders: async () => {
|
package/dist/transformer.d.ts
CHANGED
|
@@ -31,9 +31,10 @@ export declare class SvelteTransformer extends Transformer {
|
|
|
31
31
|
visitSvelteElement: (node: AST.SvelteElement) => Message[];
|
|
32
32
|
visitSvelteBoundary: (node: AST.SvelteBoundary) => Message[];
|
|
33
33
|
visitSvelteHead: (node: AST.SvelteHead) => Message[];
|
|
34
|
+
visitTitleElement: (node: AST.TitleElement) => Message[];
|
|
34
35
|
visitSvelteWindow: (node: AST.SvelteWindow) => Message[];
|
|
35
36
|
visitRoot: (node: AST.Root) => Message[];
|
|
36
37
|
visitSv: (node: AST.SvelteNode | AnyNode) => Message[];
|
|
37
|
-
transformSv: (
|
|
38
|
+
transformSv: () => TransformOutput;
|
|
38
39
|
}
|
|
39
40
|
export {};
|
package/dist/transformer.js
CHANGED
|
@@ -2,10 +2,11 @@ import MagicString from "magic-string";
|
|
|
2
2
|
import { parse } from "svelte/compiler";
|
|
3
3
|
import { Message } from 'wuchale';
|
|
4
4
|
import { Transformer, parseScript } from 'wuchale/adapter-vanilla';
|
|
5
|
-
import { MixedVisitor, nonWhitespaceText } from "wuchale/adapter-utils";
|
|
5
|
+
import { MixedVisitor, nonWhitespaceText, varNames } from "wuchale/adapter-utils";
|
|
6
6
|
const nodesWithChildren = ['RegularElement', 'Component'];
|
|
7
|
-
const rtComponent = '
|
|
8
|
-
const snipPrefix = '
|
|
7
|
+
const rtComponent = 'W_tx_';
|
|
8
|
+
const snipPrefix = '_w_snippet_';
|
|
9
|
+
const rtModuleVar = varNames.rt + 'mod_';
|
|
9
10
|
export class SvelteTransformer extends Transformer {
|
|
10
11
|
// state
|
|
11
12
|
currentElement;
|
|
@@ -15,7 +16,7 @@ export class SvelteTransformer extends Transformer {
|
|
|
15
16
|
currentSnippet = 0;
|
|
16
17
|
mixedVisitor;
|
|
17
18
|
constructor(content, filename, index, heuristic, pluralsFunc, catalogExpr, rtConf) {
|
|
18
|
-
super(content, filename, index, heuristic, pluralsFunc, catalogExpr, rtConf);
|
|
19
|
+
super(content, filename, index, heuristic, pluralsFunc, catalogExpr, rtConf, [varNames.rt, rtModuleVar]);
|
|
19
20
|
}
|
|
20
21
|
visitExpressionTag = (node) => this.visit(node.expression);
|
|
21
22
|
initMixedVisitor = () => new MixedVisitor({
|
|
@@ -49,9 +50,13 @@ export class SvelteTransformer extends Transformer {
|
|
|
49
50
|
this.mstr.appendRight(childStart, snippetBegin);
|
|
50
51
|
this.mstr.prependLeft(childEnd, '\n{/snippet}');
|
|
51
52
|
}
|
|
52
|
-
let begin = `\n<${rtComponent}
|
|
53
|
+
let begin = `\n<${rtComponent}`;
|
|
54
|
+
if (snippets.length) {
|
|
55
|
+
begin += ` t={[${snippets.join(', ')}]}`;
|
|
56
|
+
}
|
|
57
|
+
begin += ' x=';
|
|
53
58
|
if (this.inCompoundText) {
|
|
54
|
-
begin += `{${this.vars().nestCtx}}
|
|
59
|
+
begin += `{${this.vars().nestCtx}} n`;
|
|
55
60
|
}
|
|
56
61
|
else {
|
|
57
62
|
const index = this.index.get(msgInfo.toKey());
|
|
@@ -59,7 +64,7 @@ export class SvelteTransformer extends Transformer {
|
|
|
59
64
|
}
|
|
60
65
|
let end = ' />\n';
|
|
61
66
|
if (hasExprs) {
|
|
62
|
-
begin += '
|
|
67
|
+
begin += ' a={[';
|
|
63
68
|
end = ']}' + end;
|
|
64
69
|
}
|
|
65
70
|
this.mstr.appendLeft(lastChildEnd, begin);
|
|
@@ -187,16 +192,23 @@ export class SvelteTransformer extends Transformer {
|
|
|
187
192
|
...this.visitSv(node.fragment),
|
|
188
193
|
];
|
|
189
194
|
visitSvelteHead = (node) => this.visitSv(node.fragment);
|
|
195
|
+
visitTitleElement = (node) => this.visitSv(node.fragment);
|
|
190
196
|
visitSvelteWindow = (node) => node.attributes.map(this.visitSv).flat();
|
|
191
197
|
visitRoot = (node) => {
|
|
192
198
|
const msgs = [];
|
|
193
199
|
// @ts-ignore: module is a reserved keyword, not sure how to specify the type
|
|
194
200
|
if (node.module) {
|
|
201
|
+
const prevRtVar = this.currentRtVar;
|
|
202
|
+
this.currentRtVar = rtModuleVar;
|
|
195
203
|
this.additionalState = { module: true };
|
|
196
204
|
this.commentDirectives = {}; // reset
|
|
197
|
-
// @ts-
|
|
205
|
+
// @ts-expect-error
|
|
198
206
|
msgs.push(...this.visitProgram(node.module.content));
|
|
207
|
+
this.mstr.appendRight(
|
|
208
|
+
// @ts-expect-error
|
|
209
|
+
this.getRealBodyStart(node.module.content.body), this.initRuntime(this.filename, null, null, {}));
|
|
199
210
|
this.additionalState = {}; // reset
|
|
211
|
+
this.currentRtVar = prevRtVar; // reset
|
|
200
212
|
}
|
|
201
213
|
if (node.instance) {
|
|
202
214
|
this.commentDirectives = {}; // reset
|
|
@@ -232,7 +244,7 @@ export class SvelteTransformer extends Transformer {
|
|
|
232
244
|
this.commentDirectives = commentDirectivesPrev;
|
|
233
245
|
return msgs;
|
|
234
246
|
};
|
|
235
|
-
transformSv = (
|
|
247
|
+
transformSv = () => {
|
|
236
248
|
const isComponent = this.filename.endsWith('.svelte');
|
|
237
249
|
let ast;
|
|
238
250
|
if (isComponent) {
|
|
@@ -246,37 +258,32 @@ export class SvelteTransformer extends Transformer {
|
|
|
246
258
|
this.mstr = new MagicString(this.content);
|
|
247
259
|
this.mixedVisitor = this.initMixedVisitor();
|
|
248
260
|
const msgs = this.visitSv(ast);
|
|
249
|
-
|
|
250
|
-
return this.finalize(msgs, 0);
|
|
251
|
-
}
|
|
252
|
-
const headerLines = [
|
|
253
|
-
isComponent ? `\nimport ${rtComponent} from "@wuchale/svelte/runtime.svelte"` : '',
|
|
254
|
-
headerHead,
|
|
255
|
-
this.initRuntime(this.filename, null, null, {}),
|
|
256
|
-
];
|
|
257
|
-
const headerFin = headerLines.join('\n');
|
|
261
|
+
const initRuntime = this.initRuntime(this.filename, null, null, {});
|
|
258
262
|
if (ast.type === 'Program') {
|
|
259
263
|
const bodyStart = this.getRealBodyStart(ast.body);
|
|
260
|
-
this.mstr.appendRight(bodyStart,
|
|
264
|
+
this.mstr.appendRight(bodyStart, initRuntime);
|
|
261
265
|
return this.finalize(msgs, bodyStart);
|
|
262
266
|
}
|
|
263
|
-
let
|
|
267
|
+
let headerIndex = 0;
|
|
264
268
|
if (ast.module) {
|
|
265
269
|
// @ts-ignore
|
|
266
|
-
|
|
267
|
-
this.mstr.appendRight(hmrHeaderIndex, headerFin);
|
|
270
|
+
headerIndex = this.getRealBodyStart(ast.module.content.body);
|
|
268
271
|
}
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
272
|
+
if (ast.instance) {
|
|
273
|
+
if (!ast.module) {
|
|
274
|
+
// @ts-expect-error
|
|
275
|
+
headerIndex = this.getRealBodyStart(ast.instance.content.body);
|
|
276
|
+
}
|
|
277
|
+
// @ts-expect-error
|
|
278
|
+
this.mstr.appendRight(this.getRealBodyStart(ast.instance.content.body), initRuntime);
|
|
273
279
|
}
|
|
274
280
|
else {
|
|
275
281
|
this.mstr.prepend('<script>');
|
|
276
282
|
// account index for hmr data here
|
|
277
|
-
this.mstr.prependRight(0, `${
|
|
283
|
+
this.mstr.prependRight(0, `${initRuntime}\n</script>\n`);
|
|
278
284
|
// now hmr data can be prependRight(0, ...)
|
|
279
285
|
}
|
|
280
|
-
|
|
286
|
+
const headerAdd = `\nimport ${rtComponent} from "@wuchale/svelte/runtime.svelte"`;
|
|
287
|
+
return this.finalize(msgs, headerIndex, headerAdd);
|
|
281
288
|
};
|
|
282
289
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wuchale/svelte",
|
|
3
|
-
"version": "0.14.
|
|
3
|
+
"version": "0.14.2",
|
|
4
4
|
"description": "Protobuf-like i18n from plain code: Svelte adapter",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"dev": "tsc --watch",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"license": "MIT",
|
|
53
53
|
"dependencies": {
|
|
54
54
|
"svelte": "^5.37.0",
|
|
55
|
-
"wuchale": "^0.15.
|
|
55
|
+
"wuchale": "^0.15.6"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
58
|
"acorn": "^8.15.0",
|
package/src/runtime.svelte
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
<script>
|
|
2
|
-
const {
|
|
2
|
+
const {n = false, x, t, a} = $props()
|
|
3
3
|
</script>
|
|
4
4
|
|
|
5
|
-
{#each
|
|
5
|
+
{#each x as fragment, i}
|
|
6
6
|
{#if typeof fragment === 'string'}
|
|
7
7
|
{fragment}
|
|
8
8
|
{:else if typeof fragment === 'number'}
|
|
9
|
-
{#if !
|
|
10
|
-
{
|
|
9
|
+
{#if !n || i > 0}
|
|
10
|
+
{a[fragment]}
|
|
11
11
|
{/if}
|
|
12
12
|
{:else}
|
|
13
|
-
{@const tag =
|
|
13
|
+
{@const tag = t[fragment[0]]}
|
|
14
14
|
{#if tag == null}
|
|
15
15
|
[i18n-404:tag]
|
|
16
16
|
{:else}
|