@wuchale/astro 0.2.0 → 0.2.2
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 +1 -1
- package/dist/index.js +6 -6
- package/dist/runtime.js +1 -1
- package/dist/transformer.d.ts +5 -8
- package/dist/transformer.js +18 -65
- package/package.json +3 -2
- package/src/loaders/astro.js +2 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Adapter, AdapterArgs, CreateHeuristicOpts, HeuristicFunc, LoaderChoice } 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
|
package/dist/index.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import { createHeuristic, deepMergeObjects, defaultGenerateLoadID, defaultHeuristicOpts } from 'wuchale';
|
|
2
|
+
import { loaderPathResolver } from 'wuchale/adapter-utils';
|
|
3
|
+
import { pluralPattern } from 'wuchale/adapter-vanilla';
|
|
4
|
+
import { AstroTransformer } from './transformer.js';
|
|
5
5
|
/**
|
|
6
6
|
* Create a heuristic function optimized for Astro files
|
|
7
7
|
* Uses the default heuristic which handles translatable vs non-translatable strings
|
|
8
8
|
*/
|
|
9
9
|
export function createAstroHeuristic(opts) {
|
|
10
10
|
const defaultHeuristic = createHeuristic(opts);
|
|
11
|
-
return msg => {
|
|
11
|
+
return (msg) => {
|
|
12
12
|
const defRes = defaultHeuristic(msg);
|
|
13
13
|
if (!defRes) {
|
|
14
14
|
return false;
|
|
@@ -26,7 +26,7 @@ export function createAstroHeuristic(opts) {
|
|
|
26
26
|
export const astroDefaultHeuristic = createAstroHeuristic(defaultHeuristicOpts);
|
|
27
27
|
const defaultRuntime = {
|
|
28
28
|
// Astro is SSR-only, so we use non-reactive runtime by default
|
|
29
|
-
initReactive: ({ funcName }) => funcName == null ? false : null, // Only init in top-level functions
|
|
29
|
+
initReactive: ({ funcName }) => (funcName == null ? false : null), // Only init in top-level functions
|
|
30
30
|
// Astro is SSR - always use non-reactive
|
|
31
31
|
useReactive: () => false,
|
|
32
32
|
reactive: {
|
package/dist/runtime.js
CHANGED
package/dist/transformer.d.ts
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type * as Estree from
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
6
|
-
import type { ElementNode, TextNode, FragmentNode, Node, RootNode, ExpressionNode, AttributeNode, FrontmatterNode, ComponentNode, CustomElementNode } from "@astrojs/compiler/types";
|
|
1
|
+
import type { AttributeNode, ComponentNode, CustomElementNode, ElementNode, ExpressionNode, FragmentNode, FrontmatterNode, Node, RootNode, TextNode } from '@astrojs/compiler/types';
|
|
2
|
+
import type * as Estree from 'acorn';
|
|
3
|
+
import type { CatalogExpr, CodePattern, HeuristicFunc, IndexTracker, Message, RuntimeConf, TransformOutput, UrlMatcher } from 'wuchale';
|
|
4
|
+
import { MixedVisitor } from 'wuchale/adapter-utils';
|
|
5
|
+
import { Transformer } from 'wuchale/adapter-vanilla';
|
|
7
6
|
type MixedAstroNodes = Node;
|
|
8
7
|
export declare class AstroTransformer extends Transformer {
|
|
9
8
|
currentElement?: string;
|
|
10
9
|
inCompoundText: boolean;
|
|
11
|
-
commentDirectivesStack: CommentDirectives[];
|
|
12
|
-
lastVisitIsComment: boolean;
|
|
13
10
|
frontMatterStart?: number;
|
|
14
11
|
mixedVisitor: MixedVisitor<MixedAstroNodes>;
|
|
15
12
|
correctedExprRanges: WeakMap<Node, {
|
package/dist/transformer.js
CHANGED
|
@@ -1,22 +1,14 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { parse } from "@astrojs/compiler";
|
|
1
|
+
import { parse } from '@astrojs/compiler';
|
|
2
|
+
import MagicString from 'magic-string';
|
|
3
|
+
import { MixedVisitor, nonWhitespaceText } from 'wuchale/adapter-utils';
|
|
4
|
+
import { parseScript, Transformer } from 'wuchale/adapter-vanilla';
|
|
6
5
|
// Astro nodes that can have children
|
|
7
|
-
const nodesWithChildren = [
|
|
8
|
-
|
|
9
|
-
"component",
|
|
10
|
-
"custom-element",
|
|
11
|
-
"fragment",
|
|
12
|
-
];
|
|
13
|
-
const rtRenderFunc = "_w_Tx_";
|
|
6
|
+
const nodesWithChildren = ['element', 'component', 'custom-element', 'fragment'];
|
|
7
|
+
const rtRenderFunc = '_w_Tx_';
|
|
14
8
|
export class AstroTransformer extends Transformer {
|
|
15
9
|
// state
|
|
16
10
|
currentElement;
|
|
17
11
|
inCompoundText = false;
|
|
18
|
-
commentDirectivesStack = [];
|
|
19
|
-
lastVisitIsComment = false;
|
|
20
12
|
frontMatterStart;
|
|
21
13
|
mixedVisitor;
|
|
22
14
|
// astro's compiler gives wrong offsets for expressions
|
|
@@ -44,7 +36,7 @@ export class AstroTransformer extends Transformer {
|
|
|
44
36
|
}
|
|
45
37
|
this.correctedExprRanges.set(child, {
|
|
46
38
|
start: this.content.indexOf('{', child.position?.start?.offset ?? 0),
|
|
47
|
-
end: actualEnd
|
|
39
|
+
end: actualEnd,
|
|
48
40
|
});
|
|
49
41
|
}
|
|
50
42
|
};
|
|
@@ -52,7 +44,7 @@ export class AstroTransformer extends Transformer {
|
|
|
52
44
|
if (node.type === 'expression') {
|
|
53
45
|
return this.correctedExprRanges.get(node) ?? { start: -1, end: -1 };
|
|
54
46
|
}
|
|
55
|
-
|
|
47
|
+
const { start, end } = node.position ?? {};
|
|
56
48
|
return {
|
|
57
49
|
start: start?.offset ?? -1,
|
|
58
50
|
end: end?.offset ?? -1,
|
|
@@ -62,12 +54,12 @@ export class AstroTransformer extends Transformer {
|
|
|
62
54
|
mstr: this.mstr,
|
|
63
55
|
vars: this.vars,
|
|
64
56
|
getRange: this.getRange,
|
|
65
|
-
isText: node => node.type === 'text',
|
|
66
|
-
isComment: node => node.type === 'comment',
|
|
67
|
-
leaveInPlace: node => [''].includes(node.type),
|
|
68
|
-
isExpression: node => node.type === 'expression',
|
|
57
|
+
isText: (node) => node.type === 'text',
|
|
58
|
+
isComment: (node) => node.type === 'comment',
|
|
59
|
+
leaveInPlace: (node) => [''].includes(node.type),
|
|
60
|
+
isExpression: (node) => node.type === 'expression',
|
|
69
61
|
getTextContent: (node) => node.value,
|
|
70
|
-
getCommentData: (node) => node.value,
|
|
62
|
+
getCommentData: (node) => node.value.trim(),
|
|
71
63
|
canHaveChildren: (node) => nodesWithChildren.includes(node.type),
|
|
72
64
|
visitFunc: (child, inCompoundText) => {
|
|
73
65
|
const inCompoundTextPrev = this.inCompoundText;
|
|
@@ -121,7 +113,7 @@ export class AstroTransformer extends Transformer {
|
|
|
121
113
|
msgs = this.visit(ast);
|
|
122
114
|
}
|
|
123
115
|
else {
|
|
124
|
-
msgs = ast.body.
|
|
116
|
+
msgs = ast.body.flatMap(this.visit);
|
|
125
117
|
}
|
|
126
118
|
this.mstr.offset = 0; // restore
|
|
127
119
|
return msgs;
|
|
@@ -148,7 +140,7 @@ export class AstroTransformer extends Transformer {
|
|
|
148
140
|
inCompoundText: this.inCompoundText,
|
|
149
141
|
scope: 'markup',
|
|
150
142
|
element: this.currentElement,
|
|
151
|
-
useComponent: this.currentElement !== 'title'
|
|
143
|
+
useComponent: this.currentElement !== 'title',
|
|
152
144
|
});
|
|
153
145
|
visitFragmentNode = (node) => this._visitChildren(node.children);
|
|
154
146
|
visitelement = (node) => {
|
|
@@ -213,44 +205,8 @@ export class AstroTransformer extends Transformer {
|
|
|
213
205
|
this.frontMatterStart = this.content.indexOf('---', start) + 3;
|
|
214
206
|
return this._parseAndVisitExpr(node.value, this.frontMatterStart, true);
|
|
215
207
|
};
|
|
216
|
-
visitroot = (node) =>
|
|
217
|
-
|
|
218
|
-
// ?? [] because it's undefined on an empty file
|
|
219
|
-
for (const rootChild of node.children ?? []) {
|
|
220
|
-
msgs.push(...this.visitAs(rootChild));
|
|
221
|
-
}
|
|
222
|
-
return msgs;
|
|
223
|
-
};
|
|
224
|
-
visitAs = (node) => {
|
|
225
|
-
if (node.type === 'comment') {
|
|
226
|
-
this.commentDirectives = processCommentDirectives(node.value.trim(), this.commentDirectives);
|
|
227
|
-
if (this.lastVisitIsComment) {
|
|
228
|
-
this.commentDirectivesStack[this.commentDirectivesStack.length - 1] = this.commentDirectives;
|
|
229
|
-
}
|
|
230
|
-
else {
|
|
231
|
-
this.commentDirectivesStack.push(this.commentDirectives);
|
|
232
|
-
}
|
|
233
|
-
this.lastVisitIsComment = true;
|
|
234
|
-
return [];
|
|
235
|
-
}
|
|
236
|
-
if (node.type === 'text' && !node.value.trim()) {
|
|
237
|
-
return [];
|
|
238
|
-
}
|
|
239
|
-
let msgs = [];
|
|
240
|
-
const commentDirectivesPrev = this.commentDirectives;
|
|
241
|
-
if (this.lastVisitIsComment) {
|
|
242
|
-
this.commentDirectives = this.commentDirectivesStack.pop();
|
|
243
|
-
this.lastVisitIsComment = false;
|
|
244
|
-
}
|
|
245
|
-
if (this.commentDirectives.ignoreFile) {
|
|
246
|
-
return [];
|
|
247
|
-
}
|
|
248
|
-
if (this.commentDirectives.forceType !== false) {
|
|
249
|
-
msgs = this.visit(node);
|
|
250
|
-
}
|
|
251
|
-
this.commentDirectives = commentDirectivesPrev;
|
|
252
|
-
return msgs;
|
|
253
|
-
};
|
|
208
|
+
visitroot = (node) => this._visitChildren(node.children ?? []); // can be undefined!
|
|
209
|
+
visitAs = (node) => this.visit(node);
|
|
254
210
|
transformAs = async () => {
|
|
255
211
|
const { ast } = await parse(this.content);
|
|
256
212
|
this.mstr = new MagicString(this.content);
|
|
@@ -260,10 +216,7 @@ export class AstroTransformer extends Transformer {
|
|
|
260
216
|
this.mstr.appendLeft(0, '---\n');
|
|
261
217
|
this.mstr.appendRight(0, '---\n');
|
|
262
218
|
}
|
|
263
|
-
const header = [
|
|
264
|
-
`import ${rtRenderFunc} from "@wuchale/astro/runtime.js"`,
|
|
265
|
-
this.initRuntime(),
|
|
266
|
-
].join('\n');
|
|
219
|
+
const header = [`import ${rtRenderFunc} from "@wuchale/astro/runtime.js"`, this.initRuntime()].join('\n');
|
|
267
220
|
return this.finalize(msgs, this.frontMatterStart ?? 0, header);
|
|
268
221
|
};
|
|
269
222
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wuchale/astro",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "Wuchale i18n adapter for Astro files",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"dev": "tsc --watch",
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
"translation",
|
|
14
14
|
"gettext",
|
|
15
15
|
"astro",
|
|
16
|
+
"withastro",
|
|
16
17
|
"vite",
|
|
17
18
|
"po",
|
|
18
19
|
"vite-plugin",
|
|
@@ -51,7 +52,7 @@
|
|
|
51
52
|
"@sveltejs/acorn-typescript": "^1.0.8",
|
|
52
53
|
"acorn": "^8.15.0",
|
|
53
54
|
"magic-string": "^0.30.21",
|
|
54
|
-
"wuchale": "^0.19.
|
|
55
|
+
"wuchale": "^0.19.1"
|
|
55
56
|
},
|
|
56
57
|
"devDependencies": {
|
|
57
58
|
"@types/estree-jsx": "^1.0.5",
|
package/src/loaders/astro.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
// Astro loader template (server-side, synchronous)
|
|
2
2
|
// This is a template file that wuchale will use to generate the actual loader
|
|
3
|
-
|
|
3
|
+
|
|
4
4
|
import { currentRuntime } from 'wuchale/load-utils/server'
|
|
5
|
+
import { loadCatalog, loadIDs } from '${PROXY_SYNC}'
|
|
5
6
|
|
|
6
7
|
const key = '${KEY}'
|
|
7
8
|
|