@@ -77,11 +77,13 @@ function commonViteOpts({build, ...other}: InlineConfig): InlineConfig {
7777 } ;
7878}
7979
80- function iifeBuildOpts ( { sourceFileName, entryFileName , write} : { sourceFileName : string , entryFileName : string , write ?: boolean } ) {
80+ function iifeBuildOpts ( { sourceFileName, write} : { sourceFileName : string , write ?: boolean } ) {
8181 const sourceBaseName = basename ( sourceFileName , '.ts' ) ;
82+ // HINT: VITE-OUTPUT-DIR: all outputted JS files are in "js" directory
83+ const entryFileName = `js/${ sourceBaseName } .[hash:8].js` ;
8284 return commonViteOpts ( {
8385 build : {
84- lib : { entry : join ( import . meta. dirname , 'web_src' , sourceFileName ) , name : camelize ( sourceBaseName ) , formats : [ 'iife' ] } ,
86+ lib : { entry : join ( import . meta. dirname , 'web_src/js ' , sourceFileName ) , name : camelize ( sourceBaseName ) , formats : [ 'iife' ] } ,
8587 rolldownOptions : { output : { entryFileNames : entryFileName } } ,
8688 ...( write === false && { write : false } ) ,
8789 } ,
@@ -96,17 +98,12 @@ function iifePlugin(sourceFileName: string): Plugin {
9698 const iifeModules = new Set < string > ( ) ;
9799 let isBuilding = false ;
98100
99- const sourceDir = path . dirname ( sourceFileName ) , sourceBaseName = path . basename ( sourceFileName , '.ts' ) ;
100- const sourcePathPrefix = `${ sourceDir } /${ sourceBaseName } ` ;
101+ const sourceBaseName = path . basename ( sourceFileName , '.ts' ) ;
101102 return {
102- name : `iife:${ sourcePathPrefix } ` , // plugin name
103+ name : `iife:${ sourceFileName } ` , // plugin name
103104 async configureServer ( server ) {
104105 const buildAndCache = async ( ) => {
105- const result = await build ( iifeBuildOpts ( {
106- sourceFileName : `${ sourcePathPrefix } .ts` ,
107- entryFileName : `${ sourceDir } /${ sourceBaseName } .js` ,
108- write : false ,
109- } ) ) ;
106+ const result = await build ( iifeBuildOpts ( { sourceFileName, write : false } ) ) ;
110107 const output = ( Array . isArray ( result ) ? result [ 0 ] : result ) as Rolldown . RolldownOutput ;
111108 const chunk = output . output [ 0 ] ;
112109 iifeCode = chunk . code . replace ( / \/ \/ # s o u r c e M a p p i n g U R L = .* / , `//# sourceMappingURL=${ sourceBaseName } .js.map` ) ;
@@ -139,11 +136,11 @@ function iifePlugin(sourceFileName: string): Plugin {
139136 const pathname = req . url ! . split ( '?' ) [ 0 ] ;
140137 if ( pathname === '/web_src/js/__vite_dev_server_check' ) {
141138 res . end ( 'ok' ) ;
142- } else if ( pathname === `/web_src/${ sourcePathPrefix } .ts ` ) {
139+ } else if ( pathname === `/web_src/js/ ${ sourceFileName } ` ) {
143140 res . setHeader ( 'Content-Type' , 'application/javascript' ) ;
144141 res . setHeader ( 'Cache-Control' , 'no-store' ) ;
145142 res . end ( iifeCode ) ;
146- } else if ( pathname === `/web_src/js/${ sourcePathPrefix } .js.map` ) {
143+ } else if ( pathname === `/web_src/js/${ sourceBaseName } .js.map` ) {
147144 res . setHeader ( 'Content-Type' , 'application/json' ) ;
148145 res . setHeader ( 'Cache-Control' , 'no-store' ) ;
149146 res . end ( iifeMap ) ;
@@ -153,32 +150,38 @@ function iifePlugin(sourceFileName: string): Plugin {
153150 } ) ;
154151 } ,
155152 async closeBundle ( ) {
156- for ( const file of globSync ( `${ sourcePathPrefix } .*.js*` , { cwd : outDir } ) ) unlinkSync ( join ( outDir , file ) ) ;
157- const result = await build ( iifeBuildOpts ( {
158- sourceFileName : `${ sourcePathPrefix } .ts` ,
159- entryFileName : `${ sourcePathPrefix } .[hash:8].js` ,
160- } ) ) ;
153+ for ( const file of globSync ( `js/${ sourceBaseName } .*.js*` , { cwd : outDir } ) ) unlinkSync ( join ( outDir , file ) ) ;
154+
155+ const result = await build ( iifeBuildOpts ( { sourceFileName} ) ) ;
161156 const buildOutput = ( Array . isArray ( result ) ? result [ 0 ] : result ) as Rolldown . RolldownOutput ;
162- const entry = buildOutput . output . find ( ( o ) => o . fileName . startsWith ( `${ sourcePathPrefix } .` ) ) ;
157+ const entry = buildOutput . output . find ( ( o ) => o . fileName . startsWith ( `js/ ${ sourceBaseName } .` ) ) ;
163158 if ( ! entry ) throw new Error ( 'IIFE build produced no output' ) ;
164159
165160 const manifestPath = join ( outDir , '.vite' , 'manifest.json' ) ;
166161 const manifestData = JSON . parse ( readFileSync ( manifestPath , 'utf8' ) ) ;
167- manifestData [ `web_src/${ sourcePathPrefix } .js ` ] = { file : entry . fileName , name : sourceBaseName , isEntry : true } ;
162+ manifestData [ `web_src/js/ ${ sourceFileName } ` ] = { file : entry . fileName , name : sourceBaseName , isEntry : true } ;
168163 writeFileSync ( manifestPath , JSON . stringify ( manifestData , null , 2 ) ) ;
169164 } ,
170165 } ;
171166}
172167
173168// In reduced sourcemap mode, only keep sourcemaps for main files
174169function reducedSourcemapPlugin ( ) : Plugin {
170+ const standalonePrefixes = [
171+ 'js/index.' ,
172+ 'js/iife.' ,
173+ 'js/swagger.' ,
174+ 'js/external-render-helper.' ,
175+ 'js/eventsource.sharedworker.' ,
176+ ] ;
175177 return {
176178 name : 'reduced-sourcemap' ,
177179 apply : 'build' ,
178180 closeBundle ( ) {
179181 if ( enableSourcemap !== 'reduced' ) return ;
180182 for ( const file of globSync ( '{js,css}/*.map' , { cwd : outDir } ) ) {
181- if ( ! file . startsWith ( 'js/index.' ) && ! file . startsWith ( 'js/iife.' ) && ! file . startsWith ( 'js/standalone-' ) ) unlinkSync ( join ( outDir , file ) ) ;
183+ if ( standalonePrefixes . some ( ( prefix ) => file . startsWith ( prefix ) ) ) continue ;
184+ unlinkSync ( join ( outDir , file ) ) ;
182185 }
183186 } ,
184187 } ;
@@ -255,15 +258,15 @@ export default defineConfig(commonViteOpts({
255258 rolldownOptions : {
256259 input : {
257260 index : join ( import . meta. dirname , 'web_src/js/index.ts' ) ,
258- swagger : join ( import . meta. dirname , 'web_src/js/standalone-swagger.ts' ) ,
259- 'external-render-iframe' : join ( import . meta. dirname , 'web_src/js/standalone-external-render.ts' ) ,
260- 'eventsource.sharedworker' : join ( import . meta. dirname , 'web_src/js/features/eventsource.sharedworker.ts' ) ,
261+ swagger : join ( import . meta. dirname , 'web_src/js/swagger.ts' ) ,
262+ 'eventsource.sharedworker' : join ( import . meta. dirname , 'web_src/js/eventsource.sharedworker.ts' ) ,
261263 devtest : join ( import . meta. dirname , 'web_src/css/devtest.css' ) ,
262264 ...themes ,
263265 } ,
264266 output : {
265- // all outputted JS files are in "/js" directory, so standalone source files should also be in "js" directory
266- // to keep consistent between production and dev server and avoid unexpected behaviors.
267+ // HINT: VITE-OUTPUT-DIR: all outputted JS files are in "js" directory
268+ // So standalone/iife source files should also be in "js" directory,
269+ // to keep consistent between production and dev server, avoid unexpected behaviors.
267270 entryFileNames : 'js/[name].[hash:8].js' ,
268271 chunkFileNames : 'js/[name].[hash:8].js' ,
269272 assetFileNames : ( { names} ) => {
@@ -297,8 +300,8 @@ export default defineConfig(commonViteOpts({
297300 __VUE_PROD_HYDRATION_MISMATCH_DETAILS__ : false ,
298301 } ,
299302 plugins : [
300- iifePlugin ( 'js/ iife.ts' ) ,
301- iifePlugin ( 'js/standalone- external-render.ts' ) ,
303+ iifePlugin ( 'iife.ts' ) ,
304+ iifePlugin ( 'external-render-helper .ts' ) ,
302305 viteDevServerPortPlugin ( ) ,
303306 reducedSourcemapPlugin ( ) ,
304307 filterCssUrlPlugin ( ) ,
0 commit comments