11import path from 'path' ;
22import { fileURLToPath } from 'url' ;
33import { execSync } from 'child_process' ;
4- import { defineConfig , loadEnv , ConfigEnv , LibraryFormats } from 'vite' ;
4+ import {
5+ defineConfig ,
6+ loadEnv ,
7+ ConfigEnv ,
8+ BuildEnvironmentOptions ,
9+ } from 'vite' ;
510import type { PreRenderedAsset } from 'rollup' ;
611import react from '@vitejs/plugin-react-swc' ;
712import checker from 'vite-plugin-checker' ;
@@ -12,10 +17,10 @@ import packageJson from './package.json';
1217
1318const __filename = fileURLToPath ( import . meta. url ) ;
1419const __dirname = path . dirname ( __filename ) ;
15-
1620const { version } = packageJson ;
1721
1822let gitHash = 'unknown' ;
23+
1924try {
2025 gitHash = execSync ( 'git log -1 --format=%H' ) . toString ( ) . replace ( '\n' , '' ) ;
2126} catch ( e ) {
@@ -27,11 +32,6 @@ console.info(
2732 `\nBuilding Wormhole Connect version=${ version } hash=${ gitHash } \n` ,
2833) ;
2934
30- // There are three configs this file can return.
31- // 1. local dev server
32- // 2. production build, for direct import
33- // 3. production build, hosted by unpkg.com (includes React, auto-binds to DOM)
34-
3535// TODO: consider using the "VITE_APP_" prefix which is the default for Vite
3636const envPrefix = 'REACT_APP_' ;
3737
@@ -65,12 +65,8 @@ const resolve = {
6565} ;
6666
6767const plugins = [
68- checker ( {
69- typescript : true ,
70- } ) ,
71- dts ( {
72- insertTypesEntry : true ,
73- } ) ,
68+ checker ( { typescript : true } ) ,
69+ dts ( { insertTypesEntry : true } ) ,
7470 react ( ) ,
7571 nodePolyfills ( {
7672 include : [
@@ -97,135 +93,128 @@ const plugins = [
9793 } ) ,
9894] . filter ( Boolean ) ;
9995
100- const optimizeDeps = { } ;
96+ function assetFileNames ( assetInfo : PreRenderedAsset ) {
97+ if ( assetInfo . name === 'main.css' ) {
98+ return '[name][extname]' ;
99+ }
101100
102- const output : {
103- assetFileNames : ( assetInfo : PreRenderedAsset ) => string ;
104- inlineDynamicImports : boolean ;
105- exports : 'named' | 'default' | 'none' | 'auto' ;
106- } = {
107- assetFileNames : ( assetInfo : PreRenderedAsset ) => {
108- if ( assetInfo . name === 'main.css' ) {
109- return '[name][extname]' ;
110- }
111- return '[name]-[hash][extname]' ;
101+ return '[name]-[hash][extname]' ;
102+ }
103+
104+ // Netlify and local dev server
105+ const sampleAppBuild : BuildEnvironmentOptions = {
106+ outDir : './build' ,
107+ rollupOptions : {
108+ input : {
109+ main : 'src/SampleApp.tsx' ,
110+ index : 'index.html' ,
111+ } as Record < string , string > ,
112+ output : {
113+ assetFileNames,
114+ inlineDynamicImports : false ,
115+ exports : 'named' as const ,
116+ } ,
112117 } ,
113- inlineDynamicImports : false ,
114- exports : 'named' as const ,
118+ } ;
119+
120+ // Legacy production build, hosted by unpkg.com (includes React, auto-binds to DOM)
121+ const hostedBuild : BuildEnvironmentOptions = {
122+ outDir : './dist' ,
123+ rollupOptions : {
124+ input : {
125+ main : 'src/main.tsx' ,
126+ } as Record < string , string > ,
127+ output : {
128+ entryFileNames : '[name].mjs' ,
129+ assetFileNames,
130+ inlineDynamicImports : false ,
131+ exports : 'named' as const ,
132+ } ,
133+ } ,
134+ } ;
135+
136+ // Production build, for direct import
137+ const libBuild : BuildEnvironmentOptions = {
138+ outDir : './lib' ,
139+ lib : {
140+ entry : [
141+ path . resolve ( __dirname , 'src/exports/index.ts' ) ,
142+ path . resolve ( __dirname , 'src/exports/mayan.ts' ) ,
143+ path . resolve ( __dirname , 'src/exports/ntt.ts' ) ,
144+ path . resolve ( __dirname , 'src/exports/hosted.ts' ) ,
145+ path . resolve ( __dirname , 'src/exports/executor.ts' ) ,
146+ ] ,
147+ formats : [ 'es' ] ,
148+ // fileName: (_, entryname) => {
149+ // const n = entryname.split('/').pop()!;
150+ // return `${n.split('.')[0]}.mjs`;
151+ // },
152+ } ,
153+ rollupOptions : {
154+ input : {
155+ index : 'src/exports/index.ts' ,
156+ mayan : 'src/exports/mayan.ts' ,
157+ ntt : 'src/exports/ntt.ts' ,
158+ hosted : 'src/exports/hosted.ts' ,
159+ executor : 'src/exports/executor.ts' ,
160+ } ,
161+ output : {
162+ // entryFileNames: '[name].mjs',
163+ chunkFileNames : '[name].mjs' ,
164+ assetFileNames : '[name].[ext]' ,
165+ inlineDynamicImports : false ,
166+ preserveModules : true ,
167+ preserveModulesRoot : 'src' ,
168+ entryFileNames ( chunkInfo ) {
169+ if ( chunkInfo . name . includes ( 'node_modules' ) ) {
170+ return chunkInfo . name . replace ( 'node_modules' , 'external' ) + '.mjs' ;
171+ }
172+
173+ return '[name].mjs' ;
174+ } ,
175+ } ,
176+ external : [
177+ 'react' ,
178+ 'react/jsx-runtime' ,
179+ '@emotion/react' ,
180+ '@emotion/styled' ,
181+ '@mui/material' ,
182+ '@mui/icons-material' ,
183+ '@mui/styled-engine' ,
184+ '@mui/system' ,
185+ ] ,
186+ } ,
187+ minify : false ,
188+ terserOptions : {
189+ mangle : false ,
190+ compress : false ,
191+ } ,
192+ sourcemap : true ,
115193} ;
116194
117195export default defineConfig ( ( { command, mode } : ConfigEnv ) => {
118196 const env = loadEnv ( mode , process . cwd ( ) , '' ) ;
119197 const isHosted = ! ! env . VITE_BUILD_HOSTED ;
120198 const isNetlify = ! ! env . VITE_BUILD_NETLIFY ;
121- const isAnalyze = process . env . ANALYZE === 'true' ;
122-
123- if ( command === 'serve' || ( command === 'build' && isNetlify ) ) {
124- // Local development
125- return {
126- define,
127- envPrefix,
128- resolve,
129- build : {
130- outDir : './build' ,
131- rollupOptions : {
132- input : {
133- main : 'src/SampleApp.tsx' ,
134- index : 'index.html' ,
135- } as Record < string , string > ,
136- output,
137- } ,
138- } ,
139- plugins,
140- optimizeDeps,
141- } ;
142- } else if ( command === 'build' ) {
143- //
144- // Building for production
145- // There are two possible configs here: invoked by "npm run build" and "npm run build:hosted"
146- //
147- // - by default, we build a component library that can be imported and used in React apps
148- //
149- // - alternatively, VITE_BUILD_HOSTED=1 causes Vite to build a bundle that is used
150- // via unpkg.com hosting. This build looks for a DOM element #wormhole-connect and comes
151- // bundled with a copy of React. This is "legacy mode" and useful only on web apps that don't
152- // use React.
153- //
199+ const isSampleApp = command === 'serve' || ( command === 'build' && isNetlify ) ;
200+
201+ let build : BuildEnvironmentOptions | undefined = undefined ;
154202
203+ if ( isSampleApp ) {
204+ build = sampleAppBuild ;
205+ } else if ( command === 'build' ) {
155206 if ( isHosted ) {
156- return {
157- define,
158- envPrefix,
159- resolve,
160- build : {
161- outDir : './dist' ,
162- rollupOptions : {
163- input : {
164- main : 'src/main.tsx' ,
165- } as Record < string , string > ,
166- output : {
167- entryFileNames : '[name].js' ,
168- ...output ,
169- } ,
170- } ,
171- } ,
172- plugins,
173- optimizeDeps,
174- } ;
207+ build = hostedBuild ;
175208 } else {
176- return {
177- define,
178- envPrefix,
179- resolve,
180- build : {
181- outDir : './lib' ,
182- lib : {
183- entry : [
184- path . resolve ( __dirname , 'src/exports/index.ts' ) ,
185- path . resolve ( __dirname , 'src/exports/mayan.ts' ) ,
186- path . resolve ( __dirname , 'src/exports/ntt.ts' ) ,
187- path . resolve ( __dirname , 'src/exports/hosted.ts' ) ,
188- path . resolve ( __dirname , 'src/exports/executor.ts' ) ,
189- ] ,
190- formats : ( isAnalyze ? [ 'es' ] : [ 'es' , 'cjs' ] ) as LibraryFormats [ ] ,
191- fileName : ( format , entryname ) => {
192- const n = entryname . split ( '/' ) . pop ( ) ! ;
193- return `${ n . split ( '.' ) [ 0 ] } .${ format === 'es' ? 'mjs' : 'js' } ` ;
194- } ,
195- } ,
196- rollupOptions : {
197- input : {
198- index : 'src/exports/index.ts' ,
199- mayan : 'src/exports/mayan.ts' ,
200- ntt : 'src/exports/ntt.ts' ,
201- hosted : 'src/exports/hosted.ts' ,
202- executor : 'src/exports/executor.ts' ,
203- } ,
204- output,
205- external : [
206- 'react' ,
207- 'react/jsx-runtime' ,
208- '@emotion/react' ,
209- '@emotion/styled' ,
210- '@mui/material' ,
211- '@mui/icons-material' ,
212- '@mui/styled-engine' ,
213- '@mui/system' ,
214- ] ,
215- } ,
216- } ,
217- plugins,
218- optimizeDeps,
219- } ;
209+ build = libBuild ;
220210 }
221211 }
222212
223- // Default configuration if no conditions are met
224213 return {
214+ build,
225215 define,
226216 envPrefix,
227217 resolve,
228218 plugins,
229- optimizeDeps,
230219 } ;
231220} ) ;
0 commit comments