Repository navigation
Expand file tree
/
Copy pathgro_plugin_sveltekit_app.ts
More file actions
60 lines (57 loc) 路 1.87 KB
/
Copy pathgro_plugin_sveltekit_app.ts
File metadata and controls
60 lines (57 loc) 路 1.87 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
import { args_serialize } from '@fuzdev/fuz_util/args.ts';
import { spawn_result_to_message, type SpawnedProcess } from '@fuzdev/fuz_util/process.ts';
import { to_forwarded_args } from './args.ts';
import { find_cli, spawn_cli, spawn_cli_process } from './cli.ts';
import { VITE_CLI } from './constants.ts';
import type { Plugin } from './plugin.ts';
import { TaskError } from './task.ts';
export interface GroPluginSveltekitAppOptions {
/**
* The Vite CLI to use.
*/
vite_cli?: string;
}
export const gro_plugin_sveltekit_app = ({
vite_cli = VITE_CLI
}: GroPluginSveltekitAppOptions = {}): Plugin => {
let sveltekit_process: SpawnedProcess | undefined = undefined;
return {
name: 'gro_plugin_sveltekit_app',
setup: async ({ dev, watch, log, config }) => {
const found_vite_cli = await find_cli(vite_cli);
if (!found_vite_cli)
throw Error(
`Failed to find Vite CLI \`${vite_cli}\`, do you need to run \`${config.pm_cli} i\`?`
);
if (dev) {
// `vite dev` in development mode
if (watch) {
const serialized_args = ['dev', ...args_serialize(to_forwarded_args(vite_cli))];
sveltekit_process = await spawn_cli_process(found_vite_cli, serialized_args, log);
} else {
log.debug(
`the SvelteKit app plugin is loaded but will not output anything` +
' because `dev` is true and `watch` is false'
);
}
} else {
// `vite build` in production mode
const serialized_args = ['build', ...args_serialize(to_forwarded_args(vite_cli))];
const spawned = await spawn_cli(found_vite_cli, serialized_args, log);
if (!spawned?.ok) {
throw new TaskError(
`${vite_cli} build failed: ${
spawned ? spawn_result_to_message(spawned) : 'unknown error'
}`
);
}
}
},
teardown: async () => {
if (sveltekit_process) {
sveltekit_process.child.kill();
await sveltekit_process.closed;
}
}
};
};