-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
index.js
113 lines (98 loc) · 2.84 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
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
import { createReadStream, createWriteStream, existsSync, statSync, writeFileSync } from 'fs';
import { pipeline } from 'stream';
import glob from 'tiny-glob';
import { fileURLToPath } from 'url';
import { promisify } from 'util';
import zlib from 'zlib';
const pipe = promisify(pipeline);
const files = fileURLToPath(new URL('./files', import.meta.url));
/** @type {import('.')} */
export default function ({
out = 'build',
precompress,
env: {
path: path_env = 'SOCKET_PATH',
host: host_env = 'HOST',
port: port_env = 'PORT',
origin: origin_env = 'ORIGIN',
headers: {
protocol: protocol_header_env = 'PROTOCOL_HEADER',
host: host_header_env = 'HOST_HEADER'
} = {}
} = {}
} = {}) {
return {
name: '@sveltejs/adapter-node',
async adapt(builder) {
builder.rimraf(out);
builder.log.minor('Copying assets');
builder.writeClient(`${out}/client`);
builder.writeServer(`${out}/server`);
builder.writeStatic(`${out}/static`);
builder.log.minor('Prerendering static pages');
await builder.prerender({
dest: `${out}/prerendered`
});
writeFileSync(
`${out}/manifest.js`,
`export const manifest = ${builder.generateManifest({
relativePath: './server'
})};\n`
);
builder.copy(files, out, {
replace: {
SERVER: './server/index.js',
MANIFEST: './manifest.js',
PATH_ENV: JSON.stringify(path_env),
HOST_ENV: JSON.stringify(host_env),
PORT_ENV: JSON.stringify(port_env),
ORIGIN: origin_env ? `process.env[${JSON.stringify(origin_env)}]` : 'undefined',
PROTOCOL_HEADER: JSON.stringify(protocol_header_env),
HOST_HEADER: JSON.stringify(host_header_env)
}
});
if (precompress) {
builder.log.minor('Compressing assets');
await compress(`${out}/client`);
await compress(`${out}/static`);
await compress(`${out}/prerendered`);
}
}
};
}
/**
* @param {string} directory
*/
async function compress(directory) {
if (!existsSync(directory)) {
return;
}
const files = await glob('**/*.{html,js,json,css,svg,xml,wasm}', {
cwd: directory,
dot: true,
absolute: true,
filesOnly: true
});
await Promise.all(
files.map((file) => Promise.all([compress_file(file, 'gz'), compress_file(file, 'br')]))
);
}
/**
* @param {string} file
* @param {'gz' | 'br'} format
*/
async function compress_file(file, format = 'gz') {
const compress =
format == 'br'
? zlib.createBrotliCompress({
params: {
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
[zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY,
[zlib.constants.BROTLI_PARAM_SIZE_HINT]: statSync(file).size
}
})
: zlib.createGzip({ level: zlib.constants.Z_BEST_COMPRESSION });
const source = createReadStream(file);
const destination = createWriteStream(`${file}.${format}`);
await pipe(source, compress, destination);
}