Skip to content

Commit

Permalink
feat!: change filter pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Nov 1, 2023
1 parent 0102225 commit b2d7e3d
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 14 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ npm i -D vite-plugin-sharedworker

```ts
// vite.config.ts

import { defineConfig } from 'vite'
import SharedWorker from 'vite-plugin-sharedworker'

Expand All @@ -25,7 +26,7 @@ export default defineConfig({

## Usage

Create a directory used for shared workers (default is `./worker/`). All the scripts in this directory will be transformed as RPC shared worker.
All the scripts which endswith `.sharedworker.ts` or `.sharedworker.js` will be transformed as RPC shared worker.

You can just write functions and export them like what you usually do.

Expand Down
5 changes: 3 additions & 2 deletions packages/vite-plugin-sharedworker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ npm i -D vite-plugin-sharedworker

```ts
// vite.config.ts

import { defineConfig } from 'vite'
import SharedWorker from 'vite-plugin-sharedworker'

Expand All @@ -25,12 +26,12 @@ export default defineConfig({

## Usage

Create a directory used for shared workers (default is `./worker/`). All the scripts in this directory will be transformed as RPC shared worker.
All the scripts which endswith `.sharedworker.ts` or `.sharedworker.js` will be transformed as RPC shared worker.

You can just write functions and export them like what you usually do.

```ts
// worker/index.ts
// src/math.sharedworker.ts

export async function add(a: number, b: number) {
return a + b
Expand Down
35 changes: 27 additions & 8 deletions packages/vite-plugin-sharedworker/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
import { type Plugin, normalizePath } from 'vite';
import { type Plugin } 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 ?? {};
export interface SharedWorkerOptions {
/**
* @default [/\.sharedworker\.(js|ts)$/]
*/
include?: Array<string | RegExp>;
}

export default function SharedWorker(options: SharedWorkerOptions = {}): Plugin {
const { include = [/\.sharedworker\.(js|ts)/] } = options ?? {};

const filter = (id: string) => {
const text = id.replace(/\?[^=]+=[^=]+(&[^=]+=[^=]+)*$/, '');
for (const pat of include) {
if (typeof pat === 'string') {
if (text.includes(pat)) {
return true;
}
} else {
if (pat.test(text)) {
return true;
}
}
}
return false;
};

return {
name: 'vite-plugin-sharedworker:client',
enforce: 'pre',
configResolved(config) {
root = normalizePath(path.join(config.root, workerRoot));
},
transform(code, id) {
if (id.startsWith(root)) {
if (filter(id)) {
const isClient = !id.endsWith('worker_file');

const exports = findExports(code)
Expand Down
2 changes: 1 addition & 1 deletion playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"@onekuma/reset": "^0.1.11",
"unocss": "^0.57.1",
"vite": "^4.5.0",
"vite-plugin-info": "^0.4.1",
"unplugin-info": "^0.6.0",
"vite-plugin-sharedworker": "workspace:*"
}
}
2 changes: 1 addition & 1 deletion playground/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'uno.css';
import '@onekuma/reset/tailwind.css';

import { add, sub, dispatch, addMessageListener } from '../worker';
import { add, sub, dispatch, addMessageListener } from './math.sharedworker';

addMessageListener((payload) => {
console.log('Receive:', payload);
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion playground/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import Uno from 'unocss/vite';

import BuildInfo from 'vite-plugin-info';
import SharedWorker from 'vite-plugin-sharedworker';
import SharedWorker from '../packages/vite-plugin-sharedworker/src';

export default defineConfig({
plugins: [
Expand Down

0 comments on commit b2d7e3d

Please sign in to comment.