feat(cli): allow to define dtsHeader in napi config (#2045)
This commit is contained in:
parent
a2d78fadf9
commit
b3dd94649a
11 changed files with 45 additions and 15 deletions
|
@ -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(
|
||||
|
|
|
@ -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: {},
|
||||
}
|
||||
|
|
Binary file not shown.
|
@ -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,11 +127,12 @@ export interface CommonPackageJsonFields {
|
|||
}
|
||||
|
||||
export type NapiConfig = Required<
|
||||
Pick<UserNapiConfig, 'binaryName' | 'packageName' | 'npmClient' | 'wasm'>
|
||||
> & {
|
||||
targets: Target[]
|
||||
packageJson: CommonPackageJsonFields
|
||||
}
|
||||
Pick<UserNapiConfig, 'binaryName' | 'packageName' | 'npmClient'>
|
||||
> &
|
||||
Pick<UserNapiConfig, 'wasm' | 'dtsHeader' | 'dtsHeaderFile'> & {
|
||||
targets: Target[]
|
||||
packageJson: CommonPackageJsonFields
|
||||
}
|
||||
|
||||
export async function readNapiConfig(
|
||||
path: string,
|
||||
|
@ -173,7 +185,6 @@ export async function readNapiConfig(
|
|||
targets: [],
|
||||
packageJson: pkgJson,
|
||||
npmClient: 'npm',
|
||||
wasm: {},
|
||||
},
|
||||
omit(userNapiConfig, 'targets'),
|
||||
)
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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 '': {␊
|
||||
|
|
Binary file not shown.
Binary file not shown.
4
examples/napi/dts-header.d.ts
vendored
Normal file
4
examples/napi/dts-header.d.ts
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
/* auto-generated by NAPI-RS */
|
||||
/* eslint-disable */
|
||||
|
||||
type MaybePromise<T> = T | Promise<T>
|
|
@ -1,6 +1,7 @@
|
|||
/* auto-generated by NAPI-RS */
|
||||
/* eslint-disable */
|
||||
|
||||
type MaybePromise<T> = T | Promise<T>
|
||||
|
||||
export class ExternalObject<T> {
|
||||
readonly '': {
|
||||
|
|
|
@ -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:*",
|
||||
|
|
Loading…
Reference in a new issue