Skip to content

Commit

Permalink
feat: support TS/JSX inside node_modules
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBarre committed Mar 27, 2023
1 parent 6be7e50 commit 4b9b2d5
Show file tree
Hide file tree
Showing 15 changed files with 321 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
node_modules/
dist/
playground-temp/
test-results/
.swc/
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Support TS/JSX in node_modules to help the community experiment with it. Note that for now this not supported by TS and errors from these files cannot be silenced if the user is using a stricter configuration than the library author: https://github.com/microsoft/TypeScript/issues/30511. I advise to use it only for internal libraries for now (fixes #53)

## 3.2.0

- Support HMR for MDX (fixes #52)
Expand Down
24 changes: 24 additions & 0 deletions playground/ts-lib/__tests__/ts-lib.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { expect, test } from "@playwright/test";
import { setupDevServer, setupBuildAndPreview } from "../../utils.ts";

test("TS lib build", async ({ page }) => {
const { testUrl, server } = await setupBuildAndPreview("ts-lib");
await page.goto(testUrl);
await expect(page.locator("main")).toHaveText("Home page");

await page.locator("a", { hasText: "About" }).click();
await expect(page.locator("main")).toHaveText("About page");

await server.httpServer.close();
});

test("TS lib dev", async ({ page }) => {
const { testUrl, server } = await setupDevServer("ts-lib");
await page.goto(testUrl);
await expect(page.locator("main")).toHaveText("Home page");

await page.locator("a", { hasText: "About" }).click();
await expect(page.locator("main")).toHaveText("About page");

await server.close();
});
13 changes: 13 additions & 0 deletions playground/ts-lib/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + TS lib</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/index.tsx"></script>
</body>
</html>
22 changes: 22 additions & 0 deletions playground/ts-lib/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "playground-ts-lib",
"type": "module",
"private": true,
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"@generouted/react-router": "^1.11.7",
"generouted": "^1.11.7",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.9.0"
},
"devDependencies": {
"@types/react": "^18.0.29",
"@types/react-dom": "^18.0.11",
"@vitejs/plugin-react-swc": "../../dist"
}
}
1 change: 1 addition & 0 deletions playground/ts-lib/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions playground/ts-lib/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import { Routes } from "generouted/react-router";

createRoot(document.getElementById("root")!).render(
<StrictMode>
<Routes />
</StrictMode>,
);
3 changes: 3 additions & 0 deletions playground/ts-lib/src/pages/404.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function NotFound() {
return <h1>404</h1>
}
16 changes: 16 additions & 0 deletions playground/ts-lib/src/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Link, Outlet } from "react-router-dom";

export default function App() {
return (
<section style={{ margin: 24 }}>
<header style={{ display: "flex", gap: 24 }}>
<Link to="/">Home</Link>
<Link to="/about">About</Link>
</header>

<main>
<Outlet />
</main>
</section>
);
}
3 changes: 3 additions & 0 deletions playground/ts-lib/src/pages/about.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function About() {
return <h1>About page</h1>;
}
10 changes: 10 additions & 0 deletions playground/ts-lib/src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Outlet } from "react-router-dom";

export default function Home() {
return (
<div>
<h1>Home page</h1>
<Outlet />
</div>
);
}
25 changes: 25 additions & 0 deletions playground/ts-lib/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"include": ["src", "vite.config.ts"],
"compilerOptions": {
"module": "ESNext",
"lib": ["ESNext", "DOM", "DOM.Iterable"],
"target": "ESNext",
"jsx": "react-jsx",
"types": ["vite/client"],
"noEmit": true,
"isolatedModules": true,
"skipLibCheck": true,
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"noUncheckedIndexedAccess": true,
"noPropertyAccessFromIndexSignature": true,

/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"useUnknownInCatchVariables": true
}
}
7 changes: 7 additions & 0 deletions playground/ts-lib/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react-swc";

export default defineConfig({
optimizeDeps: { include: ["react-router-dom"] },
plugins: [react()],
});
Loading

0 comments on commit 4b9b2d5

Please sign in to comment.