Skip to content

Commit f8853c2

Browse files
committed
refactor(rollup): move build mode + debug:true chunk names logic out of manualChunks
1 parent d297d3e commit f8853c2

File tree

3 files changed

+44
-13
lines changed

3 files changed

+44
-13
lines changed

packages/qwik/src/optimizer/src/plugins/plugin.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -891,14 +891,7 @@ export const manifest = ${JSON.stringify(manifest)};\n`;
891891
// Maybe a better solution would be to mark those files as entires earlier in the chain so that we can remove this check and the one above altogether.
892892
// We check .(tsx|jsx) after node_modules in case some node_modules end with .jsx or .tsx.
893893
if (/\.(tsx|jsx)$/.test(id)) {
894-
const optimizer = getOptimizer();
895-
const path = optimizer.sys.path;
896-
const relativePath = path.relative(optimizer.sys.cwd(), id);
897-
const sanitizedPath = relativePath
898-
.replace(/^(\.\.\/)+/, '')
899-
.replace(/^\/+/, '')
900-
.replace(/\//g, '-');
901-
return sanitizedPath; // We return sanitizedPath for qwikVite plugin with debug:true
894+
return id;
902895
}
903896

904897
return null;

packages/qwik/src/optimizer/src/plugins/rollup.ts

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export function qwikRollup(qwikRollupOpts: QwikRollupPluginOptions = {}): any {
7878

7979
outputOptions(rollupOutputOpts) {
8080
return normalizeRollupOutputOptionsObject(
81+
qwikPlugin.getOptimizer(),
8182
qwikPlugin.getOptions(),
8283
rollupOutputOpts,
8384
false,
@@ -160,6 +161,7 @@ export function qwikRollup(qwikRollupOpts: QwikRollupPluginOptions = {}): any {
160161
}
161162

162163
export function normalizeRollupOutputOptions(
164+
optimizer: Optimizer,
163165
opts: NormalizedQwikPluginOptions,
164166
rollupOutputOpts: Rollup.OutputOptions | Rollup.OutputOptions[] | undefined,
165167
useAssetsDir: boolean,
@@ -173,18 +175,31 @@ export function normalizeRollupOutputOptions(
173175
}
174176

175177
return rollupOutputOpts.map((outputOptsObj) => ({
176-
...normalizeRollupOutputOptionsObject(opts, outputOptsObj, useAssetsDir, manualChunks),
178+
...normalizeRollupOutputOptionsObject(
179+
optimizer,
180+
opts,
181+
outputOptsObj,
182+
useAssetsDir,
183+
manualChunks
184+
),
177185
dir: outDir || outputOptsObj.dir,
178186
}));
179187
}
180188

181189
return {
182-
...normalizeRollupOutputOptionsObject(opts, rollupOutputOpts, useAssetsDir, manualChunks),
190+
...normalizeRollupOutputOptionsObject(
191+
optimizer,
192+
opts,
193+
rollupOutputOpts,
194+
useAssetsDir,
195+
manualChunks
196+
),
183197
dir: outDir || rollupOutputOpts?.dir,
184198
};
185199
}
186200

187201
export function normalizeRollupOutputOptionsObject(
202+
optimizer: Optimizer,
188203
opts: NormalizedQwikPluginOptions,
189204
rollupOutputOptsObj: Rollup.OutputOptions | undefined,
190205
useAssetsDir: boolean,
@@ -201,9 +216,31 @@ export function normalizeRollupOutputOptionsObject(
201216
? `${opts.assetsDir}/${assetFileNames}`
202217
: assetFileNames;
203218
}
204-
// Friendly name in dev or preview with debug mode
205-
const fileName =
206-
opts.buildMode == 'production' && !opts.debug ? 'build/q-[hash].js' : 'build/[name].js';
219+
220+
let fileName: string | ((chunkInfo: Rollup.PreRenderedChunk) => string) | undefined;
221+
if (opts.buildMode === 'production' && !opts.debug) {
222+
fileName = 'build/q-[hash].js';
223+
} else {
224+
// Friendlier names in dev or preview with debug mode
225+
fileName = (chunkInfo) => {
226+
if (chunkInfo.moduleIds.some((id) => id.endsWith('core.prod.mjs'))) {
227+
return 'build/core.js';
228+
}
229+
if (chunkInfo.moduleIds.some((id) => id.endsWith('qwik-city/lib/index.qwik.mjs'))) {
230+
return 'build/qwik-city.js';
231+
}
232+
233+
// some chunk names are absolute paths and rollup doesn't accept that so we must sanitize those instead of simply returning `build/[name].js`.
234+
const path = optimizer.sys.path;
235+
const relativePath = path.relative(optimizer.sys.cwd(), chunkInfo.name);
236+
const sanitizedPath = relativePath
237+
.replace(/^(\.\.\/)+/, '')
238+
.replace(/^\/+/, '')
239+
.replace(/\//g, '-');
240+
241+
return `build/${sanitizedPath}.js`;
242+
};
243+
}
207244
// client production output
208245
if (!outputOpts.entryFileNames) {
209246
outputOpts.entryFileNames = useAssetsDir ? `${opts.assetsDir}/${fileName}` : fileName;

packages/qwik/src/optimizer/src/plugins/vite.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any {
360360
updatedViteConfig.build!.rollupOptions = {
361361
input: opts.input,
362362
output: normalizeRollupOutputOptions(
363+
qwikPlugin.getOptimizer(),
363364
opts,
364365
viteConfig.build?.rollupOptions?.output,
365366
useAssetsDir,

0 commit comments

Comments
 (0)