-
Notifications
You must be signed in to change notification settings - Fork 327
fix(plugins): resolve optimized imports past local barrels #847
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
Open
llc1123
wants to merge
13
commits into
cloudflare:main
Choose a base branch
from
llc1123:fix/issue-845-optimize-imports
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
012c532
fix(plugins): resolve optimized imports past local barrels
llc1123 e8079c8
test: cover antd App Router build regression
llc1123 e9fe4c3
fix(plugins): parse JSX before optimize-import rewriting
llc1123 68e0eff
test: cover JSX-bearing optimize-import transforms
llc1123 34ef836
test: strengthen optimize-imports build assertions
llc1123 1c2d1ee
fix(plugins): avoid phantom fallback exports
llc1123 da5fd92
fix(plugins): guard export-map reentry during barrel analysis
llc1123 9caf7fe
test: split optimize-imports transform coverage
llc1123 2394156
test: split optimize-imports export-map basics
llc1123 5109d7b
test: split optimize-imports export-map recursion cases
llc1123 a57ab08
test: add local fixture for client optimize-imports path
llc1123 15f8428
fix(plugins): restore optimize-imports sourcemaps
llc1123 277502c
test: isolate optimize-imports build fixture writes
llc1123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| import { mkdtemp, rm } from "node:fs/promises"; | ||
| import fs from "node:fs"; | ||
| import os from "node:os"; | ||
| import path from "node:path"; | ||
| import { createBuilder } from "vite"; | ||
| import { describe, expect, it } from "vite-plus/test"; | ||
| import vinext from "../packages/vinext/src/index.js"; | ||
|
|
||
| function symlinkWorkspacePackage(root: string, packageName: string) { | ||
| const source = path.resolve(import.meta.dirname, "../node_modules", packageName); | ||
| const target = path.join(root, "node_modules", packageName); | ||
| fs.mkdirSync(path.dirname(target), { recursive: true }); | ||
| fs.symlinkSync(source, target, "junction"); | ||
| } | ||
|
|
||
| async function withTempDir<T>(prefix: string, run: (tmpDir: string) => Promise<T>): Promise<T> { | ||
| const tmpDir = await mkdtemp(path.join(os.tmpdir(), prefix)); | ||
| fs.mkdirSync(path.join(tmpDir, "node_modules"), { recursive: true }); | ||
| symlinkWorkspacePackage(tmpDir, "react"); | ||
| symlinkWorkspacePackage(tmpDir, "react-dom"); | ||
| symlinkWorkspacePackage(tmpDir, "react-server-dom-webpack"); | ||
| try { | ||
| return await run(tmpDir); | ||
| } finally { | ||
| await rm(tmpDir, { recursive: true, force: true }); | ||
| } | ||
| } | ||
|
|
||
| function writeFixtureFile(root: string, filePath: string, content: string) { | ||
| const absPath = path.join(root, filePath); | ||
| fs.mkdirSync(path.dirname(absPath), { recursive: true }); | ||
| fs.writeFileSync(absPath, content); | ||
| } | ||
|
|
||
| function readTextFilesRecursive(root: string): string { | ||
| let output = ""; | ||
| for (const entry of fs.readdirSync(root, { withFileTypes: true })) { | ||
| const entryPath = path.join(root, entry.name); | ||
| if (entry.isDirectory()) { | ||
| output += readTextFilesRecursive(entryPath); | ||
| continue; | ||
| } | ||
| if (!entry.name.endsWith(".js")) continue; | ||
| output += fs.readFileSync(entryPath, "utf-8"); | ||
| } | ||
| return output; | ||
| } | ||
|
|
||
| async function buildApp(root: string) { | ||
| const rscOutDir = path.join(root, "dist", "server"); | ||
| const ssrOutDir = path.join(root, "dist", "server", "ssr"); | ||
| const clientOutDir = path.join(root, "dist", "client"); | ||
|
|
||
| const builder = await createBuilder({ | ||
| root, | ||
| configFile: false, | ||
| plugins: [vinext({ appDir: root, rscOutDir, ssrOutDir, clientOutDir })], | ||
| logLevel: "silent", | ||
| }); | ||
|
|
||
| await builder.buildApp(); | ||
| } | ||
|
|
||
| describe("optimizePackageImports production builds", () => { | ||
| it("builds an App Router app when an optimized antd barrel resolves through a use-client export-star boundary", async () => { | ||
| // issue-845 repro scaffold and package pins are recorded in: | ||
| // .sisyphus/evidence/task-1-parity-matrix.md | ||
| await withTempDir("vinext-optimize-imports-build-", async (root) => { | ||
| writeFixtureFile( | ||
| root, | ||
| "package.json", | ||
| JSON.stringify( | ||
| { name: "vinext-optimize-imports-build", private: true, type: "module" }, | ||
| null, | ||
| 2, | ||
| ), | ||
| ); | ||
| writeFixtureFile( | ||
| root, | ||
| "tsconfig.json", | ||
| JSON.stringify( | ||
| { | ||
| compilerOptions: { | ||
| target: "ES2022", | ||
| module: "ESNext", | ||
| moduleResolution: "bundler", | ||
| jsx: "react-jsx", | ||
| strict: true, | ||
| skipLibCheck: true, | ||
| types: ["vite/client", "@vitejs/plugin-rsc/types"], | ||
| }, | ||
| include: ["app", "*.ts", "*.tsx"], | ||
| }, | ||
| null, | ||
| 2, | ||
| ), | ||
| ); | ||
|
|
||
| writeFixtureFile( | ||
| root, | ||
| "app/layout.tsx", | ||
| `import type { ReactNode } from "react"; | ||
|
|
||
| export default function RootLayout({ children }: { children: ReactNode }) { | ||
| return ( | ||
| <html lang="en"> | ||
| <body>{children}</body> | ||
| </html> | ||
| ); | ||
| } | ||
| `, | ||
| ); | ||
| writeFixtureFile( | ||
| root, | ||
| "app/page.tsx", | ||
| `import AntdDemo from "./components/AntdDemo"; | ||
|
|
||
| export default function HomePage() { | ||
| return <AntdDemo />; | ||
| } | ||
| `, | ||
| ); | ||
| writeFixtureFile( | ||
| root, | ||
| "app/components/AntdDemo.tsx", | ||
| `"use client"; | ||
|
|
||
| import { Button } from "antd"; | ||
|
|
||
| export default function AntdDemo() { | ||
| return <Button />; | ||
| } | ||
| `, | ||
| ); | ||
|
|
||
| writeFixtureFile( | ||
| root, | ||
| "node_modules/antd/package.json", | ||
| JSON.stringify( | ||
| { | ||
| name: "antd", | ||
| version: "6.3.5", | ||
| type: "module", | ||
| main: "./index.js", | ||
| }, | ||
| null, | ||
| 2, | ||
| ), | ||
| ); | ||
| writeFixtureFile( | ||
| root, | ||
| "node_modules/antd/index.js", | ||
| `export { Button } from "./es/button/index.js"; | ||
| `, | ||
| ); | ||
| writeFixtureFile( | ||
| root, | ||
| "node_modules/antd/es/button/index.js", | ||
| `"use client"; | ||
|
|
||
| export * from "./button.js"; | ||
| export { default } from "./button.js"; | ||
| `, | ||
| ); | ||
| writeFixtureFile( | ||
| root, | ||
| "node_modules/antd/es/button/button.js", | ||
| `export function Button() { | ||
| return null; | ||
| } | ||
|
|
||
| export default Button; | ||
| `, | ||
| ); | ||
|
|
||
| await buildApp(root); | ||
|
|
||
| expect(fs.existsSync(path.join(root, "dist", "server", "index.js"))).toBe(true); | ||
|
llc1123 marked this conversation as resolved.
|
||
| expect(fs.existsSync(path.join(root, "dist", "server", "ssr", "index.js"))).toBe(true); | ||
| expect(fs.existsSync(path.join(root, "dist", "client"))).toBe(true); | ||
|
|
||
| const buildOutput = readTextFilesRecursive(path.join(root, "dist")); | ||
| expect(buildOutput).not.toContain(`from "antd"`); | ||
| expect(buildOutput).not.toContain("/es/button/index.js"); | ||
| expect(buildOutput).toContain("function Button"); | ||
| }); | ||
| }, 60_000); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { describe, it, expect } from "vite-plus/test"; | ||
| import { buildBarrelExportMap } from "../packages/vinext/src/plugins/optimize-imports.js"; | ||
|
|
||
| let testId = 0; | ||
| function uniquePath(name: string): string { | ||
| return `/fake/${name}-${++testId}/entry.js`; | ||
| } | ||
|
|
||
| describe("buildBarrelExportMap binding re-export cases", () => { | ||
| it("handles import * as X; export { X }", async () => { | ||
| const entryPath = uniquePath("import-ns-reexport"); | ||
| const barrelCode = `import * as AlertDialog from "@radix-ui/react-alert-dialog";\nexport { AlertDialog };`; | ||
| const map = await buildBarrelExportMap( | ||
| "test-pkg", | ||
| () => entryPath, | ||
| () => Promise.resolve(barrelCode), | ||
| ); | ||
| expect(map).not.toBeNull(); | ||
| expect(map!.get("AlertDialog")).toEqual({ | ||
| source: "@radix-ui/react-alert-dialog", | ||
| isNamespace: true, | ||
| }); | ||
| }); | ||
|
|
||
| it("handles import { X }; export { X }", async () => { | ||
| const entryPath = uniquePath("import-named-reexport"); | ||
| const barrelCode = `import { format } from "date-fns/format";\nexport { format };`; | ||
| const map = await buildBarrelExportMap( | ||
| "test-pkg", | ||
| () => entryPath, | ||
| () => Promise.resolve(barrelCode), | ||
| ); | ||
| expect(map).not.toBeNull(); | ||
| expect(map!.get("format")).toEqual({ | ||
| source: "date-fns/format", | ||
| isNamespace: false, | ||
| originalName: "format", | ||
| }); | ||
| }); | ||
|
|
||
| it("handles export { Local as Public } for same-file declarations", async () => { | ||
| const entryPath = uniquePath("local-alias-reexport"); | ||
| const barrelCode = `const Mo = {};\nexport { Mo as Listbox };`; | ||
| const map = await buildBarrelExportMap( | ||
| "test-pkg", | ||
| () => entryPath, | ||
| () => Promise.resolve(barrelCode), | ||
| ); | ||
| expect(map).not.toBeNull(); | ||
| expect(map!.get("Listbox")).toEqual({ | ||
| source: entryPath, | ||
| isNamespace: false, | ||
| originalName: "Listbox", | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { describe, it, expect } from "vite-plus/test"; | ||
| import { | ||
| buildBarrelExportMap, | ||
| DEFAULT_OPTIMIZE_PACKAGES, | ||
| } from "../packages/vinext/src/plugins/optimize-imports.js"; | ||
|
|
||
| let testId = 0; | ||
| function uniquePath(name: string): string { | ||
| return `/fake/${name}-${++testId}/entry.js`; | ||
| } | ||
|
|
||
| describe("buildBarrelExportMap failure and baseline cases", () => { | ||
| it("returns null when entry cannot be resolved", async () => { | ||
| const map = await buildBarrelExportMap( | ||
| "nonexistent-pkg", | ||
| () => null, | ||
| () => Promise.resolve(null), | ||
| ); | ||
| expect(map).toBeNull(); | ||
| }); | ||
|
|
||
| it("returns null when entry file cannot be read", async () => { | ||
| const entryPath = uniquePath("unreadable"); | ||
| const map = await buildBarrelExportMap( | ||
| "test-pkg", | ||
| () => entryPath, | ||
| () => Promise.resolve(null), | ||
| ); | ||
| expect(map).toBeNull(); | ||
| }); | ||
|
|
||
| it("returns an empty map when entry file has syntax errors", async () => { | ||
| const entryPath = uniquePath("syntax-error"); | ||
| const map = await buildBarrelExportMap( | ||
| "test-pkg", | ||
| () => entryPath, | ||
| () => Promise.resolve("export { unclosed"), | ||
| ); | ||
| expect(map).not.toBeNull(); | ||
| expect(map!.size).toBe(0); | ||
| }); | ||
|
|
||
| it("DEFAULT_OPTIMIZE_PACKAGES includes expected packages", () => { | ||
| expect(DEFAULT_OPTIMIZE_PACKAGES).toContain("lucide-react"); | ||
| expect(DEFAULT_OPTIMIZE_PACKAGES).toContain("radix-ui"); | ||
| expect(DEFAULT_OPTIMIZE_PACKAGES).toContain("antd"); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
readTextFilesRecursivefollows symlinks (thereact/react-domsymlinks created bysymlinkWorkspacePackage) and would recursively read all.jsfiles under the realreactpackage if they end up in thedist/tree. In practice this is fine because the build output won't contain symlinks to workspace packages in the output dir, but if the build ever emits a symlink (e.g., Vite preserves symlinks in some configurations), this could produce unexpectedly largebuildOutputstrings and slow assertions.Not blocking — the current build test works correctly.