@wuchale/svelte 0.16.6 → 0.17.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,6 +1,14 @@
1
- import type { HeuristicFunc, Adapter, AdapterArgs } from 'wuchale';
1
+ import type { HeuristicFunc, Adapter, AdapterArgs, LoaderChoice, CreateHeuristicOpts } from 'wuchale';
2
+ export declare function createSvelteHeuristic(opts: CreateHeuristicOpts): HeuristicFunc;
2
3
  /** Default Svelte heuristic which extracts top level variable assignments as well, leading to `$derived` being auto added when needed */
3
4
  export declare const svelteDefaultHeuristic: HeuristicFunc;
5
+ export declare const svelteKitDefaultHeuristic: HeuristicFunc;
4
6
  /** Default Svelte heuristic which requires `$derived` or `$derived.by` for top level variable assignments */
5
7
  export declare const svelteDefaultHeuristicDerivedReq: HeuristicFunc;
6
- export declare const adapter: (args?: AdapterArgs) => Adapter;
8
+ type LoadersAvailable = 'svelte' | 'sveltekit';
9
+ export declare function getDefaultLoaderPath(loader: LoaderChoice<LoadersAvailable>, bundle: boolean): string | {
10
+ client: string;
11
+ server: string;
12
+ };
13
+ export declare const adapter: (args?: AdapterArgs<LoadersAvailable>) => Adapter;
14
+ export {};
package/dist/index.js CHANGED
@@ -1,42 +1,52 @@
1
- import { defaultGenerateLoadID, defaultHeuristic, deepMergeObjects } from 'wuchale';
1
+ import { defaultGenerateLoadID, deepMergeObjects, createHeuristic, defaultHeuristicOpts } from 'wuchale';
2
2
  import { SvelteTransformer } from "./transformer.js";
3
- import { getDependencies, loaderPathResolver } from 'wuchale/adapter-utils';
3
+ import { loaderPathResolver } from 'wuchale/adapter-utils';
4
4
  import { pluralPattern } from 'wuchale/adapter-vanilla';
5
+ export function createSvelteHeuristic(opts) {
6
+ const defaultHeuristic = createHeuristic(opts);
7
+ return msg => {
8
+ const defRes = defaultHeuristic(msg);
9
+ if (!defRes) {
10
+ return false;
11
+ }
12
+ if (msg.details.scope !== 'script') {
13
+ return defRes;
14
+ }
15
+ if (msg.details.call === '$inspect') {
16
+ return false;
17
+ }
18
+ return defRes;
19
+ };
20
+ }
5
21
  /** Default Svelte heuristic which extracts top level variable assignments as well, leading to `$derived` being auto added when needed */
6
- export const svelteDefaultHeuristic = msg => {
7
- if (!defaultHeuristic(msg)) {
8
- return false;
9
- }
10
- if (msg.details.scope !== 'script') {
11
- return true;
12
- }
13
- if (msg.details.call === '$inspect') {
14
- return false;
15
- }
16
- return true;
17
- };
22
+ export const svelteDefaultHeuristic = createSvelteHeuristic(defaultHeuristicOpts);
23
+ export const svelteKitDefaultHeuristic = createSvelteHeuristic({ ...defaultHeuristicOpts, urlCalls: ['goto'] });
18
24
  /** Default Svelte heuristic which requires `$derived` or `$derived.by` for top level variable assignments */
