Skip to content

Commit aa87af6

Browse files
Merge pull request #270 from QwikDev/feat/global-render-opts
feat: global render opts
2 parents d51dded + fd31276 commit aa87af6

File tree

7 files changed

+109
-15
lines changed

7 files changed

+109
-15
lines changed

.changeset/green-jobs-jog.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
"@qwikdev/astro": patch
3+
---
4+
5+
feat: support global config for renderOpts
6+
7+
You can now pass the `renderOpts` option to the `qwik` integration to set global render options for all Qwik components.
8+
9+
For example, let's say we wanted to change the base URL for all Qwik build assets on every component used in an Astro file.
10+
11+
```ts
12+
import { defineConfig } from "astro/config";
13+
import qwik from "@qwikdev/astro";
14+
15+
export default defineConfig({
16+
integrations: [qwik({ include: "**/qwik/*", renderOpts: { base: "my-cdn-url/build" } })]
17+
});
18+
```

apps/demo/src/pages/index.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { Counter as NativeCounter } from "@components/qwik/counter";
1212
</head>
1313
<body>
1414
<div style={{ height: "1000px" }}>
15-
<NativeCounter initial={2} renderOpts={{ base: "http://192.168.68.76:4321/build" }} />
15+
<NativeCounter initial={2} />
1616
</div>
1717
</body>
1818
</html>

libs/qwikdev-astro/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
},
6262
"bugs": "https://github.com/thejackshelton/@qwikdev/astro/issues",
6363
"dependencies": {
64+
"@inox-tools/aik-mod": "^0.11.0",
6465
"astro-integration-kit": "^0.18.0"
6566
},
6667
"devDependencies": {

libs/qwikdev-astro/server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { isDev } from "@builder.io/qwik/build";
33
import type { QwikManifest } from "@builder.io/qwik/optimizer";
44
import { type RenderToStreamOptions, renderToStream } from "@builder.io/qwik/server";
55
import type { SSRResult } from "astro";
6+
import { renderOpts as globalRenderOpts } from "virtual:qwikdev-astro";
67

78
const containerMap = new WeakMap<SSRResult, boolean>();
89

@@ -73,7 +74,7 @@ export async function renderToStaticMarkup(
7374
const isInitialContainer = !containerMap.has(this.result);
7475

7576
const renderToStreamOpts: RenderToStreamOptions = {
76-
...(props.renderOpts ?? {}),
77+
...(props.renderOpts ?? globalRenderOpts ?? {}),
7778
containerAttributes: {
7879
style: "display: contents",
7980
...(isDev && { "q-astro-marker": "" })

libs/qwikdev-astro/src/index.ts

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,29 @@ import type {
66
QwikVitePluginOptions,
77
SymbolMapperFn
88
} from "@builder.io/qwik/optimizer";
9+
import type { RenderOptions } from "@builder.io/qwik/server";
910
import type { AstroConfig, AstroIntegration } from "astro";
10-
import { createResolver, defineIntegration, watchDirectory } from "astro-integration-kit";
11+
import { createResolver, defineIntegration, watchDirectory, withPlugins } from "astro-integration-kit";
1112
import { z } from "astro/zod";
1213
import { type PluginOption, build, createFilter } from "vite";
1314
import type { InlineConfig } from "vite";
15+
import aikMod from '@inox-tools/aik-mod';
16+
17+
// TODO: contributing this back to aik-mod where we export the type
18+
type DefineModuleOptions = {
19+
constExports?: Record<string, unknown>;
20+
defaultExport?: unknown;
21+
};
22+
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+
};
1432

1533
declare global {
1634
var symbolMapperFn: SymbolMapperFn;
@@ -26,12 +44,14 @@ const FilterPatternSchema = z.union([
2644
z.null()
2745
]);
2846

47+
const name = "@qwikdev/astro";
48+
2949
/**
3050
* This project uses Astro Integration Kit.
3151
* @see https://astro-integration-kit.netlify.app/
3252
*/
3353
export default defineIntegration({
34-
name: "@qwikdev/astro",
54+
name,
3555
optionsSchema: z
3656
.object({
3757
/**
@@ -48,11 +68,12 @@ export default defineIntegration({
4868
* Enable debug mode with the qwikVite plugin.
4969
*/
5070
debug: z.boolean().optional(),
51-
5271
/**
53-
* Use node's readFileSync to read the manifest. Common for deployment providers that don't support dynamic json imports. When false, please ensure your deployment provider supports dynamic json imports, through environment variables or other means.
72+
* Options passed into each Qwik component's `renderToStream` call.
5473
*/
55-
isNode: z.boolean().optional().default(true)
74+
renderOpts: z.custom<RenderOptions>((data) => {
75+
return typeof data === "object" && data !== null;
76+
}).optional()
5677
})
5778
.optional(),
5879

@@ -77,7 +98,7 @@ export default defineIntegration({
7798

7899
const lifecycleHooks: AstroIntegration["hooks"] = {
79100
"astro:config:setup": async (setupProps) => {
80-
const { addRenderer, updateConfig, config } = setupProps;
101+
const { addRenderer, updateConfig, config, defineModule } = setupProps as SetupPropsWithAikMod;
81102
astroConfig = config;
82103
// integration HMR support
83104
watchDirectory(setupProps, resolver());
@@ -86,6 +107,12 @@ export default defineIntegration({
86107
serverEntrypoint: resolver("../server.ts")
87108
});
88109

110+
defineModule('virtual:qwikdev-astro', {
111+
constExports: {
112+
renderOpts: options?.renderOpts ?? {}
113+
}
114+
});
115+
89116
/** Relative paths, as the Qwik optimizer handles normalization */
90117
srcDir = getRelativePath(astroConfig.root.pathname, astroConfig.srcDir.pathname);
91118

@@ -319,12 +346,14 @@ export default defineIntegration({
319346
}
320347
};
321348

322-
return {
323-
hooks: lifecycleHooks
324-
};
349+
return withPlugins({
350+
name,
351+
hooks: lifecycleHooks,
352+
plugins: [aikMod]
353+
});
325354
}
326355
});
327356

328357
function getRelativePath(from: string, to: string) {
329358
return to.replace(from, "") || ".";
330-
}
359+
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
declare module "inox:inline-mod:mod_0" {
2-
export const isNode: boolean;
3-
export const qAstroManifestPath: string;
1+
declare module "virtual:qwikdev-astro" {
2+
import type { RenderOptions } from "@builder.io/qwik/server";
3+
4+
const renderOpts: RenderOptions;
5+
export { renderOpts };
46
}

pnpm-lock.yaml

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)