Skip to content

Commit e7814bd

Browse files
committed
fix(build): retain previous qrl hashes
This pretends that the src/ dir is the root of the project as far as the optimizer is concerned, and makes sure the click-to-component knows about it. This way the relative paths given to the optimizer are the same as before.
1 parent b0e18fd commit e7814bd

File tree

5 files changed

+37
-23
lines changed

5 files changed

+37
-23
lines changed

.changeset/thirty-spies-hug.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
'@builder.io/qwik': minor
2+
'@builder.io/qwik': patch
33
---
44

5-
FIX: click-to-component works again, and path handling inside the optimizer is simplified. Note: This changes the QRL hashes due to a different internal path
5+
FIX: click-to-component works again, and path handling inside the optimizer is simplified.

packages/qwik/src/optimizer/src/plugins/click-to-component.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@
142142
);
143143

144144
globalThis.qwikOpenInEditor = function (path) {
145-
fetch('{{BASE}}__open-in-editor?file=' + encodeURIComponent(path));
145+
fetch('{{BASE}}__open-in-editor?file={{SRC}}' + encodeURIComponent(path));
146146
};
147147
document.addEventListener(
148148
'contextmenu',

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

+10-4
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,12 @@ export function createPlugin(optimizerOptions: OptimizerOptions = {}) {
646646
});
647647
}
648648

