@@ -151,6 +151,35 @@ export function normalizeRollupOutputOptions(
151
151
} ;
152
152
}
153
153
154
+ const getChunkFileName = (
155
+ prefix : string ,
156
+ opts : NormalizedQwikPluginOptions ,
157
+ optimizer : Optimizer
158
+ ) => {
159
+ if ( opts . buildMode === 'production' && ! opts . debug ) {
160
+ return `${ prefix } build/q-[hash].js` ;
161
+ } else {
162
+ // Friendlier names in dev or preview with debug mode
163
+ return ( chunkInfo : Rollup . PreRenderedChunk ) => {
164
+ if ( chunkInfo . moduleIds ?. some ( ( id ) => id . endsWith ( 'core.prod.mjs' ) ) ) {
165
+ return `${ prefix } build/core.js` ;
166
+ }
167
+ if ( chunkInfo . moduleIds ?. some ( ( id ) => id . endsWith ( 'qwik-router/lib/index.qwik.mjs' ) ) ) {
168
+ return `${ prefix } build/qwik-router.js` ;
169
+ }
170
+
171
+ // The chunk name can often be a path. We sanitize it to use dashes instead of slashes, to keep the same folder structure as without debug:true.
172
+ // Besides, Rollup doesn't accept absolute or relative paths as inputs for the [name] placeholder for the same reason.
173
+ const relativePath = optimizer . sys . path . relative ( optimizer . sys . cwd ( ) , chunkInfo . name ) ;
174
+ const sanitized = relativePath
175
+ . replace ( / ^ ( \. \. \/ ) + / , '' )
176
+ . replace ( / ^ \/ + / , '' )
177
+ . replace ( / \/ / g, '-' ) ;
178
+ return `${ prefix } build/${ sanitized } .js` ;
179
+ } ;
180
+ }
181
+ } ;
182
+
154
183
export function normalizeRollupOutputOptionsObject (
155
184
qwikPlugin : QwikPlugin ,
156
185
rollupOutputOptsObj : Rollup . OutputOptions | undefined ,
@@ -160,79 +189,40 @@ export function normalizeRollupOutputOptionsObject(
160
189
const opts = qwikPlugin . getOptions ( ) ;
161
190
const optimizer = qwikPlugin . getOptimizer ( ) ;
162
191
const manualChunks = qwikPlugin . manualChunks ;
163
- if ( opts . target === 'client' ) {
164
- // client output
165
- if ( ! outputOpts . assetFileNames ) {
166
- // SEO likes readable asset names
167
- const assetFileNames = 'assets/[hash]-[name].[ext]' ;
168
- outputOpts . assetFileNames = useAssetsDir
169
- ? `${ opts . assetsDir } /${ assetFileNames } `
170
- : assetFileNames ;
171
- }
172
192
173
- let fileName : string | ( ( chunkInfo : Rollup . PreRenderedChunk ) => string ) | undefined ;
174
- if ( opts . buildMode === 'production' && ! opts . debug ) {
175
- fileName = 'build/q-[hash].js' ;
176
- } else {
177
- // Friendlier names in dev or preview with debug mode
178
- fileName = ( chunkInfo ) => {
179
- if ( chunkInfo . moduleIds ?. some ( ( id ) => id . endsWith ( 'core.prod.mjs' ) ) ) {
180
- return 'build/core.js' ;
181
- }
182
- if ( chunkInfo . moduleIds ?. some ( ( id ) => id . endsWith ( 'qwik-router/lib/index.qwik.mjs' ) ) ) {
183
- return 'build/qwik-router.js' ;
184
- }
185
-
186
- // The chunk name can often be a path. We sanitize it to use dashes instead of slashes, to keep the same folder structure as without debug:true.
187
- // Besides, Rollup doesn't accept absolute or relative paths as inputs for the [name] placeholder for the same reason.
188
- const path = optimizer . sys . path ;
189
- const relativePath = path . relative ( optimizer . sys . cwd ( ) , chunkInfo . name ) ;
190
- const sanitized = relativePath
191
- . replace ( / ^ ( \. \. \/ ) + / , '' )
192
- . replace ( / ^ \/ + / , '' )
193
- . replace ( / \/ / g, '-' ) ;
194
- return `build/${ sanitized } .js` ;
195
- } ;
196
- }
197
- // client development/debug output
198
- const getFilePath = ( fileNamePattern : string | ( ( info : Rollup . PreRenderedChunk ) => string ) ) =>
199
- typeof fileNamePattern === 'string'
200
- ? useAssetsDir
201
- ? `${ opts . assetsDir } /${ fileNamePattern } `
202
- : fileNamePattern
203
- : useAssetsDir
204
- ? ( chunkInfo : Rollup . PreRenderedChunk ) =>
205
- `${ opts . assetsDir } /${ fileNamePattern ( chunkInfo ) } `
206
- : ( chunkInfo : Rollup . PreRenderedChunk ) => fileNamePattern ( chunkInfo ) ;
193
+ if ( ! outputOpts . assetFileNames ) {
194
+ // SEO likes readable asset names
195
+ // assetsDir allows assets to be in a deeper directory for serving, e.g. Astro
196
+ outputOpts . assetFileNames = `${ useAssetsDir ? `${ opts . assetsDir } /` : '' } assets/[hash]-[name].[ext]` ;
197
+ }
207
198
199
+ const chunkFileName = getChunkFileName ( useAssetsDir ? `${ opts . assetsDir } ` : '' , opts , optimizer ) ;
200
+ if ( opts . target === 'client' ) {
201
+ // client output
208
202
if ( ! outputOpts . entryFileNames ) {
209
- outputOpts . entryFileNames = getFilePath ( fileName ) ;
203
+ // we don't treat entries specially for the client
204
+ outputOpts . entryFileNames = chunkFileName ;
210
205
}
211
206
if ( ! outputOpts . chunkFileNames ) {
212
- outputOpts . chunkFileNames = getFilePath ( fileName ) ;
207
+ outputOpts . chunkFileNames = chunkFileName ;
213
208
}
214
- } else if ( opts . buildMode === 'production' ) {
215
- // server production output
216
- // everything in same dir so './@qwik-router...' imports work from entry and chunks
217
- if ( ! outputOpts . chunkFileNames ) {
218
- outputOpts . chunkFileNames = 'q-[hash].js' ;
219
- }
220
- }
221
- // all other cases, like lib output
222
- if ( ! outputOpts . assetFileNames ) {
223
- outputOpts . assetFileNames = 'assets/[hash]-[name].[ext]' ;
224
- }
225
209
226
- if ( opts . target === 'client' ) {
227
210
// client should always be es
228
211
outputOpts . format = 'es' ;
229
212
const prevManualChunks = outputOpts . manualChunks ;
230
213
if ( prevManualChunks && typeof prevManualChunks !== 'function' ) {
231
214
throw new Error ( 'manualChunks must be a function' ) ;
232
215
}
216
+
217
+ // We need custom chunking for the client build
233
218
outputOpts . manualChunks = prevManualChunks
234
219
? ( id , meta ) => prevManualChunks ( id , meta ) || manualChunks ( id , meta )
235
220
: manualChunks ;
221
+ } else {
222
+ // server production output, try to be similar to client
223
+ if ( ! outputOpts . chunkFileNames ) {
224
+ outputOpts . chunkFileNames = chunkFileName ;
225
+ }
236
226
}
237
227
238
228
if ( ! outputOpts . dir ) {
0 commit comments