Skip to content

Commit

Permalink
fix(web): 🐛 Parcel build issue for DotLottieWorker (#335)
Browse files Browse the repository at this point in the history
Parcel build was failing in version 0.30.3 due to the usage of a string
literal to create a Worker, which is not supported. This patch modifies
the worker instantiation to use a Blob instead, ensuring compatibility
with Parcel's requirements.
  • Loading branch information
theashraf authored Aug 22, 2024
1 parent 03311db commit e34ac54
Show file tree
Hide file tree
Showing 7 changed files with 1,683 additions and 190 deletions.
5 changes: 5 additions & 0 deletions .changeset/neat-falcons-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lottiefiles/dotlottie-web': patch
---

fix(web): 🐛 Resolved Parcel build failure by creating Worker using a Blob instead of a string literal.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,5 @@ tsdoc-metadata.json

# Typescript
*.tsbuildinfo

.parcel-cache/
12 changes: 12 additions & 0 deletions apps/with-parcel-example/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Parcel</title>
</head>
<body>
<canvas id="dotLottie-canvas"></canvas>
<script src="./src/main.js" type="module"></script>
</body>
</html>
16 changes: 16 additions & 0 deletions apps/with-parcel-example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "with-parcel-example",
"version": "0.0.0",
"type": "module",
"private": true,
"scripts": {
"build": "parcel build index.html --no-cache",
"dev": "parcel index.html --no-cache --open"
},
"dependencies": {
"@lottiefiles/dotlottie-web": "workspace:*"
},
"devDependencies": {
"parcel": "^2.12.0"
}
}
10 changes: 10 additions & 0 deletions apps/with-parcel-example/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { DotLottieWorker } from '@lottiefiles/dotlottie-web';

// eslint-disable-next-line no-new
new DotLottieWorker({
// eslint-disable-next-line no-undef
canvas: document.getElementById('dotLottie-canvas'),
src: 'https://lottie.host/e641272e-039b-4612-96de-138acfbede6e/bc0sW78EeR.lottie',
autoplay: true,
loop: true,
});
17 changes: 10 additions & 7 deletions packages/web/esbuild-plugins/plugin-inline-worker.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,25 @@ const PluginInlineWorker = (opt = {}, filter = /\?worker&inline$/u) => {
}

const { contents } = outputFiles[0];
const base64 = Buffer.from(contents).toString('base64');
const uint8Array = new Uint8Array(contents);

return {
loader: 'js',
contents: `
"use client";
export default class InlinedWorker {
class InlineWorker {
constructor() {
if (typeof window === 'undefined') {
throw new Error('Worker is not available in this environment.');
if (typeof Worker === 'undefined') {
throw new Error('Worker is not supported in this environment.');
}
return new Worker('data:application/javascript;base64,${base64}', {
type: '${opt.format === 'esm' ? 'module' : 'classic'}'
});
const blob = new Blob([new Uint8Array([${uint8Array.join(',')}])], { type: 'application/javascript' });
const url = URL.createObjectURL(blob);
const worker = new Worker(url);
URL.revokeObjectURL(url);
return worker;
}
}
export default InlineWorker;
`,
};
});
Expand Down
Loading

0 comments on commit e34ac54

Please sign in to comment.