Skip to content

Commit 232a303

Browse files
refactor: improve readability in vite and rollup hooks (#7759)
--------- Co-authored-by: gioboa <[email protected]>
1 parent 2ff3c24 commit 232a303

File tree

5 files changed

+21
-12
lines changed

5 files changed

+21
-12
lines changed

packages/qwik-router/src/buildtime/vite/plugin.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,8 @@ function qwikRouterPlugin(userOpts?: QwikRouterVitePluginOptions): any {
260260
},
261261

262262
async transform(code, id) {
263-
if (id.startsWith('\0')) {
263+
const isVirtualId = id.startsWith('\0');
264+
if (isVirtualId) {
264265
return;
265266
}
266267
const ext = extname(id).toLowerCase();

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import type {
2121
} from '../types';
2222
import { convertManifestToBundleGraph } from './bundle-graph';
2323
import { createLinter, type QwikLinter } from './eslint-plugin';
24-
import { isWin, parseId } from './vite-utils';
24+
import { isVirtualId, isWin, parseId } from './vite-utils';
2525

2626
const REG_CTX_NAME = ['server'];
2727

@@ -448,7 +448,7 @@ export function createQwikPlugin(optimizerOptions: OptimizerOptions = {}) {
448448
importerId: string | undefined,
449449
resolveOpts?: Parameters<Extract<Plugin['resolveId'], Function>>[2]
450450
) => {
451-
if (id.startsWith('\0')) {
451+
if (isVirtualId(id)) {
452452
return;
453453
}
454454

@@ -638,7 +638,7 @@ export function createQwikPlugin(optimizerOptions: OptimizerOptions = {}) {
638638
// This doesn't get used, but we need to return something
639639
return '"opening in editor"';
640640
}
641-
if (id.startsWith('\0') || id.startsWith('/@fs/')) {
641+
if (isVirtualId(id) || id.startsWith('/@fs/')) {
642642
return;
643643
}
644644
const count = loadCount++;
@@ -724,7 +724,7 @@ export function createQwikPlugin(optimizerOptions: OptimizerOptions = {}) {
724724
id: string,
725725
transformOpts = {} as Parameters<Extract<Plugin['transform'], Function>>[2]
726726
): Promise<Rollup.TransformResult> {
727-
if (id.startsWith('\0')) {
727+
if (isVirtualId(id)) {
728728
return;
729729
}
730730
const count = transformCount++;

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
type QwikPluginOptions,
1919
} from './plugin';
2020
import { findDepPkgJsonPath } from './utils';
21+
import { isVirtualId } from './vite-utils';
2122

2223
type QwikRollupPluginApi = {
2324
getOptimizer: () => Optimizer;
@@ -94,21 +95,21 @@ export function qwikRollup(qwikRollupOpts: QwikRollupPluginOptions = {}): any {
9495
},
9596

9697
resolveId(id, importer) {
97-
if (id.startsWith('\0')) {
98+
if (isVirtualId(id)) {
9899
return null;
99100
}
100101
return qwikPlugin.resolveId(this, id, importer);
101102
},
102103

103104
load(id) {
104-
if (id.startsWith('\0')) {
105+
if (isVirtualId(id)) {
105106
return null;
106107
}
107108
return qwikPlugin.load(this, id);
108109
},
109110

110111
transform(code, id) {
111-
if (id.startsWith('\0')) {
112+
if (isVirtualId(id)) {
112113
return null;
113114
}
114115
return qwikPlugin.transform(this, code, id);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ export const findLocation = (e: Error): Loc | undefined => {
7777
return undefined;
7878
};
7979

80+
export const isVirtualId = (id: string) => id.startsWith('\0');
81+
8082
const safeParseInt = (nu: string) => {
8183
try {
8284
return parseInt(nu, 10);

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
} from './plugin';
2828
import { createRollupError, normalizeRollupOutputOptions } from './rollup';
2929
import { configurePreviewServer, getViteIndexTags } from './dev';
30+
import { isVirtualId } from './vite-utils';
3031

3132
const DEDUPE = [
3233
QWIK_CORE_ID,
@@ -383,14 +384,16 @@ export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any {
383384
},
384385

385386
resolveId(id, importer, resolveIdOpts) {
386-
if (id.startsWith('\0') || !fileFilter(id, 'resolveId')) {
387+
const shouldResolveFile = fileFilter(id, 'resolveId');
388+
if (isVirtualId(id) || !shouldResolveFile) {
387389
return null;
388390
}
389391
return qwikPlugin.resolveId(this, id, importer, resolveIdOpts);
390392
},
391393

392394
load(id, loadOpts) {
393-
if (id.startsWith('\0') || !fileFilter(id, 'load')) {
395+
const shouldLoadFile = fileFilter(id, 'load');
396+
if (isVirtualId(id) || !shouldLoadFile) {
394397
return null;
395398
}
396399

@@ -405,7 +408,9 @@ export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any {
405408
},
406409

407410
transform(code, id, transformOpts) {
408-
if (id.startsWith('\0') || !fileFilter(id, 'transform') || id.includes('?raw')) {
411+
const shouldTransformFile = fileFilter(id, 'transform');
412+
const isStringImportId = id.includes('?raw');
413+
if (isVirtualId(id) || !shouldTransformFile || isStringImportId) {
409414
return null;
410415
}
411416
if (
@@ -801,7 +806,7 @@ interface QwikVitePluginCommonOptions {
801806
* Predicate function to filter out files from the optimizer. hook for resolveId, load, and
802807
* transform
803808
*/
804-
fileFilter?: (id: string, hook: string) => boolean;
809+
fileFilter?: (id: string, hook: keyof VitePlugin) => boolean;
805810
/**
806811
* Run eslint on the source files for the ssr build or dev server. This can slow down startup on
807812
* large projects. Defaults to `true`

0 commit comments

Comments
 (0)