Skip to content

Commit

Permalink
fix: cjs type definition (#1167)
Browse files Browse the repository at this point in the history
* fix build

* tsconfig: disable declarationMap

* type

---------

Co-authored-by: Prakash Senthil Vel <[email protected]>
  • Loading branch information
trim21 and prakashsvmx authored Jun 9, 2023
1 parent b5e01dd commit a06a034
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
34 changes: 19 additions & 15 deletions build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand All @@ -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
}
Expand All @@ -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()
4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
},
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"esModuleInterop": true,
"allowImportingTsExtensions": true,
"declaration": true,
"declarationMap": true,
"declarationMap": false,
"emitDeclarationOnly": true,
"sourceMap": true,
"outDir": "./dist/main/",
Expand Down

0 comments on commit a06a034

Please sign in to comment.