@@ -13,31 +13,14 @@ import staticData from './data.mjs';
1313 * @param {boolean } [options.server=false] - Whether this is a server-side build.
1414 */
1515export default async function bundleCode ( codeMap , { server = false } = { } ) {
16- // Store the import map HTML for later extraction
17- let importMapHtml = '' ;
18-
19- /** @type {import('rolldown').OutputOptions } */
20- const serverOutputConfig = {
21- // Inline all dynamic imports to create a single self-contained bundle
22- inlineDynamicImports : true ,
23- } ;
24-
25- /** @type {import('rolldown').InputOptions['experimental'] } */
26- const clientExperimentalConfig = {
27- // Generate an import map for cache-busted module resolution in browsers
28- // https://rolldown.rs/options/experimental#chunkimportmap
29- chunkImportMap : {
30- baseUrl : './' ,
31- fileName : 'importmap.json' ,
32- } ,
33- } ;
34-
3516 const result = await build ( {
3617 // Entry points: array of virtual module names that the virtual plugin provides
3718 input : Array . from ( codeMap . keys ( ) ) ,
3819
3920 // Experimental features: import maps for client, none for server
40- experimental : server ? { } : clientExperimentalConfig ,
21+ experimental : {
22+ chunkImportMap : ! server ,
23+ } ,
4124
4225 // Output configuration
4326 output : {
@@ -50,8 +33,8 @@ export default async function bundleCode(codeMap, { server = false } = {}) {
5033 // Server builds are usually not minified to preserve stack traces and debuggability.
5134 minify : ! server ,
5235
53- // Environment-specific output configuration
54- ... ( server ? serverOutputConfig : { } ) ,
36+ // Within server builds we want to ensure dynamic imports get inlined whenever possible.
37+ inlineDynamicImports : server ,
5538 } ,
5639
5740 // Platform informs Rolldown of the environment-specific code behavior:
@@ -106,28 +89,6 @@ export default async function bundleCode(codeMap, { server = false } = {}) {
10689 // Load CSS imports via the custom plugin.
10790 // This plugin will collect imported CSS files and return them as `source` chunks.
10891 cssLoader ( ) ,
109-
110- // Extract and transform the import map generated by Rolldown
111- {
112- name : 'extract-import-map' ,
113- /**
114- * Extracts import map from bundle and converts to HTML script tag.
115- *
116- * @param {import('rolldown').NormalizedOutputOptions } _ - Output options (unused).
117- * @param {import('rolldown').OutputBundle } bundle - Bundle object containing all output chunks.
118- */
119- generateBundle ( _ , bundle ) {
120- const chunkImportMap = bundle [ 'importmap.json' ] ;
121-
122- if ( chunkImportMap ?. type === 'asset' ) {
123- // Convert to HTML script tag for inline inclusion
124- importMapHtml = `<script type="importmap">${ chunkImportMap . source } </script>` ;
125-
126- // Remove from bundle to prevent writing as separate file
127- delete bundle [ 'importmap.json' ] ;
128- }
129- } ,
130- } ,
13192 ] ,
13293
13394 // Enable tree-shaking to remove unused code
@@ -138,16 +99,21 @@ export default async function bundleCode(codeMap, { server = false } = {}) {
13899 } ) ;
139100
140101 // Separate CSS assets from JavaScript chunks
141- const cssFiles = result . output . filter ( chunk => chunk . type === 'asset' ) ;
142- const jsChunks = result . output . filter ( chunk => chunk . type === 'chunk' ) ;
102+ const assets = result . output . filter ( c => c . type === 'asset' ) ;
103+ const chunks = result . output . filter ( c => c . type === 'chunk' ) ;
104+
105+ const importMap = assets . find ( c => c . fileName === 'importmap.json' ) ;
143106
144107 return {
145- importMapHtml,
146- css : cssFiles . map ( f => f . source ) . join ( '' ) ,
147- jsChunks : jsChunks . map ( ( { fileName, code, isEntry } ) => ( {
108+ css : assets
109+ . filter ( c => c . fileName . endsWith ( '.css' ) )
110+ . map ( f => f . source )
111+ . join ( '' ) ,
112+ jsChunks : chunks . map ( ( { fileName, code, isEntry } ) => ( {
148113 fileName : fileName . replace ( '_virtual_' , '' ) ,
149- code,
150114 isEntry,
115+ code,
151116 } ) ) ,
117+ importMapHtml : `<script type="importmap">${ importMap ?. source } </script>` ,
152118 } ;
153119}
0 commit comments