-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
sveltekit-plugin.js
236 lines (194 loc) · 6.82 KB
/
sveltekit-plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import * as path from 'path';
import * as fs from 'fs/promises';
import * as babel from '@babel/core';
import buildICUPlugin from 'babel-plugin-precompile-intl';
import pathStartsWith from 'path-starts-with'
const intlPrecompiler = buildICUPlugin('svelte-intl-precompile');
export function transformCode(code, options) {
return babel.transform(code, { ...options, plugins: [intlPrecompiler] }).code;
}
const transformScript = (content) => content
const transformJSON = async (content) => {
const { default: JSON5 } = await import('json5');
return `export default ${JSON.stringify(JSON5.parse(content))}`;
}
const transformYaml = async (content, { filename }) => {
const { load } = await import('js-yaml');
return `export default ${JSON.stringify(load(content, { filename }))}`;
}
const stdTransformers = {
// order matters as it defines which extensions are tried first
// when looking for a matching file for a locale
'.js': transformScript,
'.ts': transformScript,
'.mjs': transformScript,
// use json5 loader for json files
'.json': transformJSON,
'.json5': transformJSON,
'.yaml': transformYaml,
'.yml': transformYaml,
};
function svelteIntlPrecompile(localesRoot, prefixOrOptions) {
const {
prefix = '$locales',
transformers: customTransformers,
exclude,
} = typeof prefixOrOptions === 'string'
? { prefix: prefixOrOptions }
: { ...prefixOrOptions };
const resolvedPath = path.resolve(localesRoot);
const transformers = { ...stdTransformers, ...customTransformers }
async function loadPrefixModule() {
const code = [
`import { register } from 'svelte-intl-precompile'`,
`export function registerAll() {`
// register('en', () => import('$locales/en'))
];
const availableLocales = [];
const filesInLocalesFolder = await fs.readdir(localesRoot)
const excludeFn = typeof exclude === 'function' ? exclude : (exclude instanceof RegExp ? (s) => exclude.test(s) : () => false);
const languageFiles = filesInLocalesFolder.filter(name => !excludeFn(name));
// add register calls for each found locale
for (const file of languageFiles) {
const extname = path.extname(file);
if (transformers[extname]) {
const locale = path.basename(file, extname);
// ensure we register each locale only once
// there shouldn't be more than one file each locale
// our load(id) method only ever loads the first found
// file for a locale and it makes no sense to register
// more than one file per locale
if (!availableLocales.includes(locale)) {
availableLocales.push(locale);
code.push(
` register(${JSON.stringify(locale)}, () => import(${
JSON.stringify(`${prefix}/${locale}`)
}))`,
)
}
}
}
// Sort locales that more specific locales come first
// 'en-US' comes before 'en'
availableLocales.sort((a, b) => {
const order = a.split('-').length - b.split('-').length
if (order) return order
return a.localeCompare(b, 'en')
})
code.push(
`}`,
`export const availableLocales = ${JSON.stringify(availableLocales)}`,
);
return code.join('\n');
}
async function tranformLocale(
content,
{
filename,
extname = path.extname(filename),
basename = path.basename(filename, extname),
transform = transformers[extname] || transformScript
}
) {
const code = await transform(content, { filename, basename, extname });
return transformCode(code, { filename });
}
async function findLocale(basename) {
const filebase = path.resolve(localesRoot, basename);
// lazy loaded here because strip-bom is an es module
// and this file could be a cjs module
// by using dynamic import we can load es modules
const { default: stripBom } = await import('strip-bom');
for await (const [extname, transform] of Object.entries(transformers)) {
const filename = filebase + extname;
try {
const content = await fs.readFile(filename, { encoding: 'utf-8' });
return tranformLocale(stripBom(content), { filename, basename, extname, transform });
} catch (error) {
// incase the file did not exist try next transformer
// otherwise propagate the error
if (error.code !== 'ENOENT') {
throw error;
}
}
}
}
return {
name: 'svelte-intl-precompile', // required, will show up in warnings and errors
enforce: 'pre',
configureServer(server) {
const { ws, watcher, moduleGraph } = server;
// listen to vite files watcher
watcher.on('change', (file) => {
file = path.relative('', file);
// check if file changed is a locale
if (pathStartsWith(file, localesRoot)) {
// invalidate $locales/<locales><extname> modules
const name = `${prefix}/${path.basename(file, path.extname(file))}`;
// Alltough we are normalizing the module names
// $locales/en.js -> $locales/en
// we check all configured extensions just to be sure
// '.js', '.ts', '.json', ..., and '' ($locales/en)
for (const extname of [...Object.keys(transformers), '']) {
// check if locale file is in vite cache
const localeModule = moduleGraph.getModuleById(`${name}${extname}`);
if (localeModule) {
moduleGraph.invalidateModule(localeModule);
}
}
// invalidate $locales module
const prefixModule = moduleGraph.getModuleById(prefix)
if (prefixModule) {
moduleGraph.invalidateModule(prefixModule);
}
// trigger hmr
ws.send({ type: 'full-reload', path: '*' });
}
})
},
resolveId(id) {
if (id === prefix || id.startsWith(`${prefix}/`)) {
const extname = path.extname(id);
// "normalize" module id to have no extension
// $locales/en.js -> $locales/en
// we do this as the extension is ignored
// when loading a locale
// we always try to find a locale file by its basename
// and adding an extension from transformers
// additionally this prevents loading the same module/locale
// several times with different extensions
const normalized = extname
? id.slice(0, -extname.length)
: id
return normalized;
}
},
load(id) {
// allow to auto register locales by calling registerAll from $locales module
// import { registerAll, availableLocales } from '$locales'
if (id === prefix) {
return loadPrefixModule();
}
// import en from '$locales/en'
// import en from '$locales/en.js'
if (id.startsWith(`${prefix}/`)) {
const extname = path.extname(id);
// $locales/en -> en
// $locales/en.js -> en
// $locales/en.ts -> en
const locale = extname
? id.slice(`${prefix}/`.length, -extname.length)
: id.slice(`${prefix}/`.length)
return findLocale(locale);
}
},
transform(content, id) {
// import locale from '../locales/en.js'
if (pathStartsWith(id, resolvedPath)) {
return tranformLocale(content, { filename: id });
}
}
}
}
svelteIntlPrecompile.transformCode = transformCode;
export default svelteIntlPrecompile;