Skip to content

Commit

Permalink
WIP: added communication between vite plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
shairez committed Jan 8, 2025
1 parent 4af0105 commit 7c820d1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
38 changes: 19 additions & 19 deletions packages/qwik-city/src/buildtime/vite/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import type { QwikVitePlugin } from '@builder.io/qwik/optimizer';
import swRegister from '@qwik-city-sw-register-build';
import { createMdxTransformer, type MdxTransform } from '../markdown/mdx';
import { basename, join, resolve, extname } from 'node:path';
import type { Plugin, PluginOption, UserConfig, Rollup } from 'vite';
import fs from 'node:fs';
import { basename, extname, join, resolve } from 'node:path';
import type { Plugin, PluginOption, Rollup, UserConfig } from 'vite';
import { loadEnv } from 'vite';
import { generateQwikCityPlan } from '../runtime-generation/generate-qwik-city-plan';
import type { BuildContext } from '../types';
import { createBuildContext, resetBuildContext } from '../context';
import {
NOT_FOUND_PATHS_ID,
RESOLVED_NOT_FOUND_PATHS_ID,
RESOLVED_STATIC_PATHS_ID,
STATIC_PATHS_ID,
} from '../../adapters/shared/vite';
import { postBuild } from '../../adapters/shared/vite/post-build';
import { patchGlobalThis } from '../../middleware/node/node-fetch';
import { isMenuFileName, normalizePath, removeExtension } from '../../utils/fs';
import { validatePlugin } from './validate-plugin';
import type { QwikCityPluginApi, QwikCityVitePluginOptions } from './types';
import { build } from '../build';
import { ssrDevMiddleware, staticDistMiddleware } from './dev-server';
import { createBuildContext, resetBuildContext } from '../context';
import { createMdxTransformer, type MdxTransform } from '../markdown/mdx';
import { transformMenu } from '../markdown/menu';
import { generateQwikCityEntries } from '../runtime-generation/generate-entries';
import { patchGlobalThis } from '../../middleware/node/node-fetch';
import type { QwikVitePlugin } from '@builder.io/qwik/optimizer';
import fs from 'node:fs';
import { generateQwikCityPlan } from '../runtime-generation/generate-qwik-city-plan';
import {
generateServiceWorkerRegister,
prependManifestToServiceWorker,
} from '../runtime-generation/generate-service-worker';
import {
NOT_FOUND_PATHS_ID,
RESOLVED_NOT_FOUND_PATHS_ID,
RESOLVED_STATIC_PATHS_ID,
STATIC_PATHS_ID,
} from '../../adapters/shared/vite';
import { postBuild } from '../../adapters/shared/vite/post-build';
import type { BuildContext } from '../types';
import { ssrDevMiddleware, staticDistMiddleware } from './dev-server';
import { imagePlugin } from './image-jsx';
import type { QwikCityPluginApi, QwikCityVitePluginOptions } from './types';
import { validatePlugin } from './validate-plugin';

/** @public */
export function qwikCity(userOpts?: QwikCityVitePluginOptions): PluginOption[] {
Expand Down
17 changes: 16 additions & 1 deletion packages/qwik/src/optimizer/src/plugins/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ const FONTS = ['.woff', '.woff2', '.ttf'];
*/
type P<T> = VitePlugin<T> & { api: T; config: Extract<VitePlugin<T>['config'], Function> };

export type BundleGraphModifier = (graph: QwikBundleGraph) => QwikBundleGraph;

/**
* The types for Vite/Rollup don't allow us to be too specific about the return type. The correct
* return type is `[QwikVitePlugin, VitePlugin<never>]`, and if you search the plugin by name you'll
Expand Down Expand Up @@ -93,6 +95,8 @@ export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any {
return null;
}

const bundleGraphModifiers = new Set<BundleGraphModifier>();

const api: QwikVitePluginApi = {
getOptimizer: () => qwikPlugin.getOptimizer(),
getOptions: () => qwikPlugin.getOptions(),
Expand All @@ -102,6 +106,8 @@ export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any {
getClientOutDir: () => clientOutDir,
getClientPublicOutDir: () => clientPublicOutDir,
getAssetsDir: () => viteAssetsDir,
registerBundleGraphModifier: (modifier: BundleGraphModifier) =>
bundleGraphModifiers.add(modifier),
};

// We provide two plugins to Vite. The first plugin is the main plugin that handles all the
Expand Down Expand Up @@ -592,14 +598,22 @@ export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any {
const assetsDir = qwikPlugin.getOptions().assetsDir || '';
const useAssetsDir = !!assetsDir && assetsDir !== '_astro';
const sys = qwikPlugin.getSys();

let bundleGraph = convertManifestToBundleGraph(manifest);
if (bundleGraphModifiers.size > 0) {
for (const modifier of bundleGraphModifiers) {
bundleGraph = modifier(bundleGraph);
}
}

this.emitFile({
type: 'asset',
fileName: sys.path.join(
useAssetsDir ? assetsDir : '',
'build',
`q-bundle-graph-${manifest.manifestHash}.json`
),
source: JSON.stringify(convertManifestToBundleGraph(manifest)),
source: JSON.stringify(bundleGraph),
});
const fs: typeof import('fs') = await sys.dynamicImport('node:fs');
const workerScriptPath = (await this.resolve('@builder.io/qwik/qwik-prefetch.js'))!.id;
Expand Down Expand Up @@ -1100,6 +1114,7 @@ export interface QwikVitePluginApi {
getClientOutDir: () => string | null;
getClientPublicOutDir: () => string | null;
getAssetsDir: () => string | undefined;
registerBundleGraphModifier: (modifier: BundleGraphModifier) => void;
}

/**
Expand Down

0 comments on commit 7c820d1

Please sign in to comment.