File tree Expand file tree Collapse file tree 2 files changed +44
-2
lines changed Expand file tree Collapse file tree 2 files changed +44
-2
lines changed Original file line number Diff line number Diff line change 11import { resolve } from "path" ;
2- import { defineConfig , UserConfig } from "vite" ;
2+ import { defineConfig , PluginOption , UserConfig } from "vite" ;
33import dts from "vite-plugin-dts" ;
44
5+ const handleEnvVariablesPlugin = ( ) : PluginOption => {
6+ return {
7+ name : "handle-env-variables-plugin" ,
8+ generateBundle ( options , bundle ) {
9+ for ( const fileName in bundle ) {
10+ const file = bundle [ fileName ] ;
11+
12+ if ( file . type === "chunk" && file . fileName . endsWith ( ".js" ) ) {
13+ const transformedContent = file . code . replace (
14+ / i m p o r t \. m e t a \. e n v \. V I T E _ ( [ A - Z 0 - 9 _ ] + ) / g,
15+ "(typeof import.meta.env !== 'undefined' ? import.meta.env.VITE_$1 : undefined)" ,
16+ ) ;
17+
18+ file . code = transformedContent ;
19+ }
20+ }
21+ } ,
22+ } ;
23+ } ;
24+
525const config : UserConfig = ( ( ) => {
626 const common : Partial < UserConfig [ "build" ] > = {
727 rollupOptions : {
@@ -40,6 +60,7 @@ const config: UserConfig = (() => {
4060 rollupTypes : true ,
4161 include : [ "src/**/*.ts" ] ,
4262 } ) ,
63+ handleEnvVariablesPlugin ( ) ,
4364 ] ,
4465 define : {
4566 "import.meta.env.VITE_REVERB_APP_KEY" :
Original file line number Diff line number Diff line change 11import { resolve } from "path" ;
2- import { defineConfig , UserConfig } from "vite" ;
2+ import { defineConfig , PluginOption , UserConfig } from "vite" ;
33import dts from "vite-plugin-dts" ;
44
5+ const handleEnvVariablesPlugin = ( ) : PluginOption => {
6+ return {
7+ name : "handle-env-variables-plugin" ,
8+ generateBundle ( options , bundle ) {
9+ for ( const fileName in bundle ) {
10+ const file = bundle [ fileName ] ;
11+
12+ if ( file . type === "chunk" && file . fileName . endsWith ( ".js" ) ) {
13+ const transformedContent = file . code . replace (
14+ / i m p o r t \. m e t a \. e n v \. V I T E _ ( [ A - Z 0 - 9 _ ] + ) / g,
15+ "(typeof import.meta.env !== 'undefined' ? import.meta.env.VITE_$1 : undefined)" ,
16+ ) ;
17+
18+ file . code = transformedContent ;
19+ }
20+ }
21+ } ,
22+ } ;
23+ } ;
24+
525const config : UserConfig = ( ( ) => {
626 const common : Partial < UserConfig [ "build" ] > = {
727 rollupOptions : {
@@ -40,6 +60,7 @@ const config: UserConfig = (() => {
4060 rollupTypes : true ,
4161 include : [ "src/**/*.ts" ] ,
4262 } ) ,
63+ handleEnvVariablesPlugin ( ) ,
4364 ] ,
4465 define : {
4566 "import.meta.env.VITE_REVERB_APP_KEY" :
You can’t perform that action at this time.
0 commit comments