Skip to content

Commit f811acf

Browse files
committed
fix(core): chunking
this broke the dev-serve e2e tests
1 parent c57aeb0 commit f811acf

File tree

1 file changed

+17
-16
lines changed
  • packages/qwik/src/optimizer/src/plugins

1 file changed

+17
-16
lines changed

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

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ export function createPlugin(optimizerOptions: OptimizerOptions = {}) {
8989
const serverTransformedOutputs = new Map<string, [TransformModule, string]>();
9090
const parentIds = new Map<string, string>();
9191

92+
const npmChunks = new Map<string, number>();
93+
9294
let internalOptimizer: Optimizer | null = null;
9395
let linter: QwikLinter | undefined = undefined;
9496
let diagnosticsCallback: (
@@ -403,6 +405,7 @@ export function createPlugin(optimizerOptions: OptimizerOptions = {}) {
403405
debug(`transformedOutputs.clear()`);
404406
clientTransformedOutputs.clear();
405407
serverTransformedOutputs.clear();
408+
npmChunks.clear();
406409
};
407410

408411
const getIsServer = (viteOpts?: { ssr?: boolean }) => {
@@ -923,25 +926,23 @@ export const manifest = ${JSON.stringify(manifest)};\n`;
923926
return segment.entry;
924927
}
925928

926-
if (id.includes('node_modules')) {
927-
return null;
929+
const moduleIndex = id.indexOf('node_modules');
930+
if (moduleIndex === -1) {
931+
return;
928932
}
929933

930-
// Patch to prevent over-prefetching, we must clearly separate .tsx/.jsx chunks so that rollup doesn't mix random imports into non-entry files such as hooks.
931-
// 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.
932-
// We check .(tsx|jsx) after node_modules in case some node_modules end with .jsx or .tsx.
933-
if (/\.(tsx|jsx)$/.test(id)) {
934-
const optimizer = getOptimizer();
935-
const path = optimizer.sys.path;
936-
const relativePath = path.relative(optimizer.sys.cwd(), id);
937-
const sanitizedPath = relativePath
938-
.replace(/^(\.\.\/)+/, '')
939-
.replace(/^\/+/, '')
940-
.replace(/\//g, '-');
941-
return sanitizedPath; // We return sanitizedPath for qwikVite plugin with debug:true
934+
// Prevent over-prefetching, if a module is too big we move it to a separate chunk.
935+
const modulePath = id.slice(moduleIndex + 'node_modules'.length);
936+
const moduleName = id.startsWith('@')
937+
? modulePath.split('/').slice(0, 2).join('_')
938+
: modulePath.slice(0, modulePath.indexOf('/'));
939+
940+
let size = module.code?.length || 0;
941+
size += npmChunks.get(moduleName) || 0;
942+
npmChunks.set(moduleName, size);
943+
if (size > 10_000) {
944+
return moduleName;
942945
}
943-
944-
return null;
945946
}
946947

947948
return {

0 commit comments

Comments
 (0)