-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
utils.js
157 lines (136 loc) · 3.93 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
import * as vite from 'vite';
import { get_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();
/**
* @param {string} file
* @param {boolean} add_js
*/
function traverse(file, add_js) {
if (seen.has(file)) return;
seen.add(file);
const chunk = manifest[file];
if (add_js) imports.add(chunk.file);
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));
}
}
traverse(entry, true);
return {
file: manifest[entry].file,
imports: Array.from(imports),
stylesheets: Array.from(stylesheets)
};
}
/**
* 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 const get_default_config = function ({ 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',
outDir,
polyfillModulePreload: false,
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]`
},
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_DEV__: 'false'
},
publicDir: ssr ? false : config.kit.files.assets,
resolve: {
alias: get_aliases(config.kit)
},
ssr: {
noExternal: ['@sveltejs/kit']
},
worker: {
rollupOptions: {
output: {
entryFileNames: `${prefix}/workers/[name]-[hash].js`,
chunkFileNames: `${prefix}/workers/chunks/[name]-[hash].js`
}
}
}
};
};
/**
* @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);
}