Skip to content

Commit

Permalink
Pass resolved viteConfig to buildEnd hook (remix-run#8885)
Browse files Browse the repository at this point in the history
  • Loading branch information
markdalgleish authored and TooTallNate committed Feb 26, 2024
1 parent 62123c7 commit d954c34
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/shy-fireants-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/dev": minor
---

Pass resolved `viteConfig` to Remix Vite plugin's `buildEnd` hook
38 changes: 29 additions & 9 deletions integration/vite-presets-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ const files = {
);
export default {
build: {
assetsDir: "custom-assets-dir",
},
plugins: [remix({
presets: [
// Ensure user config is passed to remixConfig hook
Expand Down Expand Up @@ -112,9 +115,16 @@ const files = {
name: "test-preset",
remixConfig: async () => ({
async buildEnd(buildEndArgs) {
let { viteConfig, buildManifest, remixConfig } = buildEndArgs;
await fs.writeFile(
"BUILD_END_ARGS.js",
"export default " + serializeJs(buildEndArgs, { space: 2, unsafe: true }),
"BUILD_END_META.js",
[
"export const keys = " + JSON.stringify(Object.keys(buildEndArgs)) + ";",
"export const buildManifest = " + serializeJs(buildManifest, { space: 2, unsafe: true }) + ";",
"export const remixConfig = " + serializeJs(remixConfig, { space: 2, unsafe: true }) + ";",
"export const assetsDir = " + JSON.stringify(viteConfig.build.assetsDir) + ";",
].join("\\n"),
"utf-8"
);
},
Expand Down Expand Up @@ -142,10 +152,14 @@ test("Vite / presets", async () => {
return normalizePath(path.relative(cwd, pathname));
}

let buildEndArgs: any = (
await import(URL.pathToFileURL(path.join(cwd, "BUILD_END_ARGS.js")).href)
).default;
let { remixConfig } = buildEndArgs;
let buildEndArgsMeta: any = await import(
URL.pathToFileURL(path.join(cwd, "BUILD_END_META.js")).href
);

let { remixConfig } = buildEndArgsMeta;

// Smoke test Vite config
expect(buildEndArgsMeta.assetsDir).toBe("custom-assets-dir");

// Before rewriting to relative paths, assert that paths are absolute within cwd
expect(pathStartsWithCwd(remixConfig.buildDirectory)).toBe(true);
Expand Down Expand Up @@ -182,10 +196,16 @@ test("Vite / presets", async () => {
).toEqual({
remixConfigFrozen: true,
});
expect(Object.keys(buildEndArgs)).toEqual(["buildManifest", "remixConfig"]);

// Snapshot the buildEnd args keys
expect(buildEndArgsMeta.keys).toEqual([
"buildManifest",
"remixConfig",
"viteConfig",
]);

// Smoke test the resolved config
expect(Object.keys(buildEndArgs.remixConfig)).toEqual([
expect(Object.keys(remixConfig)).toEqual([
"appDirectory",
"basename",
"buildDirectory",
Expand All @@ -201,7 +221,7 @@ test("Vite / presets", async () => {
]);

// Ensure we get a valid build manifest
expect(buildEndArgs.buildManifest).toEqual({
expect(buildEndArgsMeta.buildManifest).toEqual({
routeIdToServerBundleId: {
"routes/_index": "preset-server-bundle-id",
},
Expand Down
1 change: 1 addition & 0 deletions packages/remix-dev/vite/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,5 +357,6 @@ export async function build(
await remixConfig.buildEnd?.({
buildManifest,
remixConfig,
viteConfig,
});
}
3 changes: 2 additions & 1 deletion packages/remix-dev/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,9 @@ export type VitePluginConfig = SupportedRemixEsbuildUserConfig & {
};

type BuildEndHook = (args: {
remixConfig: ResolvedVitePluginConfig;
buildManifest: BuildManifest | undefined;
remixConfig: ResolvedVitePluginConfig;
viteConfig: Vite.ResolvedConfig;
}) => void | Promise<void>;

export type ResolvedVitePluginConfig = Readonly<
Expand Down

0 comments on commit d954c34

Please sign in to comment.