Skip to content

Commit ac118d2

Browse files
committed
fix(core): throw when importing twice
this should make it easier to detect build misconfigurations
1 parent 831ad08 commit ac118d2

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

packages/qwik-router/src/ssg/worker-thread.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import type {
1414
} from './types';
1515

1616
export async function workerThread(sys: System) {
17+
// Special case: we allow importing qwik again in the same process, it's ok because we just needed the serializer
18+
// TODO: remove this once we have vite environment API and no longer need the serializer separately
19+
delete (globalThis as any).__qwik;
1720
const ssgOpts = sys.getOptions();
1821
const pendingPromises = new Set<Promise<any>>();
1922

@@ -42,6 +45,9 @@ export async function workerThread(sys: System) {
4245
}
4346

4447
export async function createSingleThreadWorker(sys: System) {
48+
// Special case: we allow importing qwik again in the same process, it's ok because we just needed the serializer
49+
// TODO: remove this once we have vite environment API and no longer need the serializer separately
50+
delete (globalThis as any).__qwik;
4551
const ssgOpts = sys.getOptions();
4652
const pendingPromises = new Set<Promise<any>>();
4753

packages/qwik/src/core/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
//////////////////////////////////////////////////////////////////////////////////////////
2+
// Protect against duplicate imports
3+
//////////////////////////////////////////////////////////////////////////////////////////
4+
import { version } from './version';
5+
if ((globalThis as any).__qwik) {
6+
console.error(
7+
`==============================================\n` +
8+
`Qwik version ${(globalThis as any).__qwik} already imported while importing ${version}. Verify external vs bundled imports etc. This can lead to issues due to duplicated shared structures.\n` +
9+
`==============================================\n`
10+
);
11+
}
12+
(globalThis as any).__qwik = version;
13+
114
//////////////////////////////////////////////////////////////////////////////////////////
215
// Developer Core API
316
//////////////////////////////////////////////////////////////////////////////////////////

starters/dev-server.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,8 @@ async function routerApp(
271271
appDir: string,
272272
) {
273273
const ssrPath = join(appDir, "server", `${qwikRouterVirtualEntry}.js`);
274-
274+
// it's ok in the devserver to import core multiple times
275+
(globalThis as any).__qwik = null;
275276
const mod = await import(file(ssrPath));
276277
const router: any = mod.router;
277278
router(req, res, () => {
@@ -289,6 +290,8 @@ async function ssrApp(
289290
manifest: QwikManifest,
290291
) {
291292
const ssrPath = join(appDir, "server", "entry.ssr.js");
293+
// it's ok in the devserver to import core multiple times
294+
(globalThis as any).__qwik = null;
292295
const mod = await import(file(ssrPath));
293296
const render: Render = mod.default ?? mod.render;
294297

0 commit comments

Comments
 (0)