Skip to content
Open
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
44 changes: 43 additions & 1 deletion integration/split-route-modules-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
reactRouterServe,
viteConfig,
reactRouterConfig,
viteMajorTemplates,
} from "./helpers/vite.js";

const js = String.raw;
Expand Down Expand Up @@ -272,7 +273,9 @@ test.describe("Split route modules", async () => {

let { status, stderr } = build({ cwd });
expect(status).toBe(0);
expect(stderr.toString()).not.toContain("SOURCEMAP_BROKEN");
expect(stderr.toString()).not.toContain(
"Sourcemap is likely to be incorrect",
);

let chunkPath = globSync(path.join(cwd, "build/client/assets/*.js")).find(
(file) => readFileSync(file, "utf8").includes(marker),
Expand Down Expand Up @@ -309,6 +312,45 @@ test.describe("Split route modules", async () => {
expect(entry.originalLine).toBe(originalLine);
});

viteMajorTemplates.forEach(({ templateName, templateDisplayName }) => {
test(`${templateDisplayName} / declares source maps for placeholder chunks of exports that can't be split`, async () => {
let cwd = await createProject(
{
"react-router.config.ts": reactRouterConfig({
splitRouteModules: true,
}),
"vite.config.js": await viteConfig.basic({ sourcemap: true }),
"app/routes/_index.tsx": js`
let shared = 0;

export async function clientLoader() {
shared++;
return shared;
}

export default function Index() {
return <h1>{shared}</h1>;
}
`,
},
templateName,
);

let { status, stderr } = build({ cwd });
expect(status).toBe(0);
expect(stderr.toString()).not.toContain(
"Sourcemap is likely to be incorrect",
);

let placeholder = globSync(
path.join(cwd, "build/client/assets/*.js"),
).find((file) =>
readFileSync(file, "utf8").includes("No clientLoader chunk"),
);
expect(placeholder).toBeDefined();
});
});

test.describe("enabled", () => {
let port: number;
let cwd: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Declare an empty source map for the placeholder chunk of a route export that can't be split, so Rollup no longer warns that `react-router:split-route-modules` dropped the source map
13 changes: 11 additions & 2 deletions packages/react-router-dev/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2145,8 +2145,17 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
code,
);

let preventEmptyChunkSnippet = ({ reason }: { reason: string }) =>
`Math.random()<0&&console.log(${JSON.stringify(reason)});`;
let preventEmptyChunkSnippet = ({
reason,
}: {
reason: string;
}): Vite.Rollup.SourceDescription => ({
code: `Math.random()<0&&console.log(${JSON.stringify(reason)});`,
// The placeholder is generated, so there's no original code to map
// back to. An empty map says so rather than leaving Rollup to warn
// that the transform dropped the source map.
map: { mappings: "" },
});

if (chunk === null) {
return preventEmptyChunkSnippet({
Expand Down