Skip to content

Commit b38f2b9

Browse files
authored
fix: vite plugin client to ssr manifest (#490)
1 parent c29d7c4 commit b38f2b9

File tree

8 files changed

+50
-42
lines changed

8 files changed

+50
-42
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": true,
33
"name": "qwik-monorepo",
4-
"version": "0.0.20-3",
4+
"version": "0.0.20-4",
55
"scripts": {
66
"build": "yarn node scripts --tsc --build --platform-binding-wasm-copy",
77
"build.full": "yarn node scripts --tsc --build --api --eslint --platform-binding --wasm",

packages/create-qwik/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "create-qwik",
3-
"version": "0.0.20-3",
3+
"version": "0.0.20-4",
44
"description": "Interactive CLI and API for generating Qwik projects.",
55
"bin": "create-qwik",
66
"main": "index.js",

packages/eslint-plugin-qwik/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eslint-plugin-qwik",
3-
"version": "0.0.20-3",
3+
"version": "0.0.20-4",
44
"description": "An Open-Source sub-framework designed with a focus on server-side-rendering, lazy-loading, and styling/animation.",
55
"main": "index.js",
66
"author": "",

packages/qwik/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@builder.io/qwik",
3-
"version": "0.0.20-3",
3+
"version": "0.0.20-4",
44
"description": "An Open-Source sub-framework designed with a focus on server-side-rendering, lazy-loading, and styling/animation.",
55
"main": "./dist/core.cjs",
66
"module": "./dist/core.mjs",

packages/qwik/src/optimizer/src/platform.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export async function getSystem() {
2727
sys.path = createPath(sys);
2828

2929
if (globalThis.IS_ESM) {
30-
sys.dynamicImport = (path: string) => import(path);
30+
sys.dynamicImport = (path) => import(path);
3131
}
3232

3333
if (globalThis.IS_CJS) {
@@ -39,7 +39,7 @@ export async function getSystem() {
3939
if (typeof TextEncoder === 'undefined') {
4040
// TextEncoder/TextDecoder needs to be on the global scope for the WASM file
4141
// https://nodejs.org/api/util.html#class-utiltextdecoder
42-
const nodeUtil: any = sys.dynamicImport('util');
42+
const nodeUtil: typeof import('util') = await sys.dynamicImport('util');
4343
global.TextEncoder = nodeUtil.TextEncoder;
4444
global.TextDecoder = nodeUtil.TextDecoder;
4545
}

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

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -525,27 +525,9 @@ export const isBrowser = ${JSON.stringify(!isServer)};
525525

526526
async function getQwikServerManifestModule(loadOpts: { ssr?: boolean }) {
527527
const isServer = opts.target === 'ssr' || !!loadOpts.ssr;
528-
529-
let manifest = opts.manifestInput;
530-
if (isServer) {
531-
const sys = getSys();
532-
if (!manifest && sys.env === 'node') {
533-
// manifest not provided, and in a nodejs environment
534-
try {
535-
// check if we can find q-manifest.json from the client build
536-
const fs: typeof import('fs') = await sys.dynamicImport('fs');
537-
const qSymbolsPath = sys.path.join(opts.outDir, Q_MANIFEST_FILENAME);
538-
const qSymbolsContent = fs.readFileSync(qSymbolsPath, 'utf-8');
539-
manifest = getValidManifest(JSON.parse(qSymbolsContent))!;
540-
} catch (e) {
541-
/** */
542-
}
543-
}
544-
}
545-
528+
const manifest = isServer ? opts.manifestInput : null;
546529
return `// @qwik-client-manifest
547-
export const manifest = ${JSON.stringify(manifest)};
548-
`;
530+
export const manifest = ${JSON.stringify(manifest)};\n`;
549531
}
550532

551533
return {

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

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ import { versions } from '../versions';
2222
export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any {
2323
let isClientDevOnly = false;
2424
let clientDevInput: undefined | string = undefined;
25-
let serverInput: undefined | string = undefined;
26-
let ssrInput: undefined | string = undefined;
25+
let tmpClientManifestPath: undefined | string = undefined;
2726

2827
const qwikPlugin = createPlugin(qwikViteOpts.optimizerOptions);
2928

@@ -39,6 +38,9 @@ export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any {
3938
async config(viteConfig, viteEnv) {
4039
await qwikPlugin.init();
4140

41+
const sys = qwikPlugin.getSys();
42+
const path = qwikPlugin.getPath();
43+
4244
qwikPlugin.log(`vite config(), command: ${viteEnv.command}, env.mode: ${viteEnv.mode}`);
4345

4446
isClientDevOnly = viteEnv.command === 'serve' && viteEnv.mode !== 'ssr';
@@ -91,19 +93,29 @@ export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any {
9193
pluginOpts.manifestOutput = qwikViteOpts.client?.manifestOutput;
9294
}
9395

94-
const opts = qwikPlugin.normalizeOptions(pluginOpts);
95-
const path = qwikPlugin.getPath();
96-
97-
if (opts.target === 'ssr') {
98-
if (typeof viteConfig.build?.ssr === 'string') {
99-
// entry.express.tsx user config
100-
serverInput = opts.input[0];
101-
} else {
102-
// entry.ssr.tsx render() input
103-
ssrInput = opts.input[0];
96+
if (sys.env === 'node') {
97+
// In a NodeJs environment, create a path to a q-manifest.json file within the
98+
// OS tmp directory. This path should always be the same for both client and ssr.
99+
// Client build will write to this path, and SSR will read from it. For this reason,
100+
// the Client build should always start and finish before the SSR build.
101+
const nodeOs: typeof import('os') = await sys.dynamicImport('os');
102+
tmpClientManifestPath = path.join(nodeOs.tmpdir(), `vite-plugin-qwik-q-manifest.json`);
103+
104+
if (target === 'ssr' && !pluginOpts.manifestInput) {
105+
// This is a SSR build so we should load the client build's manifest
106+
// so it can be used as the manifestInput of the SSR build
107+
const fs: typeof import('fs') = await sys.dynamicImport('fs');
108+
try {
109+
const clientManifestStr = await fs.promises.readFile(tmpClientManifestPath, 'utf-8');
110+
pluginOpts.manifestInput = JSON.parse(clientManifestStr);
111+
} catch (e) {
112+
/**/
113+
}
104114
}
105115
}
106116

117+
const opts = qwikPlugin.normalizeOptions(pluginOpts);
118+
107119
if (typeof qwikViteOpts.client?.devInput === 'string') {
108120
clientDevInput = path.resolve(opts.rootDir, qwikViteOpts.client.devInput);
109121
} else {
@@ -214,7 +226,6 @@ export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any {
214226

215227
async generateBundle(_, rollupBundle) {
216228
const opts = qwikPlugin.getOptions();
217-
const path = qwikPlugin.getPath();
218229

219230
const outputAnalyzer = qwikPlugin.createOutputAnalyzer();
220231

@@ -241,15 +252,23 @@ export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any {
241252

242253
if (opts.target === 'client') {
243254
// client build
255+
const clientManifestStr = JSON.stringify(manifest, null, 2);
244256
this.emitFile({
245257
type: 'asset',
246258
fileName: Q_MANIFEST_FILENAME,
247-
source: JSON.stringify(manifest, null, 2),
259+
source: clientManifestStr,
248260
});
249261

250262
if (typeof opts.manifestOutput === 'function') {
251263
await opts.manifestOutput(manifest);
252264
}
265+
266+
const sys = qwikPlugin.getSys();
267+
if (tmpClientManifestPath && sys.env === 'node') {
268+
// Client build should write the manifest to a tmp dir
269+
const fs: typeof import('fs') = await sys.dynamicImport('fs');
270+
await fs.promises.writeFile(tmpClientManifestPath, clientManifestStr);
271+
}
253272
}
254273
},
255274

@@ -329,7 +348,7 @@ export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any {
329348

330349
qwikPlugin.log(`handleSSR()`, 'symbols', manifest);
331350

332-
const renderInputModule = await server.moduleGraph.getModuleByUrl(ssrInput!);
351+
const renderInputModule = await server.moduleGraph.getModuleByUrl(opts.input[0]!);
333352
if (renderInputModule) {
334353
renderInputModule.importedModules.forEach((moduleNode) => {
335354
if (moduleNode.url.endsWith('.css')) {

yarn.lock

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,14 @@ __metadata:
722722
languageName: node
723723
linkType: hard
724724

725-
"@builder.io/[email protected], @builder.io/qwik@workspace:packages/qwik":
725+
"@builder.io/qwik@npm:0.0.20-3":
726+
version: 0.0.20-3
727+
resolution: "@builder.io/qwik@npm:0.0.20-3"
728+
checksum: 58f12ee450ae3970cc51b4dda2fd7f09211973d4f25bd3a937b9aac87f7295c5d8697d11fc1e2a5b8829aff7c1e34d158381106b388e7a9d908c25dddab39813
729+
languageName: node
730+
linkType: hard
731+
732+
"@builder.io/qwik@workspace:packages/qwik":
726733
version: 0.0.0-use.local
727734
resolution: "@builder.io/qwik@workspace:packages/qwik"
728735
languageName: unknown

0 commit comments

Comments
 (0)