@linguanahq/vite 0.1.0 → 0.1.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/adapters.test.d.ts +1 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +622 -0
- package/dist/preview-client.d.ts +2 -0
- package/dist/preview.d.ts +41 -0
- package/dist/preview.test.d.ts +1 -0
- package/dist/react.d.ts +2 -0
- package/dist/react.js +19 -0
- package/dist/shared.d.ts +34 -0
- package/dist/solid.d.ts +2 -0
- package/dist/solid.js +21 -0
- package/dist/svelte.d.ts +2 -0
- package/dist/svelte.js +282 -0
- package/dist/vue.d.ts +2 -0
- package/dist/vue.js +241 -0
- package/package.json +64 -49
- package/src/index.ts +0 -173
- package/src/react.ts +0 -19
- package/src/shared.ts +0 -145
- package/src/solid.ts +0 -21
- package/src/svelte.ts +0 -226
- package/src/vue.ts +0 -191
package/src/vue.ts
DELETED
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
import type { SourceAdapter } from "@linguanahq/build-core";
|
|
2
|
-
import type { Diagnostic, ManifestMessage } from "@linguanahq/catalog";
|
|
3
|
-
import { NodeTypes } from "@vue/compiler-dom";
|
|
4
|
-
import { parse } from "@vue/compiler-sfc";
|
|
5
|
-
import MagicString from "magic-string";
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
attributeMessage,
|
|
9
|
-
EXCLUDED_ELEMENTS,
|
|
10
|
-
escapeAttribute,
|
|
11
|
-
explicitMessage,
|
|
12
|
-
message,
|
|
13
|
-
renderedText,
|
|
14
|
-
TRANSLATABLE_ATTRIBUTES,
|
|
15
|
-
unsafeDiagnostic,
|
|
16
|
-
} from "#shared";
|
|
17
|
-
|
|
18
|
-
type VueAttribute = {
|
|
19
|
-
type: number;
|
|
20
|
-
name: string;
|
|
21
|
-
value?: { content: string };
|
|
22
|
-
loc: {
|
|
23
|
-
start: { offset: number; line: number; column: number };
|
|
24
|
-
end: { offset: number };
|
|
25
|
-
};
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
type VueNode = {
|
|
29
|
-
type: number;
|
|
30
|
-
tag?: string;
|
|
31
|
-
content?: string;
|
|
32
|
-
props?: VueAttribute[];
|
|
33
|
-
children?: VueNode[];
|
|
34
|
-
loc: {
|
|
35
|
-
start: { offset: number; line: number; column: number };
|
|
36
|
-
end: { offset: number };
|
|
37
|
-
};
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
export function vue(): SourceAdapter {
|
|
41
|
-
return {
|
|
42
|
-
id: "vue",
|
|
43
|
-
version: "0.1.0",
|
|
44
|
-
extensions: [".vue"],
|
|
45
|
-
runtimeModule: "@linguanahq/vue/compiler-runtime",
|
|
46
|
-
async transform(input) {
|
|
47
|
-
const parsed = parse(input.code, {
|
|
48
|
-
filename: input.filename,
|
|
49
|
-
sourceMap: true,
|
|
50
|
-
});
|
|
51
|
-
const template = parsed.descriptor.template;
|
|
52
|
-
if (!template?.ast) return null;
|
|
53
|
-
const output = new MagicString(input.code);
|
|
54
|
-
const messages: ManifestMessage[] = [];
|
|
55
|
-
const diagnostics: Diagnostic[] = [];
|
|
56
|
-
const visit = (
|
|
57
|
-
node: VueNode,
|
|
58
|
-
excluded = false,
|
|
59
|
-
parentTag = "template",
|
|
60
|
-
) => {
|
|
61
|
-
const tag =
|
|
62
|
-
node.type === NodeTypes.ELEMENT ? String(node.tag) : parentTag;
|
|
63
|
-
const nextExcluded =
|
|
64
|
-
excluded ||
|
|
65
|
-
EXCLUDED_ELEMENTS.has(tag.toLowerCase()) ||
|
|
66
|
-
(node.props ?? []).some(
|
|
67
|
-
(prop) =>
|
|
68
|
-
prop.type === NodeTypes.ATTRIBUTE &&
|
|
69
|
-
["data-no-translate", "hidden"].includes(prop.name),
|
|
70
|
-
);
|
|
71
|
-
if (
|
|
72
|
-
node.type === NodeTypes.ELEMENT &&
|
|
73
|
-
["T", "LinguanaMessage"].includes(tag)
|
|
74
|
-
) {
|
|
75
|
-
const source = (node.props ?? []).find(
|
|
76
|
-
(prop) =>
|
|
77
|
-
prop.type === NodeTypes.ATTRIBUTE &&
|
|
78
|
-
["default-message", "defaultMessage"].includes(prop.name),
|
|
79
|
-
)?.value?.content;
|
|
80
|
-
const explicitId = (node.props ?? []).find(
|
|
81
|
-
(prop) => prop.type === NodeTypes.ATTRIBUTE && prop.name === "id",
|
|
82
|
-
)?.value?.content;
|
|
83
|
-
if (source)
|
|
84
|
-
messages.push(
|
|
85
|
-
explicitMessage({
|
|
86
|
-
source,
|
|
87
|
-
explicitId,
|
|
88
|
-
filename: input.filename,
|
|
89
|
-
root: input.root,
|
|
90
|
-
line: node.loc.start.line,
|
|
91
|
-
column: node.loc.start.column - 1,
|
|
92
|
-
sourceLocale: input.sourceLocale,
|
|
93
|
-
}),
|
|
94
|
-
);
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
|
-
if (
|
|
98
|
-
node.type === NodeTypes.ELEMENT &&
|
|
99
|
-
(node.children ?? []).some(
|
|
100
|
-
(child: { type: number; content?: string }) =>
|
|
101
|
-
child.type === NodeTypes.TEXT &&
|
|
102
|
-
Boolean(renderedText(child.content ?? "")),
|
|
103
|
-
) &&
|
|
104
|
-
(node.children ?? []).some(
|
|
105
|
-
(child: { type: number }) => child.type !== NodeTypes.TEXT,
|
|
106
|
-
)
|
|
107
|
-
) {
|
|
108
|
-
diagnostics.push({
|
|
109
|
-
code: "ambiguous-rich-text",
|
|
110
|
-
severity: "warning",
|
|
111
|
-
message:
|
|
112
|
-
"Mixed text and Vue template nodes require an explicit <T> message.",
|
|
113
|
-
location: {
|
|
114
|
-
file: input.filename.slice(input.root.length + 1),
|
|
115
|
-
line: node.loc.start.line,
|
|
116
|
-
column: node.loc.start.column - 1,
|
|
117
|
-
},
|
|
118
|
-
remediation: "Use <T default-message> or data-no-translate.",
|
|
119
|
-
});
|
|
120
|
-
return;
|
|
121
|
-
}
|
|
122
|
-
if (node.type === NodeTypes.ELEMENT && !nextExcluded) {
|
|
123
|
-
for (const prop of node.props ?? []) {
|
|
124
|
-
if (
|
|
125
|
-
prop.type !== NodeTypes.ATTRIBUTE ||
|
|
126
|
-
!prop.value ||
|
|
127
|
-
!TRANSLATABLE_ATTRIBUTES.has(prop.name)
|
|
128
|
-
)
|
|
129
|
-
continue;
|
|
130
|
-
const item = attributeMessage({
|
|
131
|
-
source: prop.value.content,
|
|
132
|
-
filename: input.filename,
|
|
133
|
-
root: input.root,
|
|
134
|
-
line: prop.loc.start.line,
|
|
135
|
-
column: prop.loc.start.column - 1,
|
|
136
|
-
sourceLocale: input.sourceLocale,
|
|
137
|
-
element: tag,
|
|
138
|
-
attribute: prop.name,
|
|
139
|
-
});
|
|
140
|
-
messages.push(item);
|
|
141
|
-
const expression = `$tAttr(${JSON.stringify(item.id)}, ${JSON.stringify(prop.value.content)})`;
|
|
142
|
-
const replacement = `:${prop.name}='${expression.replaceAll("&", "&").replaceAll("'", "'")}'`;
|
|
143
|
-
output.overwrite(
|
|
144
|
-
prop.loc.start.offset,
|
|
145
|
-
prop.loc.end.offset,
|
|
146
|
-
replacement,
|
|
147
|
-
);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
if (node.type === NodeTypes.TEXT && !nextExcluded) {
|
|
151
|
-
const source = renderedText(node.content ?? "");
|
|
152
|
-
if (source) {
|
|
153
|
-
const item = message({
|
|
154
|
-
source,
|
|
155
|
-
filename: input.filename,
|
|
156
|
-
root: input.root,
|
|
157
|
-
line: node.loc.start.line,
|
|
158
|
-
column: node.loc.start.column - 1,
|
|
159
|
-
sourceLocale: input.sourceLocale,
|
|
160
|
-
element: parentTag,
|
|
161
|
-
});
|
|
162
|
-
const unsafe = unsafeDiagnostic(item);
|
|
163
|
-
if (unsafe) diagnostics.push(unsafe);
|
|
164
|
-
else {
|
|
165
|
-
messages.push(item);
|
|
166
|
-
output.overwrite(
|
|
167
|
-
node.loc.start.offset,
|
|
168
|
-
node.loc.end.offset,
|
|
169
|
-
`<LinguanaMessage id="${item.id}" default-message="${escapeAttribute(source)}" />`,
|
|
170
|
-
);
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
for (const child of node.children ?? [])
|
|
175
|
-
visit(child, nextExcluded, tag);
|
|
176
|
-
};
|
|
177
|
-
visit(template.ast as unknown as VueNode);
|
|
178
|
-
if (!messages.length && !diagnostics.length) return null;
|
|
179
|
-
return {
|
|
180
|
-
code: output.toString(),
|
|
181
|
-
map: output.generateMap({
|
|
182
|
-
hires: true,
|
|
183
|
-
source: input.filename,
|
|
184
|
-
includeContent: true,
|
|
185
|
-
}) as unknown as Record<string, unknown>,
|
|
186
|
-
messages,
|
|
187
|
-
diagnostics,
|
|
188
|
-
};
|
|
189
|
-
},
|
|
190
|
-
};
|
|
191
|
-
}
|