Skip to content
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

Vite: Pass remixUserConfig to preset config hook #8797

Merged
merged 1 commit into from
Feb 20, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/mighty-carpets-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/dev": patch
---

Vite: Pass `remixUserConfig` to preset `remixConfig` hook
6 changes: 3 additions & 3 deletions docs/future/presets.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ Presets conform to the following `Preset` type:
type Preset = {
name: string;

remixConfig?: () =>
| RemixConfigPreset
| Promise<RemixConfigPreset>;
remixConfig?: (args: {
remixUserConfig: VitePluginConfig;
}) => RemixConfigPreset | Promise<RemixConfigPreset>;

remixConfigResolved?: (args: {
remixConfig: ResolvedVitePluginConfig;
Expand Down
63 changes: 55 additions & 8 deletions integration/vite-presets-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,46 @@ import dedent from "dedent";

import { viteBuild, test, createProject } from "./helpers/vite.js";

const js = String.raw;

const files = {
"vite.config.ts": dedent`
"vite.config.ts": dedent(js`
import { vitePlugin as remix } from "@remix-run/dev";
import fs from "node:fs/promises";
import serializeJs from "serialize-javascript";

let isDeepFrozen = (obj: any) =>
Object.isFrozen(obj) &&
Object.keys(obj).every(
prop => typeof obj[prop] !== 'object' || isDeepFrozen(obj[prop])
);

export default {
plugins: [remix({
presets: [
// Ensure user config is passed to remixConfig hook
{
name: "test-preset",
remixConfig: async ({ remixUserConfig: { presets, ...restUserConfig } }) => {
if (!Array.isArray(presets)) {
throw new Error("Remix user config doesn't have presets array.");
}

let expected = JSON.stringify({ appDirectory: "app"});
let actual = JSON.stringify(restUserConfig);

if (actual !== expected) {
throw new Error([
"Remix user config wasn't passed to remixConfig hook.",
"Expected: " + expected,
"Actual: " + actual,
].join(" "));
}

return {};
},
},

// Ensure preset config takes lower precedence than user config
{
name: "test-preset",
Expand Down Expand Up @@ -46,16 +77,20 @@ const files = {
}),
},

// Ensure remixConfig is called with a frozen Remix user config
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good thing we had a test for a frozen resolved config object. It prompted me to add this behaviour here too.

{
name: "test-preset",
remixConfig: async ({ remixUserConfig }) => {
await fs.writeFile("PRESET_REMIX_CONFIG_META.json", JSON.stringify({
remixUserConfigFrozen: isDeepFrozen(remixUserConfig),
}), "utf-8");
}
},

// Ensure remixConfigResolved is called with a frozen Remix config
{
name: "test-preset",
remixConfigResolved: async ({ remixConfig }) => {
let isDeepFrozen = (obj: any) =>
Object.isFrozen(obj) &&
Object.keys(obj).every(
prop => typeof obj[prop] !== 'object' || isDeepFrozen(obj[prop])
);

await fs.writeFile("PRESET_REMIX_CONFIG_RESOLVED_META.json", JSON.stringify({
remixConfigFrozen: isDeepFrozen(remixConfig),
}), "utf-8");
Expand Down Expand Up @@ -90,7 +125,7 @@ const files = {
appDirectory: "app",
})],
}
`,
`),
};

test("Vite / presets", async () => {
Expand Down Expand Up @@ -124,6 +159,18 @@ test("Vite / presets", async () => {
// Ensure preset config takes lower precedence than user config
expect(remixConfig.serverModuleFormat).toBe("esm");

// Ensure `remixConfig` is called with a frozen Remix user config
expect(
JSON.parse(
await fs.readFile(
path.join(cwd, "PRESET_REMIX_CONFIG_META.json"),
"utf-8"
)
)
).toEqual({
remixUserConfigFrozen: true,
});

// Ensure `remixConfigResolved` is called with a frozen Remix config
expect(
JSON.parse(
Expand Down
9 changes: 7 additions & 2 deletions packages/remix-dev/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ type RemixConfigPreset = Omit<VitePluginConfig, ExcludedRemixConfigPresetKey>;

export type Preset = {
name: string;
remixConfig?: () => RemixConfigPreset | Promise<RemixConfigPreset>;
remixConfig?: (args: {
remixUserConfig: VitePluginConfig;
}) => RemixConfigPreset | Promise<RemixConfigPreset>;
remixConfigResolved?: (args: {
remixConfig: ResolvedVitePluginConfig;
}) => void | Promise<void>;
Expand Down Expand Up @@ -527,6 +529,9 @@ let deepFreeze = (o: any) => {

export type RemixVitePlugin = (config?: VitePluginConfig) => Vite.Plugin[];
export const remixVitePlugin: RemixVitePlugin = (remixUserConfig = {}) => {
// Prevent mutations to the user config
remixUserConfig = deepFreeze(remixUserConfig);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I figured it's safest to freeze the user config up front rather than delaying it.


let viteCommand: Vite.ResolvedConfig["command"];
let viteUserConfig: Vite.UserConfig;
let viteConfigEnv: Vite.ConfigEnv;
Expand Down Expand Up @@ -556,7 +561,7 @@ export const remixVitePlugin: RemixVitePlugin = (remixUserConfig = {}) => {
}

let remixConfigPreset: VitePluginConfig = omit(
await preset.remixConfig(),
await preset.remixConfig({ remixUserConfig }),
excludedRemixConfigPresetKeys
);

Expand Down