-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
92 additions
and
78 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,3 +1,47 @@ | ||
export function hello() { | ||
return 'world'; | ||
} | ||
import { type Plugin, normalizePath } from 'vite'; | ||
|
||
import path from 'path'; | ||
import { findExports } from 'mlly'; | ||
|
||
export default function SharedWorker(options?: { root?: string }): Plugin { | ||
let root = process.cwd(); | ||
const { root: workerRoot = './worker' } = options ?? {}; | ||
|
||
return { | ||
name: 'vite-plugin-sharedworker:client', | ||
enforce: 'pre', | ||
configResolved(config) { | ||
root = normalizePath(path.join(config.root, workerRoot)); | ||
}, | ||
transform(code, id) { | ||
if (id.startsWith(root)) { | ||
const isClient = !id.endsWith('worker_file'); | ||
|
||
// console.log(id); | ||
if (isClient) { | ||
const exports = findExports(code); | ||
|
||
return [ | ||
`const worker = new SharedWorker(new URL(${JSON.stringify( | ||
id | ||
)}, import.meta.url), { type: 'module', name: 'worker' });`, | ||
`worker.port.start()`, | ||
...exports | ||
.filter((ex) => ex.name) | ||
.map((ex) => `export const ${ex.name} = (...args: any[]) => {}`) | ||
].join('\n'); | ||
} else { | ||
const imports = [ | ||
`// vite-plugin-sharedworker starts`, | ||
`import { defineSharedWorker } from 'vite-plugin-sharedworker/runtime'`, | ||
`defineSharedWorker(self)`, | ||
`// vite-plugin-sharedworker ends`, | ||
'' | ||
]; | ||
|
||
return imports.join('\n') + '\n' + code; | ||
} | ||
} | ||
} | ||
}; | ||
} |
This file contains 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 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 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 |
---|---|---|
@@ -1,17 +1,16 @@ | ||
const worker1 = new SharedWorker(new URL('./worker.ts', import.meta.url), { | ||
type: 'module' | ||
}); | ||
|
||
worker1.port.start(); | ||
import { add } from './worker'; | ||
|
||
worker1.port.postMessage('hello 1'); | ||
console.log(add(1, 2)); | ||
|
||
const worker2 = new SharedWorker(new URL('./worker.ts', import.meta.url), { | ||
type: 'module' | ||
const worker = new SharedWorker(new URL('./worker/index.ts', import.meta.url), { | ||
type: 'module', | ||
name: 'worker' | ||
}); | ||
|
||
worker2.port.start(); | ||
worker.port.start(); | ||
|
||
worker.port.postMessage('hello 1'); | ||
|
||
worker2.port.postMessage('hello 2'); | ||
console.log('OK'); | ||
|
||
export {}; |
This file contains 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 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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import { defineConfig } from 'vite'; | ||
|
||
import BuildInfo from 'vite-plugin-info'; | ||
import SharedWorkerClient from 'vite-plugin-sharedworker'; | ||
|
||
export default defineConfig({ | ||
plugins: [BuildInfo()] | ||
plugins: [BuildInfo(), SharedWorkerClient()] | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains 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 @@ | ||
// /// <reference lib="WebWorker" /> | ||
// declare const self: SharedWorkerGlobalScope; | ||
// import { defineSharedWorker } from 'vite-plugin-sharedworker/runtime'; | ||
// defineSharedWorker(self); | ||
// export type {}; | ||
|
||
export function add(a: number, b: number) { | ||
return a + b; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.