Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
153 lines (135 loc) · 4.28 KB
/
Copy pathindex.js
File metadata and controls
153 lines (135 loc) · 4.28 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
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
import fs from 'fs';
import { join } from 'path';
import { pipeline } from 'stream';
import { fileURLToPath } from 'url';
import { promisify } from 'util';
import { appResolver } from '@sveltejs/kit/adapter';
import esbuild from 'esbuild';
import glob from 'tiny-glob';
import zlib from 'zlib';
const pipe = promisify(pipeline);
/**
* @typedef {import('esbuild').BuildOptions} BuildOptions
*/
/** @type {import('.')} */
export default function ({
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');
fs.writeFileSync(
'.svelte-kit/node/env.js', // types pointed in src/env.d.ts
`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 default_options = {
entryPoints: ['.svelte-kit/node/middlewares.js'],
outfile: join(out, 'middlewares.js'),
bundle: true,
external: Object.keys(
JSON.parse(fs.readFileSync('package.json', 'utf8')).dependencies || {}
),
format: 'esm',
platform: 'node',
target: 'node12',
inject: [join(files, 'shims.js')],
plugins: [appResolver()],
define: {
APP_DIR: `"/${config.kit.appDir}/"`
}
};
const build_options = esbuild_config
? await esbuild_config(default_options)
: default_options;
await esbuild.build(build_options);
utils.log.minor('Building SvelteKit reference server');
/** @type {BuildOptions} */
const default_options_ref_server = {
entryPoints: ['.svelte-kit/node/index.js'],
outfile: join(out, 'index.js'),
bundle: true,
external: ['./middlewares.js'], // does not work, eslint does not exclude middlewares from target
format: 'esm',
platform: 'node',
target: 'node12',
// external exclude workaround, see https://github.com/evanw/esbuild/issues/514
plugins: [
{
name: 'fix-middlewares-exclude',
setup(build) {
// Match an import called "./middlewares.js" and mark it as external
build.onResolve({ filter: /^\.\/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 && fs.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]: fs.statSync(file).size
}
})
: zlib.createGzip({ level: zlib.constants.Z_BEST_COMPRESSION });
const source = fs.createReadStream(file);
const destination = fs.createWriteStream(`${file}.${format}`);
await pipe(source, compress, destination);
}