Skip to content

Commit

Permalink
fix(bindgen): define an index-all.ts that includes Node types
Browse files Browse the repository at this point in the history
For the "types" and "default" export, provide both Node.js and browser
definitions.
  • Loading branch information
thewtex committed Dec 19, 2023
1 parent d5d47cb commit 635af24
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/core/typescript/itk-wasm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "itk-wasm",
"version": "1.0.0-b.159",
"version": "1.0.0-b.160",
"packageManager": "[email protected]",
"description": "High-performance spatial analysis in a web browser, Node.js, and reproducible execution across programming languages and hardware architectures.",
"type": "module",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
"description": "",
"type": "module",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"types": "./dist/index-all.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"types": "./dist/index-all.d.ts",
"browser": "./dist/index.js",
"node": "./dist/index-node.js",
"default": "./dist/index.js"
"default": "./dist/index-all.js"
}
},
"scripts": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function bindgenResource(filePath) {
function typescriptBindings (outputDir, buildDir, wasmBinaries, options, forNode=false) {
// index module
let indexContent = ''
let indexCommonContent = ''
const nodeTextKebab = forNode ? '-node' : ''
const nodeTextCamel = forNode ? 'Node' : ''

Expand Down Expand Up @@ -53,7 +54,7 @@ function typescriptBindings (outputDir, buildDir, wasmBinaries, options, forNode
packageJson.module = `./dist/index.js`
packageJson.exports['.'].browser = `./dist/index.js`
packageJson.exports['.'].node = `./dist/index-node.js`
packageJson.exports['.'].default = `./dist/index.js`
packageJson.exports['.'].default = `./dist/index-all.js`
if (options.repository) {
packageJson.repository = { 'type': 'git', 'url': options.repository }
}
Expand Down Expand Up @@ -140,8 +141,8 @@ if (!params.has('functionName')) {

const { readmeOptions } = optionsModule(srcOutputDir, interfaceJson, modulePascalCase, nodeTextCamel, moduleKebabCase, haveOptions)
if (haveOptions) {
indexContent += `import ${modulePascalCase}Options from './${moduleKebabCase}-options.js'\n`
indexContent += `export type { ${modulePascalCase}Options }\n\n`
indexCommonContent += `import ${modulePascalCase}Options from './${moduleKebabCase}-options.js'\n`
indexCommonContent += `export type { ${modulePascalCase}Options }\n\n`
}

const { readmeFunction, usedInterfaceTypes } = functionModule(srcOutputDir, forNode, interfaceJson, modulePascalCase, moduleKebabCase, moduleCamelCase, nodeTextCamel, nodeTextKebab, haveOptions)
Expand All @@ -156,8 +157,8 @@ if (!params.has('functionName')) {
})

if (allUsedInterfaceTypes.size > 0) {
indexContent += '\n'
allUsedInterfaceTypes.forEach(iType => indexContent += `export type { ${iType} } from 'itk-wasm'\n`)
indexCommonContent += '\n'
allUsedInterfaceTypes.forEach(iType => indexCommonContent += `export type { ${iType} } from 'itk-wasm'\n`)
}

if (!forNode) {
Expand All @@ -168,9 +169,25 @@ if (!params.has('functionName')) {
readmeInterface += `} from "${packageName}"\n\`\`\`\n`
readmeInterface += readmePipelines

const indexPath = path.join(srcOutputDir, `index${nodeTextKebab}.ts`)
const indexPath = path.join(srcOutputDir, `index${nodeTextKebab}-only.ts`)
writeIfOverrideNotPresent(indexPath, indexContent)

const indexCommonPath = path.join(srcOutputDir, `index-common.ts`)
writeIfOverrideNotPresent(indexCommonPath, indexCommonContent)

const indexEnvContent = `export * from './index-common.js'
export * from './index${nodeTextKebab}-only.js'
`
const indexEnvPath = path.join(srcOutputDir, `index${nodeTextKebab}.ts`)
writeIfOverrideNotPresent(indexEnvPath, indexEnvContent)

const indexAllContent = `export * from './index-common.js'
export * from './index-only.js'
export * from './index-node-only.js'
`
const indexAllPath = path.join(srcOutputDir, `index-all.ts`)
writeIfOverrideNotPresent(indexAllPath, indexAllContent)

if (!forNode) {
const demoIndexPath = path.join(outputDir, 'test', 'browser', 'demo-app', 'index.html')
let demoIndexContent = fs.readFileSync(bindgenResource(path.join('demo-app', 'index.html')), { encoding: 'utf8', flag: 'r' })
Expand Down

0 comments on commit 635af24

Please sign in to comment.