649-
const filePath = path.relative(opts.rootDir, pathId);
649+
/**
650+
* We provide paths relative to srcDir for backwards compatibility. That way the qrl hashes
651+
* remain unchanged.
652+
*/
653+
const rootDir = opts.srcDir || opts.rootDir;
654+
const filePath = path.relative(rootDir, pathId);
650655
const entryStrategy: EntryStrategy = opts.entryStrategy;
651656
const transformOpts: TransformModulesOptions = {
652657
input: [{ code, path: filePath }],
@@ -658,9 +663,10 @@ export function createPlugin(optimizerOptions: OptimizerOptions = {}) {
658663
transpileJsx: true,
659664
explicitExtensions: true,
660665
preserveFilenames: true,
661-
// TODO remove
666+
// TODO remove this in v2
662667
srcDir: '',
663-
rootDir: opts.rootDir,
668+
// Sourcemaps need to know this
669+
rootDir,
664670
mode,
665671
scope: opts.scope || undefined,
666672
isServer,
@@ -695,7 +701,7 @@ export function createPlugin(optimizerOptions: OptimizerOptions = {}) {
695701
const deps = new Set<string>();
696702
for (const mod of newOutput.modules) {
697703
if (mod !== module) {
698-
// All modules are in the same directory as the parent
704+
// All segments are in the same directory as the parent
699705
const key = path.join(dir, mod.segment!.canonicalFilename + '.' + mod.segment?.extension);
700706
debug(`transform(${count})`, `segment ${key}`, mod.segment!.displayName);
701707
parentIds.set(key, id);

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

+19-11
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { SYNC_QRL } from '../../../core/qrl/qrl-class';
88
import type { OptimizerSystem, Path, QwikManifest, SymbolMapper, SymbolMapperFn } from '../types';
99
import clickToComponentSrc from './click-to-component.html?raw';
1010
import errorHost from './error-host.html?raw';
11-
import imageDevTools from './image-size-runtime.html?raw';
11+
import imageDevToolsSrc from './image-size-runtime.html?raw';
1212
import perfWarning from './perf-warning.html?raw';
1313
import { parseId, type NormalizedQwikPluginOptions } from './plugin';
1414
import type { QwikViteDevResponse } from './vite';
@@ -27,7 +27,7 @@ function getOrigin(req: IncomingMessage) {
2727
return `${protocol}://${host}`;
2828
}
2929

30-
function createSymbolMapper(base: string): SymbolMapperFn {
30+
function createSymbolMapper(base: string, srcRelative: string): SymbolMapperFn {
3131
return (
3232
symbolName: string,
3333
_mapper: SymbolMapper | undefined,
@@ -45,7 +45,7 @@ function createSymbolMapper(base: string): SymbolMapperFn {
4545
}
4646
// In dev mode, the `parent` is the Vite URL for the parent, not the real absolute path.
4747
// It is always absolute but when on Windows that's without a /
48-
const qrlFile = `${base}${parent.startsWith('/') ? parent.slice(1) : parent}_${symbolName}.js`;
48+
const qrlFile = `${base}${srcRelative}${parent.startsWith('/') ? parent.slice(1) : parent}_${symbolName}.js`;
4949
return [symbolName, qrlFile];
5050
};
5151
}
@@ -77,7 +77,10 @@ export async function configureDevServer(
7777
clientDevInput: string | undefined,
7878
devSsrServer: boolean
7979
) {
80-
symbolMapper = lazySymbolMapper = createSymbolMapper(base);
80+
symbolMapper = lazySymbolMapper = createSymbolMapper(
81+
base,
82+
opts.srcDir ? `${path.relative(opts.rootDir, opts.srcDir)}/` : ''
83+
);
8184
if (!devSsrServer) {
8285
// we just needed the symbolMapper
8386
return;
@@ -212,7 +215,7 @@ export async function configureDevServer(
212215
if ('html' in result) {
213216
res.write((result as any).html);
214217
}
215-
res.write(END_SSR_SCRIPT(opts, base));
218+
res.write(END_SSR_SCRIPT(path, opts, base));
216219
res.end();
217220
} else {
218221
next();
@@ -376,25 +379,30 @@ function relativeURL(url: string, base: string) {
376379
return url;
377380
}
378381

379-
const DEV_QWIK_INSPECTOR = (opts: NormalizedQwikPluginOptions['devTools'], base: string) => {
382+
const DEV_QWIK_INSPECTOR = (path: Path, opts: NormalizedQwikPluginOptions, base: string) => {
383+
const { srcDir, rootDir } = opts;
384+
const srcRelative = srcDir ? `${path.relative(rootDir, srcDir)}/` : '';
385+
const { clickToSource, imageDevTools } = opts.devTools;
380386
const qwikdevtools = {
381-
hotKeys: opts.clickToSource ?? [],
387+
hotKeys: clickToSource ?? [],
382388
};
383389
return (
384390
`<script>
385391
globalThis.qwikdevtools = ${JSON.stringify(qwikdevtools)};
386392
</script>` +
387-
(opts.imageDevTools ? imageDevTools : '') +
388-
(opts.clickToSource ? clickToComponentSrc.replaceAll('{{BASE}}', base) : '')
393+
(imageDevTools ? imageDevToolsSrc : '') +
394+
(clickToSource
395+
? clickToComponentSrc.replaceAll('{{BASE}}', base).replaceAll('{{SRC}}', srcRelative)
396+
: '')
389397
);
390398
};
391399

392-
const END_SSR_SCRIPT = (opts: NormalizedQwikPluginOptions, base: string) => `
400+
const END_SSR_SCRIPT = (path: Path, opts: NormalizedQwikPluginOptions, base: string) => `
393401
<style>${VITE_ERROR_OVERLAY_STYLES}</style>
394402
<script type="module" src="/@vite/client"></script>
395403
${errorHost}
396404
${perfWarning}
397-
${DEV_QWIK_INSPECTOR(opts.devTools, base)}
405+
${DEV_QWIK_INSPECTOR(path, opts, base)}
398406
`;
399407

400408
function getViteDevIndexHtml(entryUrl: string, serverData: Record<string, any>) {

starters/e2e/e2e.render.spec.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,31 @@ test.describe("render", () => {
2828

2929
const attributes = page.locator("#attributes");
3030

31-
await expect(attributes).toHaveClass("⭐️ju3o0m-1 even stable0");
31+
await expect(attributes).toHaveClass("⭐️unvb18-1 even stable0");
3232
await expect(attributes).toHaveAttribute("aria-hidden", "true");
3333
await expect(attributes).toHaveAttribute("preventdefault:click", "");
3434

3535
await increment.click();
3636

37-
await expect(attributes).toHaveClass("⭐️ju3o0m-1 odd stable0");
37+
await expect(attributes).toHaveClass("⭐️unvb18-1 odd stable0");
3838
await expect(attributes).toHaveAttribute("aria-hidden", "true");
3939
await expect(attributes).toHaveAttribute("preventdefault:click", "");
4040

4141
await toggle.click();
4242

43-
await expect(attributes).toHaveClass("⭐️ju3o0m-1");
43+
await expect(attributes).toHaveClass("⭐️unvb18-1");
4444
await expect(attributes).not.hasAttribute("aria-hidden");
4545
await expect(attributes).not.hasAttribute("preventdefault:click");
4646

4747
await increment.click();
4848

49-
await expect(attributes).toHaveClass("⭐️ju3o0m-1");
49+
await expect(attributes).toHaveClass("⭐️unvb18-1");
5050
await expect(attributes).not.hasAttribute("aria-hidden");
5151
await expect(attributes).not.hasAttribute("preventdefault:click");
5252

5353
await toggle.click();
5454

55-
await expect(attributes).toHaveClass("⭐️ju3o0m-1 even stable0");
55+
await expect(attributes).toHaveClass("⭐️unvb18-1 even stable0");
5656
await expect(attributes).toHaveAttribute("aria-hidden", "true");
5757
await expect(attributes).toHaveAttribute("preventdefault:click", "");
5858
});

0 commit comments

Comments
 (0)