Skip to content

Commit 33a15ac

Browse files
authored
Merge pull request #7932 from QwikDev/throw-qwik-externals
fix(core vite): improve check for external qwik imports
2 parents b470ba3 + c7066b1 commit 33a15ac

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

.changeset/lemon-dingos-dance.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
'@builder.io/qwik': minor
33
---
44

5-
BREAKING: (slightly) Qwik will no longer scan all modules at build start to detect Qwik modules. Instead, a runtime check is done to prevent duplicate core imports. If you get a runtime error, you need to fix your build settings so they don't externalize qwik-related libraries.
5+
BREAKING: (slightly) Qwik will no longer scan all modules at build start to detect Qwik modules (which should be bundled into your server code). Instead, a much faster build-time check is done, and Qwik will tell you if you need to update your `ssr.noExternal` settings in your Vite config.

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -699,24 +699,36 @@ async function checkExternals() {
699699
configResolved: (config) => {
700700
rootDir = config.root;
701701
},
702+
// We check all SSR build lookups for external Qwik deps
702703
resolveId: {
703704
order: 'pre',
704705
async handler(source, importer, options) {
705-
if (source.startsWith('node:') || seen.has(source)) {
706+
if (!options.ssr || /^([./]|node:|[^a-z])/.test(source) || seen.has(source)) {
707+
return;
708+
}
709+
const packageName = (
710+
source.startsWith('@') ? source.split('/').slice(0, 2).join('/') : source.split('/')[0]
711+
).split('?')[0];
712+
if (seen.has(packageName)) {
706713
return;
707714
}
708715
// technically we should check for each importer, but this is ok
709716
seen.add(source);
710-
const result = await this.resolve(source, importer, { ...options, skipSelf: true });
717+
seen.add(packageName);
718+
const result = await this.resolve(packageName, importer, { ...options, skipSelf: true });
711719
if (result?.external) {
712-
// For Qwik files, check if they should be externalized
713-
if (await isQwikDep(source, importer ? path.dirname(importer) : rootDir)) {
720+
// Qwik deps should not be external
721+
if (await isQwikDep(packageName, importer ? path.dirname(importer) : rootDir)) {
714722
// TODO link to docs
715723
throw new Error(
716-
`\n==============\n${source} is being treated as an external dependency, but it should be included in the server bundle, because it uses Qwik.\nPlease add the package to "ssr.noExternal" in the Vite config. \n==============`
724+
`\n==============\n${packageName} is being treated as an external dependency, but it should be included in the server bundle, because it uses Qwik.\nPlease add the package to "ssr.noExternal" in the Vite config. \n==============`
717725
);
718726
}
719727
}
728+
if (packageName === source) {
729+
// We already resolved it, so return that result
730+
return result;
731+
}
720732
},
721733
},
722734
} as const satisfies VitePlugin<never>;

0 commit comments

Comments
 (0)