forked from sveltejs/svelte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.js
100 lines (91 loc) · 2.36 KB
/
start.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
import { readFileSync, writeFileSync } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { watch } from 'rollup';
import serve from 'rollup-plugin-serve';
import * as svelte from '../../packages/svelte/src/compiler/index.js';
const __dirname = fileURLToPath(new URL('.', import.meta.url));
const runtime_path = path.resolve(__dirname, '../../packages/svelte/src/runtime');
/** @returns {import('rollup').Plugin}*/
function create_plugin(ssr = false) {
return {
name: 'custom-svelte-ssr-' + ssr,
resolveId(id) {
if (id === 'svelte') {
return path.resolve(runtime_path, ssr ? 'ssr.js' : 'index.js');
} else if (id.startsWith('svelte/')) {
return path.resolve(runtime_path, `${id.slice(7)}/index.js`);
}
},
transform(code, id) {
code = code.replaceAll('import.meta.env.SSR', ssr);
if (!id.endsWith('.svelte')) {
return {
code,
map: null
};
}
const compiled = svelte.compile(code, {
filename: id,
generate: ssr ? 'ssr' : 'dom',
hydratable: true,
css: 'injected'
});
return compiled.js;
}
};
}
const client_plugin = create_plugin();
const ssr_plugin = create_plugin(true);
const watcher = watch([
{
input: 'src/entry-client.js',
output: {
dir: 'dist',
format: 'esm',
sourcemap: true
},
plugins: [client_plugin, serve('dist')]
},
{
input: 'src/entry-server.js',
output: {
dir: 'dist-ssr',
format: 'iife',
indent: false
},
plugins: [
ssr_plugin,
{
async generateBundle(_, bundle) {
const result = bundle['entry-server.js'];
const mod = (0, eval)(result.code);
const { html, head } = mod.render();
writeFileSync(
'dist/index.html',
readFileSync('src/template.html', 'utf-8')
.replace('<!--app-head-->', head)
.replace('<!--app-html-->', html)
.replace('<!--app-title-->', svelte.VERSION)
);
writeFileSync('dist/version.json', Date.now().toString());
}
}
],
onwarn(warning, handler) {
if (warning.code === 'MISSING_NAME_OPTION_FOR_IIFE_EXPORT') return;
handler(warning);
}
}
]);
watcher
.on('change', (id) => {
console.log(`changed ${id}`);
})
.on('event', (event) => {
if (event.code === 'ERROR') {
console.error(event.error);
} else if (event.code === 'BUNDLE_END') {
console.log(`Generated ${event.output} in ${event.duration}ms`);
}
});