@wuchale/svelte 0.17.6 → 0.17.8
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.d.ts +2 -2
- package/dist/transformer.d.ts +3 -3
- package/dist/transformer.js +37 -18
- package/package.json +3 -2
package/dist/index.d.ts
CHANGED
|
@@ -10,6 +10,6 @@ export type SvelteArgs = AdapterArgs<LoadersAvailable>;
|
|
|
10
10
|
export declare function getDefaultLoaderPath(loader: LoaderChoice<LoadersAvailable>, bundle: boolean): string | {
|
|
11
11
|
client: string;
|
|
12
12
|
server: string;
|
|
13
|
-
};
|
|
14
|
-
export declare const adapter: (args?: SvelteArgs) => Adapter;
|
|
13
|
+
} | null;
|
|
14
|
+
export declare const adapter: (args?: Partial<SvelteArgs>) => Adapter;
|
|
15
15
|
export {};
|
package/dist/transformer.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { AnyNode, VariableDeclarator } from "acorn";
|
|
|
2
2
|
import { type AST } from "svelte/compiler";
|
|
3
3
|
import { Message } from 'wuchale';
|
|
4
4
|
import { Transformer } from 'wuchale/adapter-vanilla';
|
|
5
|
-
import type { IndexTracker, HeuristicFunc, TransformOutput, CatalogExpr, RuntimeConf, CodePattern } from 'wuchale';
|
|
5
|
+
import type { IndexTracker, HeuristicFunc, TransformOutput, CatalogExpr, RuntimeConf, CodePattern, UrlMatcher } from 'wuchale';
|
|
6
6
|
import { MixedVisitor, type CommentDirectives } from "wuchale/adapter-utils";
|
|
7
7
|
type MixedNodesTypes = AST.Text | AST.Tag | AST.ElementLike | AST.Block | AST.Comment;
|
|
8
8
|
export declare class SvelteTransformer extends Transformer {
|
|
@@ -13,7 +13,7 @@ export declare class SvelteTransformer extends Transformer {
|
|
|
13
13
|
currentSnippet: number;
|
|
14
14
|
moduleExportRanges: [number, number][];
|
|
15
15
|
mixedVisitor: MixedVisitor<MixedNodesTypes>;
|
|
16
|
-
constructor(content: string, filename: string, index: IndexTracker, heuristic: HeuristicFunc, patterns: CodePattern[], catalogExpr: CatalogExpr, rtConf: RuntimeConf, matchUrl:
|
|
16
|
+
constructor(content: string, filename: string, index: IndexTracker, heuristic: HeuristicFunc, patterns: CodePattern[], catalogExpr: CatalogExpr, rtConf: RuntimeConf, matchUrl: UrlMatcher);
|
|
17
17
|
visitExpressionTag: (node: AST.ExpressionTag) => Message[];
|
|
18
18
|
visitVariableDeclarator: (node: VariableDeclarator) => Message[];
|
|
19
19
|
initMixedVisitor: () => MixedVisitor<MixedNodesTypes>;
|
|
@@ -41,6 +41,6 @@ export declare class SvelteTransformer extends Transformer {
|
|
|
41
41
|
visitSv: (node: AST.SvelteNode | AnyNode) => Message[];
|
|
42
42
|
/** collects the ranges that will be checked if a snippet identifier is exported using RegExp test to simplify */
|
|
43
43
|
collectModuleExportRanges: (script: AST.Script) => void;
|
|
44
|
-
transformSv: () => TransformOutput
|
|
44
|
+
transformSv: () => Promise<TransformOutput>;
|
|
45
45
|
}
|
|
46
46
|
export {};
|
package/dist/transformer.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import MagicString from "magic-string";
|
|
2
|
-
import { parse } from "svelte/compiler";
|
|
2
|
+
import { parse, preprocess } from "svelte/compiler";
|
|
3
3
|
import { Message } from 'wuchale';
|
|
4
4
|
import { Transformer, parseScript } from 'wuchale/adapter-vanilla';
|
|
5
5
|
import { MixedVisitor, nonWhitespaceText, processCommentDirectives, varNames } from "wuchale/adapter-utils";
|
|
@@ -7,6 +7,16 @@ const nodesWithChildren = ['RegularElement', 'Component'];
|
|
|
7
7
|
const rtComponent = 'W_tx_';
|
|
8
8
|
const snipPrefix = '_w_snippet_';
|
|
9
9
|
const rtModuleVar = varNames.rt + 'mod_';
|
|
10
|
+
// for use before actually parsing the code,
|
|
11
|
+
// to remove the contents of e.g. <style lang="scss">
|
|
12
|
+
// without messing up indices for magic-string
|
|
13
|
+
const removeSCSS = ({ attributes, content }) => {
|
|
14
|
+
if (attributes.lang) {
|
|
15
|
+
return {
|
|
16
|
+
code: ' '.repeat(content.length),
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
};
|
|
10
20
|
export class SvelteTransformer extends Transformer {
|
|
11
21
|
// state
|
|
12
22
|
currentElement;
|
|
@@ -22,11 +32,12 @@ export class SvelteTransformer extends Transformer {
|
|
|
22
32
|
visitExpressionTag = (node) => this.visit(node.expression);
|
|
23
33
|
visitVariableDeclarator = (node) => {
|
|
24
34
|
const msgs = this.defaultVisitVariableDeclarator(node);
|
|
25
|
-
|
|
35
|
+
const init = node.init;
|
|
36
|
+
if (!msgs.length || this.declaring != null || init == null || ['ArrowFunctionExpression', 'FunctionExpression'].includes(init.type)) {
|
|
26
37
|
return msgs;
|
|
27
38
|
}
|
|
28
39
|
const needsWrapping = msgs.some(msg => {
|
|
29
|
-
if (['$props', '$state', '$derived', '$derived.by'].includes(msg.details.topLevelCall)) {
|
|
40
|
+
if (msg.details.topLevelCall && ['$props', '$state', '$derived', '$derived.by'].includes(msg.details.topLevelCall)) {
|
|
30
41
|
return false;
|
|
31
42
|
}
|
|
32
43
|
if (msg.details.declaring !== 'variable') {
|
|
@@ -37,10 +48,10 @@ export class SvelteTransformer extends Transformer {
|
|
|
37
48
|
if (!needsWrapping) {
|
|
38
49
|
return msgs;
|
|
39
50
|
}
|
|
40
|
-
const isExported = this.moduleExportRanges.some(([start, end]) =>
|
|
51
|
+
const isExported = this.moduleExportRanges.some(([start, end]) => init.start >= start && init.end <= end);
|
|
41
52
|
if (!isExported) {
|
|
42
|
-
this.mstr.appendLeft(
|
|
43
|
-
this.mstr.appendRight(
|
|
53
|
+
this.mstr.appendLeft(init.start, '$derived(');
|
|
54
|
+
this.mstr.appendRight(init.end, ')');
|
|
44
55
|
}
|
|
45
56
|
return msgs;
|
|
46
57
|
};
|
|
@@ -228,10 +239,10 @@ export class SvelteTransformer extends Transformer {
|
|
|
228
239
|
];
|
|
229
240
|
};
|
|
230
241
|
visitAwaitBlock = (node) => {
|
|
231
|
-
const msgs =
|
|
232
|
-
|
|
233
|
-
...this.visitFragment(node.then)
|
|
234
|
-
|
|
242
|
+
const msgs = this.visit(node.expression);
|
|
243
|
+
if (node.then) {
|
|
244
|
+
msgs.push(...this.visitFragment(node.then));
|
|
245
|
+
}
|
|
235
246
|
if (node.pending) {
|
|
236
247
|
msgs.push(...this.visitFragment(node.pending));
|
|
237
248
|
}
|
|
@@ -259,9 +270,12 @@ export class SvelteTransformer extends Transformer {
|
|
|
259
270
|
this.commentDirectives = {}; // reset
|
|
260
271
|
// @ts-expect-error
|
|
261
272
|
msgs.push(...this.visitProgram(node.module.content));
|
|
262
|
-
this.
|
|
263
|
-
|
|
264
|
-
|
|
273
|
+
const runtimeInit = this.initRuntime(this.filename);
|
|
274
|
+
if (runtimeInit) {
|
|
275
|
+
this.mstr.appendRight(
|
|
276
|
+
// @ts-expect-error
|
|
277
|
+
this.getRealBodyStart(node.module.content.body) ?? node.module.content.start, runtimeInit);
|
|
278
|
+
}
|
|
265
279
|
this.additionalState = {}; // reset
|
|
266
280
|
this.currentRtVar = prevRtVar; // reset
|
|
267
281
|
}
|
|
@@ -330,11 +344,12 @@ export class SvelteTransformer extends Transformer {
|
|
|
330
344
|
}
|
|
331
345
|
}
|
|
332
346
|
};
|
|
333
|
-
transformSv = () => {
|
|
347
|
+
transformSv = async () => {
|
|
334
348
|
const isComponent = this.filename.endsWith('.svelte');
|
|
335
349
|
let ast;
|
|
336
350
|
if (isComponent) {
|
|
337
|
-
|
|
351
|
+
const prepd = await preprocess(this.content, { style: removeSCSS });
|
|
352
|
+
ast = parse(prepd.code, { modern: true });
|
|
338
353
|
}
|
|
339
354
|
else {
|
|
340
355
|
const [pAst, comments] = parseScript(this.content);
|
|
@@ -347,10 +362,12 @@ export class SvelteTransformer extends Transformer {
|
|
|
347
362
|
this.collectModuleExportRanges(ast.module);
|
|
348
363
|
}
|
|
349
364
|
const msgs = this.visitSv(ast);
|
|
350
|
-
const initRuntime = this.initRuntime(this.filename
|
|
365
|
+
const initRuntime = this.initRuntime(this.filename);
|
|
351
366
|
if (ast.type === 'Program') {
|
|
352
367
|
const bodyStart = this.getRealBodyStart(ast.body) ?? 0;
|
|
353
|
-
|
|
368
|
+
if (initRuntime) {
|
|
369
|
+
this.mstr.appendRight(bodyStart, initRuntime);
|
|
370
|
+
}
|
|
354
371
|
return this.finalize(msgs, bodyStart);
|
|
355
372
|
}
|
|
356
373
|
let headerIndex = 0;
|
|
@@ -364,7 +381,9 @@ export class SvelteTransformer extends Transformer {
|
|
|
364
381
|
if (!ast.module) {
|
|
365
382
|
headerIndex = instanceBodyStart;
|
|
366
383
|
}
|
|
367
|
-
|
|
384
|
+
if (initRuntime) {
|
|
385
|
+
this.mstr.appendRight(instanceBodyStart, initRuntime);
|
|
386
|
+
}
|
|
368
387
|
}
|
|
369
388
|
else {
|
|
370
389
|
const instanceStart = ast.module?.end ?? 0;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wuchale/svelte",
|
|
3
|
-
"version": "0.17.
|
|
3
|
+
"version": "0.17.8",
|
|
4
4
|
"description": "Protobuf-like i18n from plain code: Svelte adapter",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"dev": "tsc --watch",
|
|
@@ -52,7 +52,8 @@
|
|
|
52
52
|
"license": "MIT",
|
|
53
53
|
"dependencies": {
|
|
54
54
|
"svelte": "^5.37.0",
|
|
55
|
-
"
|
|
55
|
+
"magic-string": "^0.30.21",
|
|
56
|
+
"wuchale": "^0.18.8"
|
|
56
57
|
},
|
|
57
58
|
"devDependencies": {
|
|
58
59
|
"acorn": "^8.15.0",
|