From 61f7415ffd763fc9129c87a4f7125091dcf22a1c Mon Sep 17 00:00:00 2001 From: Matt McCormick Date: Thu, 29 Dec 2022 08:40:11 -0500 Subject: [PATCH] feat(bindgen): Generate Node build configuration --- src/bindgen/rollup.node.config.js | 29 +++++++++++++++++++++++++++++ src/bindgen/template.package.json | 7 +++++++ src/itk-wasm-cli.js | 13 ++++++++++--- 3 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 src/bindgen/rollup.node.config.js diff --git a/src/bindgen/rollup.node.config.js b/src/bindgen/rollup.node.config.js new file mode 100644 index 000000000..33ed0cbf7 --- /dev/null +++ b/src/bindgen/rollup.node.config.js @@ -0,0 +1,29 @@ +import { nodeResolve } from '@rollup/plugin-node-resolve' +import typescript from '@rollup/plugin-typescript' +import commonjs from '@rollup/plugin-commonjs' +import json from '@rollup/plugin-json' +import { terser } from 'rollup-plugin-terser' +import packageJson from './package.json' assert { type: 'json' } + +export default { + input: './src/indexNode.ts', + output: [ + { + file: `./dist/${packageJson.name}.node.js`, + format: 'es', + sourcemap: true, + plugins: [terser(),], + }, + ], + plugins: [ + commonjs({ + transformMixedEsModules: true + }), + nodeResolve({ + preferBuiltins: true, + browser: false, + }), + typescript(), + json(), + ], +} diff --git a/src/bindgen/template.package.json b/src/bindgen/template.package.json index 3ef26e4f2..8a0c727c0 100644 --- a/src/bindgen/template.package.json +++ b/src/bindgen/template.package.json @@ -6,6 +6,8 @@ "main": "./dist/index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", + "build": "npm run build:tsc && npm run build:node", + "build:node": "rollup -c ./rollup.node.config.js", "build:tsc": "tsc --pretty" }, "keywords": [ @@ -20,6 +22,11 @@ "itk-wasm": "^1.0.0-b.53" }, "devDependencies": { + "@rollup/plugin-commonjs": "^24.0.0", + "@rollup/plugin-json": "^6.0.0", + "@rollup/plugin-node-resolve": "^15.0.1", + "@rollup/plugin-typescript": "^10.0.1", + "rollup": "^3.9.0", "typescript": "^4.9.4" } } diff --git a/src/itk-wasm-cli.js b/src/itk-wasm-cli.js index 90dbdea00..0886e2da6 100755 --- a/src/itk-wasm-cli.js +++ b/src/itk-wasm-cli.js @@ -305,12 +305,19 @@ function typescriptBindings(outputDir, buildDir, wasmBinaries, options, forNode= } } - const tsConfigPath = path.join(outputDir, 'tsconfig.json') - if (!fs.existsSync(tsConfigPath)) { - fs.copyFileSync(bindgenResource('tsconfig.json'), tsConfigPath) + } + + if (forNode) { + const rollupConfigPath = path.join(outputDir, 'rollup.node.config.js') + if (!fs.existsSync(rollupConfigPath)) { + fs.copyFileSync(bindgenResource('rollup.node.config.js'), rollupConfigPath) } } + const tsConfigPath = path.join(outputDir, 'tsconfig.json') + if (!fs.existsSync(tsConfigPath)) { + fs.copyFileSync(bindgenResource('tsconfig.json'), tsConfigPath) + } wasmBinaries.forEach((wasmBinaryName) => { let wasmBinaryRelativePath = `${buildDir}/${wasmBinaryName}` if (!fs.existsSync(wasmBinaryRelativePath)) {