From 635af24e6967189918cb4930a0832fd3f711b585 Mon Sep 17 00:00:00 2001 From: Matt McCormick Date: Tue, 19 Dec 2023 15:20:18 -0500 Subject: [PATCH] fix(bindgen): define an index-all.ts that includes Node types For the "types" and "default" export, provide both Node.js and browser definitions. --- .../core/typescript/itk-wasm/package.json | 2 +- .../resources/template.package.json | 6 ++-- .../bindgen/typescript/typescript-bindings.js | 29 +++++++++++++++---- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/packages/core/typescript/itk-wasm/package.json b/packages/core/typescript/itk-wasm/package.json index b14397450..938a42d2b 100644 --- a/packages/core/typescript/itk-wasm/package.json +++ b/packages/core/typescript/itk-wasm/package.json @@ -1,6 +1,6 @@ { "name": "itk-wasm", - "version": "1.0.0-b.159", + "version": "1.0.0-b.160", "packageManager": "pnpm@8.11.0", "description": "High-performance spatial analysis in a web browser, Node.js, and reproducible execution across programming languages and hardware architectures.", "type": "module", diff --git a/packages/core/typescript/itk-wasm/src/bindgen/typescript/resources/template.package.json b/packages/core/typescript/itk-wasm/src/bindgen/typescript/resources/template.package.json index cb0800ffc..b9a93c9d7 100644 --- a/packages/core/typescript/itk-wasm/src/bindgen/typescript/resources/template.package.json +++ b/packages/core/typescript/itk-wasm/src/bindgen/typescript/resources/template.package.json @@ -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": { diff --git a/packages/core/typescript/itk-wasm/src/bindgen/typescript/typescript-bindings.js b/packages/core/typescript/itk-wasm/src/bindgen/typescript/typescript-bindings.js index c268469d1..70a18ef0f 100644 --- a/packages/core/typescript/itk-wasm/src/bindgen/typescript/typescript-bindings.js +++ b/packages/core/typescript/itk-wasm/src/bindgen/typescript/typescript-bindings.js @@ -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' : '' @@ -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 } } @@ -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) @@ -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) { @@ -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' })