From a06a034d3daa1878902caad43967af0297baab2c Mon Sep 17 00:00:00 2001 From: Trim21 Date: Fri, 9 Jun 2023 10:47:56 +0800 Subject: [PATCH] fix: cjs type definition (#1167) * fix build * tsconfig: disable declarationMap * type --------- Co-authored-by: Prakash Senthil Vel <23444145+prakashsvmx@users.noreply.github.com> --- build.mjs | 34 +++++++++++++++++++--------------- package.json | 4 ---- tsconfig.json | 2 +- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/build.mjs b/build.mjs index 0ab77f34..c2629be9 100644 --- a/build.mjs +++ b/build.mjs @@ -58,6 +58,7 @@ const extMap = { cjs: '.js', esm: '.mjs' } async function buildFiles({ files, module, outDir }) { console.log(`building for ${module}`) await promisify(exec)(`npx tsc --outDir ${outDir}`, { stdio: 'inherit' }) + const ext = extMap[module] const opt = options(module) for (const file of files) { @@ -69,7 +70,7 @@ async function buildFiles({ files, module, outDir }) { const outDirPath = path.dirname(outFilePath) await fsp.mkdir(outDirPath, { recursive: true }) - const distCodePath = outFilePath.replace(/\.[tj]s$/g, extMap[module]) + const distCodePath = outFilePath.replace(/\.[tj]s$/g, ext) if (file.path.endsWith('.d.ts')) { await fsp.copyFile(file.path, outFilePath) @@ -88,19 +89,8 @@ async function buildFiles({ files, module, outDir }) { throw e } } -} - -async function main() { - await fsp.rm('dist', { recursive: true, force: true }) - - const entries = fsWalk.walkSync('src/') - await Promise.all([ - buildFiles({ files: entries, module: 'cjs', outDir: './dist/main/' }), - buildFiles({ files: entries, module: 'esm', outDir: './dist/esm/' }), - ]) - - for (const file of fsWalk.walkSync('dist/esm/')) { + for (const file of fsWalk.walkSync(outDir)) { if (file.dirent.isDirectory()) { continue } @@ -114,14 +104,28 @@ async function main() { const mts = babel.transformSync(fileContent, { filename: file.path, sourceMaps: true, - plugins: [['@babel/plugin-syntax-typescript'], ['replace-import-extension', { extMapping: { '.ts': '.mjs' } }]], + plugins: [['@babel/plugin-syntax-typescript'], ['replace-import-extension', { extMapping: { '.ts': ext } }]], }) await fsp.unlink(file.path) - const outFilePath = file.path.slice(0, file.path.length - '.d.ts'.length) + '.d.mts' + let outFilePath = file.path.slice(0, file.path.length - '.d.ts'.length) + '.d.ts' + if (module === 'esm') { + outFilePath = file.path.slice(0, file.path.length - '.d.ts'.length) + '.d.mts' + } await fsp.writeFile(outFilePath, mts.code) } } +async function main() { + await fsp.rm('dist', { recursive: true, force: true }) + + const entries = fsWalk.walkSync('src/') + + await Promise.all([ + buildFiles({ files: entries, module: 'cjs', outDir: './dist/main/' }), + buildFiles({ files: entries, module: 'esm', outDir: './dist/esm/' }), + ]) +} + await main() diff --git a/package.json b/package.json index 66a7cf9a..9fac01ee 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,6 @@ "description": "S3 Compatible Cloud Storage client", "main": "./dist/main/minio.js", "module": "./dist/esm/minio.mjs", - "types": "./dist/main/minio.d.ts", "scripts": { "prepare": "husky install", "tsc": "tsc", @@ -21,19 +20,16 @@ }, "exports": { ".": { - "types": "./dist/main/minio.d.ts", "require": "./dist/main/minio.js", "default": "./dist/esm/minio.mjs" }, "./dist/main/internal/*": null, "./dist/main/*": { - "types": "./dist/main/*", "require": "./dist/main/*", "default": null }, "./dist/esm/internal/*": null, "./dist/esm/*": { - "types": "./dist/esm/*", "import": "./dist/esm/*", "default": null }, diff --git a/tsconfig.json b/tsconfig.json index 670aeab2..b608cb3f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,7 +11,7 @@ "esModuleInterop": true, "allowImportingTsExtensions": true, "declaration": true, - "declarationMap": true, + "declarationMap": false, "emitDeclarationOnly": true, "sourceMap": true, "outDir": "./dist/main/",