-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
esbuild_plugin_sveltekit_shim_env.ts
46 lines (43 loc) 路 1.08 KB
/
esbuild_plugin_sveltekit_shim_env.ts
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
import type * as esbuild from 'esbuild';
import {render_env_shim_module} from './sveltekit_shim_env.js';
export interface Options {
dev: boolean;
public_prefix?: string;
private_prefix?: string;
env_dir?: string;
env_files?: string[];
ambient_env?: Record<string, string>;
}
export const esbuild_plugin_sveltekit_shim_env = ({
dev,
public_prefix,
private_prefix,
env_dir,
env_files,
ambient_env,
}: Options): esbuild.Plugin => ({
name: 'sveltekit_shim_env',
setup: (build) => {
const namespace = 'sveltekit_shim_env';
const filter = /^\$env\/(static|dynamic)\/(public|private)$/u;
build.onResolve({filter}, ({path}) => ({path, namespace}));
build.onLoad({filter: /.*/u, namespace}, async ({path}) => {
const matches = filter.exec(path);
const mode = matches![1] as 'static' | 'dynamic';
const visibility = matches![2] as 'public' | 'private';
return {
loader: 'ts',
contents: await render_env_shim_module(
dev,
mode,
visibility,
public_prefix,
private_prefix,
env_dir,
env_files,
ambient_env,
),
};
});
},
});