1
- import fs from 'fs';
2
- import path from 'path';
3
- import type { Plugin, ResolvedConfig } from 'vite';
1
+ import type { Plugin } from 'vite';
4
2
5
3
export function createEnvConfigContent(variables: string[], template: boolean): string {
6
4
let templateContent = '';
@@ -26,14 +24,9 @@ export interface EnvConfigOptions {
26
24
}
27
25
28
26
export function envConfig(userOptions: Partial<EnvConfigOptions> = {}): Plugin {
29
- let config: ResolvedConfig;
30
27
return {
31
28
name: 'vite-plugin-env-config',
32
29
33
- configResolved(resolvedConfig) {
34
- config = resolvedConfig;
35
- },
36
-
37
30
configureServer(server) {
38
31
const envConfigContent = createEnvConfigContent(userOptions.variables || [], false);
39
32
@@ -52,12 +45,13 @@ export function envConfig(userOptions: Partial<EnvConfigOptions> = {}): Plugin {
52
45
});
53
46
},
54
47
55
- closeBundle () {
48
+ generateBundle () {
56
49
const templateContent = createEnvConfigContent(userOptions.variables || [], true);
57
-
58
- const TEMPLATE_PATH = path.join(config.root, config.build.outDir, 'env-config.template.js');
59
- fs.mkdirSync(path.dirname(TEMPLATE_PATH), { recursive: true });
60
- fs.writeFileSync(TEMPLATE_PATH, templateContent, 'utf8');
50
+ this.emitFile({
51
+ type: 'asset',
52
+ fileName: 'env-config.template.js',
53
+ source: templateContent,
54
+ });
61
55
},
62
56
63
57
transformIndexHtml(html) {
0 commit comments