Skip to content

Commit

Permalink
feat(remix-dev/vite): pass entire remixConfig to buildEnd (#8595)
Browse files Browse the repository at this point in the history
  • Loading branch information
markdalgleish committed Jan 25, 2024
1 parent e4aab77 commit ec21f5d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 45 deletions.
74 changes: 48 additions & 26 deletions integration/vite-adapter-test.ts
Expand Up @@ -38,7 +38,18 @@ test.describe(async () => {
},
async buildEnd(args) {
let fs = await import("node:fs/promises");
await fs.writeFile("BUILD_END_ARGS.json", JSON.stringify(args, null, 2), "utf-8");
await fs.writeFile(
"BUILD_END_ARGS.json",
JSON.stringify(
args,
function replacer(key, value) {
return typeof value === "function"
? value.toString()
: value;
},
2,
),
"utf-8");
}
}),
Expand Down Expand Up @@ -68,34 +79,45 @@ test.describe(async () => {
// Rewrite path args to be relative and normalized for snapshot test
remixConfig.buildDirectory = relativeToCwd(remixConfig.buildDirectory);

expect(buildEndArgs).toEqual({
remixConfig: {
buildDirectory: "build",
serverBuildFile: "index.js",
unstable_ssr: true,
expect(Object.keys(buildEndArgs)).toEqual(["buildManifest", "remixConfig"]);

// Smoke test the resolved config
expect(Object.keys(buildEndArgs.remixConfig)).toEqual([
"adapter",
"appDirectory",
"buildDirectory",
"future",
"manifest",
"publicPath",
"routes",
"serverBuildFile",
"serverBundles",
"serverModuleFormat",
"unstable_ssr",
]);

// Ensure we get a valid build manifest
expect(buildEndArgs.buildManifest).toEqual({
routeIdToServerBundleId: {
"routes/_index": "user-options--adapter-options",
},
buildManifest: {
routeIdToServerBundleId: {
"routes/_index": "user-options--adapter-options",
routes: {
root: {
file: "app/root.tsx",
id: "root",
path: "",
},
routes: {
root: {
file: "app/root.tsx",
id: "root",
path: "",
},
"routes/_index": {
file: "app/routes/_index.tsx",
id: "routes/_index",
index: true,
parentId: "root",
},
"routes/_index": {
file: "app/routes/_index.tsx",
id: "routes/_index",
index: true,
parentId: "root",
},
serverBundles: {
"user-options--adapter-options": {
file: "build/server/user-options--adapter-options/index.js",
id: "user-options--adapter-options",
},
},
serverBundles: {
"user-options--adapter-options": {
file: "build/server/user-options--adapter-options/index.js",
id: "user-options--adapter-options",
},
},
});
Expand Down
8 changes: 1 addition & 7 deletions packages/remix-dev/vite/build.ts
Expand Up @@ -289,14 +289,8 @@ export async function build(
);
}

let { buildDirectory, serverBuildFile, ssr } = remixConfig;

await remixConfig.adapter?.buildEnd?.({
buildManifest,
remixConfig: {
buildDirectory,
serverBuildFile,
unstable_ssr: ssr,
},
remixConfig,
});
}
20 changes: 8 additions & 12 deletions packages/remix-dev/vite/plugin.ts
Expand Up @@ -175,10 +175,7 @@ export type VitePluginConfig = RemixEsbuildUserConfigJsdocOverrides &
};

type BuildEndHook = (args: {
remixConfig: Pick<
ResolvedVitePluginConfig,
"buildDirectory" | "serverBuildFile"
> & { unstable_ssr: boolean };
remixConfig: ResolvedVitePluginConfig;
buildManifest: BuildManifest | undefined;
}) => void | Promise<void>;

Expand All @@ -191,7 +188,7 @@ export type ResolvedVitePluginConfig = Pick<
manifest: boolean;
serverBuildFile: string;
serverBundles?: ServerBundlesFunction;
ssr: boolean;
unstable_ssr: boolean;
};

export type ServerBundleBuildConfig = {
Expand Down Expand Up @@ -473,9 +470,8 @@ export const remixVitePlugin: RemixVitePlugin = (remixUserConfig = {}) => {
let rootDirectory =
viteUserConfig.root ?? process.env.REMIX_ROOT ?? process.cwd();

let { manifest, unstable_ssr: ssr } = resolvedRemixUserConfig;

let isSpaMode = !ssr;
let { manifest, unstable_ssr } = resolvedRemixUserConfig;
let isSpaMode = !unstable_ssr;

// Only select the Remix esbuild config options that the Vite plugin uses
let {
Expand Down Expand Up @@ -531,7 +527,7 @@ export const remixVitePlugin: RemixVitePlugin = (remixUserConfig = {}) => {
serverBuildFile,
serverBundles,
serverModuleFormat,
ssr,
unstable_ssr,
};

let serverContext: RemixPluginServerContext =
Expand Down Expand Up @@ -579,7 +575,7 @@ export const remixVitePlugin: RemixVitePlugin = (remixUserConfig = {}) => {
)
)};
export const future = ${JSON.stringify(ctx.remixConfig.future)};
export const isSpaMode = ${!ctx.remixConfig.ssr};
export const isSpaMode = ${!ctx.remixConfig.unstable_ssr};
export const publicPath = ${JSON.stringify(ctx.remixConfig.publicPath)};
export const entry = { module: entryServer };
export const routes = {
Expand Down Expand Up @@ -1093,7 +1089,7 @@ export const remixVitePlugin: RemixVitePlugin = (remixUserConfig = {}) => {
);
}

if (!ctx.remixConfig.ssr) {
if (!ctx.remixConfig.unstable_ssr) {
await handleSpaMode(
serverBuildDirectory,
ctx.remixConfig.serverBuildFile,
Expand Down Expand Up @@ -1234,7 +1230,7 @@ export const remixVitePlugin: RemixVitePlugin = (remixUserConfig = {}) => {
let route = getRoute(ctx.remixConfig, id);
if (!route) return;

if (!ctx.remixConfig.ssr) {
if (!ctx.remixConfig.unstable_ssr) {
let serverOnlyExports = esModuleLexer(code)[1]
.map((exp) => exp.n)
.filter((exp) => SERVER_ONLY_ROUTE_EXPORTS.includes(exp));
Expand Down

0 comments on commit ec21f5d

Please sign in to comment.