-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.config.ts
65 lines (55 loc) · 1.62 KB
/
build.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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: [
{
input: './src/index',
name: 'index',
builder: 'rollup',
declaration: true,
outDir: './dist',
},
],
hooks: {
'build:prepare'(ctx) {
ctx.options.declaration = ctx.options.entries.some(
(entry) => entry.declaration,
)
},
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(ctx.options.outDir, 'locale-data')
const writtenFiles = generateLocaleData({ outDir })
ctx.buildEntries.push({
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)
}
},
},
})