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.envs.TYPE_DEF_TMP_PATH,
this.options.constEnum ?? true, this.options.constEnum ?? true,
!this.options.noDtsHeader !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, name,
this.config.packageName, this.config.packageName,
wasiRegisterFunctions, wasiRegisterFunctions,
this.config.wasm.initialMemory, this.config.wasm?.initialMemory,
this.config.wasm.maximumMemory, this.config.wasm?.maximumMemory,
) + ) +
exportsCode + exportsCode +
'\n', '\n',
@ -888,8 +901,8 @@ class Builder {
createWasiBrowserBinding( createWasiBrowserBinding(
name, name,
wasiRegisterFunctions, wasiRegisterFunctions,
this.config.wasm.initialMemory, this.config.wasm?.initialMemory,
this.config.wasm.maximumMemory, this.config.wasm?.maximumMemory,
) + ) +
idents idents
.map( .map(

View file

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

View file

@ -42,6 +42,17 @@ export interface UserNapiConfig {
*/ */
constEnum?: boolean 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 * wasm compilation options
*/ */
@ -116,11 +127,12 @@ export interface CommonPackageJsonFields {
} }
export type NapiConfig = Required< export type NapiConfig = Required<
Pick<UserNapiConfig, 'binaryName' | 'packageName' | 'npmClient' | 'wasm'> Pick<UserNapiConfig, 'binaryName' | 'packageName' | 'npmClient'>
> & { > &
Pick<UserNapiConfig, 'wasm' | 'dtsHeader' | 'dtsHeaderFile'> & {
targets: Target[] targets: Target[]
packageJson: CommonPackageJsonFields packageJson: CommonPackageJsonFields
} }
export async function readNapiConfig( export async function readNapiConfig(
path: string, path: string,
@ -173,7 +185,6 @@ export async function readNapiConfig(
targets: [], targets: [],
packageJson: pkgJson, packageJson: pkgJson,
npmClient: 'npm', npmClient: 'npm',
wasm: {},
}, },
omit(userNapiConfig, 'targets'), omit(userNapiConfig, 'targets'),
) )

View file

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

View file

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

View file

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