Skip to content

Commit

Permalink
v5 refactoring (#140)
Browse files Browse the repository at this point in the history
* inital v5

* add symbol export, check ../../

* ff

* version bump
  • Loading branch information
mathe42 authored May 26, 2024
1 parent ac52f91 commit 200dae2
Show file tree
Hide file tree
Showing 11 changed files with 551 additions and 128 deletions.
123 changes: 78 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> This plugins requires vite >=2.8 for WebWorkers and vite >= 2.9.6 for shared worker to work properly.
Use WebWorkers with comlink.
Use WebWorkers with comlink.

This plugin removes the need to call `expose`, `wrap` from comlink and also you don't need to create the worker on your own.

Expand All @@ -14,11 +14,9 @@ Make sure that you change the worker plugin config to a function for example lik
// vite.config.js
export default {
worker: {
plugins: () => ([
comlink()
])
}
}
plugins: () => [comlink()],
},
};
```

see https://github.com/vitejs/vite/pull/14685 for details.
Expand All @@ -31,95 +29,119 @@ npm i --save comlink # yarn add comlink
```

### Comlink install

As you don't want to wait for a new release for this plugin when a new version of comlink is released, this plugin has comlink as a peer dependency so you can install the version of comlink that you need.

### Add it to vite.config.js

```ts
// vite.config.js
import { comlink } from 'vite-plugin-comlink'
import { comlink } from "vite-plugin-comlink";

export default {
plugins: [
comlink()
],
plugins: [comlink()],
worker: {
plugins: () => ([
comlink()
])
}
}
plugins: () => [comlink()],
},
};
```

### Plugin order
Put this plugin as one of the first plugins. Only other plugins that create `ComlinkWorker` or `ComlinkSharedWorker` or transform files based on the existence of `ComlinkWorker` or `ComlinkSharedWorker` should come before this plugin!

## Usage
Put this plugin as one of the first plugins. Only other plugins that create `ComlinkWorker` or `ComlinkSharedWorker` or transform files based on the existence of `ComlinkWorker` or `ComlinkSharedWorker` should come before this plugin!

## Usage

```ts
// worker.js
export const add = (a: number, b: number) => a + b
export const add = (a: number, b: number) => a + b;

// main.ts

// Create Worker
const instance = new ComlinkWorker(new URL('./worker.js', import.meta.url), {/* normal Worker options*/})
const result = await instance.add(2, 3)

result === 5
const instance = new ComlinkWorker(new URL("./worker.js", import.meta.url), {
/* normal Worker options*/
});
const result = await instance.add(2, 3);

result === 5;

// Create SharedWorker
const instance = new ComlinkSharedWorker(new URL('./worker.js', import.meta.url), {/* normal Worker options*/})
const result = await instance.add(2, 3)
const instance = new ComlinkSharedWorker(
new URL("./worker.js", import.meta.url),
{
/* normal Worker options*/
}
);
const result = await instance.add(2, 3);

result === 5
result === 5;
```

### With TypeScript
Add

Add

```ts
/// <reference types="vite-plugin-comlink/client" />
```

to your vite-env.d.ts file to make sure typescript will use `vite-plugin-comlink/client`.

```ts
// worker.ts
export const add = (a: number, b: number) => a + b
export const add = (a: number, b: number) => a + b;

// main.ts

// Create Worker
const instance = new ComlinkWorker<typeof import('./worker')>(new URL('./worker', import.meta.url), {/* normal Worker options*/})
const result = await instance.add(2, 3)

result === 5
const instance = new ComlinkWorker<typeof import("./worker")>(
new URL("./worker", import.meta.url),
{
/* normal Worker options*/
}
);
const result = await instance.add(2, 3);

result === 5;

// Create SharedWorker
const instance = new ComlinkSharedWorker<typeof import('./worker')>(new URL('./worker', import.meta.url), {/* normal Worker options*/})
const result = await instance.add(2, 3)
const instance = new ComlinkSharedWorker<typeof import("./worker")>(
new URL("./worker", import.meta.url),
{
/* normal Worker options*/
}
);
const result = await instance.add(2, 3);

result === 5
result === 5;
```

### Get Worker Instance

You can get to the worker instance like this:

```ts
import { endpointSymbol } from 'vite-plugin-comlink/symbol'
import { endpointSymbol } from "vite-plugin-comlink/symbol";

const api = new ComlinkWorker<typeof import('./worker')>(new URL('./worker', import.meta.url), {/* normal Worker options*/})
const api = new ComlinkWorker<typeof import("./worker")>(
new URL("./worker", import.meta.url),
{
/* normal Worker options*/
}
);
const worker = api[endpointSymbol];
```

## Module Worker

Not all Browsers support module Workers (see https://caniuse.com/mdn-api_worker_worker_ecmascript_modules).

This results in some drawbacks for fastest and best support:

For fast development we use module Workers as bundling the complete worker on the fly is not performant.

In default settings we bundle the whole worker at build to a single file. Therefore all browsers that support Workers, work in production.
In default settings we bundle the whole worker at build to a single file. Therefore all browsers that support Workers, work in production.

This is the same behavior as vite and it is NOT CHANGEABLE!

Expand All @@ -131,17 +153,28 @@ If you want a worker to be a module worker in production, add `type: 'module'` t
2. In production all browsers are supported

## Breaking changes

### v2 to v3
* remove of customConfigs breaking FF support in development for some projects and removing the abbility for inline worker. This is a limitation of vite so if vite adds support of it this plugin will follow
* remove of typefile. For typescript support please write your own type file or switch to the new syntax.
* remove of ServiceWorker support. This was considered unstable an it was hacky so it got removed. If vite adds support for building ServiceWorker this will be added!
* you have to add comlink to `worker.plugins` array.

- remove of customConfigs breaking FF support in development for some projects and removing the abbility for inline worker. This is a limitation of vite so if vite adds support of it this plugin will follow
- remove of typefile. For typescript support please write your own type file or switch to the new syntax.
- remove of ServiceWorker support. This was considered unstable an it was hacky so it got removed. If vite adds support for building ServiceWorker this will be added!
- you have to add comlink to `worker.plugins` array.

### v3 to v4
* the import syntax will be removed you have to switch to the new syntax!
* Removal of Warnings of leagacy (v2) options
* ESM support
* Better Source Maps

- the import syntax will be removed you have to switch to the new syntax!
- Removal of Warnings of leagacy (v2) options
- ESM support
- Better Source Maps

### v4 to v5

- some undocumented internal options got removed.
- full rewrite of the core transformer to fix a lot of bugs
- should be breaking free but as this is a big rewrite there might be breaking changes

## Resources

https://github.com/GoogleChromeLabs/comlink
https://github.com/surma/rollup-plugin-comlink
Loading

0 comments on commit 200dae2

Please sign in to comment.