Repository: prisma/prisma
File: packages/client-generator-ts/src/utils/wasm.ts (TypeScript)

Code before:
export type BuildWasmModuleOptions = {
  runtimeName: RuntimeName
  runtimeBase: string
  target: RuntimeTargetInternal
  activeProvider: ActiveConnectorType
  moduleFormat: ModuleFormat
}

const debug = Debug('prisma:client-generator-ts:wasm')

function usesEdgeWasmRuntime(runtimeName: RuntimeName) {
  return runtimeName === 'wasm-compiler-edge'
}

export function buildGetWasmModule({ runtimeName, runtimeBase, activeProvider, moduleFormat }: BuildWasmModuleOptions) {
  const component = 'compiler'
  const capitalizedComponent = capitalize(component)

  const extension = match(moduleFormat)
    .with('esm', () => 'mjs')
    .with('cjs', () => 'js')
    .exhaustive()

  const buildNonEdgeLoader = match(runtimeName)
    .with('client', () => component === 'compiler')
    .otherwise(() => false)

  const buildEdgeLoader = usesEdgeWasmRuntime(runtimeName)

  let wasmPathBase: string
  let wasmBindingsPath: string
  let wasmModulePath: string

  // When using import statements to load the Wasm modules, we put the related
  // files in the generated client directory since some bundlers act weird when
  // importing them from the `@prisma/client` package. We can then rely on the
  // bundler to copy the WebAssembly module to the output directory and modify
  // the paths as needed.
  //
  // Conversely, when import base64-encoded Wasm definitions in Node.js-like environments,
  // potential usage of bundlers (or even transpiling TS with tsc into a
  // different directory!) would break loading the modules if we copied
  // them to the generated client directory because they would not be where we
  // expect them to be anymore. Therefore, we don't copy anything when
  // generating code for `library` or `client` runtimes, and we always load from
  // `@prisma/client/runtime` when doing it manually with `fs.readFile`. The
  // issues that can arise when importing Wasm from node_modules with bundlers
  // don't apply in this case because we are not *importing* them, we're just
  // reading a file on disk.
  if (buildEdgeLoader) {
    wasmPathBase = `./query_${component}_bg`

Review comment (suggestion):
```suggestion
  const buildNonEdgeLoader = runtimeName === 'client'
```

Code after:
  runtimeName: RuntimeName
  runtimeBase: string
  target: RuntimeTargetInternal
  activeProvider: ActiveConnectorType
  moduleFormat: ModuleFormat
}

const debug = Debug('prisma:client-generator-ts:wasm')

function usesEdgeWasmRuntime(runtimeName: RuntimeName) {
  return runtimeName === 'wasm-compiler-edge'
}

export function buildGetWasmModule({ runtimeName, runtimeBase, activeProvider, moduleFormat }: BuildWasmModuleOptions) {
  const extension = match(moduleFormat)
    .with('esm', () => 'mjs')
    .with('cjs', () => 'js')
    .exhaustive()

  const buildNonEdgeLoader = runtimeName === 'client'
  const buildEdgeLoader = !buildNonEdgeLoader

  let wasmPathBase: string
  let wasmBindingsPath: string
  let wasmModulePath: string

  // When using import statements to load the Wasm modules, we put the related
  // files in the generated client directory since some bundlers act weird when
  // importing them from the `@prisma/client` package. We can then rely on the
  // bundler to copy the WebAssembly module to the output directory and modify
  // the paths as needed.
  //
  // Conversely, when import base64-encoded Wasm definitions in Node.js-like environments,
  // potential usage of bundlers (or even transpiling TS with tsc into a
  // different directory!) would break loading the modules if we copied
  // them to the generated client directory because they would not be where we
  // expect them to be anymore. Therefore, we don't copy anything when
  // generating code for `library` or `client` runtimes, and we always load from
  // `@prisma/client/runtime` when doing it manually with `fs.readFile`. The
  // issues that can arise when importing Wasm from node_modules with bundlers
  // don't apply in this case because we are not *importing* them, we're just
  // reading a file on disk.
  if (buildEdgeLoader) {
    wasmPathBase = `./query_compiler_bg`
    wasmBindingsPath = `${wasmPathBase}.js`
    wasmModulePath = `${wasmPathBase}.wasm`
  } else {
    wasmPathBase = `${runtimeBase}/query_compiler_bg.${activeProvider}`
    wasmBindingsPath = `${wasmPathBase}.mjs`
    wasmModulePath = `${wasmPathBase}.wasm`
  }