Skip to content

Commit

Permalink
feat(compress-stringify): Use assets hosted on jsDelivr by default
Browse files Browse the repository at this point in the history
Prevent the need to copy and host the Wasm modules, web worker, and
configure a project's build to find these assets.

The demo uses local assets for local testing and to demonstrate how to
configure a build for vendored assets.
  • Loading branch information
thewtex committed Jan 24, 2023
1 parent 52da854 commit 3f86d7f
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/compress-stringify/typescript/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './pipelines-base-url.js'

export * from './pipeline-worker-url.js'


import CompressStringifyResult from './compress-stringify-result.js'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
let pipelineWorkerUrl: string | URL | null = new URL('/web-workers/pipeline.worker.js', document.location.origin).href
import packageJson from '../package.json'
let pipelineWorkerUrl: string | URL | null = `https://cdn.jsdelivr.net/npm/itk-compress-stringify@${packageJson.version}/dist/web-workers/pipeline.worker.js`

export function setPipelineWorkerUrl (workerUrl: string | URL | null): void {
pipelineWorkerUrl = workerUrl
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
let pipelinesBaseUrl: string | URL = new URL('/pipelines', document.location.origin).href
import packageJson from '../package.json'
let pipelinesBaseUrl: string | URL = `https://cdn.jsdelivr.net/npm/itk-compress-stringify@${packageJson.version}/dist/pipelines`

export function setPipelinesBaseUrl (baseUrl: string | URL): void {
pipelinesBaseUrl = baseUrl
Expand Down
6 changes: 6 additions & 0 deletions packages/compress-stringify/typescript/test/browser/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import * as itkCompressStringify from '../../dist/bundles/itk-compress-stringify.js'

// Use local, vendored WebAssembly module assets
const pipelinesBaseUrl: string | URL = new URL('/pipelines', document.location.origin).href
itkCompressStringify.setPipelinesBaseUrl(pipelinesBaseUrl)
let pipelineWorkerUrl: string | URL | null = new URL('/web-workers/pipeline.worker.js', document.location.origin).href
itkCompressStringify.setPipelineWorkerUrl(pipelineWorkerUrl)


// promise-file-reader
function readAsArrayBuffer (file) {
Expand Down
3 changes: 2 additions & 1 deletion src/bindgen/typescript-resources/pipeline-worker-url.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
let pipelineWorkerUrl: string | URL | null = new URL('/web-workers/pipeline.worker.js', document.location.origin).href
import packageJson from '../package.json'
let pipelineWorkerUrl: string | URL | null = `https://cdn.jsdelivr.net/npm/<bindgenPackageName>@${packageJson.version}/dist/web-workers/pipeline.worker.js`

export function setPipelineWorkerUrl (workerUrl: string | URL | null): void {
pipelineWorkerUrl = workerUrl
Expand Down
3 changes: 2 additions & 1 deletion src/bindgen/typescript-resources/pipelines-base-url.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
let pipelinesBaseUrl: string | URL = new URL('/pipelines', document.location.origin).href
import packageJson from '../package.json'
let pipelinesBaseUrl: string | URL = `https://cdn.jsdelivr.net/npm/<bindgenPackageName>@${packageJson.version}/dist/pipelines`

export function setPipelinesBaseUrl (baseUrl: string | URL): void {
pipelinesBaseUrl = baseUrl
Expand Down
10 changes: 8 additions & 2 deletions src/bindgen/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ function typescriptBindings(outputDir, buildDir, wasmBinaries, options, forNode=
}

if (!forNode) {
indexContent += "export * from './pipelines-base-url.js'\n\n"
indexContent += "export * from './pipelines-base-url.js'\n"
indexContent += "export * from './pipeline-worker-url.js'\n"
try {
fs.mkdirSync(path.join(outputDir, 'build'), { recursive: true })
} catch (err) {
Expand Down Expand Up @@ -484,10 +485,15 @@ function typescriptBindings(outputDir, buildDir, wasmBinaries, options, forNode=
const pipelinesBaseUrlPath = path.join(outputDir, 'src', 'pipelines-base-url.ts')
if (!fs.existsSync(pipelinesBaseUrlPath)) {
fs.copyFileSync(bindgenResource('pipelines-base-url.ts'), pipelinesBaseUrlPath)
let pipelinesBaseUrlPathContent = fs.readFileSync(bindgenResource('pipelines-base-url.ts'), { encoding: 'utf8', flag: 'r' })
pipelinesBaseUrlPathContent = pipelinesBaseUrlPathContent.replaceAll('<bindgenPackageName>', packageName)
fs.writeFileSync(pipelinesBaseUrlPath, pipelinesBaseUrlPathContent)
}
const pipelineWorkerUrlPath = path.join(outputDir, 'src', 'pipeline-worker-url.ts')
if (!fs.existsSync(pipelineWorkerUrlPath)) {
fs.copyFileSync(bindgenResource('pipeline-worker-url.ts'), pipelineWorkerUrlPath)
let pipelineWorkerUrlPathContent = fs.readFileSync(bindgenResource('pipeline-worker-url.ts'), { encoding: 'utf8', flag: 'r' })
pipelineWorkerUrlPathContent = pipelineWorkerUrlPathContent.replaceAll('<bindgenPackageName>', packageName)
fs.writeFileSync(pipelineWorkerUrlPath, pipelineWorkerUrlPathContent)
}

const itkConfigPath = path.join(outputDir, 'src', 'itkConfig.js')
Expand Down

0 comments on commit 3f86d7f

Please sign in to comment.