-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
utils.js
195 lines (168 loc) · 4.8 KB
/
utils.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import fs from 'fs';
import path from 'path';
import * as vite from 'vite';
import { get_config_aliases, get_app_aliases } from '../utils.js';
/**
* @typedef {import('rollup').RollupOutput} RollupOutput
* @typedef {import('rollup').OutputChunk} OutputChunk
* @typedef {import('rollup').OutputAsset} OutputAsset
*/
/**
* Invokes Vite.
* @param {import('vite').UserConfig} config
*/
export async function create_build(config) {
const { output } = /** @type {RollupOutput} */ (
await vite.build({ ...config, configFile: false })
);
const chunks = output.filter(
/** @returns {output is OutputChunk} */ (output) => output.type === 'chunk'
);
const assets = output.filter(
/** @returns {output is OutputAsset} */ (output) => output.type === 'asset'
);
return { chunks, assets };
}
/**
* Adds transitive JS and CSS dependencies to the js and css inputs.
* @param {import('vite').Manifest} manifest
* @param {string} entry
* @param {boolean} add_dynamic_css
*/
export function find_deps(manifest, entry, add_dynamic_css) {
/** @type {Set<string>} */
const seen = new Set();
/** @type {Set<string>} */
const imports = new Set();
/** @type {Set<string>} */
const stylesheets = new Set();
/** @type {Set<string>} */
const fonts = new Set();
/**
* @param {string} current
* @param {boolean} add_js
*/
function traverse(current, add_js) {
if (seen.has(current)) return;
seen.add(current);
const { chunk } = resolve_symlinks(manifest, current);
if (add_js) imports.add(chunk.file);
if (chunk.assets) {
for (const asset of chunk.assets) {
if (/\.(woff2?|ttf|otf)$/.test(asset)) {
fonts.add(asset);
}
}
}
if (chunk.css) {
chunk.css.forEach((file) => stylesheets.add(file));
}
if (chunk.imports) {
chunk.imports.forEach((file) => traverse(file, add_js));
}
if (add_dynamic_css && chunk.dynamicImports) {
chunk.dynamicImports.forEach((file) => traverse(file, false));
}
}
const { chunk, file } = resolve_symlinks(manifest, entry);
traverse(file, true);
return {
file: chunk.file,
imports: Array.from(imports),
stylesheets: Array.from(stylesheets),
fonts: Array.from(fonts)
};
}
/**
* @param {import('vite').Manifest} manifest
* @param {string} file
*/
export function resolve_symlinks(manifest, file) {
while (!manifest[file]) {
file = path.relative('.', fs.realpathSync(file));
}
const chunk = manifest[file];
return { chunk, file };
}
/**
* The Vite configuration that we use by default.
* @param {{
* config: import('types').ValidatedConfig;
* input: Record<string, string>;
* ssr: boolean;
* outDir: string;
* }} options
* @return {import('vite').UserConfig}
*/
export function get_default_build_config({ config, input, ssr, outDir }) {
const prefix = `${config.kit.appDir}/immutable`;
return {
appType: 'custom',
base: ssr ? assets_base(config.kit) : './',
build: {
cssCodeSplit: true,
// don't use the default name to avoid collisions with 'static/manifest.json'
manifest: 'vite-manifest.json',
modulePreload: {
polyfill: false
},
outDir,
rollupOptions: {
input,
output: {
format: 'esm',
entryFileNames: ssr ? '[name].js' : `${prefix}/[name]-[hash].js`,
chunkFileNames: ssr ? 'chunks/[name].js' : `${prefix}/chunks/[name]-[hash].js`,
assetFileNames: `${prefix}/assets/[name]-[hash][extname]`,
hoistTransitiveImports: false
},
preserveEntrySignatures: 'strict'
},
ssr,
target: ssr ? 'node14.8' : undefined
},
define: {
__SVELTEKIT_ADAPTER_NAME__: JSON.stringify(config.kit.adapter?.name),
__SVELTEKIT_APP_VERSION__: JSON.stringify(config.kit.version.name),
__SVELTEKIT_APP_VERSION_FILE__: JSON.stringify(`${config.kit.appDir}/version.json`),
__SVELTEKIT_APP_VERSION_POLL_INTERVAL__: JSON.stringify(config.kit.version.pollInterval),
__SVELTEKIT_BROWSER__: ssr ? 'false' : 'true',
__SVELTEKIT_DEV__: 'false'
},
publicDir: ssr ? false : config.kit.files.assets,
resolve: {
alias: [...get_app_aliases(config.kit), ...get_config_aliases(config.kit)]
},
optimizeDeps: {
exclude: ['@sveltejs/kit']
},
ssr: {
noExternal: ['@sveltejs/kit']
},
worker: {
rollupOptions: {
output: {
entryFileNames: `${prefix}/workers/[name]-[hash].js`,
chunkFileNames: `${prefix}/workers/chunks/[name]-[hash].js`,
hoistTransitiveImports: false
}
}
}
};
}
/**
* @param {import('types').ValidatedKitConfig} config
* @returns {string}
*/
export function assets_base(config) {
return (config.paths.assets || config.paths.base || '.') + '/';
}
const method_names = new Set(['GET', 'HEAD', 'PUT', 'POST', 'DELETE', 'PATCH']);
// If we'd written this in TypeScript, it could be easy...
/**
* @param {string} str
* @returns {str is import('types').HttpMethod}
*/
export function is_http_method(str) {
return method_names.has(str);
}