forked from sveltejs/kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (59 loc) · 1.86 KB
/
index.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
import { readFileSync, writeFileSync } from 'fs';
import { fileURLToPath } from 'url';
import * as esbuild from 'esbuild';
const files = fileURLToPath(new URL('./files', import.meta.url).href);
/** @type {import('.').default} */
export default function (opts = {}) {
// TODO remove for 1.0
// @ts-expect-error
if (opts.env) {
throw new Error(
'options.env has been removed in favour of options.envPrefix. Consult the adapter-node README: https://github.com/sveltejs/kit/tree/master/packages/adapter-node'
);
}
const { out = 'build', precompress, envPrefix = '' } = opts;
return {
name: '@sveltejs/adapter-node',
async adapt(builder) {
const tmp = builder.getBuildDirectory('adapter-node');
builder.rimraf(out);
builder.rimraf(tmp);
builder.mkdirp(tmp);
builder.log.minor('Copying assets');
builder.writeClient(`${out}/client`);
builder.writePrerendered(`${out}/prerendered`);
if (precompress) {
builder.log.minor('Compressing assets');
await builder.compress(`${out}/client`);
await builder.compress(`${out}/prerendered`);
}
builder.log.minor('Building server');
builder.writeServer(tmp);
writeFileSync(
`${tmp}/manifest.js`,
`export const manifest = ${builder.generateManifest({ relativePath: './' })};`
);
const pkg = JSON.parse(readFileSync('package.json', 'utf8'));
await esbuild.build({
platform: 'node',
sourcemap: 'linked',
target: 'es2022',
entryPoints: [`${tmp}/index.js`, `${tmp}/manifest.js`],
outdir: `${out}/server`,
splitting: true,
format: 'esm',
bundle: true,
external: [...Object.keys(pkg.dependencies || {})]
});
builder.copy(files, out, {
replace: {
ENV: './env.js',
HANDLER: './handler.js',
MANIFEST: './server/manifest.js',
SERVER: `./server/index.js`,
ENV_PREFIX: JSON.stringify(envPrefix)
}
});
}
};
}