@wuchale/svelte 0.18.0 → 0.18.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,5 +1,5 @@
1
- import type { HeuristicFunc, Adapter, AdapterArgs, LoaderChoice, CreateHeuristicOpts } from 'wuchale';
2
- import { type RuntimeCtxSv } from "./transformer.js";
1
+ import type { Adapter, AdapterArgs, CreateHeuristicOpts, HeuristicFunc, LoaderChoice } from 'wuchale';
2
+ import { type RuntimeCtxSv } from './transformer.js';
3
3
  export type { RuntimeCtxSv };
4
4
  export declare function createSvelteHeuristic(opts: CreateHeuristicOpts): HeuristicFunc;
5
5
  /** Default Svelte heuristic which extracts top level variable assignments as well, leading to `$derived` being auto added when needed */
package/dist/index.js CHANGED
@@ -1,10 +1,10 @@
1
- import { defaultGenerateLoadID, deepMergeObjects, createHeuristic, defaultHeuristicOpts } from 'wuchale';
2
- import { SvelteTransformer } from "./transformer.js";
1
+ import { createHeuristic, deepMergeObjects, defaultGenerateLoadID, defaultHeuristicOpts } from 'wuchale';
3
2
  import { loaderPathResolver } from 'wuchale/adapter-utils';
4
3
  import { pluralPattern } from 'wuchale/adapter-vanilla';
