-
Notifications
You must be signed in to change notification settings - Fork 852
Expand file tree
/
Copy pathsvelte-pre-render-plugin.js
More file actions
59 lines (50 loc) · 1.51 KB
/
svelte-pre-render-plugin.js
File metadata and controls
59 lines (50 loc) · 1.51 KB
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
const fs = require("fs");
function rerequire(path) {
delete require.cache[path];
return require(path);
}
class SveltePreRenderPlugin {
constructor(options = {}) {
this.options = options;
}
apply(compiler) {
const locales = fs.readdirSync("jsi18n/jsi18n");
compiler.hooks.assetEmitted.tap(
"svelte-pre-render-plugin",
(file, { outputPath, targetPath }) => {
const routes = this.options[file];
if (routes) {
const { render } = require("svelte/server");
const Component = rerequire(targetPath).default;
for (const locale of locales) {
const jsi18n = rerequire(`../jsi18n/jsi18n/${locale}/djangojs.js`);
Object.assign(global, jsi18n);
for (const route of routes) {
const { head = "", body: html } = render(Component, {
props: { url: `/${locale}${route}`, locale },
});
fs.mkdirSync(
`${outputPath}/${locale}${route
.split("/")
.slice(0, -1)
.join("/")}`,
{
recursive: true,
}
);
fs.writeFileSync(
`${outputPath}/${locale}${route}.head.html`,
head
);
fs.writeFileSync(
`${outputPath}/${locale}${route}.html`,
html
);
}
}
}
}
);
}
}
module.exports = SveltePreRenderPlugin;