From bd8721d56fbc9a9e403e100c41870489f7c1fe8d Mon Sep 17 00:00:00 2001 From: eunwoo song Date: Mon, 7 Sep 2026 05:37:30 +0900 Subject: [PATCH] fix(csv-stringify): isolate browser types from Node --- packages/csv-stringify/dist/esm/index.d.ts | 2 -- packages/csv-stringify/package.json | 5 +++-- .../csv-stringify/scripts/postbuild-types.js | 17 +++++++++++++++++ .../csv-stringify/test/browser-types/index.ts | 6 ++++++ .../test/browser-types/tsconfig.json | 13 +++++++++++++ packages/csv-stringify/tsconfig.json | 3 ++- 6 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 packages/csv-stringify/scripts/postbuild-types.js create mode 100644 packages/csv-stringify/test/browser-types/index.ts create mode 100644 packages/csv-stringify/test/browser-types/tsconfig.json diff --git a/packages/csv-stringify/dist/esm/index.d.ts b/packages/csv-stringify/dist/esm/index.d.ts index b1209700..db0d19f3 100644 --- a/packages/csv-stringify/dist/esm/index.d.ts +++ b/packages/csv-stringify/dist/esm/index.d.ts @@ -1,5 +1,3 @@ -/// - import * as stream from "stream"; export type Callback = (err: Error | undefined, output: string) => void; diff --git a/packages/csv-stringify/package.json b/packages/csv-stringify/package.json index f41fe07d..2bea470c 100644 --- a/packages/csv-stringify/package.json +++ b/packages/csv-stringify/package.json @@ -80,12 +80,13 @@ "build": "npm run build:rollup && npm run build:ts", "build:rollup": "npx rollup -c", "build:ts": "cp lib/index.d.ts dist/cjs/index.d.cts && cp lib/sync.d.ts dist/cjs/sync.d.cts && cp lib/*.ts dist/esm", - "postbuild:ts": "find dist/cjs -name '*.d.cts' -exec sh -c \"sed -i \"s/\\.js'/\\.cjs'/g\" {} || sed -i '' \"s/\\.js'/\\.cjs'/g\" {}\" \\;", + "postbuild:ts": "node scripts/postbuild-types.js", "lint:check": "eslint", "lint:fix": "eslint --fix", "lint:ts": "tsc --noEmit true", "preversion": "npm run build && git add dist", - "test": "npx tsc --noEmit && mocha 'test/**/*.{js,ts}'", + "test": "npx tsc --noEmit && npm run test:browser-types && mocha 'test/**/*.{js,ts}'", + "test:browser-types": "npx tsc --project test/browser-types/tsconfig.json", "test:16": "mocha 'test/**/*.js'", "test:legacy": "mocha --ignore test/api.callback.js --ignore test/api.web_stream.js 'test/**/*.{js,ts}'" }, diff --git a/packages/csv-stringify/scripts/postbuild-types.js b/packages/csv-stringify/scripts/postbuild-types.js new file mode 100644 index 00000000..4ca40921 --- /dev/null +++ b/packages/csv-stringify/scripts/postbuild-types.js @@ -0,0 +1,17 @@ +import { readFile, readdir, writeFile } from "node:fs/promises"; + +const cjsDirectory = new URL("../dist/cjs/", import.meta.url); +for (const entry of await readdir(cjsDirectory)) { + if (!entry.endsWith(".d.cts")) continue; + const declaration = new URL(entry, cjsDirectory); + const source = await readFile(declaration, "utf8"); + await writeFile(declaration, source.replace(/\.js(["'])/g, ".cjs$1")); +} + +const browserDeclaration = new URL("../dist/esm/index.d.ts", import.meta.url); +const nodeReference = '/// \n\n'; +const source = await readFile(browserDeclaration, "utf8"); +if (!source.startsWith(nodeReference)) { + throw new Error("Expected the Node type reference in the source declaration"); +} +await writeFile(browserDeclaration, source.slice(nodeReference.length)); diff --git a/packages/csv-stringify/test/browser-types/index.ts b/packages/csv-stringify/test/browser-types/index.ts new file mode 100644 index 00000000..79ec5dd2 --- /dev/null +++ b/packages/csv-stringify/test/browser-types/index.ts @@ -0,0 +1,6 @@ +import { stringify } from "csv-stringify/browser/esm"; + +const timeout: number = setTimeout(() => {}, 0); + +void stringify; +void timeout; diff --git a/packages/csv-stringify/test/browser-types/tsconfig.json b/packages/csv-stringify/test/browser-types/tsconfig.json new file mode 100644 index 00000000..71409030 --- /dev/null +++ b/packages/csv-stringify/test/browser-types/tsconfig.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "lib": ["DOM", "ES2022"], + "module": "NodeNext", + "moduleResolution": "NodeNext", + "noEmit": true, + "skipLibCheck": true, + "strict": true, + "target": "ES2022", + "types": [] + }, + "include": ["index.ts"] +} diff --git a/packages/csv-stringify/tsconfig.json b/packages/csv-stringify/tsconfig.json index 5867967a..39a8cc48 100644 --- a/packages/csv-stringify/tsconfig.json +++ b/packages/csv-stringify/tsconfig.json @@ -7,6 +7,7 @@ "strict": true, "target": "esnext", // Prevent error "Cannot find name 'it'" - "types": ["mocha"], + "types": ["mocha"] }, + "exclude": ["test/browser-types"] }