Skip to content

refactor: improve readability in vite and rollup hooks #7759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: build/v2
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/qwik-router/src/buildtime/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ function qwikRouterPlugin(userOpts?: QwikRouterVitePluginOptions): any {
},

async transform(code, id) {
if (id.startsWith('\0')) {
const isVirtualId = id.startsWith('\0');

if (isVirtualId) {
return;
}
const ext = extname(id).toLowerCase();
Expand Down
8 changes: 4 additions & 4 deletions packages/qwik/src/optimizer/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import type {
} from '../types';
import { convertManifestToBundleGraph } from './bundle-graph';
import { createLinter, type QwikLinter } from './eslint-plugin';
import { isWin, parseId } from './vite-utils';
import { isVirtualId, isWin, parseId } from './vite-utils';

const REG_CTX_NAME = ['server'];

Expand Down Expand Up @@ -470,7 +470,7 @@ export function createQwikPlugin(optimizerOptions: OptimizerOptions = {}) {
importerId: string | undefined,
resolveOpts?: Parameters<Extract<Plugin['resolveId'], Function>>[2]
) => {
if (id.startsWith('\0')) {
if (isVirtualId(id)) {
return;
}
const count = resolveIdCount++;
Expand Down Expand Up @@ -627,7 +627,7 @@ export function createQwikPlugin(optimizerOptions: OptimizerOptions = {}) {
id: string,
loadOpts?: Parameters<Extract<Plugin['load'], Function>>[1]
): Promise<LoadResult> => {
if (id.startsWith('\0') || id.startsWith('/@fs/')) {
if (isVirtualId(id) || id.startsWith('/@fs/')) {
return;
}
const count = loadCount++;
Expand Down Expand Up @@ -705,7 +705,7 @@ export function createQwikPlugin(optimizerOptions: OptimizerOptions = {}) {
id: string,
transformOpts: Parameters<Extract<Plugin['transform'], Function>>[2] = {}
): Promise<TransformResult> {
if (id.startsWith('\0')) {
if (isVirtualId(id)) {
return;
}
const count = transformCount++;
Expand Down
7 changes: 4 additions & 3 deletions packages/qwik/src/optimizer/src/plugins/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
type QwikPlugin,
type QwikPluginOptions,
} from './plugin';
import { isVirtualId } from './vite-utils';

type QwikRollupPluginApi = {
getOptimizer: () => Optimizer;
Expand Down Expand Up @@ -94,21 +95,21 @@ export function qwikRollup(qwikRollupOpts: QwikRollupPluginOptions = {}): any {
},

resolveId(id, importer) {
if (id.startsWith('\0')) {
if (isVirtualId(id)) {
return null;
}
return qwikPlugin.resolveId(this, id, importer);
},

load(id) {
if (id.startsWith('\0')) {
if (isVirtualId(id)) {
return null;
}
return qwikPlugin.load(this, id);
},

transform(code, id) {
if (id.startsWith('\0')) {
if (isVirtualId(id)) {
return null;
}
return qwikPlugin.transform(this, code, id);
Expand Down
2 changes: 2 additions & 0 deletions packages/qwik/src/optimizer/src/plugins/vite-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ export const findLocation = (e: Error): Loc | undefined => {
return undefined;
};

export const isVirtualId = (id: string) => id.startsWith('\0');

const safeParseInt = (nu: string) => {
try {
return parseInt(nu, 10);
Expand Down
25 changes: 17 additions & 8 deletions packages/qwik/src/optimizer/src/plugins/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
} from './plugin';
import { createRollupError, normalizeRollupOutputOptions } from './rollup';
import { VITE_DEV_CLIENT_QS, configureDevServer, configurePreviewServer } from './vite-dev-server';
import { parseId } from './vite-utils';
import { isVirtualId, parseId } from './vite-utils';

const DEDUPE = [QWIK_CORE_ID, QWIK_JSX_RUNTIME_ID, QWIK_JSX_DEV_RUNTIME_ID];

Expand Down Expand Up @@ -409,24 +409,30 @@ export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any {
},

resolveId(id, importer, resolveIdOpts) {
if (id.startsWith('\0') || !fileFilter(id, 'resolveId')) {
const shouldResolveFile = fileFilter(id, 'resolveId');
const isDevClientModule = isClientDevOnly && id === VITE_CLIENT_MODULE;

if (isVirtualId(id) || !shouldResolveFile) {
return null;
}
if (isClientDevOnly && id === VITE_CLIENT_MODULE) {
if (isDevClientModule) {
return id;
}
return qwikPlugin.resolveId(this, id, importer, resolveIdOpts);
},

load(id, loadOpts) {
if (id.startsWith('\0') || !fileFilter(id, 'load')) {
const shouldLoadFile = fileFilter(id, 'load');
const isDevClientModule = isClientDevOnly && id === VITE_CLIENT_MODULE;

if (isVirtualId(id) || !shouldLoadFile) {
return null;
}

id = qwikPlugin.normalizePath(id);
const opts = qwikPlugin.getOptions();

if (isClientDevOnly && id === VITE_CLIENT_MODULE) {
if (isDevClientModule) {
const opts = qwikPlugin.getOptions();
return getViteDevModule(opts);
}
if (viteCommand === 'serve' && id.endsWith(QWIK_CLIENT_MANIFEST_ID)) {
Expand All @@ -438,7 +444,10 @@ export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any {
},

transform(code, id, transformOpts) {
if (id.startsWith('\0') || !fileFilter(id, 'transform') || id.includes('?raw')) {
const shouldTransformFile = fileFilter(id, 'transform');
const isStringImportId = id.includes('?raw');

if (isVirtualId(id) || !shouldTransformFile || isStringImportId) {
return null;
}

Expand Down Expand Up @@ -880,7 +889,7 @@ interface QwikVitePluginCommonOptions {
* Predicate function to filter out files from the optimizer. hook for resolveId, load, and
* transform
*/
fileFilter?: (id: string, hook: string) => boolean;
fileFilter?: (id: string, hook: keyof VitePlugin) => boolean;
/**
* Run eslint on the source files for the ssr build or dev server. This can slow down startup on
* large projects. Defaults to `true`
Expand Down
Loading