Skip to content

Commit 3ac2164

Browse files
authored
fix: vite plugin strategy and qwik-city imports
1 parent 83cb227 commit 3ac2164

File tree

15 files changed

+169
-134
lines changed

15 files changed

+169
-134
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": true,
33
"name": "qwik-monorepo",
4-
"version": "0.0.19-1",
4+
"version": "0.0.19-2",
55
"scripts": {
66
"build": "yarn node scripts --tsc --build --api --eslint --platform-binding --wasm",
77
"build.platform": "yarn node scripts --platform-binding",
@@ -27,7 +27,7 @@
2727
"test.e2e.chromium.debug": "PWDEBUG=1 playwright test starters --browser=chromium --config starters/playwright.config.ts",
2828
"test.e2e.firefox": "playwright test starters --browser=firefox --config starters/playwright.config.ts",
2929
"test.e2e.webkit": "playwright test starters --browser=webkit --config starters/playwright.config.ts",
30-
"serve": "yarn node --inspect starters/dev-server.cjs 3300",
30+
"serve": "yarn node -r esbuild-register --inspect starters/dev-server.ts 3300",
3131
"docs.fetch.hackMD": "yarn node --trace-warnings -r esbuild-register scripts/docs_sync/fetch_hackmd.ts",
3232
"docs.sync": "yarn node scripts/docs_sync",
3333
"prepare": "husky install",

packages/docs/functions/[[path]].ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ export const onRequestGet: PagesFunction = async ({ request, next, waitUntil })
6464

6565
// Return Qwik SSR response
6666
return response;
67-
} catch (e) {
67+
} catch (e: any) {
6868
// 500 Error
69-
return new Response(String(e), { status: 500 });
69+
return new Response(String(e.stack || e), { status: 500 });
7070
}
7171
};

