Skip to content
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "react-router-devtools",
"description": "Devtools for React Router - debug, trace, find hydration errors, catch bugs and inspect server/client data with react-router-devtools",
"author": "Alem Tuzlak",
"version": "1.1.9",
"version": "1.1.10",
"license": "MIT",
"keywords": [
"react-router",
Expand Down
50 changes: 50 additions & 0 deletions src/vite/utils/inject-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,56 @@ describe("transform", () => {
expect(removeWhitespace(result.code)).toStrictEqual(expected)
})

it("should wrap the default export properly even if it's declared as a variable and then exported via export { name as default }", () => {
const result = injectRdtClient(
`
const App = () => {};
export { App as default };
`,
'{ "config": { }, "plugins": "[]" }',
"",
"/file/path"
)
const expected = removeWhitespace(`
import { withViteDevTools as _withViteDevTools } from "react-router-devtools/client";
import rdtStylesheet from "react-router-devtools/client.css?url";
const App = () => {};
export default _withViteDevTools(App, {
config: { },
plugins: []
});
export const links = () => [{ rel: "stylesheet", href: rdtStylesheet }];
`)
expect(removeWhitespace(result.code)).toStrictEqual(expected)
})

it("should wrap the default export properly even if it's declared as a variable and then exported via export { name as default } and has other exports as well", () => {
const result = injectRdtClient(
`
import { test } from "./file/path";
const App = () => {};
export { App as default, test };
`,
'{ "config": { }, "plugins": "[]" }',
"",
"/file/path"
)
const expected = removeWhitespace(`
import { withViteDevTools as _withViteDevTools } from "react-router-devtools/client";
import rdtStylesheet from "react-router-devtools/client.css?url";
import { test } from "./file/path";
const App = () => {};
export default _withViteDevTools(App, {
config: { },
plugins: []
});
export { test };
export const links = () => [{ rel: "stylesheet", href: rdtStylesheet }];

`)
expect(removeWhitespace(result.code)).toStrictEqual(expected)
})

it("should wrap the default export properly even if it's declared as a function and then exported", () => {
const result = injectRdtClient(
`
Expand Down
36 changes: 35 additions & 1 deletion src/vite/utils/inject-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const transform = (ast: ParseResult<Babel.File>, clientConfig: string) => {
function uppercaseFirstLetter(str: string) {
return str.charAt(0).toUpperCase() + str.slice(1)
}
const transformations: Array<() => void> = []
trav(ast, {
ExportDeclaration(path) {
if (path.isExportDefaultDeclaration()) {
Expand Down Expand Up @@ -83,10 +84,43 @@ const transform = (ast: ParseResult<Babel.File>, clientConfig: string) => {
])
)
}

// Handle `export { App as default };`
const specifiers = path.node.specifiers
for (const specifier of specifiers) {
if (
t.isExportSpecifier(specifier) &&
t.isIdentifier(specifier.exported) &&
specifier.exported.name === "default" &&
t.isIdentifier(specifier.local)
) {
const localName = specifier.local.name
const uid = getHocUid(path, "withViteDevTools")

// Insert the wrapped default export
transformations.push(() => {
path.insertBefore(
t.exportDefaultDeclaration(t.callExpression(uid, [t.identifier(localName), clientConfigExpression]))
)

// Remove the original export specifier
const remainingSpecifiers = path.node.specifiers.filter(
(s) => !(t.isExportSpecifier(s) && t.isIdentifier(s.exported) && s.exported.name === "default")
)
if (remainingSpecifiers.length > 0) {
path.replaceWith(t.exportNamedDeclaration(null, remainingSpecifiers, path.node.source))
} else {
path.remove()
}
})
}
}
}
},
})

for (const transformation of transformations) {
transformation()
}
if (hocs.length > 0) {
ast.program.body.unshift(
t.importDeclaration(
Expand Down
4 changes: 3 additions & 1 deletion test-apps/react-router-vite/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const action =async ({devTools}: ActionFunctionArgs) => {
return ({ message: "Hello World", bigInt: BigInt(10) });
}

export default function App() {
function App() {
return (
<html lang="en">
<head>
Expand All @@ -66,3 +66,5 @@ export default function App() {
</html>
);
}

export { App as default }
Loading