Skip to content

Commit

Permalink
fix: incorrect typescript declarations
Browse files Browse the repository at this point in the history
- Fixes data files declarations
- Renames all emitted .d.ts declarations to .d.mts
- Removes forced types conditional export
- Uses pathe in build configuration
- Includes build/*.ts to build tsconfig
  • Loading branch information
brawaru committed Mar 14, 2023
1 parent 654cd8f commit 7827cfa
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 13 deletions.
34 changes: 30 additions & 4 deletions build.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import * as path from 'node:path'
import * as fs from 'node:fs/promises'
import * as path from 'pathe'
import { defineBuildConfig } from 'unbuild'
import chalk from 'chalk'
import { generateLocaleData } from './build/localeData'

async function fileExists(fp: string) {
try {
await fs.stat(fp)
return true
} catch {
return false
}
}

export default defineBuildConfig({
entries: [
{
Expand All @@ -19,21 +29,37 @@ export default defineBuildConfig({
(entry) => entry.declaration,
)
},
'rollup:done'(ctx) {
async 'rollup:done'(ctx) {
const isCI = process.env.ci || process.env.test
// eslint-disable-next-line no-console
console.info(
`${isCI ? '[task]' : chalk.yellow('↯')} Generating locale data`,
)

const outDir = path.join(process.cwd(), 'dist', 'locale-data')
const outDir = path.join(ctx.options.outDir, 'locale-data')

const writtenFiles = generateLocaleData({ outDir })

ctx.buildEntries.push({
path: outDir,
path: path.relative(ctx.options.outDir, outDir),
bytes: writtenFiles.reduce((total, emit) => total + emit[1], 0),
})

// fix incorrect .d.ts extension for .mjs files
for (const entry of ctx.buildEntries) {
if (path.extname(entry.path) !== '.mjs') continue

const dtsPath = path.resolve(
ctx.options.outDir,
entry.path.replace(/\.mjs$/, '.d.ts'),
)

if (!(await fileExists(dtsPath))) continue

const mdtsPath = dtsPath.replace(/\.d\.ts$/, '.d.mts')

await fs.rename(dtsPath, mdtsPath)
}
},
},
})
9 changes: 4 additions & 5 deletions build/localeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,8 @@ import { addLocaleData } from '../index.mjs';
addLocaleData(${JSON.stringify(locale)}, data);`
}

const dataFileDTS = `declare const _default: Parameters<
typeof import('../index')['addLocaleData']
>[1]
const dataFileDTS = `import type { addLocaleData } from '../index.mjs'
declare const _default: Parameters<typeof addLocaleData>[1]
export default _default`

const exporterFileDTS = 'export {};'
Expand Down Expand Up @@ -220,10 +219,10 @@ export function generateLocaleData({
const numbers = readJSONSync(path.join(localesDir, locale, 'numbers.json'))

output(`${locale}.data.mjs`, generateDataFile(locale, numbers))
output(`${locale}.data.d.ts`, dataFileDTS)
output(`${locale}.data.d.mts`, dataFileDTS)

output(`${locale}.mjs`, generateESMExporter(locale))
output(`${locale}.d.ts`, exporterFileDTS)
output(`${locale}.d.mts`, exporterFileDTS)
}

return writtenFiles
Expand Down
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,13 @@
"type": "module",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs"
},
"./locale-data/*": {
"types": "./dist/locale-data/*.d.ts",
"import": "./dist/locale-data/*.mjs"
},
"./package.json": "./package.json"
},
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"files": [
"dist"
],
Expand All @@ -59,6 +55,7 @@
"cldr-numbers-modern": "^42.0.0",
"eslint": "^8.36.0",
"fs-extra": "^11.1.0",
"pathe": "^1.1.0",
"prettier": "^2.8.4",
"prettier-plugin-jsdoc": "^0.4.2",
"semantic-release": "^20.1.1",
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"types": ["node"]
},
"include": [
"./build/*.ts",
"./build.config.ts",
"./vitest.config.ts",
"./test/*.test.ts",
Expand Down

0 comments on commit 7827cfa

Please sign in to comment.