Skip to content

Commit

Permalink
fix: write envConfig template to outDir from config
Browse files Browse the repository at this point in the history
  • Loading branch information
lukashass committed Mar 31, 2022
1 parent 196893e commit b28bd1d
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/envConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs';
import path from 'path';
import type { Plugin } from 'vite';
import type { Plugin, ResolvedConfig } from 'vite';

export function createEnvConfigContent(variables: string[], template: boolean): string {
let templateContent = '';
Expand All @@ -26,12 +26,12 @@ export interface EnvConfigOptions {
}

export function envConfig(userOptions: Partial<EnvConfigOptions> = {}): Plugin {
let root: string;
let config: ResolvedConfig;
return {
name: 'vite-plugin-env-config',

configResolved(config) {
root = config.root;
configResolved(resolvedConfig) {
config = resolvedConfig;
},

configureServer(server) {
Expand All @@ -55,7 +55,7 @@ export function envConfig(userOptions: Partial<EnvConfigOptions> = {}): Plugin {
closeBundle() {
const templateContent = createEnvConfigContent(userOptions.variables || [], true);

const TEMPLATE_PATH = path.join(root, 'dist', 'env-config.template.js');
const TEMPLATE_PATH = path.join(config.root, config.build.outDir, 'env-config.template.js');
fs.mkdirSync(path.dirname(TEMPLATE_PATH), { recursive: true });
fs.writeFileSync(TEMPLATE_PATH, templateContent, 'utf8');
},
Expand Down

0 comments on commit b28bd1d

Please sign in to comment.