@wuchale/svelte 0.18.1 → 0.19.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/README.md CHANGED
@@ -10,5 +10,5 @@ automatically extracts and replaces translatable strings at build time.
10
10
 
11
11
  - Main documentation: [wuchale.dev](https://wuchale.dev)
12
12
  - Setup instructions: [Getting started](https://wuchale.dev/intro/start/)
13
- - Adapter docs: [JSX](https://wuchale.dev/adapters/svelte)
13
+ - Adapter docs: [Svelte](https://wuchale.dev/adapters/svelte)
14
14
  - Repository: [wuchalejs/wuchale](https://github.com/wuchalejs/wuchale)
package/dist/index.d.ts CHANGED
@@ -9,6 +9,7 @@ export declare const svelteKitDefaultHeuristic: HeuristicFunc;
9
9
  export declare const svelteDefaultHeuristicDerivedReq: HeuristicFunc;
10
10
  type LoadersAvailable = 'svelte' | 'sveltekit';
11
11
  export type SvelteArgs = AdapterArgs<LoadersAvailable, RuntimeCtxSv>;
12
+ export declare const defaultArgs: SvelteArgs;
12
13
  export declare function getDefaultLoaderPath(loader: LoaderChoice<LoadersAvailable>, bundle: boolean): string | {
13
14
  client: string;
14
15
  server: string;
package/dist/index.js CHANGED
@@ -1,10 +1,10 @@
1
- import { createHeuristic, deepMergeObjects, defaultGenerateLoadID, defaultHeuristicOpts } from 'wuchale';
1
+ import { createHeuristic, deepMergeObjects, defaultGenerateLoadID, defaultHeuristicOpts, pofile } from 'wuchale';
2
2
  import { loaderPathResolver } from 'wuchale/adapter-utils';
3
3
  import { pluralPattern } from 'wuchale/adapter-vanilla';
4
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;
@@ -38,9 +38,9 @@ export const svelteDefaultHeuristicDerivedReq = (msg) => {
38
38
  }
39
39
  return false;
40
40
  };
41
- const defaultArgs = {
41
+ export const defaultArgs = {
42
42
  files: ['src/**/*.svelte', 'src/**/*.svelte.{js,ts}'],
43
- localesDir: './src/locales',
43
+ storage: pofile(),
44
44
  patterns: [pluralPattern],
45
45
  heuristic: svelteKitDefaultHeuristic,
46
46
  granularLoad: false,
@@ -57,12 +57,12 @@ const defaultArgs = {
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,5 +1,6 @@
1
1
  import MagicString from 'magic-string';
2
2
  import { parse, preprocess } from 'svelte/compiler';
3
+ import { getKey } from 'wuchale';
3
4
  import { MixedVisitor, nonWhitespaceText, varNames } from 'wuchale/adapter-utils';
4
5
  import { parseScript, Transformer } from 'wuchale/adapter-vanilla';
5
6
  const nodesWithChildren = ['RegularElement', 'Component'];
@@ -8,15 +9,11 @@ const headerAdd = `\nimport ${rtComponent} from "@wuchale/svelte/runtime.svelte"
8
9
  const snipPrefix = '_w_snippet_';
9
10
  const rtModuleVar = varNames.rt + 'mod_';
10
11
  // for use before actually parsing the code,
11
- // to remove the contents of e.g. <style lang="scss">
12
+ // to remove the contents of e.g. <style lang="scss"> which can cause parse errors
12
13
  // 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
- };
14
+ const removeCSS = ({ content }) => ({
15
+ code: ' '.repeat(content.length),
16
+ });
20
17
  export class SvelteTransformer extends Transformer {
21
18
  // state
22
19
  currentElement;
@@ -38,7 +35,7 @@ export class SvelteTransformer extends Transformer {
38
35
  ['ArrowFunctionExpression', 'FunctionExpression'].includes(init.type)) {
39
36
  return msgs;
40
37
  }
41
- const needsWrapping = msgs.some((msg) => {
38
+ const needsWrapping = msgs.some(msg => {
42
39
  if (msg.details.topLevelCall &&
43
40
  ['$props', '$state', '$derived', '$derived.by'].includes(msg.details.topLevelCall)) {
44
41
  return false;
@@ -61,11 +58,11 @@ export class SvelteTransformer extends Transformer {
61
58
  initMixedVisitor = () => new MixedVisitor({
62
59
  mstr: this.mstr,
63
60
  vars: this.vars,
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',
61
+ getRange: node => ({ start: node.start, end: node.end }),
62
+ isText: node => node.type === 'Text',
63
+ isComment: node => node.type === 'Comment',
64
+ leaveInPlace: node => ['ConstTag', 'SnippetBlock'].includes(node.type),
65
+ isExpression: node => node.type === 'ExpressionTag',
69
66
  getTextContent: (node) => node.data,
70
67
  getCommentData: (node) => node.data.trim(),
71
68
  canHaveChildren: (node) => nodesWithChildren.includes(node.type),
@@ -100,7 +97,7 @@ export class SvelteTransformer extends Transformer {
100
97
  begin += `{${this.vars().nestCtx}} n`;
101
98
  }
102
99
  else {
103
- const index = this.index.get(msgInfo.toKey());
100
+ const index = this.index.get(getKey(msgInfo.msgStr, msgInfo.context));
104
101
  begin += `{${this.vars().rtCtx}(${index})}`;
105
102
  }
106
103
  let end = ' />\n';
@@ -141,7 +138,7 @@ export class SvelteTransformer extends Transformer {
141
138
  if (!pass) {
142
139
  return [];
143
140
  }
144
- this.mstr.update(node.start + startWh, node.end - endWh, `{${this.vars().rtTrans}(${this.index.get(msgInfo.toKey())})}`);
141
+ this.mstr.update(node.start + startWh, node.end - endWh, `{${this.vars().rtTrans}(${this.index.get(getKey(msgInfo.msgStr, msgInfo.context))})}`);
145
142
  return [msgInfo];
146
143
  };
147
144
  visitSpreadAttribute = (node) => this.visit(node.expression);
@@ -188,7 +185,7 @@ export class SvelteTransformer extends Transformer {
188
185
  if (!pass) {
189
186
  return [];
190
187
  }
191
- this.mstr.update(value.start, value.end, `{${this.vars().rtTrans}(${this.index.get(msgInfo.toKey())})}`);
188
+ this.mstr.update(value.start, value.end, `{${this.literalRepl(msgInfo)}}`);
192
189
  if (`'"`.includes(this.content[value.start - 1])) {
193
190
  this.mstr.remove(value.start - 1, value.start);
194
191
  this.mstr.remove(value.end, value.end + 1);
@@ -316,7 +313,7 @@ export class SvelteTransformer extends Transformer {
316
313
  const isComponent = this.heuristciDetails.file.endsWith('.svelte');
317
314
  let ast;
318
315
  if (isComponent) {
319
- const prepd = await preprocess(this.content, { style: removeSCSS });
316
+ const prepd = await preprocess(this.content, { style: removeCSS });
320
317
  ast = parse(prepd.code, { modern: true });
321
318
  }
322
319
  else {
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@wuchale/svelte",
3
- "version": "0.18.1",
3
+ "version": "0.19.0",
4
4
  "description": "Protobuf-like i18n from plain code: Svelte adapter",
5
5
  "scripts": {
6
6
  "dev": "tsc --watch",
7
7
  "build": "tsc",
8
- "test": "node tests/index.ts"
8
+ "test": "node --import ../wuchale/testing/resolve.ts --test"
9
9
  },
10
10
  "keywords": [
11
11
  "i18n",
@@ -51,12 +51,12 @@
51
51
  "author": "K1DV5",
52
52
  "license": "MIT",
53
53
  "dependencies": {
54
- "svelte": "^5.37.0",
55
54
  "magic-string": "^0.30.21",
56
- "wuchale": "^0.19.1"
55
+ "svelte": "^5",
56
+ "wuchale": "^0.21.0"
57
57
  },
58
58
  "devDependencies": {
59
- "acorn": "^8.15.0",
59
+ "acorn": "^8.16.0",
60
60
  "typescript": "^5.9.3"
61
61
  },
62
62
  "type": "module"
@@ -4,7 +4,7 @@ import { locales } from '${DATA}'
4
4
  let locale = $state(locales[0])
5
5
 
6
6
  /**
7
- * @param {string} newLocale
7
+ * @param {import('${DATA}').Locale} newLocale
8
8
  */
9
9
  export function setLocale(newLocale) {
10
10
  locale = newLocale
@@ -14,7 +14,7 @@ export function setLocale(newLocale) {
14
14
  /**
15
15
  * @param {{ [locale: string]: import("wuchale/runtime").CatalogModule }} catalogs
16
16
  */
17
- export const getRuntime = (catalogs) => toRuntime(catalogs[locale], locale)
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