Skip to content

Commit

Permalink
only unlink/rename polyfills if file exists
Browse files Browse the repository at this point in the history
Fix: #35

PR-URL: #36
Credit: @timsuchanek
Close: #36
Reviewed-by: @isaacs
  • Loading branch information
timsuchanek authored and isaacs committed Nov 22, 2023
1 parent 1492941 commit 033fa9a
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/build-commonjs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import chalk from 'chalk'
import { spawnSync } from 'node:child_process'
import { renameSync, unlinkSync } from 'node:fs'
import { existsSync, renameSync, unlinkSync } from 'node:fs'
import { relative, resolve } from 'node:path/posix'
import buildFail from './build-fail.js'
import config from './config.js'
Expand All @@ -10,6 +10,10 @@ import setFolderDialect from './set-folder-dialect.js'
import './tsconfig.js'
const { commonjsDialects = [] } = config

const unlinkIfExist = (f: string) => existsSync(f) && unlinkSync(f)
const renameIfExist = (f: string, to: string) =>
existsSync(f) && renameSync(f, to)

export const buildCommonJS = () => {
setFolderDialect('src', 'commonjs')
for (const d of ['commonjs', ...commonjsDialects]) {
Expand All @@ -33,10 +37,12 @@ export const buildCommonJS = () => {
`.tshy-build/${d}`,
relative(resolve('src'), resolve(orig))
).replace(/\.tsx?$/, '')
unlinkSync(`${stemTo}.js.map`)
unlinkSync(`${stemTo}.d.ts.map`)
renameSync(`${stemFrom}.cjs`, `${stemTo}.js`)
renameSync(`${stemFrom}.d.cts`, `${stemTo}.d.ts`)
const stemToPath = `${stemTo}.js.map`
const stemToDtsPath = `${stemTo}.d.ts.map`
unlinkIfExist(stemToPath)
unlinkIfExist(stemToDtsPath)
renameIfExist(`${stemFrom}.cjs`, `${stemTo}.js`)
renameIfExist(`${stemFrom}.d.cts`, `${stemTo}.d.ts`)
}
console.error(chalk.cyan.bold('built commonjs'))
}
Expand Down

1 comment on commit 033fa9a

@timsuchanek
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

Please sign in to comment.