Skip to content

Commit 5b07ca2

Browse files
authored
fix: write envConfig template in generateBundle hook (#21)
1 parent 6776360 commit 5b07ca2

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

src/envConfig.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import fs from 'fs';
2-
import path from 'path';
3-
import type { Plugin, ResolvedConfig } from 'vite';
1+
import type { Plugin } from 'vite';
42

53
export function createEnvConfigContent(variables: string[], template: boolean): string {
64
let templateContent = '';
@@ -26,14 +24,9 @@ export interface EnvConfigOptions {
2624
}
2725

2826
export function envConfig(userOptions: Partial<EnvConfigOptions> = {}): Plugin {
29-
let config: ResolvedConfig;
3027
return {
3128
name: 'vite-plugin-env-config',
3229

33-
configResolved(resolvedConfig) {
34-
config = resolvedConfig;
35-
},
36-
3730
configureServer(server) {
3831
const envConfigContent = createEnvConfigContent(userOptions.variables || [], false);
3932

@@ -52,12 +45,13 @@ export function envConfig(userOptions: Partial<EnvConfigOptions> = {}): Plugin {
5245
});
5346
},
5447

55-
closeBundle() {
48+
generateBundle() {
5649
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+
});
6155
},
6256

6357
transformIndexHtml(html) {

0 commit comments

Comments
 (0)