Skip to content

Commit

Permalink
feat: add support vite renderBuiltUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewPattell committed Jan 15, 2025
1 parent 1133e36 commit 44eaf8a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/services/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ class Build {
buildDir: this.viteConfig.build.outDir,
viteAliases: this.viteConfig.resolve.alias,
basename: this.viteConfig.base,
renderBuiltUrl: this.viteConfig?.experimental?.renderBuiltUrl,
}).buildRoutesManifest();
}

Expand Down
21 changes: 18 additions & 3 deletions src/services/ssr-manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Socket } from 'node:net';
import path from 'node:path';
import chalk from 'chalk';
import type { RouterState } from 'react-router';
import type { Alias, ModuleNode } from 'vite';
import type { Alias, ModuleNode, RenderBuiltAssetUrl } from 'vite';
import type { IAsyncRoute } from '@helpers/import-route';
import type { IRequestContext } from '@node/render';
import type { TRoutesTree } from '@services/parse-routes';
Expand All @@ -15,6 +15,7 @@ interface ISsrManifestParams {
buildDir?: string;
viteAliases?: Alias[];
basename?: string;
renderBuiltUrl?: RenderBuiltAssetUrl;
}

interface IManifest {
Expand Down Expand Up @@ -96,6 +97,11 @@ class SsrManifest {
*/
protected readonly basename?: string;

/**
* Vite renderBuiltUrl config func
*/
protected readonly renderBuiltUrl?: RenderBuiltAssetUrl;

/**
* Loaded assets manifest file
*/
Expand All @@ -106,14 +112,15 @@ class SsrManifest {
*/
protected constructor(
config: ServerConfig,
{ buildDir, viteAliases, basename }: ISsrManifestParams = {},
{ buildDir, viteAliases, basename, renderBuiltUrl }: ISsrManifestParams = {},
) {
this.config = config;
this.root = config.getParams().root;
this.buildDir = buildDir;
this.viteAliases = viteAliases ?? config.getVite()?.config?.resolve.alias;
this.pathNormalize = new PathNormalize(config, viteAliases);
this.basename = basename;
this.renderBuiltUrl = renderBuiltUrl;
}

/**
Expand Down Expand Up @@ -239,8 +246,16 @@ class SsrManifest {

// keep only js,css,image,fonts files
if (type) {
const filename = path.posix.normalize(`${this.basename}/${asset}`);
const modifiedFilename = this.renderBuiltUrl?.(filename, {
type: 'asset',
ssr: true,
hostId: '',
hostType: filename.split('.').at(-1)?.toLowerCase() as 'js',
});

res[asset] = {
url: path.posix.normalize(`${this.basename}/${asset}`),
url: typeof modifiedFilename === 'string' ? modifiedFilename : filename,
weight: isEntry ? 1.9 : this.getAssetWeight(asset),
type,
isNested,
Expand Down

0 comments on commit 44eaf8a

Please sign in to comment.