mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-20 18:57:17 +03:00
fix(ui): list types before import in packed exports and export select.css (#62914)
This commit is contained in:
committed by
GitHub
Unverified
parent
d592629d1c
commit
33ba535c9f
@@ -11,13 +11,13 @@ export const pack = async () => {
|
||||
// oxlint-disable-next-line typescript-eslint/no-unsafe-type-assertion -- package.json is validated by the package schema and build checks.
|
||||
const pkg = JSON.parse(original) as {
|
||||
readonly version: string
|
||||
exports: Record<string, string | { readonly import: string; readonly types: string }>
|
||||
exports: Record<string, string | { readonly types: string; readonly import: string }>
|
||||
}
|
||||
|
||||
for (const [key, value] of Object.entries(pkg.exports)) {
|
||||
if (typeof value !== "string") continue
|
||||
const file = value.replace("./src/", "./dist/").replace(/\.ts$/, "")
|
||||
pkg.exports[key] = { import: `${file}.js`, types: `${file}.d.ts` }
|
||||
pkg.exports[key] = { types: `${file}.d.ts`, import: `${file}.js` }
|
||||
}
|
||||
await Bun.write("package.json", JSON.stringify(pkg, null, 2))
|
||||
try {
|
||||
|
||||
@@ -42,7 +42,8 @@ export async function pack(
|
||||
|
||||
if (!/\.tsx?$/.test(value)) return [key, value]
|
||||
|
||||
return [key, { import: options.source ? value : output(value), types: declaration(value) }]
|
||||
// TypeScript takes the first condition it matches, so `types` goes first or it type-checks the `import` target.
|
||||
return [key, { types: declaration(value), import: options.source ? value : output(value) }]
|
||||
}),
|
||||
)
|
||||
const imports = pkg.imports
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
import { $ } from "bun"
|
||||
import { afterEach, describe, expect, test } from "bun:test"
|
||||
import { mkdtemp, rm } from "node:fs/promises"
|
||||
import { tmpdir } from "node:os"
|
||||
import path from "node:path"
|
||||
import { pack } from "../src/pack"
|
||||
|
||||
const cleanup: string[] = []
|
||||
|
||||
afterEach(async () => {
|
||||
await Promise.all(cleanup.splice(0).map((file) => rm(file, { recursive: true, force: true })))
|
||||
})
|
||||
|
||||
describe("pack", () => {
|
||||
test("lists each TypeScript export's types before its import", async () => {
|
||||
const directory = await mkdtemp(path.join(tmpdir(), "opencode-pack-"))
|
||||
cleanup.push(directory)
|
||||
await Bun.write(path.join(directory, "src/button.tsx"), "export const Button = 1\n")
|
||||
await Bun.write(path.join(directory, "src/button.css"), "button {}\n")
|
||||
await Bun.write(
|
||||
path.join(directory, "package.json"),
|
||||
JSON.stringify({
|
||||
name: "@opencode/pack-fixture",
|
||||
version: "0.0.0-test",
|
||||
type: "module",
|
||||
scripts: { build: "true" },
|
||||
files: ["src"],
|
||||
exports: { "./button": "./src/button.tsx", "./button.css": "./src/button.css" },
|
||||
}),
|
||||
)
|
||||
|
||||
const tarball = await pack(directory, { source: true })
|
||||
cleanup.push(tarball)
|
||||
// A relative name, since Windows' GNU tar reads the `C:` in an absolute path as a remote host.
|
||||
const manifest = JSON.parse(
|
||||
await $`tar -xOzf ${path.basename(tarball)} package/package.json`.cwd(path.dirname(tarball)).text(),
|
||||
)
|
||||
|
||||
expect(manifest.exports["./button"]).toEqual({ types: "./dist/button.d.ts", import: "./src/button.tsx" })
|
||||
expect(Object.keys(manifest.exports["./button"])).toEqual(["types", "import"])
|
||||
expect(manifest.exports["./button.css"]).toBe("./src/button.css")
|
||||
})
|
||||
})
|
||||
@@ -33,7 +33,7 @@ try {
|
||||
const pkg = JSON.parse(original) as {
|
||||
name: string
|
||||
dependencies?: Record<string, string>
|
||||
exports?: Record<string, string | { import: string; types: string }>
|
||||
exports?: Record<string, string | { types: string; import: string }>
|
||||
imports?: Record<string, Record<string, string>>
|
||||
}
|
||||
const archive = join(temporary, `${name}.tgz`)
|
||||
@@ -55,7 +55,7 @@ try {
|
||||
pkg.exports = Object.fromEntries(
|
||||
Object.entries(pkg.exports).map(([key, value]) => {
|
||||
if (typeof value !== "string") return [key, value]
|
||||
return [key, { import: output(name, value), types: output(name, value, true) }]
|
||||
return [key, { types: output(name, value, true), import: output(name, value) }]
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -70,6 +70,7 @@
|
||||
"./search-list": "./src/forms/search-list/search-list.tsx",
|
||||
"./segmented-control": "./src/navigation/segmented-control/segmented-control.tsx",
|
||||
"./select": "./src/forms/select/select.tsx",
|
||||
"./select.css": "./src/forms/select/select.css",
|
||||
"./split-button": "./src/actions/split-button/split-button.tsx",
|
||||
"./switch": "./src/forms/switch/switch.tsx",
|
||||
"./tab-state-indicator": "./src/navigation/tab-state-indicator.tsx",
|
||||
|
||||
Reference in New Issue
Block a user