Skip to content

Commit 7c820d1

Browse files
committed
WIP: added communication between vite plugins
1 parent 4af0105 commit 7c820d1

File tree

2 files changed

+35
-20
lines changed
  • packages

2 files changed

+35
-20
lines changed

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

+19-19
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1+
import type { QwikVitePlugin } from '@builder.io/qwik/optimizer';
12
import swRegister from '@qwik-city-sw-register-build';
2-
import { createMdxTransformer, type MdxTransform } from '../markdown/mdx';
3-
import { basename, join, resolve, extname } from 'node:path';
4-
import type { Plugin, PluginOption, UserConfig, Rollup } from 'vite';
3+
import fs from 'node:fs';
4+
import { basename, extname, join, resolve } from 'node:path';
5+
import type { Plugin, PluginOption, Rollup, UserConfig } from 'vite';
56
import { loadEnv } from 'vite';
6-
import { generateQwikCityPlan } from '../runtime-generation/generate-qwik-city-plan';
7-
import type { BuildContext } from '../types';
8-
import { createBuildContext, resetBuildContext } from '../context';
7+
import {
8+
NOT_FOUND_PATHS_ID,
9+
RESOLVED_NOT_FOUND_PATHS_ID,
10+
RESOLVED_STATIC_PATHS_ID,
11+
STATIC_PATHS_ID,
12+
} from '../../adapters/shared/vite';
13+
import { postBuild } from '../../adapters/shared/vite/post-build';
14+
import { patchGlobalThis } from '../../middleware/node/node-fetch';
915
import { isMenuFileName, normalizePath, removeExtension } from '../../utils/fs';
10-
import { validatePlugin } from './validate-plugin';
11-
import type { QwikCityPluginApi, QwikCityVitePluginOptions } from './types';
1216
import { build } from '../build';
13-
import { ssrDevMiddleware, staticDistMiddleware } from './dev-server';
17+
import { createBuildContext, resetBuildContext } from '../context';
18+
import { createMdxTransformer, type MdxTransform } from '../markdown/mdx';
1419
import { transformMenu } from '../markdown/menu';
1520
import { generateQwikCityEntries } from '../runtime-generation/generate-entries';
16-
import { patchGlobalThis } from '../../middleware/node/node-fetch';
17-
import type { QwikVitePlugin } from '@builder.io/qwik/optimizer';
18-
import fs from 'node:fs';
21+
import { generateQwikCityPlan } from '../runtime-generation/generate-qwik-city-plan';
1922
import {
2023
generateServiceWorkerRegister,
2124
prependManifestToServiceWorker,
2225
} from '../runtime-generation/generate-service-worker';
23-
import {
24-
NOT_FOUND_PATHS_ID,
25-
RESOLVED_NOT_FOUND_PATHS_ID,
26-
RESOLVED_STATIC_PATHS_ID,
27-
STATIC_PATHS_ID,
28-
} from '../../adapters/shared/vite';
29-
import { postBuild } from '../../adapters/shared/vite/post-build';
26+
import type { BuildContext } from '../types';
27+
import { ssrDevMiddleware, staticDistMiddleware } from './dev-server';
3028
import { imagePlugin } from './image-jsx';
29+
import type { QwikCityPluginApi, QwikCityVitePluginOptions } from './types';
30+
import { validatePlugin } from './validate-plugin';
3131

3232
/** @public */
3333
export function qwikCity(userOpts?: QwikCityVitePluginOptions): PluginOption[] {

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

+16-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ const FONTS = ['.woff', '.woff2', '.ttf'];
4949
*/
5050
type P<T> = VitePlugin<T> & { api: T; config: Extract<VitePlugin<T>['config'], Function> };
5151

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

98+
const bundleGraphModifiers = new Set<BundleGraphModifier>();
99+
96100
const api: QwikVitePluginApi = {
97101
getOptimizer: () => qwikPlugin.getOptimizer(),
98102
getOptions: () => qwikPlugin.getOptions(),
@@ -102,6 +106,8 @@ export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any {
102106
getClientOutDir: () => clientOutDir,
103107
getClientPublicOutDir: () => clientPublicOutDir,
104108
getAssetsDir: () => viteAssetsDir,
109+
registerBundleGraphModifier: (modifier: BundleGraphModifier) =>
110+
bundleGraphModifiers.add(modifier),
105111
};
106112

107113
// We provide two plugins to Vite. The first plugin is the main plugin that handles all the
@@ -592,14 +598,22 @@ export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any {
592598
const assetsDir = qwikPlugin.getOptions().assetsDir || '';
593599
const useAssetsDir = !!assetsDir && assetsDir !== '_astro';
594600
const sys = qwikPlugin.getSys();
601+
602+
let bundleGraph = convertManifestToBundleGraph(manifest);
603+
if (bundleGraphModifiers.size > 0) {
604+
for (const modifier of bundleGraphModifiers) {
605+
bundleGraph = modifier(bundleGraph);
606+
}
607+
}
608+
595609
this.emitFile({
596610
type: 'asset',
597611
fileName: sys.path.join(
598612
useAssetsDir ? assetsDir : '',
599613
'build',
600614
`q-bundle-graph-${manifest.manifestHash}.json`
601615
),
602-
source: JSON.stringify(convertManifestToBundleGraph(manifest)),
616+
source: JSON.stringify(bundleGraph),
603617
});
604618
const fs: typeof import('fs') = await sys.dynamicImport('node:fs');
605619
const workerScriptPath = (await this.resolve('@builder.io/qwik/qwik-prefetch.js'))!.id;
@@ -1100,6 +1114,7 @@ export interface QwikVitePluginApi {
11001114
getClientOutDir: () => string | null;
11011115
getClientPublicOutDir: () => string | null;
11021116
getAssetsDir: () => string | undefined;
1117+
registerBundleGraphModifier: (modifier: BundleGraphModifier) => void;
11031118
}
11041119

11051120
/**

0 commit comments

Comments
 (0)