19
25
  export const svelteDefaultHeuristicDerivedReq = msg => {
20
- if (!svelteDefaultHeuristic(msg)) {
26
+ const defRes = svelteDefaultHeuristic(msg);
27
+ if (!defRes) {
21
28
  return false;
22
29
  }
23
30
  if (msg.details.scope !== 'script' || msg.details.declaring !== 'variable') {
24
- return true;
31
+ return defRes;
25
32
  }
26
33
  if (!msg.details.topLevelCall) {
27
34
  return false;
28
35
  }
29
- return ['$derived', '$derived.by'].includes(msg.details.topLevelCall);
36
+ if (['$derived', '$derived.by'].includes(msg.details.topLevelCall)) {
37
+ return defRes;
38
+ }
39
+ return false;
30
40
  };
31
41
  const defaultArgs = {
32
42
  files: ['src/**/*.svelte', 'src/**/*.svelte.{js,ts}'],
33
- catalog: './src/locales/{locale}',
43
+ localesDir: './src/locales',
34
44
  patterns: [pluralPattern],
35
- heuristic: svelteDefaultHeuristic,
45
+ heuristic: svelteKitDefaultHeuristic,
36
46
  granularLoad: false,
37
47
  bundleLoad: false,
38
48
  generateLoadID: defaultGenerateLoadID,
39
- writeFiles: {},
49
+ loader: 'svelte',
40
50
  runtime: {
41
51
  useReactive: ({ file, funcName, additional }) => {
42
52
  const inTopLevel = funcName == null;
@@ -47,47 +57,37 @@ const defaultArgs = {
47
57
  };
48
58
  },
49
59
  reactive: {
50
- importName: 'default',
51
60
  wrapInit: expr => `$derived(${expr})`,
52
61
  wrapUse: expr => expr,
53
62
  },
54
63
  plain: {
55
- importName: 'get',
56
64
  wrapInit: expr => expr,
57
65
  wrapUse: expr => expr,
58
66
  },
59
67
  },
60
68
  };
61
69
  const resolveLoaderPath = loaderPathResolver(import.meta.url, '../src/loaders', 'svelte.js');
70
+ export function getDefaultLoaderPath(loader, bundle) {
71
+ if (bundle) {
72
+ return resolveLoaderPath('bundle');
73
+ }
74
+ if (loader === 'sveltekit') {
75
+ return {
76
+ client: resolveLoaderPath('svelte'),
77
+ server: resolveLoaderPath('sveltekit.ssr'),
78
+ };
79
+ }
80
+ return resolveLoaderPath(loader);
81
+ }
62
82
  export const adapter = (args = defaultArgs) => {
63
- const { heuristic, patterns, runtime, ...rest } = deepMergeObjects(args, defaultArgs);
83
+ const { heuristic, patterns, runtime, loader, ...rest } = deepMergeObjects(args, defaultArgs);
64
84
  return {
65
- transform: ({ content, filename, index, expr }) => {
66
- return new SvelteTransformer(content, filename, index, heuristic, patterns, expr, runtime).transformSv();
85
+ transform: ({ content, filename, index, expr, matchUrl }) => {
86
+ return new SvelteTransformer(content, filename, index, heuristic, patterns, expr, runtime, matchUrl).transformSv();
67
87
  },
68
88
  loaderExts: ['.svelte.js', '.svelte.ts', '.js', '.ts'],
69
- defaultLoaders: async () => {
70
- if (rest.bundleLoad) {
71
- return ['bundle'];
72
- }
73
- const deps = await getDependencies();
74
- const available = ['svelte'];
75
- if (deps.has('@sveltejs/kit')) {
76
- available.unshift('sveltekit');
77
- }
78
- return available;
79
- },
80
- defaultLoaderPath: loader => {
81
- if (loader === 'sveltekit') {
82
- return {
83
- client: resolveLoaderPath('svelte'),
84
- server: resolveLoaderPath('sveltekit.ssr'),
85
- };
86
- }
87
- return resolveLoaderPath(loader);
88
- },
89
+ defaultLoaderPath: getDefaultLoaderPath(loader, rest.bundleLoad),
89
90
  runtime,
90
91
  ...rest,
91
- docsUrl: 'https://wuchale.dev/adapters/svelte'
92
92
  };
93
93
  };
@@ -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);
16
+ constructor(content: string, filename: string, index: IndexTracker, heuristic: HeuristicFunc, patterns: CodePattern[], catalogExpr: CatalogExpr, rtConf: RuntimeConf, matchUrl: (url: string) => string);
17
17
  visitExpressionTag: (node: AST.ExpressionTag) => Message[];
18
18
  visitVariableDeclarator: (node: VariableDeclarator) => Message[];
19
19
  initMixedVisitor: () => MixedVisitor<MixedNodesTypes>;
@@ -16,8 +16,8 @@ export class SvelteTransformer extends Transformer {
16
16
  currentSnippet = 0;
17
17
  moduleExportRanges = []; // to choose which runtime var to use for snippets
18
18
  mixedVisitor;
19
- constructor(content, filename, index, heuristic, patterns, catalogExpr, rtConf) {
20
- super(content, filename, index, heuristic, patterns, catalogExpr, rtConf, [varNames.rt, rtModuleVar]);
19
+ constructor(content, filename, index, heuristic, patterns, catalogExpr, rtConf, matchUrl) {
20
+ super(content, filename, index, heuristic, patterns, catalogExpr, rtConf, matchUrl, [varNames.rt, rtModuleVar]);
21
21
  }
22
22
  visitExpressionTag = (node) => this.visit(node.expression);
23
23
  visitVariableDeclarator = (node) => {
@@ -64,7 +64,7 @@ export class SvelteTransformer extends Transformer {
64
64
  },
65
65
  visitExpressionTag: this.visitExpressionTag,
66
66
  fullHeuristicDetails: this.fullHeuristicDetails,
67
- checkHeuristic: this.checkHeuristicBool,
67
+ checkHeuristic: this.getHeuristicMessageType,
68
68
  index: this.index,
69
69
  wrapNested: (msgInfo, hasExprs, nestedRanges, lastChildEnd) => {
70
70
  const snippets = [];
@@ -153,14 +153,24 @@ export class SvelteTransformer extends Transformer {
153
153
  });
154
154
  }
155
155
  const value = values[0];
156
- if (value.type !== 'Text') {
157
- return this.visitSv(value);
158
- }
159
- const [pass, msgInfo] = this.checkHeuristic(value.data, {
160
- scope: 'attribute',
156
+ const heuDetails = {
157
+ scope: 'script',
161
158
  element: this.currentElement,
162
159
  attribute: node.name,
163
- });
160
+ };
161
+ if (value.type === 'ExpressionTag') {
162
+ if (value.expression.type === 'Literal') {
163
+ const expr = value.expression;
164
+ return this.visitWithCommentDirectives(expr, () => this.visitLiteral(expr, heuDetails));
165
+ }
166
+ if (value.expression.type === 'TemplateLiteral') {
167
+ const expr = value.expression;
168
+ return this.visitWithCommentDirectives(expr, () => this.visitTemplateLiteral(expr, heuDetails));
169
+ }
170
+ return this.visitSv(value);
171
+ }
172
+ heuDetails.scope = 'attribute';
173
+ const [pass, msgInfo] = this.checkHeuristic(value.data, heuDetails);
164
174
  if (!pass) {
165
175
  return [];
166
176
  }
@@ -286,7 +296,7 @@ export class SvelteTransformer extends Transformer {
286
296
  if (this.commentDirectives.ignoreFile) {
287
297
  return [];
288
298
  }
289
- if (this.commentDirectives.forceInclude !== false) {
299
+ if (this.commentDirectives.forceType !== false) {
290
300
  msgs = this.visit(node);
291
301
  }
292
302
  this.commentDirectives = commentDirectivesPrev;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wuchale/svelte",
3
- "version": "0.16.6",
3
+ "version": "0.17.0",
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.5"
55
+ "wuchale": "^0.18.0"
56
56
  },
57
57
  "devDependencies": {
58
58
  "acorn": "^8.15.0",
@@ -1,6 +1,8 @@
1
1
  // This is just the default loader.
2
2
  // You can customize it however you want, it will not be overwritten once it exists and is not empty.
3
3
 
4
+ import toRuntime from "wuchale/runtime"
5
+
4
6
  let locale = $state('en')
5
7
 
6
8
  /**
@@ -14,7 +16,7 @@ export function setLocale(newLocale) {
14
16
  /**
15
17
  * @param {{ [locale: string]: import("wuchale/runtime").CatalogModule }} catalogs
16
18
  */
17
- export const get = catalogs => catalogs[locale]
19
+ export const getRuntime = catalogs => toRuntime(catalogs[locale], locale)
18
20
 
19
21
  // same function, only will be inside $derived when used
20
- export default get
22
+ export const getRuntimeRx = getRuntime
@@ -1,15 +1,15 @@
1
1
  // This is just the default loader.
2
2
  // You can customize it however you want, it will not be overwritten once it exists and is not empty.
3
3
 
4
- /// <reference types="wuchale/virtual" />
5
-
6
- import { loadCatalog, loadIDs, key } from 'virtual:wuchale/proxy' // or proxy/sync
4
+ import { loadCatalog, loadIDs } from '${PROXY}'
7
5
  import { registerLoaders, defaultCollection } from 'wuchale/load-utils'
8
6
 
9
- const catalogs = $state({})
7
+ const key = '${KEY}'
8
+
9
+ const runtimes = $state({})
10
10
 
11
11
  // for non-reactive
12
- export const get = registerLoaders(key, loadCatalog, loadIDs, defaultCollection(catalogs))
12
+ export const getRuntime = registerLoaders(key, loadCatalog, loadIDs, defaultCollection(runtimes))
13
13
 
14
14
  // same function, only will be inside $derived when used
15
- export default get
15
+ export const getRuntimeRx = getRuntime
@@ -1,15 +1,15 @@
1
1
  // This is just the default loader.
2
2
  // You can customize it however you want, it will not be overwritten once it exists and is not empty.
3
3
 
4
- /// <reference types="wuchale/virtual" />
4
+ import { loadCatalog, loadIDs } from '${PROXY_SYNC}'
5
+ import { currentRuntime } from 'wuchale/load-utils/server'
5
6
 
6
- import { loadCatalog, loadIDs, key } from 'virtual:wuchale/proxy/sync' // because it's on the server
7
- import { currentCatalog } from 'wuchale/load-utils/server'
7
+ const key = '${KEY}'
8
8
 
9
9
  export { loadCatalog, loadIDs, key } // for hooks.server.{js,ts}
10
10
 
11
11
  // for non-reactive
12
- export const get = (/** @type {string} */ loadID) => currentCatalog(key, loadID)
12
+ export const getRuntime = (/** @type {string} */ loadID) => currentRuntime(key, loadID)
13
13
 
14
14
  // same function, only will be inside $derived when used
15
- export default get
15
+ export const getRuntimeRx = getRuntime