-
Notifications
You must be signed in to change notification settings - Fork 327
feat: support vite8 resolve.tsconfigPaths #494
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import { afterEach, describe, expect, it } from "vitest"; | ||
| import fs from "node:fs"; | ||
| import os from "node:os"; | ||
| import path from "node:path"; | ||
| import type { Plugin, PluginOption } from "vite"; | ||
| import vinext from "../packages/vinext/src/index.js"; | ||
|
|
||
| const originalCwd = process.cwd(); | ||
|
|
||
| function setupProject(viteVersion: string): string { | ||
| const root = fs.mkdtempSync(path.join(os.tmpdir(), "vinext-vite-major-")); | ||
| fs.mkdirSync(path.join(root, "pages"), { recursive: true }); | ||
| fs.mkdirSync(path.join(root, "node_modules", "vite"), { recursive: true }); | ||
| fs.writeFileSync( | ||
| path.join(root, "package.json"), | ||
| JSON.stringify({ name: "test-project", version: "1.0.0" }, null, 2), | ||
| ); | ||
| fs.writeFileSync( | ||
| path.join(root, "node_modules", "vite", "package.json"), | ||
| JSON.stringify({ name: "vite", version: viteVersion }, null, 2), | ||
| ); | ||
| fs.writeFileSync( | ||
| path.join(root, "pages", "index.tsx"), | ||
| "export default function Page() { return <div>hello</div>; }\n", | ||
| ); | ||
| return root; | ||
| } | ||
|
|
||
| function isPlugin(plugin: PluginOption): plugin is Plugin { | ||
| return !!plugin && !Array.isArray(plugin) && typeof plugin === "object" && "name" in plugin; | ||
| } | ||
|
|
||
| function findNamedPlugin(plugins: ReturnType<typeof vinext>, name: string) { | ||
| return plugins.find((plugin): plugin is Plugin => isPlugin(plugin) && plugin.name === name); | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| process.chdir(originalCwd); | ||
| }); | ||
|
|
||
| describe("Vite tsconfig paths support", () => { | ||
| it("keeps vite-tsconfig-paths on Vite 7", async () => { | ||
| const root = setupProject("7.3.1"); | ||
| process.chdir(root); | ||
|
|
||
| const plugins = vinext({ appDir: root }); | ||
|
|
||
| expect(findNamedPlugin(plugins, "vite-tsconfig-paths")).toBeDefined(); | ||
|
|
||
| fs.rmSync(root, { recursive: true, force: true }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: The temp directory cleanup ( Non-blocking — the leaked dirs are in |
||
| }); | ||
|
|
||
| it("uses resolve.tsconfigPaths on Vite 8 instead of vite-tsconfig-paths", async () => { | ||
| const root = setupProject("8.0.0-beta.18"); | ||
| process.chdir(root); | ||
|
|
||
| const plugins = vinext({ appDir: root }); | ||
|
|
||
| expect(findNamedPlugin(plugins, "vite-tsconfig-paths")).toBeUndefined(); | ||
|
|
||
| const configPlugin = findNamedPlugin(plugins, "vinext:config") as { | ||
| config?: ( | ||
| config: { root: string }, | ||
| env: { command: "serve"; mode: string }, | ||
| ) => Promise<{ | ||
| resolve?: Record<string, unknown>; | ||
| }>; | ||
| }; | ||
| const resolvedConfig = await configPlugin.config?.( | ||
| { root }, | ||
| { command: "serve", mode: "development" }, | ||
| ); | ||
|
|
||
| expect(resolvedConfig?.resolve?.tsconfigPaths).toBe(true); | ||
|
|
||
| fs.rmSync(root, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| it("does not override user-defined resolve.tsconfigPaths on Vite 8", async () => { | ||
| const root = setupProject("8.0.0-beta.18"); | ||
| process.chdir(root); | ||
|
|
||
| const plugins = vinext({ appDir: root }); | ||
| const configPlugin = findNamedPlugin(plugins, "vinext:config") as { | ||
| config?: ( | ||
| config: { root: string; resolve?: Record<string, unknown> }, | ||
| env: { command: "serve"; mode: string }, | ||
| ) => Promise<{ | ||
| resolve?: Record<string, unknown>; | ||
| }>; | ||
| }; | ||
| const resolvedConfig = await configPlugin.config?.( | ||
| { root, resolve: { tsconfigPaths: false } }, | ||
| { command: "serve", mode: "development" }, | ||
| ); | ||
|
|
||
| expect(resolvedConfig?.resolve?.tsconfigPaths).toBeUndefined(); | ||
|
|
||
| fs.rmSync(root, { recursive: true, force: true }); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Once Vite 8 ships a stable release with
tsconfigPathsin its publicUserConfig["resolve"]type, this custom type can be removed and you can use Vite's type directly. Worth a TODO comment so it doesn't get forgotten.Non-blocking.