-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
feat: support node workers out of the box #3932
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
Closed
Closed
Changes from 10 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
3cee968
feat: support node workers out of the box
prathameshnetake 1e03217
chore: fix conflicts
prathameshnetake 220db24
Merge branch 'vitejs:main' into main
prathameshnetake e4aec89
Merge branch 'vitejs:main' into main
prathameshnetake 784ecd1
Merge branch 'vitejs:main' into main
prathameshnetake a5a192b
Merge branch 'vitejs:main' into main
prathameshnetake cf09753
feat: Start tests for worker_thread node
prathameshnetake 432ca53
chore: merge master
prathameshnetake 13472aa
feat: Add support for node worker threads inline and file chunk
prathameshnetake c278201
wip: Support node worker threads tests
prathameshnetake 0f39f11
feat: worker_threads node tests complete
prathameshnetake e10dffb
refactor: add isTargetNode utility function
prathameshnetake a4a72b7
refactor: suggested code changes
prathameshnetake 4820e7b
fix: token error with inline worker if minify is false
prathameshnetake 0fc8032
refactor: revert worker type from union to web worker
prathameshnetake b434633
refactor: remove unwanted imports
prathameshnetake aafe437
feat: node worker docs
prathameshnetake 76a1368
fix: merge conflict
prathameshnetake 314fc94
Merge branch 'main' into main
prathameshnetake File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
packages/playground/worker-thread-node/__tests__/worker-thread-node.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { run } from '../dist/main.cjs' | ||
| import { isBuild } from '../../testUtils' | ||
|
|
||
| if (isBuild) { | ||
| test('should return result from worker thread', async () => { | ||
| const a = await run('ping') | ||
| expect(a).toBe('pong') | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import MyWorker from './worker?worker' | ||
| import InlineWorker from './worker-inline?worker&inline' | ||
| import { Worker } from 'worker_threads' | ||
|
|
||
| export const run = async (message: string): Promise<string> => { | ||
| return new Promise((resolve, reject) => { | ||
| const worker = new MyWorker() as Worker | ||
| worker.postMessage(message) | ||
| worker.on('message', (msg) => { | ||
| resolve(msg) | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| export const inlineWorker = async (message: string): Promise<string> => { | ||
| return new Promise((resolve, reject) => { | ||
| const worker = new InlineWorker() as Worker | ||
| worker.postMessage(message) | ||
| worker.on('message', (msg) => { | ||
| resolve(msg) | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| run('ping').then((a) => { | ||
| console.log(a) | ||
| }) | ||
|
|
||
| inlineWorker('ping').then((a) => { | ||
| console.log(a) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "name": "worker-thread-node", | ||
| "private": true, | ||
| "version": "0.0.0", | ||
| "scripts": { | ||
| "dev": "vite", | ||
| "build": "node scripts/build.js", | ||
| "start": "node dist/main.cjs.js" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| const vite = require('vite') | ||
| const { build } = vite | ||
| build({ configFile: 'vite.config.ts' }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { defineConfig } from 'vite' | ||
| import { builtinModules } from 'module' | ||
|
|
||
| export default defineConfig({ | ||
| build: { | ||
| target: 'node16', | ||
| outDir: 'dist', | ||
| lib: { | ||
| entry: 'main.ts', | ||
| formats: ['cjs'], | ||
| fileName: 'main' | ||
| }, | ||
| rollupOptions: { | ||
| external: [...builtinModules] | ||
| }, | ||
| emptyOutDir: true | ||
| } | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { parentPort } from 'worker_threads' | ||
|
|
||
| parentPort.on('message', () => { | ||
| parentPort.postMessage('this is inline node worker') | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { parentPort } from 'worker_threads' | ||
|
|
||
| parentPort.on('message', (message) => { | ||
| parentPort.postMessage('pong') | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.