Skip to content

Commit 3fe17aa

Browse files
authored
Merge pull request #18 from tapava/develop
docs: added minification warning for remoteDependencies
2 parents 5ec6db9 + cb4730b commit 3fe17aa

6 files changed

Lines changed: 72 additions & 5 deletions

File tree

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,12 +390,32 @@ const kit = new ComputeKit({
390390
],
391391
});
392392

393+
// Declare the global type for TypeScript support
394+
declare const _: typeof import('lodash');
395+
393396
kit.register('processData', (data: number[]) => {
394-
// @ts-ignore - lodash loaded via importScripts
395397
return _.chunk(data, 3);
396398
});
397399
```
398400

401+
> ⚠️ **Important: Minification Compatibility**
402+
>
403+
> When using remote dependencies, use `declare const` instead of `import` to ensure compatibility with production minifiers (Vite, esbuild, etc.).
404+
>
405+
> ```typescript
406+
> // ✅ Correct - works after minification
407+
> declare const dayjs: typeof import('dayjs');
408+
> kit.register('format', (d) => dayjs(d).format());
409+
>
410+
> // ❌ Incorrect - breaks after minification
411+
> import dayjs from 'dayjs';
412+
> kit.register('format', (d) => dayjs(d).format());
413+
> ```
414+
>
415+
> This is because minifiers rename imported variables but preserve free variables declared with `declare const`.
416+
417+
````
418+
399419
#### Methods
400420
401421
| Method | Description |
@@ -414,7 +434,7 @@ await kit.run('myFunction', data, {
414434
signal: abortController.signal, // Abort support
415435
onProgress: (p) => {}, // Progress callback
416436
});
417-
```
437+
````
418438
419439
---
420440

docs/api-reference.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ new ComputeKit(options?: ComputeKitOptions)
4444
| `useSharedMemory` | `boolean` | `true` | Use SharedArrayBuffer when available |
4545
| `remoteDependencies` | `string[]` | `[]` | External scripts to load in workers |
4646

47+
{: .note }
48+
49+
> When using `remoteDependencies`, use `declare const` instead of `import` for the libraries. See [Using External Libraries]({{ site.baseurl }}/examples#using-external-libraries) for details on minification compatibility.
50+
4751
---
4852

4953
## Typed Registry

docs/examples.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,13 +376,15 @@ const kit = new ComputeKit({
376376
],
377377
});
378378

379+
// Declare globals for TypeScript support (and minification compatibility!)
380+
declare const math: typeof import('mathjs');
381+
declare const _: typeof import('lodash');
382+
379383
kit.register('advancedMath', (expression: string) => {
380-
// @ts-ignore - math.js loaded via importScripts
381384
return math.evaluate(expression);
382385
});
383386

384387
kit.register('processData', (data: number[]) => {
385-
// @ts-ignore - lodash loaded via importScripts
386388
return _.chain(data)
387389
.filter((n) => n > 0)
388390
.map((n) => n * 2)
@@ -394,6 +396,22 @@ const result = await kit.run('advancedMath', 'sqrt(16) + sin(pi/2)');
394396
console.log(result); // 5
395397
```
396398

399+
{: .warning }
400+
401+
> **Important: Minification Compatibility**
402+
>
403+
> Always use `declare const` instead of `import` for libraries loaded via `remoteDependencies`. Minifiers rename imported variables, breaking function serialization in production builds.
404+
>
405+
> ```typescript
406+
> // ✅ Correct - preserved after minification
407+
> declare const dayjs: typeof import('dayjs');
408+
> kit.register('format', (d) => dayjs(d).format());
409+
>
410+
> // ❌ Incorrect - "dayjs" gets renamed to "a" or similar
411+
> import dayjs from 'dayjs';
412+
> kit.register('format', (d) => dayjs(d).format());
413+
> ```
414+
397415
---
398416
399417
## Live Demo

docs/getting-started.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ const kit = new ComputeKit({
154154
'https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js',
155155
],
156156
});
157+
158+
// When using remoteDependencies, declare globals instead of importing
159+
declare const _: typeof import('lodash');
160+
161+
kit.register('chunk', (arr: number[]) => _.chunk(arr, 2));
157162
```
158163

159164
---

docs/react-hooks.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ function App() {
5252
| `debug` | `boolean` | `false` | Enable debug logging |
5353
| `remoteDependencies` | `string[]` | `[]` | External scripts for workers |
5454

55+
{: .note }
56+
57+
> When using `remoteDependencies`, use `declare const` instead of `import` for external libraries to ensure minification compatibility. See the [Examples page]({{ site.baseurl }}/examples#using-external-libraries) for details.
58+
5559
---
5660

5761
## useComputeKit

packages/core/README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,31 @@ const kit = new ComputeKit({
5858
],
5959
});
6060

61+
// Declare the global type for TypeScript support
62+
declare const _: typeof import('lodash');
63+
6164
// Now you can use lodash inside your compute functions
6265
kit.register('processData', (data: number[]) => {
63-
// @ts-ignore - lodash is loaded via importScripts
6466
return _.chunk(data, 3);
6567
});
6668
```
6769

6870
**Note:** Remote scripts must be served with proper CORS headers.
6971

72+
> ⚠️ **Minification Warning**
73+
>
74+
> Use `declare const` instead of `import` for remote dependencies. Minifiers rename imported variables, which breaks function serialization:
75+
>
76+
> ```typescript
77+
> // ✅ Works after minification
78+
> declare const dayjs: typeof import('dayjs');
79+
> kit.register('format', (d) => dayjs(d).format());
80+
>
81+
> // ❌ Breaks after minification
82+
> import dayjs from 'dayjs';
83+
> kit.register('format', (d) => dayjs(d).format());
84+
> ```
85+
7086
### `kit.register(name, fn)`
7187
7288
Register a function to run in workers.

0 commit comments

Comments
 (0)