Skip to content

Commit a2aead6

Browse files
committed
fix(dev): vite base and qrl segment fixes
- fixes nx monorepo path lookup in dev mode - fix vite prefix for css etc - fix qrl segment path encoding for windows in dev mode - only pass parent filename since path is always the same - simplify resolveId path handling - change starter to only add service worker in production - add base url to manifest
1 parent 4485d32 commit a2aead6

3 files changed

Lines changed: 41 additions & 45 deletions

File tree

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

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -494,17 +494,28 @@ export function createPlugin(optimizerOptions: OptimizerOptions = {}) {
494494
}
495495
return;
496496
}
497-
let isAbsoluteDevFile;
497+
importerId = normalizePath(importerId);
498+
const parsedImporterId = parseId(importerId);
499+
const dir = path.dirname(parsedImporterId.pathId);
498500
if (opts.target === 'ssr' && !isSSR && importerId.endsWith('.html') && server) {
499501
// This is a request from a dev-mode browser
500502
// we uri-encode chunk paths in dev mode, and other imported files don't have % in their paths (hopefully)
501503
// These will be individual source files and their QRL segments
502504
id = decodeURIComponent(id);
505+
// Support absolute paths for qrl segments, due to e.g. pnpm linking
506+
const isAbsoluteFile = id.startsWith('/@fs/');
507+
if (isAbsoluteFile) {
508+
id = id.slice(4);
509+
}
503510
// Check for parent passed via QRL
504511
const match = /^([^?]*)\?_qrl_parent=(.*)/.exec(id);
505512
if (match) {
506513
id = match[1];
507-
const parentId = match[2];
514+
const parentId = id.slice(0, id.lastIndexOf('/') + 1) + match[2];
515+
if (!isAbsoluteFile) {
516+
// We know for sure that the path is relative to the html importer even though it starts with /
517+
id = normalizePath(path.join(dir, id));
518+
}
508519
// building here via ctx.load doesn't seem to work (target is always ssr?)
509520
// instead we use the devserver directly
510521
if (!clientResults.has(parentId)) {
@@ -513,41 +524,20 @@ export function createPlugin(optimizerOptions: OptimizerOptions = {}) {
513524
// The QRL segment should exist now
514525
}
515526
}
516-
// Support absolute paths for qrl segments, due to e.g. pnpm linking
517-
isAbsoluteDevFile = id.startsWith('/@fs/');
518-
if (isAbsoluteDevFile) {
519-
id = id.slice(4);
520-
}
521527
}
522528
const parsedId = parseId(id);
523529
let importeePathId = normalizePath(parsedId.pathId);
524-
if (isAbsoluteDevFile) {
530+
const ext = path.extname(importeePathId).toLowerCase();
531+
if (ext in RESOLVE_EXTS) {
532+
debug(`resolveId("${importeePathId}", "${importerId}")`);
533+
// resolve relative paths
534+
importeePathId = normalizePath(path.resolve(dir, importeePathId));
535+
525536
if (transformedOutputs.has(importeePathId)) {
526537
debug(`resolveId() Resolved ${importeePathId} from transformedOutputs`);
527-
return { id: importeePathId + parsedId.query };
528-
}
529-
// fall through to standard resolve
530-
} else {
531-
const ext = path.extname(importeePathId).toLowerCase();
532-
if (ext in RESOLVE_EXTS) {
533-
importerId = normalizePath(importerId);
534-
debug(`resolveId("${importeePathId}", "${importerId}")`);
535-
const parsedImporterId = parseId(importerId);
536-
const dir = path.dirname(parsedImporterId.pathId);
537-
if (parsedImporterId.pathId.endsWith('.html') && !importeePathId.endsWith('.html')) {
538-
// dev mode browser requests, add rootDir
539-
importeePathId = normalizePath(path.join(dir, importeePathId));
540-
} else {
541-
importeePathId = normalizePath(path.resolve(dir, importeePathId));
542-
}
543-
const transformedOutput = transformedOutputs.get(importeePathId);
544-
545-
if (transformedOutput) {
546-
debug(`resolveId() Resolved ${importeePathId} from transformedOutputs`);
547-
return {
548-
id: importeePathId + parsedId.query,
549-
};
550-
}
538+
return {
539+
id: importeePathId + parsedId.query,
540+
};
551541
}
552542
}
553543
} else if (path.isAbsolute(id)) {
@@ -556,9 +546,8 @@ export function createPlugin(optimizerOptions: OptimizerOptions = {}) {
556546
const ext = path.extname(importeePathId).toLowerCase();
557547
if (ext in RESOLVE_EXTS) {
558548
debug(`resolveId("${importeePathId}", "${importerId}")`);
559-
const transformedOutput = transformedOutputs.get(importeePathId);
560549

561-
if (transformedOutput) {
550+
if (transformedOutputs.has(importeePathId)) {
562551
debug(`resolveId() Resolved ${importeePathId} from transformedOutputs`);
563552
return {
564553
id: importeePathId + parsedId.query,
@@ -929,20 +918,20 @@ export function parseId(originalId: string) {
929918
};
930919
}
931920

932-
const TRANSFORM_EXTS: { [ext: string]: boolean } = {
921+
const TRANSFORM_EXTS = {
933922
'.jsx': true,
934923
'.ts': true,
935924
'.tsx': true,
936-
};
925+
} as const;
937926

938-
const RESOLVE_EXTS: { [ext: string]: boolean } = {
927+
const RESOLVE_EXTS = {
939928
'.tsx': true,
940929
'.ts': true,
941930
'.jsx': true,
942931
'.js': true,
943932
'.mjs': true,
944933
'.cjs': true,
945-
};
934+
} as const;
946935

947936
const TRANSFORM_REGEX = /\.qwik\.[mc]?js$/;
948937

packages/qwik/src/optimizer/src/plugins/vite-dev-server.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export async function configureDevServer(
126126
location: 'head',
127127
attributes: {
128128
rel: 'stylesheet',
129-
href: url,
129+
href: `${base}${url.slice(1)}`,
130130
},
131131
});
132132
}
@@ -161,12 +161,13 @@ export async function configureDevServer(
161161
return [symbolName, `${base}${symbolName.toLowerCase()}.js`];
162162
}
163163
const parentPath = path.dirname(parent);
164+
const parentFile = path.basename(parent);
164165
// DX: if the file isn't under the source dir (e.g. a symlink from node_modules)
165166
// use the full path, otherwise relative to the source
166167
const qrlPath = parentPath.startsWith(opts.rootDir)
167-
? path.relative(opts.rootDir, parentPath)
168-
: `@fs${parentPath}`;
169-
const qrlFile = `${encode(qrlPath)}/${symbolName.toLowerCase()}.js?_qrl_parent=${encode(parent)}`;
168+
? path.posix.normalize(path.relative(opts.rootDir, parentPath))
169+
: `@fs${path.posix.normalize(parentPath)}`;
170+
const qrlFile = `${encode(qrlPath)}/${symbolName.toLowerCase()}.js?_qrl_parent=${encode(parentFile)}`;
170171
return [symbolName, `${base}${qrlFile}`];
171172
},
172173
prefetchStrategy: null,
@@ -193,7 +194,7 @@ export async function configureDevServer(
193194
pathId.endsWith(ext)
194195
)
195196
) {
196-
res.write(`<link rel="stylesheet" href="${v.url}">`);
197+
res.write(`<link rel="stylesheet" href="${base}${v.url.slice(1)}">`);
197198
}
198199
});
199200
});

starters/apps/basic/src/root.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { component$ } from "@builder.io/qwik";
2+
import { isDev } from "@builder.io/qwik/build";
23
import {
34
QwikCityProvider,
45
RouterOutlet,
@@ -20,9 +21,14 @@ export default component$(() => {
2021
<QwikCityProvider>
2122
<head>
2223
<meta charset="utf-8" />
23-
<link rel="manifest" href="/manifest.json" />
24+
{!isDev && (
25+
<link
26+
rel="manifest"
27+
href={`${import.meta.env.BASE_URL}manifest.json`}
28+
/>
29+
)}
2430
<RouterHead />
25-
<ServiceWorkerRegister />
31+
{!isDev && <ServiceWorkerRegister />}
2632
</head>
2733
<body lang="en">
2834
<RouterOutlet />

0 commit comments

Comments
 (0)