Skip to content

Commit

Permalink
Merge pull request #136 from superfaceai/fix/wasm_imports_again
Browse files Browse the repository at this point in the history
fix: asset imports
  • Loading branch information
TheEdward162 committed Feb 26, 2024
2 parents 322a4b9 + db8f162 commit 3dbb282
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
9 changes: 7 additions & 2 deletions packages/nodejs_comlink/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ export type * from './model'
function comlinkWasmPath(): string {
// use new URL constructor to get webpack to bundle the asset and non-bundled code to work correctly in all contexts
const url = new URL('../assets/comlink.wasm', import.meta.url)
// resolve in case we get a relative path, like with next
const path = resolvePath(url.pathname)

let path = url.pathname;
if (path.charAt(0) !== '/') {
// when bundled with webpack (in nextjs at least), we get an invalid URL here where only the pathname is filled in
// this pathname is additionally relative, so we must resolve it
path = resolvePath(path)
}

// reconstruct the URL but only as a string and have node parse it using platform-specific code
// if we reconstructed it as URL here we would get an mismatch because somehow there are two URL classes when bundled for next
Expand Down
11 changes: 8 additions & 3 deletions packages/nodejs_host/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ import { fileURLToPath } from 'node:url';
function coreWasmPath(): string {
// use new URL constructor to get webpack to bundle the asset and non-bundled code to work correctly in all contexts
const url = new URL('../assets/core-async.wasm', import.meta.url)
// resolve in case we get a relative path, like with next
// if we reconstructed it as URL here we would get an mismatch because somehow there are two URL classes when bundled for next
const path = resolvePath(url.pathname)

let path = url.pathname;
if (path.charAt(0) !== '/') {
// when bundled with webpack (in nextjs at least), we get an invalid URL here where only the pathname is filled in
// this pathname is additionally relative, so we must resolve it
path = resolvePath(path)
}

// reconstruct the URL but only as a string and have node parse it using platform-specific code
// if we reconstructed it as URL here we would get an mismatch because somehow there are two URL classes when bundled for next
return fileURLToPath(`file://${path}`)
}

Expand Down

0 comments on commit 3dbb282

Please sign in to comment.