-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
62 lines (51 loc) · 1.62 KB
/
index.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { DotenvConfigOptions, config as dotenvConfig } from 'dotenv';
const PLUGIN_NAME = 'vite-plugin-vue-env';
type Variables = Record<string, string>;
interface Options {
fileRegexp: RegExp;
getEnvFullName(name: string): string;
variablePrefix: string;
dotenvConfigOptions?: DotenvConfigOptions;
debug?: boolean;
}
function pluginVueEnv(variables: Variables = {}, args: Partial<Options> = {}) {
const options: Options = {
fileRegexp: /\.(m?jsx?|tsx?|vue)$/i,
getEnvFullName: (name: string) => `process.env.${name}`,
variablePrefix: 'VUE_APP_',
debug: false,
...args,
};
const dotenvVars = Object.entries(dotenvConfig(options.dotenvConfigOptions).parsed || {}).reduce((acc, [name, value]) => {
if (name.toLowerCase().startsWith(String(options.variablePrefix).toLowerCase())) {
acc[options.getEnvFullName(name)] = JSON.stringify(value);
}
return acc;
}, {} as Variables);
const userVars = Object.entries(variables).reduce((acc, [name, value]) => {
acc[options.getEnvFullName(name)] = JSON.stringify(value);
return acc;
}, {} as Variables);
const allVars = {
...dotenvVars,
...userVars,
};
if (options.debug) {
console.group(PLUGIN_NAME);
console.log(allVars);
console.groupEnd();
}
return {
name: PLUGIN_NAME,
transform(code: string, id: string) {
const isJs = id.match(options.fileRegexp);
if (isJs) {
return {
code: Object.entries(allVars).reduce((acc, [name, value]) => acc.replace(new RegExp(name, 'g'), value), code),
};
}
},
};
}
export { pluginVueEnv };
export default pluginVueEnv;