Skip to content

Commit

Permalink
feat(bindgen): Use vite or webpack shipped pipelineWorker
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Jan 23, 2023
1 parent 590e8de commit 0b7bb9f
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 10 deletions.
20 changes: 20 additions & 0 deletions packages/compress-stringify/typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import {
parseStringDecompress,
setPipelinesBaseUrl,
getPipelinesBaseUrl,
setPipelineWorkerUrl,
getPipelineWorkerUrl,
} from "itk-compress-stringify"
```

Expand Down Expand Up @@ -109,6 +111,24 @@ function setPipelinesBaseUrl(
function getPipelinesBaseUrl() : string | URL
```

#### setPipelineWorkerUrl

*Set base URL for the itk-wasm pipeline worker script when vendored.*

```ts
function setPipelineWorkerUrl(
baseUrl: string | URL
) : void
```

#### getPipelineWorkerUrl

*Get base URL for the itk-wasm pipeline worker script when vendored.*

```ts
function getPipelineWorkerUrl() : string | URL
```

### Node interface

Import:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default defineConfig({
viteStaticCopy({
targets: [
{ src: '../../dist/pipelines/*', dest: 'pipelines' },
{ src: '../../dist/web-workers/*', dest: 'web-workers' },
],
})
],
Expand Down
2 changes: 1 addition & 1 deletion packages/compress-stringify/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"author": "",
"license": "Apache-2.0",
"dependencies": {
"itk-wasm": "^1.0.0-b.60"
"itk-wasm": "^1.0.0-b.62"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^24.0.0",
Expand Down
8 changes: 4 additions & 4 deletions packages/compress-stringify/typescript/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import CompressStringifyResult from './compress-stringify-result.js'

import { getPipelinesBaseUrl } from './pipelines-base-url.js'


import { getPipelineWorkerUrl } from './pipeline-worker-url.js'

/**
* Given a binary, compress and optionally base64 encode.
*
Expand Down Expand Up @@ -55,7 +58,7 @@ async function compressStringify(
returnValue,
stderr,
outputs
} = await runPipeline(webWorker, pipelinePath, args, desiredOutputs, inputs, getPipelinesBaseUrl())
} = await runPipeline(webWorker, pipelinePath, args, desiredOutputs, inputs, getPipelinesBaseUrl(), getPipelineWorkerUrl())
if (returnValue !== 0) {
throw new Error(stderr)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import ParseStringDecompressResult from './parse-string-decompress-result.js'

import { getPipelinesBaseUrl } from './pipelines-base-url.js'


import { getPipelineWorkerUrl } from './pipeline-worker-url.js'

/**
* Given a binary or string produced with compress-stringify, decompress and optionally base64 decode.
*
Expand Down Expand Up @@ -49,7 +52,7 @@ async function parseStringDecompress(
returnValue,
stderr,
outputs
} = await runPipeline(webWorker, pipelinePath, args, desiredOutputs, inputs, getPipelinesBaseUrl())
} = await runPipeline(webWorker, pipelinePath, args, desiredOutputs, inputs, getPipelinesBaseUrl(), getPipelineWorkerUrl())
if (returnValue !== 0) {
throw new Error(stderr)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let pipelineWorkerUrl: string | URL | null = new URL('/web-workers/pipeline.worker.js', document.location.origin).href

export function setPipelineWorkerUrl (workerUrl: string | URL | null): void {
pipelineWorkerUrl = workerUrl
}

export function getPipelineWorkerUrl (): string | URL | null {
return pipelineWorkerUrl
}
9 changes: 9 additions & 0 deletions src/bindgen/typescript-resources/pipeline-worker-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let pipelineWorkerUrl: string | URL | null = new URL('/web-workers/pipeline.worker.js', document.location.origin).href

export function setPipelineWorkerUrl (workerUrl: string | URL | null): void {
pipelineWorkerUrl = workerUrl
}

export function getPipelineWorkerUrl (): string | URL | null {
return pipelineWorkerUrl
}
2 changes: 1 addition & 1 deletion src/bindgen/typescript-resources/template.package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"author": "",
"license": "Apache-2.0",
"dependencies": {
"itk-wasm": "^1.0.0-b.60"
"itk-wasm": "^1.0.0-b.62"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^24.0.0",
Expand Down
1 change: 1 addition & 0 deletions src/bindgen/typescript-resources/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default defineConfig({
viteStaticCopy({
targets: [
{ src: '../../dist/pipelines/*', dest: 'pipelines' },
{ src: '../../dist/web-workers/*', dest: 'web-workers' },
],
})
],
Expand Down
7 changes: 6 additions & 1 deletion src/bindgen/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ function typescriptBindings(outputDir, buildDir, wasmBinaries, options, forNode=
functionContent += "\nimport path from 'path'\n\n"
} else {
functionContent += "\nimport { getPipelinesBaseUrl } from './pipelines-base-url.js'\n\n"
functionContent += "\nimport { getPipelineWorkerUrl } from './pipeline-worker-url.js'\n\n"
}

const readmeParametersTable = [['Parameter', 'Type', 'Description'],]
Expand Down Expand Up @@ -446,7 +447,7 @@ function typescriptBindings(outputDir, buildDir, wasmBinaries, options, forNode=
functionContent += ` const {\n returnValue,\n stderr,\n outputs\n } = await runPipelineNode(pipelinePath, args, desiredOutputs, inputs)\n`
} else {
functionContent += `\n const pipelinePath = '${moduleKebabCase}'\n\n`
functionContent += ` const {\n webWorker: usedWebWorker,\n returnValue,\n stderr,\n outputs\n } = await runPipeline(webWorker, pipelinePath, args, desiredOutputs, inputs, getPipelinesBaseUrl())\n`
functionContent += ` const {\n webWorker: usedWebWorker,\n returnValue,\n stderr,\n outputs\n } = await runPipeline(webWorker, pipelinePath, args, desiredOutputs, inputs, getPipelinesBaseUrl(), getPipelineWorkerUrl())\n`
}

functionContent += ' if (returnValue !== 0) {\n throw new Error(stderr)\n }\n\n'
Expand Down Expand Up @@ -484,6 +485,10 @@ function typescriptBindings(outputDir, buildDir, wasmBinaries, options, forNode=
if (!fs.existsSync(pipelinesBaseUrlPath)) {
fs.copyFileSync(bindgenResource('pipelines-base-url.ts'), pipelinesBaseUrlPath)
}
const pipelineWorkerUrlPath = path.join(outputDir, 'src', 'pipeline-worker-url.ts')
if (!fs.existsSync(pipelineWorkerUrlPath)) {
fs.copyFileSync(bindgenResource('pipeline-worker-url.ts'), pipelineWorkerUrlPath)
}

const itkConfigPath = path.join(outputDir, 'src', 'itkConfig.js')
if (!fs.existsSync(itkConfigPath)) {
Expand Down
2 changes: 1 addition & 1 deletion src/itk-wasm-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const program = new Command()

// Array of types that will require an import from itk-wasm
const typesRequireImport = ['Image']
const defaultImageTag = '20221222-d37dad3f'
const defaultImageTag = '20230122-5e3aea6c'

function processCommonOptions() {
const options = program.opts()
Expand Down

0 comments on commit 0b7bb9f

Please sign in to comment.