@wuchale/svelte 0.17.8 → 0.18.0

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 CHANGED
@@ -1,4 +1,6 @@
1
1
  import type { HeuristicFunc, Adapter, AdapterArgs, LoaderChoice, CreateHeuristicOpts } from 'wuchale';
2
+ import { type RuntimeCtxSv } from "./transformer.js";
3
+ export type { RuntimeCtxSv };
2
4
  export declare function createSvelteHeuristic(opts: CreateHeuristicOpts): HeuristicFunc;
3
5
  /** Default Svelte heuristic which extracts top level variable assignments as well, leading to `$derived` being auto added when needed */
4
6
  export declare const svelteDefaultHeuristic: HeuristicFunc;
@@ -6,10 +8,9 @@ export declare const svelteKitDefaultHeuristic: HeuristicFunc;
6
8
  /** Default Svelte heuristic which requires `$derived` or `$derived.by` for top level variable assignments */
7
9
  export declare const svelteDefaultHeuristicDerivedReq: HeuristicFunc;
8
10
  type LoadersAvailable = 'svelte' | 'sveltekit';
9
- export type SvelteArgs = AdapterArgs<LoadersAvailable>;
11
+ export type SvelteArgs = AdapterArgs<LoadersAvailable, RuntimeCtxSv>;
10
12
  export declare function getDefaultLoaderPath(loader: LoaderChoice<LoadersAvailable>, bundle: boolean): string | {
11
13
  client: string;
12
14
  server: string;
13
15
  } | null;
14
16
  export declare const adapter: (args?: Partial<SvelteArgs>) => Adapter;
15
- export {};
package/dist/index.js CHANGED
@@ -48,13 +48,13 @@ const defaultArgs = {
48
48
  generateLoadID: defaultGenerateLoadID,
49
49
  loader: 'svelte',
50
50
  runtime: {
51
- useReactive: ({ file, funcName, additional }) => {
51
+ initReactive: ({ file, funcName, module }) => {
52
52
  const inTopLevel = funcName == null;
53
- const inModule = file.endsWith('.svelte.js') || additional.module;
54
- return {
55
- init: inModule ? inTopLevel : (inTopLevel ? true : null),
56
- use: inModule ? inTopLevel : true,
57
- };
53
+ return file.endsWith('.svelte.js') || module ? inTopLevel : (inTopLevel ? true : null);
54
+ },
55
+ useReactive: ({ file, funcName, module }) => {
56
+ const inTopLevel = funcName == null;
57
+ return file.endsWith('.svelte.js') || module ? inTopLevel : true;
58
58
  },
59
59
  reactive: {
60
60
  wrapInit: expr => `$derived(${expr})`,
@@ -5,7 +5,10 @@ import { Transformer } from 'wuchale/adapter-vanilla';
5
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
- export declare class SvelteTransformer extends Transformer {
8
+ export type RuntimeCtxSv = {
9
+ module: boolean;
10
+ };
11
+ export declare class SvelteTransformer extends Transformer<RuntimeCtxSv> {
9
12
  currentElement?: string;
10
13
  inCompoundText: boolean;
11
14
  commentDirectivesStack: CommentDirectives[];
@@ -13,7 +16,7 @@ export declare class SvelteTransformer extends Transformer {
13
16
  currentSnippet: number;
14
17
  moduleExportRanges: [number, number][];
15
18
  mixedVisitor: MixedVisitor<MixedNodesTypes>;
16
- constructor(content: string, filename: string, index: IndexTracker, heuristic: HeuristicFunc, patterns: CodePattern[], catalogExpr: CatalogExpr, rtConf: RuntimeConf, matchUrl: UrlMatcher);
19
+ constructor(content: string, filename: string, index: IndexTracker, heuristic: HeuristicFunc, patterns: CodePattern[], catalogExpr: CatalogExpr, rtConf: RuntimeConf<RuntimeCtxSv>, matchUrl: UrlMatcher);
17
20
  visitExpressionTag: (node: AST.ExpressionTag) => Message[];
18
21
  visitVariableDeclarator: (node: VariableDeclarator) => Message[];
19
22
  initMixedVisitor: () => MixedVisitor<MixedNodesTypes>;
@@ -28,12 +28,13 @@ export class SvelteTransformer extends Transformer {
28
28
  mixedVisitor;
29
29
  constructor(content, filename, index, heuristic, patterns, catalogExpr, rtConf, matchUrl) {
30
30
  super(content, filename, index, heuristic, patterns, catalogExpr, rtConf, matchUrl, [varNames.rt, rtModuleVar]);
31
+ this.heuristciDetails.insideProgram = false;
31
32
  }
32
33
  visitExpressionTag = (node) => this.visit(node.expression);
33
34
  visitVariableDeclarator = (node) => {
34
35
  const msgs = this.defaultVisitVariableDeclarator(node);
35
36
  const init = node.init;
36
- if (!msgs.length || this.declaring != null || init == null || ['ArrowFunctionExpression', 'FunctionExpression'].includes(init.type)) {
37
+ if (!msgs.length || this.heuristciDetails.declaring != null || init == null || ['ArrowFunctionExpression', 'FunctionExpression'].includes(init.type)) {
37
38
  return msgs;
38
39
  }
39
40
  const needsWrapping = msgs.some(msg => {
@@ -61,7 +62,7 @@ export class SvelteTransformer extends Transformer {
61
62
  getRange: node => ({ start: node.start, end: node.end }),
62
63
  isText: node => node.type === 'Text',
63
64
  isComment: node => node.type === 'Comment',
64
- leaveInPlace: node => node.type === 'ConstTag',
65
+ leaveInPlace: node => ['ConstTag', 'SnippetBlock'].includes(node.type),
65
66
  isExpression: node => node.type === 'ExpressionTag',
66
67
  getTextContent: (node) => node.data,
67
68
  getCommentData: (node) => node.data,
@@ -86,7 +87,7 @@ export class SvelteTransformer extends Transformer {
86
87
  this.currentSnippet++;
87
88
  const snippetBegin = `\n{#snippet ${snippetName}(${haveCtx ? this.vars().nestCtx : ''})}\n`;
88
89
  this.mstr.appendRight(childStart, snippetBegin);
89
- this.mstr.prependLeft(childEnd, '\n{/snippet}');
90
+ this.mstr.prependLeft(childEnd, '\n{/snippet}\n');
90
91
  }
91
92
  let begin = `\n<${rtComponent}`;
92
93
  if (snippets.length) {
@@ -266,17 +267,17 @@ export class SvelteTransformer extends Transformer {
266
267
  if (node.module) {
267
268
  const prevRtVar = this.currentRtVar;
268
269
  this.currentRtVar = rtModuleVar;
269
- this.additionalState = { module: true };
270
+ this.runtimeCtx = { module: true };
270
271
  this.commentDirectives = {}; // reset
271
272
  // @ts-expect-error
272
273
  msgs.push(...this.visitProgram(node.module.content));
273
- const runtimeInit = this.initRuntime(this.filename);
274
+ const runtimeInit = this.initRuntime();
274
275
  if (runtimeInit) {
275
276
  this.mstr.appendRight(
276
277
  // @ts-expect-error
277
278
  this.getRealBodyStart(node.module.content.body) ?? node.module.content.start, runtimeInit);
278
279
  }
279
- this.additionalState = {}; // reset
280
+ this.runtimeCtx = { module: false }; // reset
280
281
  this.currentRtVar = prevRtVar; // reset
281
282
  }
282
283
  if (node.instance) {
@@ -345,7 +346,7 @@ export class SvelteTransformer extends Transformer {
345
346
  }
346
347
  };
347
348
  transformSv = async () => {
348
- const isComponent = this.filename.endsWith('.svelte');
349
+ const isComponent = this.heuristciDetails.file.endsWith('.svelte');
349
350
  let ast;
350
351
  if (isComponent) {
351
352
  const prepd = await preprocess(this.content, { style: removeSCSS });
@@ -362,7 +363,7 @@ export class SvelteTransformer extends Transformer {
362
363
  this.collectModuleExportRanges(ast.module);
363
364
  }
364
365
  const msgs = this.visitSv(ast);
365
- const initRuntime = this.initRuntime(this.filename);
366
+ const initRuntime = this.initRuntime();
366
367
  if (ast.type === 'Program') {
367
368
  const bodyStart = this.getRealBodyStart(ast.body) ?? 0;
368
369
  if (initRuntime) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wuchale/svelte",
3
- "version": "0.17.8",
3
+ "version": "0.18.0",
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
  "svelte": "^5.37.0",
55
55
  "magic-string": "^0.30.21",
56
- "wuchale": "^0.18.8"
56
+ "wuchale": "^0.19.0"
57
57
  },
58
58
  "devDependencies": {
59
59
  "acorn": "^8.15.0",