Skip to content

Commit 635af24

Browse files
committed
fix(bindgen): define an index-all.ts that includes Node types
For the "types" and "default" export, provide both Node.js and browser definitions.
1 parent d5d47cb commit 635af24

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

packages/core/typescript/itk-wasm/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "itk-wasm",
3-
"version": "1.0.0-b.159",
3+
"version": "1.0.0-b.160",
44
"packageManager": "pnpm@8.11.0",
55
"description": "High-performance spatial analysis in a web browser, Node.js, and reproducible execution across programming languages and hardware architectures.",
66
"type": "module",

packages/core/typescript/itk-wasm/src/bindgen/typescript/resources/template.package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
"description": "",
66
"type": "module",
77
"module": "./dist/index.js",
8-
"types": "./dist/index.d.ts",
8+
"types": "./dist/index-all.d.ts",
99
"exports": {
1010
".": {
11-
"types": "./dist/index.d.ts",
11+
"types": "./dist/index-all.d.ts",
1212
"browser": "./dist/index.js",
1313
"node": "./dist/index-node.js",
14-
"default": "./dist/index.js"
14+
"default": "./dist/index-all.js"
1515
}
1616
},
1717
"scripts": {

packages/core/typescript/itk-wasm/src/bindgen/typescript/typescript-bindings.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ function bindgenResource(filePath) {
2525
function typescriptBindings (outputDir, buildDir, wasmBinaries, options, forNode=false) {
2626
// index module
2727
let indexContent = ''
28+
let indexCommonContent = ''
2829
const nodeTextKebab = forNode ? '-node' : ''
2930
const nodeTextCamel = forNode ? 'Node' : ''
3031

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

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

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

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

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

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

175+
const indexCommonPath = path.join(srcOutputDir, `index-common.ts`)
176+
writeIfOverrideNotPresent(indexCommonPath, indexCommonContent)
177+
178+
const indexEnvContent = `export * from './index-common.js'
179+
export * from './index${nodeTextKebab}-only.js'
180+
`
181+
const indexEnvPath = path.join(srcOutputDir, `index${nodeTextKebab}.ts`)
182+
writeIfOverrideNotPresent(indexEnvPath, indexEnvContent)
183+
184+
const indexAllContent = `export * from './index-common.js'
185+
export * from './index-only.js'
186+
export * from './index-node-only.js'
187+
`
188+
const indexAllPath = path.join(srcOutputDir, `index-all.ts`)
189+
writeIfOverrideNotPresent(indexAllPath, indexAllContent)
190+
174191
if (!forNode) {
175192
const demoIndexPath = path.join(outputDir, 'test', 'browser', 'demo-app', 'index.html')
176193
let demoIndexContent = fs.readFileSync(bindgenResource(path.join('demo-app', 'index.html')), { encoding: 'utf8', flag: 'r' })

0 commit comments

Comments
 (0)