Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

npm packages use web worker #13209

Closed
7 tasks done
ywenhao opened this issue May 15, 2023 · 3 comments
Closed
7 tasks done

npm packages use web worker #13209

ywenhao opened this issue May 15, 2023 · 3 comments
Labels
duplicate This issue or pull request already exists

Comments

@ywenhao
Copy link

ywenhao commented May 15, 2023

Describe the bug

I want to use web worker to develop a tool library in vite.
I use new URL for new Worker in npm packages.

export function getPreHashWorker(file: File) {
  const workURL = new URL('./worker/preHash.worker.ts', import.meta.url)

  return createWorkPromise<{ file: File }, { hash: string }>(workURL, { file })
}

export async function createWorkPromise<Params, Result>(workURL: URL, params: Params): Promise<Result> {
  return new Promise((resolve, reject) => {
    const worker = new Worker(workURL, {
      type: 'module',
    })
    worker.onmessage = (event) => {
      resolve(event.data)
    }
    worker.onerror = (event) => {
      reject(event)
    }
    worker.postMessage(params)
  })
}

image
image
image

image

This is my npm packages unbuild config.ts

import { defineBuildConfig } from 'unbuild'

export default defineBuildConfig({
  entries: [
    'src/index',
    {
      builder: 'mkdist',
      input: './src/utils/worker/',
      outDir: './dist/worker',
      ext: 'ts',
    },
  ],
  declaration: true,
  clean: true,
  rollup: {
    emitCJS: true,
  },
})

Reproduction

https://stackblitz.com/edit/vitejs-vite-1szu71?file=src/App.vue

Steps to reproduce

No response

System Info

System:
    OS: Windows 10 10.0.22000
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i5-11320H @ 3.20GHz
    Memory: 5.31 GB / 15.79 GB
  Binaries:
    Node: 18.15.0 - D:\Program Files\nodejs\node.EXE
    Yarn: 1.22.19 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 9.6.6 - D:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.22000.120.0), Chromium (113.0.1774.42)
    Internet Explorer: 11.0.22000.120
  npmPackages:
    @vitejs/plugin-vue: ^4.0.0 => 4.1.0
    vite: ^4.2.0 => 4.2.0

Used Package Manager

pnpm

Logs

No response

Validations

@stackblitz
Copy link

stackblitz bot commented May 15, 2023

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@patak-dev
Copy link
Member

Duplicate of #8427

@patak-dev patak-dev marked this as a duplicate of #8427 May 24, 2023
@patak-dev patak-dev closed this as not planned Won't fix, can't repro, duplicate, stale May 24, 2023
@patak-dev patak-dev added duplicate This issue or pull request already exists and removed pending triage labels May 24, 2023
@ywenhao
Copy link
Author

ywenhao commented May 26, 2023

Thanks.I can use optimizeDeps.exclude in vite.config.ts

@github-actions github-actions bot locked and limited conversation to collaborators Jun 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants