@wuchale/svelte 0.16.0 → 0.16.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/index.d.ts CHANGED
@@ -1,2 +1,6 @@
1
- import type { Adapter, AdapterArgs } from 'wuchale';
1
+ import type { HeuristicFunc, Adapter, AdapterArgs } from 'wuchale';
2
+ /** Default Svelte heuristic which extracts top level variable assignments as well, leading to `$derived` being auto added when needed */
3
+ export declare const svelteDefaultHeuristic: HeuristicFunc;
4
+ /** Default Svelte heuristic which requires `$derived` or `$derived.by` for top level variable assignments */
5
+ export declare const svelteDefaultHeuristicDerivedReq: HeuristicFunc;
2
6
  export declare const adapter: (args?: AdapterArgs) => Adapter;
package/dist/index.js CHANGED
@@ -2,7 +2,8 @@ import { defaultGenerateLoadID, defaultHeuristic, deepMergeObjects } from 'wucha
2
2
  import { SvelteTransformer } from "./transformer.js";
3
3
  import { getDependencies, loaderPathResolver } from 'wuchale/adapter-utils';
4
4
  import { pluralPattern } from 'wuchale/adapter-vanilla';
5
- const svelteHeuristic = msg => {
5
+ /** Default Svelte heuristic which extracts top level variable assignments as well, leading to `$derived` being auto added when needed */
6
+ export const svelteDefaultHeuristic = msg => {
6
7
  if (!defaultHeuristic(msg)) {
7
8
  return false;
8
9
  }
@@ -14,11 +15,24 @@ const svelteHeuristic = msg => {
14
15
  }
15
16
  return true;
16
17
  };
18
+ /** Default Svelte heuristic which requires `$derived` or `$derived.by` for top level variable assignments */
19
+ export const svelteDefaultHeuristicDerivedReq = msg => {
20
+ if (!svelteDefaultHeuristic(msg)) {
21
+ return false;
22
+ }
23
+ if (msg.details.scope !== 'script' || msg.details.declaring !== 'variable') {
24
+ return true;
25
+ }
26
+ if (!msg.details.topLevelCall) {
27
+ return false;
28
+ }
29
+ return ['$derived', '$derived.by'].includes(msg.details.topLevelCall);
30
+ };
17
31
  const defaultArgs = {
18
32
  files: ['src/**/*.svelte', 'src/**/*.svelte.{js,ts}'],
19
33
  catalog: './src/locales/{locale}',
20
34
  patterns: [pluralPattern],
21
- heuristic: svelteHeuristic,
35
+ heuristic: svelteDefaultHeuristic,
22
36
  granularLoad: false,
23
37
  bundleLoad: false,
24
38
  generateLoadID: defaultGenerateLoadID,
@@ -11,6 +11,7 @@ export declare class SvelteTransformer extends Transformer {
11
11
  commentDirectivesStack: CommentDirectives[];
12
12
  lastVisitIsComment: boolean;
13
13
  currentSnippet: number;
14
+ hasModuleScript: boolean;
14
15
  mixedVisitor: MixedVisitor<MixedNodesTypes>;
15
16
  constructor(content: string, filename: string, index: IndexTracker, heuristic: HeuristicFunc, patterns: CodePattern[], catalogExpr: CatalogExpr, rtConf: RuntimeConf);
16
17
  visitExpressionTag: (node: AST.ExpressionTag) => Message[];
@@ -14,6 +14,7 @@ export class SvelteTransformer extends Transformer {
14
14
  commentDirectivesStack = [];
15
15
  lastVisitIsComment = false;
16
16
  currentSnippet = 0;
17
+ hasModuleScript = false; // to choose which runtime var to use for snippets
17
18
  mixedVisitor;
18
19
  constructor(content, filename, index, heuristic, patterns, catalogExpr, rtConf) {
19
20
  super(content, filename, index, heuristic, patterns, catalogExpr, rtConf, [varNames.rt, rtModuleVar]);
@@ -165,7 +166,16 @@ export class SvelteTransformer extends Transformer {
165
166
  }
166
167
  return [msgInfo];
167
168
  };
168
- visitSnippetBlock = (node) => this.visitFragment(node.body);
169
+ visitSnippetBlock = (node) => {
170
+ // use module runtime var because the snippet may be exported from the module
171
+ const prevRtVar = this.currentRtVar;
172
+ if (this.hasModuleScript) {
173
+ this.currentRtVar = rtModuleVar;
174
+ }
175
+ const msgs = this.visitFragment(node.body);
176
+ this.currentRtVar = prevRtVar;
177
+ return msgs;
178
+ };
169
179
  visitIfBlock = (node) => {
170
180
  const msgs = this.visit(node.test);
171
181
  msgs.push(...this.visitSv(node.consequent));
@@ -218,7 +228,6 @@ export class SvelteTransformer extends Transformer {
218
228
  visitSvelteWindow = (node) => node.attributes.map(this.visitSv).flat();
219
229
  visitRoot = (node) => {
220
230
  const msgs = [];
221
- // @ts-ignore: module is a reserved keyword, not sure how to specify the type
222
231
  if (node.module) {
223
232
  const prevRtVar = this.currentRtVar;
224
233
  this.currentRtVar = rtModuleVar;
@@ -282,6 +291,7 @@ export class SvelteTransformer extends Transformer {
282
291
  }
283
292
  this.mstr = new MagicString(this.content);
284
293
  this.mixedVisitor = this.initMixedVisitor();
294
+ this.hasModuleScript = ast.type === 'Root' && ast.module != null;
285
295
  const msgs = this.visitSv(ast);
286
296
  const initRuntime = this.initRuntime(this.filename, null, null, {});
287
297
  if (ast.type === 'Program') {
@@ -291,7 +301,7 @@ export class SvelteTransformer extends Transformer {
291
301
  }
292
302
  let headerIndex = 0;
293
303
  if (ast.module) {
294
- // @ts-ignore
304
+ // @ts-expect-error
295
305
  headerIndex = this.getRealBodyStart(ast.module.content.body) ?? ast.module.content.start;
296
306
  }
297
307
  if (ast.instance) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wuchale/svelte",
3
- "version": "0.16.0",
3
+ "version": "0.16.1",
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.17.0"
55
+ "wuchale": "^0.17.1"
56
56
  },
57
57
  "devDependencies": {
58
58
  "acorn": "^8.15.0",