4
+ import { SvelteTransformer } from './transformer.js';
5
5
  export function createSvelteHeuristic(opts) {
6
6
  const defaultHeuristic = createHeuristic(opts);
7
- return msg => {
7
+ return (msg) => {
8
8
  const defRes = defaultHeuristic(msg);
9
9
  if (!defRes) {
10
10
  return false;
@@ -22,7 +22,7 @@ export function createSvelteHeuristic(opts) {
22
22
  export const svelteDefaultHeuristic = createSvelteHeuristic(defaultHeuristicOpts);
23
23
  export const svelteKitDefaultHeuristic = createSvelteHeuristic({ ...defaultHeuristicOpts, urlCalls: ['goto'] });
24
24
  /** Default Svelte heuristic which requires `$derived` or `$derived.by` for top level variable assignments */
25
- export const svelteDefaultHeuristicDerivedReq = msg => {
25
+ export const svelteDefaultHeuristicDerivedReq = (msg) => {
26
26
  const defRes = svelteDefaultHeuristic(msg);
27
27
  if (!defRes) {
28
28
  return false;
@@ -50,19 +50,19 @@ const defaultArgs = {
50
50
  runtime: {
51
51
  initReactive: ({ file, funcName, module }) => {
52
52
  const inTopLevel = funcName == null;
53
- return file.endsWith('.svelte.js') || module ? inTopLevel : (inTopLevel ? true : null);
53
+ return file.endsWith('.svelte.js') || module ? inTopLevel : inTopLevel ? true : null;
54
54
  },
55
55
  useReactive: ({ file, funcName, module }) => {
56
56
  const inTopLevel = funcName == null;
57
57
  return file.endsWith('.svelte.js') || module ? inTopLevel : true;
58
58
  },
59
59
  reactive: {
60
- wrapInit: expr => `$derived(${expr})`,
61
- wrapUse: expr => expr,
60
+ wrapInit: (expr) => `$derived(${expr})`,
61
+ wrapUse: (expr) => expr,
62
62
  },
63
63
  plain: {
64
- wrapInit: expr => expr,
65
- wrapUse: expr => expr,
64
+ wrapInit: (expr) => expr,
65
+ wrapUse: (expr) => expr,
66
66
  },
67
67
  },
68
68
  };
@@ -1,9 +1,8 @@
1
- import type { AnyNode, VariableDeclarator } from "acorn";
2
- import { type AST } from "svelte/compiler";
3
- import { Message } from 'wuchale';
1
+ import type { AnyNode, VariableDeclarator } from 'acorn';
2
+ import { type AST } from 'svelte/compiler';
3
+ import type { CatalogExpr, CodePattern, HeuristicFunc, IndexTracker, Message, RuntimeConf, TransformOutput, UrlMatcher } from 'wuchale';
4
+ import { MixedVisitor } from 'wuchale/adapter-utils';
4
5
  import { Transformer } from 'wuchale/adapter-vanilla';
5
- import type { IndexTracker, HeuristicFunc, TransformOutput, CatalogExpr, RuntimeConf, CodePattern, UrlMatcher } from 'wuchale';
6
- import { MixedVisitor, type CommentDirectives } from "wuchale/adapter-utils";
7
6
  type MixedNodesTypes = AST.Text | AST.Tag | AST.ElementLike | AST.Block | AST.Comment;
8
7
  export type RuntimeCtxSv = {
9
8
  module: boolean;
@@ -11,8 +10,6 @@ export type RuntimeCtxSv = {
11
10
  export declare class SvelteTransformer extends Transformer<RuntimeCtxSv> {
12
11
  currentElement?: string;
13
12
  inCompoundText: boolean;
14
- commentDirectivesStack: CommentDirectives[];
15
- lastVisitIsComment: boolean;
16
13
  currentSnippet: number;
17
14
  moduleExportRanges: [number, number][];
18
15
  mixedVisitor: MixedVisitor<MixedNodesTypes>;
@@ -1,10 +1,10 @@
1
- import MagicString from "magic-string";
2
- import { parse, preprocess } from "svelte/compiler";
3
- import { Message } from 'wuchale';
4
- import { Transformer, parseScript } from 'wuchale/adapter-vanilla';
5
- import { MixedVisitor, nonWhitespaceText, processCommentDirectives, varNames } from "wuchale/adapter-utils";
1
+ import MagicString from 'magic-string';
2
+ import { parse, preprocess } from 'svelte/compiler';
3
+ import { MixedVisitor, nonWhitespaceText, varNames } from 'wuchale/adapter-utils';
4
+ import { parseScript, Transformer } from 'wuchale/adapter-vanilla';
6
5
  const nodesWithChildren = ['RegularElement', 'Component'];
7
6
  const rtComponent = 'W_tx_';
7
+ const headerAdd = `\nimport ${rtComponent} from "@wuchale/svelte/runtime.svelte"`;
8
8
  const snipPrefix = '_w_snippet_';
9
9
  const rtModuleVar = varNames.rt + 'mod_';
10
10
  // for use before actually parsing the code,
@@ -21,8 +21,6 @@ export class SvelteTransformer extends Transformer {
21
21
  // state
22
22
  currentElement;
23
23
  inCompoundText = false;
24
- commentDirectivesStack = [];
25
- lastVisitIsComment = false;
26
24
  currentSnippet = 0;
27
25
  moduleExportRanges = []; // to choose which runtime var to use for snippets
28
26
  mixedVisitor;
@@ -34,11 +32,15 @@ export class SvelteTransformer extends Transformer {
34
32
  visitVariableDeclarator = (node) => {
35
33
  const msgs = this.defaultVisitVariableDeclarator(node);
36
34
  const init = node.init;
37
- if (!msgs.length || this.heuristciDetails.declaring != null || init == null || ['ArrowFunctionExpression', 'FunctionExpression'].includes(init.type)) {
35
+ if (!msgs.length ||
36
+ this.heuristciDetails.declaring != null ||
37
+ init == null ||
38
+ ['ArrowFunctionExpression', 'FunctionExpression'].includes(init.type)) {
38
39
  return msgs;
39
40
  }
40
- const needsWrapping = msgs.some(msg => {
41
- if (msg.details.topLevelCall && ['$props', '$state', '$derived', '$derived.by'].includes(msg.details.topLevelCall)) {
41
+ const needsWrapping = msgs.some((msg) => {
42
+ if (msg.details.topLevelCall &&
43
+ ['$props', '$state', '$derived', '$derived.by'].includes(msg.details.topLevelCall)) {
42
44
  return false;
43
45
  }
44
46
  if (msg.details.declaring !== 'variable') {
@@ -59,13 +61,13 @@ export class SvelteTransformer extends Transformer {
59
61
  initMixedVisitor = () => new MixedVisitor({
60
62
  mstr: this.mstr,
61
63
  vars: this.vars,
62
- getRange: node => ({ start: node.start, end: node.end }),
63
- isText: node => node.type === 'Text',
64
- isComment: node => node.type === 'Comment',
65
- leaveInPlace: node => ['ConstTag', 'SnippetBlock'].includes(node.type),
66
- isExpression: node => node.type === 'ExpressionTag',
64
+ getRange: (node) => ({ start: node.start, end: node.end }),
65
+ isText: (node) => node.type === 'Text',
66
+ isComment: (node) => node.type === 'Comment',
67
+ leaveInPlace: (node) => ['ConstTag', 'SnippetBlock'].includes(node.type),
68
+ isExpression: (node) => node.type === 'ExpressionTag',
67
69
  getTextContent: (node) => node.data,
68
- getCommentData: (node) => node.data,
70
+ getCommentData: (node) => node.data.trim(),
69
71
  canHaveChildren: (node) => nodesWithChildren.includes(node.type),
70
72
  visitFunc: (child, inCompoundText) => {
71
73
  const inCompoundTextPrev = this.inCompoundText;
@@ -116,7 +118,7 @@ export class SvelteTransformer extends Transformer {
116
118
  inCompoundText: this.inCompoundText,
117
119
  scope: 'markup',
118
120
  element: this.currentElement,
119
- useComponent: this.currentElement !== 'title'
121
+ useComponent: this.currentElement !== 'title',
120
122
  });
121
123
  visitRegularElement = (node) => {
122
124
  const currentElement = this.currentElement;
@@ -221,10 +223,7 @@ export class SvelteTransformer extends Transformer {
221
223
  return msgs;
222
224
  };
223
225
  visitEachBlock = (node) => {
224
- const msgs = [
225
- ...this.visit(node.expression),
226
- ...this.visitSv(node.body),
227
- ];
226
+ const msgs = [...this.visit(node.expression), ...this.visitSv(node.body)];
228
227
  if (node.key) {
229
228
  msgs.push(...this.visit(node.key));
230
229
  }
@@ -234,10 +233,7 @@ export class SvelteTransformer extends Transformer {
234
233
  return msgs;
235
234
  };
236
235
  visitKeyBlock = (node) => {
237
- return [
238
- ...this.visit(node.expression),
239
- ...this.visitSv(node.fragment),
240
- ];
236
+ return [...this.visit(node.expression), ...this.visitSv(node.fragment)];
241
237
  };
242
238
  visitAwaitBlock = (node) => {
243
239
  const msgs = this.visit(node.expression);
@@ -252,16 +248,16 @@ export class SvelteTransformer extends Transformer {
252
248
  }
253
249
  return msgs;
254
250
  };
255
- visitSvelteBody = (node) => node.attributes.map(this.visitSv).flat();
256
- visitSvelteDocument = (node) => node.attributes.map(this.visitSv).flat();
257
- visitSvelteElement = (node) => node.attributes.map(this.visitSv).flat();
251
+ visitSvelteBody = (node) => node.attributes.flatMap(this.visitSv);
252
+ visitSvelteDocument = (node) => node.attributes.flatMap(this.visitSv);
253
+ visitSvelteElement = (node) => node.attributes.flatMap(this.visitSv);
258
254
  visitSvelteBoundary = (node) => [
259
- ...node.attributes.map(this.visitSv).flat(),
255
+ ...node.attributes.flatMap(this.visitSv),
260
256
  ...this.visitSv(node.fragment),
261
257
  ];
262
258
  visitSvelteHead = (node) => this.visitSv(node.fragment);
263
259
  visitTitleElement = (node) => this.visitRegularElement(node);
264
- visitSvelteWindow = (node) => node.attributes.map(this.visitSv).flat();
260
+ visitSvelteWindow = (node) => node.attributes.flatMap(this.visitSv);
265
261
  visitRoot = (node) => {
266
262
  const msgs = [];
267
263
  if (node.module) {
@@ -287,36 +283,7 @@ export class SvelteTransformer extends Transformer {
287
283
  msgs.push(...this.visitFragment(node.fragment));
288
284
  return msgs;
289
285
  };
290
- visitSv = (node) => {
291
- if (node.type === 'Comment') {
292
- this.commentDirectives = processCommentDirectives(node.data.trim(), this.commentDirectives);
293
- if (this.lastVisitIsComment) {
294
- this.commentDirectivesStack[this.commentDirectivesStack.length - 1] = this.commentDirectives;
295
- }
296
- else {
297
- this.commentDirectivesStack.push(this.commentDirectives);
298
- }
299
- this.lastVisitIsComment = true;
300
- return [];
301
- }
302
- if (node.type === 'Text' && !node.data.trim()) {
303
- return [];
304
- }
305
- let msgs = [];
306
- const commentDirectivesPrev = this.commentDirectives;
307
- if (this.lastVisitIsComment) {
308
- this.commentDirectives = this.commentDirectivesStack.pop();
309
- this.lastVisitIsComment = false;
310
- }
311
- if (this.commentDirectives.ignoreFile) {
312
- return [];
313
- }
314
- if (this.commentDirectives.forceType !== false) {
315
- msgs = this.visit(node);
316
- }
317
- this.commentDirectives = commentDirectivesPrev;
318
- return msgs;
319
- };
286
+ visitSv = (node) => this.visit(node);
320
287
  /** collects the ranges that will be checked if a snippet identifier is exported using RegExp test to simplify */
321
288
  collectModuleExportRanges = (script) => {
322
289
  for (const stmt of script.content.body) {
@@ -353,9 +320,8 @@ export class SvelteTransformer extends Transformer {
353
320
  ast = parse(prepd.code, { modern: true });
354
321
  }
355
322
  else {
356
- const [pAst, comments] = parseScript(this.content);
357
- ast = pAst;
358
- this.comments = comments;
323
+ ;
324
+ [ast, this.comments] = parseScript(this.content);
359
325
  }
360
326
  this.mstr = new MagicString(this.content);
361
327
  this.mixedVisitor = this.initMixedVisitor();
@@ -393,7 +359,6 @@ export class SvelteTransformer extends Transformer {
393
359
  this.mstr.prependRight(instanceStart, `${initRuntime}\n</script>\n`);
394
360
  // now hmr data can be prependRight(0, ...)
395
361
  }
396
- const headerAdd = `\nimport ${rtComponent} from "@wuchale/svelte/runtime.svelte"`;
397
362
  return this.finalize(msgs, headerIndex, headerAdd);
398
363
  };
399
364
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wuchale/svelte",
3
- "version": "0.18.0",
3
+ "version": "0.18.1",
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.19.0"
56
+ "wuchale": "^0.19.1"
57
57
  },
58
58
  "devDependencies": {
59
59
  "acorn": "^8.15.0",
@@ -1,11 +1,11 @@
1
- import toRuntime from "wuchale/runtime"
1
+ import toRuntime from 'wuchale/runtime'
2
2
  import { locales } from '${DATA}'
3
3
 
4
4
  let locale = $state(locales[0])
5
5
 
6
6
  /**
7
7
  * @param {string} newLocale
8
- */
8
+ */
9
9
  export function setLocale(newLocale) {
10
10
  locale = newLocale
11
11
  }
@@ -13,8 +13,8 @@ export function setLocale(newLocale) {
13
13
  // for non-reactive
14
14
  /**
15
15
  * @param {{ [locale: string]: import("wuchale/runtime").CatalogModule }} catalogs
16
- */
17
- export const getRuntime = catalogs => toRuntime(catalogs[locale], locale)
16
+ */
17
+ export const getRuntime = (catalogs) => toRuntime(catalogs[locale], locale)
18
18
 
19
19
  // same function, only will be inside $derived when used
20
20
  export const getRuntimeRx = getRuntime
@@ -1,5 +1,5 @@
1
+ import { defaultCollection, registerLoaders } from 'wuchale/load-utils'
1
2
  import { loadCatalog, loadIDs } from '${PROXY}'
2
- import { registerLoaders, defaultCollection } from 'wuchale/load-utils'
3
3
 
4
4
  const key = '${KEY}'
5
5
 
@@ -1,5 +1,5 @@
1
- import { loadCatalog, loadIDs } from '${PROXY_SYNC}'
2
1
  import { currentRuntime } from 'wuchale/load-utils/server'
2
+ import { loadCatalog, loadIDs } from '${PROXY_SYNC}'
3
3
 
4
4
  const key = '${KEY}'
5
5
 
@@ -1,5 +1,5 @@
1
1
  <script>
2
- const {n = false, x, t, a} = $props()
2
+ const { n = false, x, t, a } = $props()
3
3
  </script>
4
4
 
5
5
  {#each x as fragment, i}