Skip to content

Commit 179a72c

Browse files
committed
fix(plugin): correctly detect qwik external deps
1 parent 783adea commit 179a72c

File tree

2 files changed

+51
-35
lines changed

2 files changed

+51
-35
lines changed

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -714,14 +714,6 @@ export function createQwikPlugin(optimizerOptions: OptimizerOptions = {}) {
714714
return null;
715715
};
716716

717-
/** Optimized deps can contain Qwik libraries, process them too */
718-
const isOptimizedQwikDep = (id: string, code: string) => {
719-
if (devServer && id.includes('.vite/deps/') && code.slice(0, 10000).includes('qwik')) {
720-
return true;
721-
}
722-
return false;
723-
};
724-
725717
let transformCount = 0;
726718
const transform = async function (
727719
ctx: Rollup.PluginContext,
@@ -748,7 +740,7 @@ export function createQwikPlugin(optimizerOptions: OptimizerOptions = {}) {
748740
const dir = parsedPathId.dir;
749741
const base = parsedPathId.base;
750742
const ext = parsedPathId.ext.toLowerCase();
751-
if (ext in TRANSFORM_EXTS || TRANSFORM_REGEX.test(pathId) || isOptimizedQwikDep(id, code)) {
743+
if (ext in TRANSFORM_EXTS || TRANSFORM_REGEX.test(pathId)) {
752744
/** Strip client|server code from qwik server|client, but not in lib/test */
753745
const strip = opts.target === 'client' || opts.target === 'ssr';
754746
debug(

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

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -408,25 +408,25 @@ export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any {
408408
},
409409

410410
transform(code, id, transformOpts) {
411-
const shouldTransformFile = fileFilter(id, 'transform');
412-
const isStringImportId = id.includes('?raw');
413-
if (isVirtualId(id) || !shouldTransformFile || isStringImportId) {
414-
return null;
415-
}
416411
if (
417412
id.includes('.vite/deps/') &&
418413
code.slice(0, 5000).includes('qwik') &&
419414
/import[^\n]*qwik[^\n]*\n/.test(code)
420415
) {
421416
const relPath = rootDir && id.startsWith(rootDir) ? id.slice(rootDir.length) : id;
422417
throw new Error(
423-
`\n==============\n` +
424-
`This dependency was pre-bundled by Vite, but it seems to use Qwik, which needs processing by the optimizer.\n` +
425-
`Please add the original modulename to the "optimizeDeps.exclude" array in your Vite config:\n` +
426-
`${relPath}\n` +
427-
`==============\n`
418+
`\n\n==============\n\n` +
419+
`⚠️ IMPORTANT: This dependency was pre-bundled by Vite, but it seems to use Qwik, which needs processing by the optimizer.\n\n` +
420+
`👉 Please add the original modulename to the "optimizeDeps.exclude" array in your Vite config\n` +
421+
`👉 ${relPath}\n\n` +
422+
`==============\n\n`
428423
);
429424
}
425+
const shouldTransformFile = fileFilter(id, 'transform');
426+
const isStringImportId = id.includes('?raw');
427+
if (isVirtualId(id) || !shouldTransformFile || isStringImportId) {
428+
return null;
429+
}
430430

431431
return qwikPlugin.transform(this, code, id, transformOpts);
432432
},
@@ -663,11 +663,30 @@ async function checkExternals() {
663663
configResolved: (config) => {
664664
rootDir = config.root;
665665
},
666+
// Attempt to mark the Qwik dependencies as non-optimizeable
667+
config: {
668+
order: 'post',
669+
async handler(config) {
670+
const toExclude = [];
671+
const externals = [config.ssr?.noExternal, config.environments?.ssr?.resolve?.noExternal]
672+
.flat()
673+
.filter((t) => typeof t === 'string');
674+
const optimizeDepsExclude = config.optimizeDeps?.exclude ?? [];
675+
for (const dep of externals) {
676+
if (!optimizeDepsExclude.includes(dep)) {
677+
if (await isQwikDep(dep, config.root || process.cwd())) {
678+
toExclude.push(dep);
679+
}
680+
}
681+
}
682+
return { optimizeDeps: { exclude: toExclude } };
683+
},
684+
},
666685
// We check all SSR build lookups for external Qwik deps
667686
resolveId: {
668687
order: 'pre',
669688
async handler(source, importer, options) {
670-
if (!options.ssr || /^([./]|node:|[^a-z])/.test(source) || seen.has(source)) {
689+
if (!options.ssr || /^([./]|node:|[^a-z@])/i.test(source) || seen.has(source)) {
671690
return;
672691
}
673692
const packageName = (
@@ -679,22 +698,27 @@ async function checkExternals() {
679698
// technically we should check for each importer, but this is ok
680699
seen.add(source);
681700
seen.add(packageName);
682-
const result = await this.resolve(packageName, importer, { ...options, skipSelf: true });
683-
if (result?.external) {
684-
// Qwik deps should not be external
685-
if (await isQwikDep(packageName, importer ? path.dirname(importer) : rootDir)) {
686-
// TODO link to docs
687-
throw new Error(
688-
`\n==============\n` +
689-
`${packageName} is being treated as an external dependency, but it should be included in the server bundle, because it uses Qwik and it needs to be processed by the optimizer.\n` +
690-
`Please add the package to "ssr.noExternal[]" as well as "optimizeDeps.exclude[]" in the Vite config. \n` +
691-
`==============\n`
692-
);
701+
try {
702+
const result = await this.resolve(packageName, importer, { ...options, skipSelf: true });
703+
if (result?.external) {
704+
// Qwik deps should not be external
705+
if (await isQwikDep(packageName, importer ? path.dirname(importer) : rootDir)) {
706+
// TODO link to docs
707+
throw new Error(
708+
`\n==============\n` +
709+
`${packageName} is being treated as an external dependency, but it should be included in the server bundle, because it uses Qwik and it needs to be processed by the optimizer.\n` +
710+
`Please add the package to "ssr.noExternal[]" as well as "optimizeDeps.exclude[]" in the Vite config. \n` +
711+
`==============\n`
712+
);
713+
}
693714
}
694-
}
695-
if (packageName === source) {
696-
// We already resolved it, so return that result
697-
return result;
715+
if (packageName === source) {
716+
// We already resolved it, so return that result
717+
return result;
718+
}
719+
} catch {
720+
/* ignore, let vite figure it out */
721+
return;
698722
}
699723
},
700724
},

0 commit comments

Comments
 (0)