Skip to content

Commit

Permalink
Merge pull request #218 from QwikDev/update-server-bundle
Browse files Browse the repository at this point in the history
updated the server bundle
  • Loading branch information
thejackshelton authored Jan 4, 2025
2 parents bfaa577 + 911821a commit 3fa307c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 65 deletions.
5 changes: 5 additions & 0 deletions .changeset/thirty-jars-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@qwikdev/astro": patch
---

fix: q-manifest gets added into the bundled code at build time
1 change: 0 additions & 1 deletion libs/qwikdev-astro/q-astro-manifest.json

This file was deleted.

40 changes: 1 addition & 39 deletions libs/qwikdev-astro/server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isNode, qAstroManifestPath } from "inox:inline-mod:mod_0";
import { type JSXNode, jsx } from "@builder.io/qwik";
import { isDev } from "@builder.io/qwik/build";
import type { QwikManifest } from "@builder.io/qwik/optimizer";
Expand Down Expand Up @@ -63,41 +62,6 @@ export async function renderToStaticMarkup(

let html = "";

let integrationManifest = null;

/**
* fallback to dynamic import if node is false. Node is preferred because most deployment providers still use older versions of node by default, so dynamic json imports will fail.
*
* Until this improves, we'll use node's readFileSync, with dynamic json imports for those not using node.
*/
if (isNode) {
try {
const { readFileSync } = await import("node:fs");
const manifestContent = readFileSync(qAstroManifestPath, "utf-8");
integrationManifest = JSON.parse(manifestContent);
} catch (error) {
throw new Error(
`@qwikdev/astro: Failed to read the q-astro-manifest.json file. This file is required for the @qwikdev/astro integration to work.
It seems like you're using node. If this is not the case, please set the isNode option to false in the integration options in astro.config.mjs.
Also make sure this is the case with both your local and deployed environment.`
);
}
} else {
try {
integrationManifest = await import(/* @vite-ignore */ qAstroManifestPath, {
with: { type: "json" }
});
} catch (error) {
throw new Error(
`@qwikdev/astro: Failed to read the q-astro-manifest.json file. This file is required for the @qwikdev/astro integration to work.
Because isNode is set to false, the integration will use dynamic json imports to read the q-astro-manifest.json file. Check to make sure this environment supports dynamic json imports.`
);
}
}

const renderToStreamOpts: RenderToStreamOptions = {
containerAttributes: { style: "display: contents" },
containerTagName: "div",
Expand All @@ -107,8 +71,7 @@ export async function renderToStaticMarkup(
symbolMapper: globalThis.symbolMapperFn
}
: {
manifest:
globalThis.qManifest || integrationManifest?.default || integrationManifest
manifest: globalThis.qManifest
}),
serverData: props,
qwikPrefetchServiceWorker: {
Expand Down Expand Up @@ -226,6 +189,5 @@ export async function renderToStaticMarkup(
export default {
renderToStaticMarkup,
supportsAstroStaticSlot: true,
testing123: true,
check
};
51 changes: 26 additions & 25 deletions libs/qwikdev-astro/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { writeFileSync } from "node:fs";
import fs from "node:fs";
import { join } from "node:path";
import { qwikVite, symbolMapper } from "@builder.io/qwik/optimizer";
import type {
QwikManifest,
QwikVitePluginOptions,
SymbolMapperFn
} from "@builder.io/qwik/optimizer";
import { inlineModule } from "@inox-tools/inline-mod/vite";
import inlineMod from "@inox-tools/inline-mod/vite";
import type { AstroConfig, AstroIntegration } from "astro";
import { createResolver, defineIntegration, watchDirectory } from "astro-integration-kit";
import { z } from "astro/zod";
Expand Down Expand Up @@ -75,26 +74,12 @@ export default defineIntegration({
let astroConfig: AstroConfig | null = null;
const { resolve: resolver } = createResolver(import.meta.url);
const filter = createFilter(options?.include, options?.exclude);
const qAstroManifestPath = resolver("../q-astro-manifest.json");

const lifecycleHooks: AstroIntegration["hooks"] = {
"astro:config:setup": async (setupProps) => {
const { addRenderer, updateConfig, config, command } = setupProps;
const { addRenderer, updateConfig, config } = setupProps;
astroConfig = config;

// passes config values to other runtimes with a virtual module
inlineModule({
constExports: {
isNode: options?.isNode ?? true,
qAstroManifestPath
}
});

/* q-astro-manifest.json doesn't error in dev */
if (command === "dev") {
writeFileSync(qAstroManifestPath, "{}", "utf-8");
}

// integration HMR support
watchDirectory(setupProps, resolver());

Expand Down Expand Up @@ -235,12 +220,7 @@ export default defineIntegration({
}
}
},
plugins: [
inlineMod(),
astroQwikPlugin,
qwikVite(qwikSetupConfig),
overrideEsbuildPlugin
]
plugins: [astroQwikPlugin, qwikVite(qwikSetupConfig), overrideEsbuildPlugin]
}
});
},
Expand Down Expand Up @@ -269,7 +249,28 @@ export default defineIntegration({
outDir: finalDir,
manifestOutput: (manifest) => {
globalThis.qManifest = manifest;
writeFileSync(qAstroManifestPath, JSON.stringify(manifest), "utf-8");

if (astroConfig?.adapter) {
const serverChunksDir = join(serverDir, "chunks");
const files = fs.readdirSync(serverChunksDir);
const serverFile = files.find(
(f) => f.startsWith("server_") && f.endsWith(".mjs")
);

if (serverFile) {
const serverPath = join(serverChunksDir, serverFile);
const content = fs.readFileSync(serverPath, "utf-8");

// Replace the manifest handling in the bundled code
const manifestJson = JSON.stringify(manifest);
const newContent = content.replace(
"globalThis.qManifest",
`globalThis.qManifest || ${manifestJson}`
);

fs.writeFileSync(serverPath, newContent);
}
}
}
},
debug: options?.debug ?? false
Expand Down

0 comments on commit 3fa307c

Please sign in to comment.