-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
utils.js
169 lines (150 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
import fs from 'fs';
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 imports = new Set();
/** @type {Set<string>} */
const stylesheets = new Set();
/**
* @param {string} file
* @param {boolean} add_js
*/
function traverse(file, add_js) {
const chunk = manifest[file];
if (imports.has(chunk.file)) return;
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 }) {
return {
appType: 'custom',
base: assets_base(config.kit),
build: {
cssCodeSplit: true,
manifest: true,
outDir,
polyfillModulePreload: false,
rollupOptions: {
input,
output: {
format: 'esm',
entryFileNames: ssr ? '[name].js' : `${config.kit.appDir}/immutable/[name]-[hash].js`,
chunkFileNames: `${config.kit.appDir}/immutable/chunks/[name]-[hash].js`,
assetFileNames: `${config.kit.appDir}/immutable/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)
},
// prevent Vite copying the contents of `config.kit.files.assets`,
// if it happens to be 'public' instead of 'static'
publicDir: false,
resolve: {
alias: get_aliases(config.kit)
},
ssr: {
// when developing against the Kit src code, we want to ensure that
// our dependencies are bundled so that apps don't need to install
// them as peerDependencies
noExternal: process.env.BUNDLED
? []
: Object.keys(
JSON.parse(fs.readFileSync(new URL('../../../package.json', import.meta.url), 'utf-8'))
.devDependencies
)
}
};
};
/**
* @param {import('types').ValidatedKitConfig} config
* @returns {string}
*/
export function assets_base(config) {
// TODO this is so that Vite's preloading works. Unfortunately, it fails
// during `svelte-kit preview`, because we use a local asset path. This
// may be fixed in Vite 3: https://github.com/vitejs/vite/issues/2009
const { base, assets } = config.paths;
return `${assets || base}/`;
}
/**
* vite.config.js will contain vite-plugin-svelte-kit, which kicks off the server and service
* worker builds in a hook. When running the server and service worker builds we must remove
* the SvelteKit plugin so that we do not kick off additional instances of these builds.
* @param {import('vite').UserConfig} config
*/
export function remove_svelte_kit(config) {
// TODO i feel like there's a more elegant way to do this
// @ts-expect-error - it can't handle infinite type expansion
config.plugins = (config.plugins || [])
.flat(Infinity)
.filter((plugin) => plugin.name !== 'vite-plugin-svelte-kit');
}
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);
}