packages/docs/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@
55
"homepage": "https://qwik.builder.io/",
66
"main": "src/main.tsx",
77
"scripts": {
8-
"build": "yarn build.qwikcity && yarn build.client && yarn build.ssr",
8+
"build": "yarn build.client && yarn build.ssr",
99
"build.client": "vite build",
1010
"build.ssr": "vite build --mode ssr",
1111
"build.qwikcity": "cd ../qwik-city && yarn build",
1212
"dev": "vite --force",
1313
"dev.ssr": "vite --mode ssr",
14-
"serve": "yarn build && wrangler pages dev ./dist",
14+
"serve": "wrangler pages dev ./dist",
1515
"start": "yarn dev",
1616
"fmt": "prettier --write .",
1717
"fmt.check": "prettier --check ."
1818
},
1919
"devDependencies": {
2020
"@builder.io/partytown": "^0.5.2",
21-
"@builder.io/qwik": "0.0.19-0",
22-
"@builder.io/qwik-city": "workspace:*",
21+
"@builder.io/qwik": "0.0.19-1",
22+
"@builder.io/qwik-city": "0.0.2",
2323
"@cloudflare/kv-asset-handler": "0.2.0",
2424
"@cloudflare/workers-types": "^3.7.1",
2525
"autoprefixer": "10.4.5",

packages/docs/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default defineConfig(() => {
1818
rollupOptions: {
1919
input: [
2020
resolve('src', 'components', 'app', 'app.tsx'),
21-
resolve('src', 'components', 'repl', 'worker', 'repl-service-worker.tsx'),
21+
resolve('src', 'components', 'repl', 'worker', 'repl-service-worker.ts'),
2222
],
2323
},
2424
},

packages/qwik-city/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-city",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"description": "Static Site Generator for Qwik",
55
"main": "./dist/index.cjs",
66
"module": "./dist/index.mjs",

packages/qwik-city/src/runtime/page.ts

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const usePage = async (hostElm: any) => {
1313
};
1414

1515
const loadPage = async (href: string): Promise<PageHandler | null> => {
16-
let mod: any = null;
16+
let pageModule: any = null;
1717

1818
const url = normalizeUrl(href);
1919
const modulePath = url.pathname.endsWith('/') ? url.pathname + 'index' : url.pathname;
@@ -25,43 +25,38 @@ const loadPage = async (href: string): Promise<PageHandler | null> => {
2525
return null;
2626
}
2727

28-
mod = await pageImporter();
28+
pageModule = await pageImporter();
2929
} else {
3030
// page modules are dynamically imported
3131
try {
3232
// ./pages/guide/getting-started.js
33-
let pagePath = './pages' + modulePath + '.js';
34-
if (IS_CLIENT) {
35-
pagePath += '?v=' + BUILD_ID;
36-
}
37-
38-
mod = await import(/* @vite-ignore */ pagePath);
33+
const pagePath = `./pages${modulePath}.js?v=${BUILD_ID}`;
34+
pageModule = await import(/* @vite-ignore */ pagePath);
3935
} catch (e) {
4036
console.error(e);
4137
return null;
4238
}
4339
}
44-
if (!mod || !mod.default) {
40+
if (!pageModule || !pageModule.default) {
4541
return null;
4642
}
4743

48-
const layoutImporter = LAYOUTS[mod.attributes.layout] || LAYOUTS.default;
44+
const layoutImporter = LAYOUTS[pageModule.attributes.layout] || LAYOUTS.default;
4945
if (!layoutImporter) {
5046
return null;
5147
}
5248

5349
const layout = await layoutImporter();
50+
const layoutModule = layout.default || layout;
5451

5552
return {
56-
attributes: mod.attributes,
57-
breadcrumbs: mod.breadcrumbs,
58-
content: mod.default,
59-
headings: mod.headings,
60-
index: mod.index,
61-
layout: layout.default,
62-
source: mod.source,
53+
attributes: pageModule.attributes,
54+
breadcrumbs: pageModule.breadcrumbs,
55+
content: pageModule.default,
56+
headings: pageModule.headings,
57+
index: pageModule.index,
58+
layout: layoutModule,
59+
source: pageModule.source,
6360
url,
6461
};
6562
};
66-
67-
const IS_CLIENT = typeof document !== 'undefined';
Lines changed: 67 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,86 @@
11
import type { PluginContext } from './types';
22

3-
export function createBuildCode(ctx: PluginContext, inlineModules: boolean) {
4-
const c = [];
3+
export function createDynamicImportedCode(ctx: PluginContext) {
4+
const c: string[] = [];
55

66
c.push(`export const LAYOUTS = {`);
77
Object.entries(ctx.opts.layouts).forEach(([layoutName, layoutPath]) => {
8-
let importPath = layoutPath;
9-
if (importPath.endsWith('.tsx') || importPath.endsWith('.jsx')) {
10-
importPath = importPath.substring(0, importPath.length - 4);
11-
} else if (importPath.endsWith('.ts') || importPath.endsWith('.js')) {
12-
importPath = importPath.substring(0, importPath.length - 3);
13-
}
14-
8+
const importPath = getImportPath(layoutPath);
159
c.push(` ${JSON.stringify(layoutName)}: () => import(${JSON.stringify(importPath)}),`);
1610
});
1711
c.push(`};`);
1812

19-
c.push(`export const INDEXES = {`);
20-
for (const i of ctx.indexes) {
21-
c.push(` ${JSON.stringify(i.pathname)}: ${JSON.stringify(i)},`);
13+
c.push(`export const PAGES = {`);
14+
for (const p of ctx.pages) {
15+
c.push(` ${JSON.stringify(p.pathname)}: () => import(${JSON.stringify(p.filePath)}),`);
2216
}
2317
c.push(`};`);
2418

19+
c.push(`export const INLINED_MODULES = false`);
20+
21+
c.push(...createPageIndex(ctx));
22+
23+
c.push(createBuildId());
24+
25+
return c.join('\n');
26+
}
27+
28+
export function createEsmImportedCode(ctx: PluginContext) {
29+
const esmImports: string[] = [];
30+
const c: string[] = [];
31+
32+
c.push(`export const LAYOUTS = {`);
33+
Object.entries(ctx.opts.layouts).forEach(([layoutName, layoutPath], index) => {
34+
const importPath = getImportPath(layoutPath);
35+
36+
const importName = `layout_${index}`;
37+
esmImports.push(`import ${importName} from ${JSON.stringify(importPath)};`);
38+
c.push(` ${JSON.stringify(layoutName)}: () => ${importName},`);
39+
});
40+
c.push(`};`);
41+
2542
c.push(`export const PAGES = {`);
26-
if (inlineModules) {
27-
for (const p of ctx.pages) {
28-
c.push(` ${JSON.stringify(p.pathname)}: () => import(${JSON.stringify(p.filePath)}),`);
29-
}
43+
let importPageIndex = 0;
44+
for (const p of ctx.pages) {
45+
const importName = `page_${importPageIndex++}`;
46+
esmImports.push(`import ${importName} from ${JSON.stringify(p.filePath)};`);
47+
c.push(` ${JSON.stringify(p.pathname)}: () => import(${JSON.stringify(p.filePath)}),`);
3048
}
3149
c.push(`};`);
3250

33-
c.push(`export const INLINED_MODULES = ${JSON.stringify(inlineModules)};`);
51+
c.push(`export const INLINED_MODULES = true`);
3452

35-
c.push(
36-
`export const BUILD_ID = ${JSON.stringify(
37-
Math.round(Math.random() * Number.MAX_SAFE_INTEGER)
38-
.toString(36)
39-
.toLowerCase()
40-
)};`
41-
);
53+
c.push(...createPageIndex(ctx));
4254

43-
return c.join('\n');
55+
c.push(createBuildId());
56+
57+
return esmImports.join('\n') + c.join('\n');
58+
}
59+
60+
function createPageIndex(ctx: PluginContext) {
61+
const c: string[] = [];
62+
c.push(`export const INDEXES = {`);
63+
for (const i of ctx.indexes) {
64+
c.push(` ${JSON.stringify(i.pathname)}: ${JSON.stringify(i)},`);
65+
}
66+
c.push(`};`);
67+
return c;
68+
}
69+
70+
function createBuildId() {
71+
return `export const BUILD_ID = ${JSON.stringify(
72+
Math.round(Math.random() * Number.MAX_SAFE_INTEGER)
73+
.toString(36)
74+
.toLowerCase()
75+
)};`;
76+
}
77+
78+
function getImportPath(importPath: string) {
79+
if (importPath.endsWith('.tsx') || importPath.endsWith('.jsx')) {
80+
return importPath.substring(0, importPath.length - 4);
81+
}
82+
if (importPath.endsWith('.ts') || importPath.endsWith('.js')) {
83+
return importPath.substring(0, importPath.length - 3);
84+
}
85+
return importPath;
4486
}

packages/qwik-city/src/vite/plugin.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createMdxTransformer, MdxTransform } from './mdx';
22
import { stat } from 'fs/promises';
33
import { isAbsolute } from 'path';
44
import type { ModuleGraph, ModuleNode, Plugin, ViteDevServer } from 'vite';
5-
import { createBuildCode } from './code-generation';
5+
import { createDynamicImportedCode, createEsmImportedCode } from './code-generation';
66
import { loadPages } from './load-pages';
77
import type { PluginContext, PluginOptions } from './types';
88
import { getPagesBuildPath, normalizeOptions } from './utils';
@@ -23,8 +23,9 @@ export function qwikCity(options: PluginOptions) {
2323

2424
enforce: 'pre',
2525

26-
config(userConfig) {
27-
inlinedModules = !!userConfig.build?.ssr;
26+
config(config) {
27+
const isSSR = !!config.build?.ssr || config.mode === 'ssr';
28+
inlinedModules = isSSR;
2829
},
2930

3031
configureServer(server) {
@@ -78,22 +79,20 @@ export function qwikCity(options: PluginOptions) {
7879
await loadPages(ctx, (msg) => this.warn(msg));
7980

8081
if (inlinedModules) {
81-
// vite dev server build (esbuild)
82-
qwikCityBuildCode = createBuildCode(ctx, true);
82+
qwikCityBuildCode = createEsmImportedCode(ctx);
8383
} else {
84-
// production (rollup)
85-
qwikCityBuildCode = createBuildCode(ctx, false);
86-
87-
ctx.pages.forEach((p) => {
88-
this.emitFile({
89-
type: 'chunk',
90-
id: p.filePath,
91-
fileName: getPagesBuildPath(p.pathname),
92-
preserveSignature: 'allow-extension',
93-
});
94-
});
84+
qwikCityBuildCode = createDynamicImportedCode(ctx);
9585
}
9686

87+
ctx.pages.forEach((p) => {
88+
this.emitFile({
89+
type: 'chunk',
90+
id: p.filePath,
91+
fileName: getPagesBuildPath(p.pathname),
92+
preserveSignature: 'allow-extension',
93+
});
94+
});
95+
9796
return qwikCityBuildCode;
9897
}
9998
return null;

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,8 @@ export function createPlugin(optimizerOptions: OptimizerOptions = {}) {
6363
if (updatedOpts.entryStrategy && typeof updatedOpts.entryStrategy === 'object') {
6464
opts.entryStrategy = { ...updatedOpts.entryStrategy };
6565
}
66-
if (!opts.entryStrategy) {
67-
if (opts.isDevBuild) {
68-
opts.entryStrategy = { type: 'hook' };
69-
} else {
70-
opts.entryStrategy = { type: 'single' };
71-
}
66+
if (!opts.entryStrategy || opts.isDevBuild) {
67+
opts.entryStrategy = { type: 'hook' };
7268
}
7369

7470
if (typeof updatedOpts.forceFullBuild === 'boolean') {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ describe('qwik plugin', () => {
1212
expect(opts.isDevBuild).toBe(false);
1313
expect(opts.buildMode).toBe('client');
1414
expect(opts.rootDir).toBe(cwd);
15-
expect(opts.forceFullBuild).toBe(true);
15+
expect(opts.forceFullBuild).toBe(false);
1616
expect(opts.outClientDir).toBe(resolve(cwd, 'dist'));
1717
expect(opts.outServerDir).toBe(resolve(cwd, 'server'));
1818
expect(opts.srcDir).toBe(resolve(cwd, 'src'));
1919
expect(opts.srcInputs).toBe(null);
2020
expect(opts.srcRootInput).toEqual([resolve(cwd, 'src', 'root.tsx')]);
2121
expect(opts.srcEntryServerInput).toBe(resolve(cwd, 'src', 'entry.server.tsx'));
22-
expect(opts.entryStrategy).toEqual({ type: 'single' });
22+
expect(opts.entryStrategy).toEqual({ type: 'hook' });
2323
expect(opts.minify).toBe('minify');
2424
expect(opts.symbolsOutput).toBe(null);
2525
});

0 commit comments

Comments
 (0)