Skip to content

Commit fd9074f

Browse files
authored
fix: return null instead of throwing on unresolved modules in resolveId (#273)
* fix: return null in resolveId * chore: add changeset * chore: add debug log
1 parent 280da2c commit fd9074f

File tree

6 files changed

+46
-38
lines changed

6 files changed

+46
-38
lines changed

.changeset/witty-papayas-obey.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@qwikdev/astro": major
3+
---
4+
5+
fix: return null instead of throwing on unresolved modules in resolveId
Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { Slot, component$, useSignal } from "@builder.io/qwik";
2-
import { type RenderOptions } from "@builder.io/qwik/server";
2+
import type { RenderOptions } from "@builder.io/qwik/server";
33

4-
export const Counter = component$<{ initial: number; renderOpts?: RenderOptions }>((props) => {
5-
const counter = useSignal(props.initial);
4+
export const Counter = component$<{ initial: number; renderOpts?: RenderOptions }>(
5+
(props) => {
6+
const counter = useSignal(props.initial);
67

7-
return (
8-
<button type="button" onClick$={() => counter.value++}>
9-
<Slot /> {counter.value}
10-
</button>
11-
);
12-
});
8+
return (
9+
<button type="button" onClick$={() => counter.value++}>
10+
<Slot /> {counter.value}
11+
</button>
12+
);
13+
}
14+
);

libs/qwikdev-astro/package.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,7 @@
4040
"./utils": "./src/utils.ts",
4141
"./q-astro-manifest.json": "./q-astro-manifest.json"
4242
},
43-
"files": [
44-
"src",
45-
"src/index.ts",
46-
"server.ts",
47-
"env.d.ts"
48-
],
43+
"files": ["src", "src/index.ts", "server.ts", "env.d.ts"],
4944
"keywords": [
5045
"astro-integration",
5146
"astro-component",

libs/qwikdev-astro/server.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import { renderOpts as globalRenderOpts } from "virtual:qwikdev-astro";
12
import { type JSXNode, jsx } from "@builder.io/qwik";
23
import { isDev } from "@builder.io/qwik/build";
34
import type { QwikManifest } from "@builder.io/qwik/optimizer";
45
import { type RenderToStreamOptions, renderToStream } from "@builder.io/qwik/server";
56
import type { SSRResult } from "astro";
6-
import { renderOpts as globalRenderOpts } from "virtual:qwikdev-astro";
77

88
const containerMap = new WeakMap<SSRResult, boolean>();
99

@@ -62,7 +62,6 @@ export async function renderToStaticMarkup(
6262
props: Record<string, unknown>,
6363
slotted: any
6464
) {
65-
6665
try {
6766
if (!isQwikComponent(component)) {
6867
return;
@@ -93,7 +92,7 @@ export async function renderToStaticMarkup(
9392
write: (chunk) => {
9493
html += chunk;
9594
}
96-
},
95+
}
9796
};
9897

9998
// https://qwik.dev/docs/components/overview/#inline-components

libs/qwikdev-astro/src/index.ts

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,29 @@ import type {
77
SymbolMapperFn
88
} from "@builder.io/qwik/optimizer";
99
import type { RenderOptions } from "@builder.io/qwik/server";
10+
import aikMod from "@inox-tools/aik-mod";
1011
import type { AstroConfig, AstroIntegration } from "astro";
11-
import { createResolver, defineIntegration, watchDirectory, withPlugins } from "astro-integration-kit";
12+
import {
13+
createResolver,
14+
defineIntegration,
15+
watchDirectory,
16+
withPlugins
17+
} from "astro-integration-kit";
1218
import { z } from "astro/zod";
1319
import { type PluginOption, build, createFilter } from "vite";
1420
import type { InlineConfig } from "vite";
15-
import aikMod from '@inox-tools/aik-mod';
1621

1722
// TODO: contributing this back to aik-mod where we export the type
1823
type DefineModuleOptions = {
1924
constExports?: Record<string, unknown>;
2025
defaultExport?: unknown;
2126
};
2227

23-
type SetupPropsWithAikMod =
24-
Parameters<
25-
NonNullable<AstroIntegration["hooks"]["astro:config:setup"]>
26-
>[0] & {
27-
defineModule: (
28-
name: string,
29-
options: DefineModuleOptions
30-
) => string;
31-
};
28+
type SetupPropsWithAikMod = Parameters<
29+
NonNullable<AstroIntegration["hooks"]["astro:config:setup"]>
30+
>[0] & {
31+
defineModule: (name: string, options: DefineModuleOptions) => string;
32+
};
3233

3334
declare global {
3435
var symbolMapperFn: SymbolMapperFn;
@@ -69,11 +70,13 @@ export default defineIntegration({
6970
*/
7071
debug: z.boolean().optional(),
7172
/**
72-
* Options passed into each Qwik component's `renderToStream` call.
73+
* Options passed into each Qwik component's `renderToStream` call.
7374
*/
74-
renderOpts: z.custom<RenderOptions>((data) => {
75-
return typeof data === "object" && data !== null;
76-
}).optional()
75+
renderOpts: z
76+
.custom<RenderOptions>((data) => {
77+
return typeof data === "object" && data !== null;
78+
})
79+
.optional()
7780
})
7881
.optional(),
7982

@@ -98,7 +101,8 @@ export default defineIntegration({
98101

99102
const lifecycleHooks: AstroIntegration["hooks"] = {
100103
"astro:config:setup": async (setupProps) => {
101-
const { addRenderer, updateConfig, config, defineModule } = setupProps as SetupPropsWithAikMod;
104+
const { addRenderer, updateConfig, config, defineModule } =
105+
setupProps as SetupPropsWithAikMod;
102106
astroConfig = config;
103107
// integration HMR support
104108
watchDirectory(setupProps, resolver());
@@ -107,7 +111,7 @@ export default defineIntegration({
107111
serverEntrypoint: resolver("../server.ts")
108112
});
109113

110-
defineModule('virtual:qwikdev-astro', {
114+
defineModule("virtual:qwikdev-astro", {
111115
constExports: {
112116
renderOpts: options?.renderOpts ?? {}
113117
}
@@ -174,7 +178,10 @@ export default defineIntegration({
174178

175179
const resolved = await this.resolve(id, importer);
176180
if (!resolved) {
177-
throw new Error(`Could not resolve ${id} from ${importer}`);
181+
if (options?.debug) {
182+
console.debug(`Could not resolve ${id} from ${importer}`);
183+
}
184+
return null;
178185
}
179186

180187
if (resolved.id.includes(".qwik.")) {
@@ -356,4 +363,4 @@ export default defineIntegration({
356363

357364
function getRelativePath(from: string, to: string) {
358365
return to.replace(from, "") || ".";
359-
}
366+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
declare module "virtual:qwikdev-astro" {
22
import type { RenderOptions } from "@builder.io/qwik/server";
3-
3+
44
const renderOpts: RenderOptions;
55
export { renderOpts };
66
}

0 commit comments

Comments
 (0)