@wuchale/astro 0.2.8 → 0.3.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,4 +1,4 @@
1
- import type { Adapter, AdapterArgs, CreateHeuristicOpts, HeuristicFunc, LoaderChoice } from 'wuchale';
1
+ import type { Adapter, AdapterArgs, CreateHeuristicOpts, HeuristicFunc, LoaderChoice, RuntimeConf } from 'wuchale';
2
2
  /**
3
3
  * Create a heuristic function optimized for Astro files
4
4
  * Uses the default heuristic which handles translatable vs non-translatable strings
@@ -7,9 +7,10 @@ export declare function createAstroHeuristic(opts: CreateHeuristicOpts): Heurist
7
7
  /** Default Svelte heuristic which extracts top level variable assignments as well, leading to `$derived` being auto added when needed */
8
8
  export declare const astroDefaultHeuristic: HeuristicFunc;
9
9
  type LoadersAvailable = 'default';
10
- export type AstroArgs = AdapterArgs<LoadersAvailable>;
10
+ export type AstroArgs = Omit<AdapterArgs<LoadersAvailable>, 'bundleLoad' | 'granularLoad' | 'generateLoadID' | 'runtime'>;
11
+ export declare const defaultRuntime: RuntimeConf;
11
12
  export declare const defaultArgs: AstroArgs;
12
- export declare function getDefaultLoaderPath(loader: LoaderChoice<LoadersAvailable>, bundle: boolean): string | null;
13
+ export declare function getDefaultLoaderPath(loader: LoaderChoice<LoadersAvailable>): string | null;
13
14
  /**
14
15
  * Create an Astro adapter for wuchale
15
16
  *
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
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 { AstroTransformer } from './transformer.js';
@@ -24,7 +24,7 @@ export function createAstroHeuristic(opts) {
24
24
  }
25
25
  /** Default Svelte heuristic which extracts top level variable assignments as well, leading to `$derived` being auto added when needed */
26
26
  export const astroDefaultHeuristic = createAstroHeuristic(defaultHeuristicOpts);
