forked from sveltejs/kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
158 lines (143 loc) · 4.51 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
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
import esbuild from 'esbuild';
import {
createReadStream,
createWriteStream,
existsSync,
readFileSync,
statSync,
writeFileSync
} from 'fs';
import { join, resolve } from 'path';
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);
/**
* @typedef {import('esbuild').BuildOptions} BuildOptions
*/
/** @type {import('.')} */
export default function ({
entryPoint = '.svelte-kit/node/index.js',
out = 'build',
precompress,
env: { path: path_env = 'SOCKET_PATH', host: host_env = 'HOST', port: port_env = 'PORT' } = {},
esbuild: esbuild_config
} = {}) {
return {
name: '@sveltejs/adapter-node',
async adapt({ utils, config }) {
utils.rimraf(out);
utils.log.minor('Copying assets');
const static_directory = join(out, 'assets');
utils.copy_client_files(static_directory);
utils.copy_static_files(static_directory);
if (precompress) {
utils.log.minor('Compressing assets');
await compress(static_directory);
}
utils.log.minor('Building SvelteKit middleware');
const files = fileURLToPath(new URL('./files', import.meta.url));
utils.copy(files, '.svelte-kit/node');
writeFileSync(
'.svelte-kit/node/env.js',
`export const path = process.env[${JSON.stringify(
path_env
)}] || false;\nexport const host = process.env[${JSON.stringify(
host_env
)}] || '0.0.0.0';\nexport const port = process.env[${JSON.stringify(
port_env
)}] || (!path && 3000);`
);
/** @type {BuildOptions} */
const defaultOptions = {
entryPoints: ['.svelte-kit/node/middlewares.js'],
outfile: join(out, 'middlewares.js'),
bundle: true,
external: Object.keys(JSON.parse(readFileSync('package.json', 'utf8')).dependencies || {}),
format: 'esm',
platform: 'node',
target: 'node14',
inject: [join(files, 'shims.js')],
define: {
APP_DIR: `"/${config.kit.appDir}/"`
}
};
const build_options = esbuild_config ? await esbuild_config(defaultOptions) : defaultOptions;
await esbuild.build(build_options);
utils.log.minor('Building SvelteKit server');
/** @type {BuildOptions} */
const default_options_ref_server = {
entryPoints: [entryPoint],
outfile: join(out, 'index.js'),
bundle: true,
format: 'esm',
platform: 'node',
target: 'node14',
// external exclude workaround, see https://github.com/evanw/esbuild/issues/514
plugins: [
{
name: 'fix-middlewares-exclude',
setup(build) {
// Match an import of "middlewares.js" and mark it as external
const internal_middlewares_path = resolve('.svelte-kit/node/middlewares.js');
const build_middlewares_path = resolve(out, 'middlewares.js');
build.onResolve({ filter: /\/middlewares\.js$/ }, ({ path, resolveDir }) => {
const resolved = resolve(resolveDir, path);
if (resolved === internal_middlewares_path || resolved === build_middlewares_path) {
return { path: './middlewares.js', external: true };
}
});
}
}
]
};
const build_options_ref_server = esbuild_config
? await esbuild_config(default_options_ref_server)
: default_options_ref_server;
await esbuild.build(build_options_ref_server);
utils.log.minor('Prerendering static pages');
await utils.prerender({
dest: `${out}/prerendered`
});
if (precompress && existsSync(`${out}/prerendered`)) {
utils.log.minor('Compressing prerendered pages');
await compress(`${out}/prerendered`);
}
}
};
}
/**
* @param {string} directory
*/
async function compress(directory) {
const files = await glob('**/*.{html,js,json,css,svg,xml}', {
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);
}