feat(cli): allow to define dtsHeader in napi config (#2045)

This commit is contained in:
LongYinan 2024-04-16 17:40:41 +08:00 committed by GitHub
parent a2d78fadf9
commit b3dd94649a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 45 additions and 15 deletions

View file

@ -798,7 +798,20 @@ class Builder {
this.envs.TYPE_DEF_TMP_PATH,
this.options.constEnum ?? true,
!this.options.noDtsHeader
? this.options.dtsHeader ?? DEFAULT_TYPE_DEF_HEADER
? this.options.dtsHeader ??
(this.config.dtsHeaderFile
? await readFileAsync(
join(this.cwd, this.config.dtsHeaderFile),
'utf-8',
).catch(() => {
debug.warn(
`Failed to read dts header file ${this.config.dtsHeaderFile}`,
)
return null
})
: null) ??
this.config.dtsHeader ??
DEFAULT_TYPE_DEF_HEADER
: '',
)
@ -876,8 +889,8 @@ class Builder {
name,
this.config.packageName,
wasiRegisterFunctions,
this.config.wasm.initialMemory,
this.config.wasm.maximumMemory,
this.config.wasm?.initialMemory,
this.config.wasm?.maximumMemory,
) +
exportsCode +
'\n',
@ -888,8 +901,8 @@ class Builder {
createWasiBrowserBinding(
name,
wasiRegisterFunctions,
this.config.wasm.initialMemory,
this.config.wasm.maximumMemory,
this.config.wasm?.initialMemory,
this.config.wasm?.maximumMemory,
) +
idents
.map(

View file

@ -48,7 +48,6 @@ Generated by [AVA](https://avajs.dev).
triple: 'x86_64-apple-darwin',
},
],
wasm: {},
}
## should be able to read config from napi.json
@ -95,5 +94,4 @@ Generated by [AVA](https://avajs.dev).
triple: 'aarch64-apple-darwin',
},
],
wasm: {},
}

View file

@ -42,6 +42,17 @@ export interface UserNapiConfig {
*/
constEnum?: boolean
/**
* dts header prepend to the generated dts file
*/
dtsHeader?: string
/**
* dts header file path to be prepended to the generated dts file
* if both dtsHeader and dtsHeaderFile are provided, dtsHeaderFile will be used
*/
dtsHeaderFile?: string
/**
* wasm compilation options
*/
@ -116,8 +127,9 @@ export interface CommonPackageJsonFields {
}
export type NapiConfig = Required<
Pick<UserNapiConfig, 'binaryName' | 'packageName' | 'npmClient' | 'wasm'>
> & {
Pick<UserNapiConfig, 'binaryName' | 'packageName' | 'npmClient'>
> &
Pick<UserNapiConfig, 'wasm' | 'dtsHeader' | 'dtsHeaderFile'> & {
targets: Target[]
packageJson: CommonPackageJsonFields
}
@ -173,7 +185,6 @@ export async function readNapiConfig(
targets: [],
packageJson: pkgJson,
npmClient: 'npm',
wasm: {},
},
omit(userNapiConfig, 'targets'),
)

View file

@ -64,7 +64,7 @@ export async function processTypeDef(
const defs = await readIntermediateTypeFile(intermediateTypeFile)
const groupedDefs = preprocessTypeDef(defs)
header = header ? header + '\n' : ''
header = header ?? ''
let dts = ''
sortBy(Array.from(groupedDefs), ([namespace]) => namespace).forEach(

View file

@ -11,6 +11,7 @@ Generated by [AVA](https://avajs.dev).
`/* auto-generated by NAPI-RS */␊
/* eslint-disable */␊
type MaybePromise<T> = T | Promise<T>
export class ExternalObject<T> {␊
readonly '': {␊

4
examples/napi/dts-header.d.ts vendored Normal file
View file

@ -0,0 +1,4 @@
/* auto-generated by NAPI-RS */
/* eslint-disable */
type MaybePromise<T> = T | Promise<T>

View file

@ -1,6 +1,7 @@
/* auto-generated by NAPI-RS */
/* eslint-disable */
type MaybePromise<T> = T | Promise<T>
export class ExternalObject<T> {
readonly '': {

View file

@ -14,7 +14,9 @@
"binaryName": "example",
"wasm": {
"initialMemory": 16384
}
},
"dtsHeader": "type MaybePromise<T> = T | Promise<T>",
"dtsHeaderFile": "./dts-header.d.ts"
},
"devDependencies": {
"@napi-rs/cli": "workspace:*",