27
- const defaultRuntime = {
27
+ export const defaultRuntime = {
28
28
  // Astro is SSR-only, so we use non-reactive runtime by default
29
29
  initReactive: ({ funcName }) => (funcName == null ? false : null), // Only init in top-level functions
30
30
  // Astro is SSR - always use non-reactive
@@ -40,26 +40,18 @@ const defaultRuntime = {
40
40
  };
41
41
  export const defaultArgs = {
42
42
  files: 'src/**/*.astro',
43
- localesDir: './src/locales',
43
+ storage: pofile(),
44
44
  patterns: [pluralPattern],
45
45
  heuristic: astroDefaultHeuristic,
46
- granularLoad: false,
47
- bundleLoad: false,
48
46
  loader: 'default',
49
- generateLoadID: defaultGenerateLoadID,
50
- runtime: defaultRuntime,
51
47
  };
52
48
  const resolveLoaderPath = loaderPathResolver(import.meta.url, '../src/loaders', 'js');
53
- export function getDefaultLoaderPath(loader, bundle) {
49
+ export function getDefaultLoaderPath(loader) {
54
50
  if (loader === 'custom') {
55
51
  return null;
56
52
  }
57
53
  // just 'default', so
58
- let loaderName = 'astro';
59
- if (bundle) {
60
- loaderName += '.bundle';
61
- }
62
- return resolveLoaderPath(loaderName);
54
+ return resolveLoaderPath('astro');
63
55
  }
64
56
  /**
65
57
  * Create an Astro adapter for wuchale
@@ -77,14 +69,17 @@ export function getDefaultLoaderPath(loader, bundle) {
77
69
  * ```
78
70
  */
79
71
  export const adapter = (args = {}) => {
80
- const { heuristic, patterns, runtime, loader, ...rest } = deepMergeObjects(args, defaultArgs);
72
+ const { heuristic, patterns, loader, ...rest } = deepMergeObjects(args, defaultArgs);
81
73
  return {
82
74
  transform: async ({ content, filename, index, expr, matchUrl }) => {
83
- return new AstroTransformer(content, filename, index, heuristic, patterns, expr, runtime, matchUrl).transformAs();
75
+ return new AstroTransformer(content, filename, index, heuristic, patterns, expr, defaultRuntime, matchUrl).transformAs();
84
76
  },
85
77
  loaderExts: ['.js', '.ts'],
86
- defaultLoaderPath: getDefaultLoaderPath(loader, rest.bundleLoad),
87
- runtime,
78
+ defaultLoaderPath: getDefaultLoaderPath(loader),
79
+ granularLoad: false,
80
+ bundleLoad: false,
81
+ generateLoadID: defaultGenerateLoadID,
82
+ runtime: defaultRuntime,
88
83
  ...rest,
89
84
  };
90
85
  };
@@ -2,6 +2,7 @@ import { parse } from '@astrojs/compiler';
2
2
  import { tsPlugin } from '@sveltejs/acorn-typescript';
3
3
  import { Parser } from 'acorn';
4
4
  import MagicString from 'magic-string';
5
+ import { getKey } from 'wuchale';
5
6
  import { MixedVisitor, nonWhitespaceText } from 'wuchale/adapter-utils';
6
7
  import { parseScript, scriptParseOptionsWithComments, Transformer } from 'wuchale/adapter-vanilla';
7
8
  const ExprParser = Parser.extend(tsPlugin());
@@ -101,7 +102,7 @@ export class AstroTransformer extends Transformer {
101
102
  begin += `${this.vars().nestCtx},\nn: true`;
102
103
  }
103
104
  else {
104
- const index = this.index.get(msgInfo.toKey());
105
+ const index = this.index.get(getKey(msgInfo.msgStr, msgInfo.context));
105
106
  begin += `${this.vars().rtCtx}(${index})`;
106
107
  }
107
108
  if (nestedRanges.length > 0) {
@@ -199,7 +200,7 @@ export class AstroTransformer extends Transformer {
199
200
  if (!pass) {
200
201
  return [];
201
202
  }
202
- this.mstr.update(start, start + node.value.length + 2, `{${this.vars().rtTrans}(${this.index.get(msgInfo.toKey())})}`);
203
+ this.mstr.update(start, start + node.value.length + 2, `{${this.literalRepl(msgInfo)}}`);
203
204
  return [msgInfo];
204
205
  }
205
206
  if (node.kind === 'expression') {
@@ -224,7 +225,7 @@ export class AstroTransformer extends Transformer {
224
225
  return [];
225
226
  }
226
227
  const { start, end } = this.getRange(node);
227
- this.mstr.update(start + startWh, end - endWh, `{${this.vars().rtTrans}(${this.index.get(msgInfo.toKey())})}`);
228
+ this.mstr.update(start + startWh, end - endWh, `{${this.literalRepl(msgInfo)}}`);
228
229
  return [msgInfo];
229
230
  };
230
231
  visitfrontmatter = (node) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wuchale/astro",
3
- "version": "0.2.8",
3
+ "version": "0.3.1",
4
4
  "description": "Wuchale i18n adapter for Astro files",
5
5
  "scripts": {
6
6
  "dev": "tsc --watch",
@@ -48,11 +48,11 @@
48
48
  "author": "K1DV5",
49
49
  "license": "MIT",
50
50
  "dependencies": {
51
- "@astrojs/compiler": "^2.13.1",
51
+ "@astrojs/compiler": "^3",
52
52
  "@sveltejs/acorn-typescript": "^1.0.9",
53
- "acorn": "^8.15.0",
53
+ "acorn": "^8.16.0",
54
54
  "magic-string": "^0.30.21",
55
- "wuchale": "^0.20.0"
55
+ "wuchale": "^0.22.0"
56
56
  },
57
57
  "devDependencies": {
58
58
  "@types/estree-jsx": "^1.0.5",
@@ -1,21 +0,0 @@
1
- // Astro bundle loader template (server-side, synchronous, all locales bundled)
2
- import { toRuntime } from 'wuchale/runtime'
3
-
4
- const catalogs = __CATALOGS__
5
- const locales = Object.keys(catalogs)
6
-
7
- const store = {}
8
- for (const locale of locales) {
9
- store[locale] = toRuntime(catalogs[locale], locale)
10
- }
11
-
12
- // Get current locale from global context (set by middleware)
13
- function getCurrentLocale() {
14
- return globalThis.__wuchale_locale__ || locales[0]
15
- }
16
-
17
- export const getRuntime = (/** @type {string} */ _loadID) => {
18
- return store[getCurrentLocale()]
19
- }
20
-
21
- export const getRuntimeRx = getRuntime