Skip to content

Commit 7bf3a09

Browse files
committed
add support for paint worklets
1 parent 5a7cca1 commit 7bf3a09

10 files changed

+91
-10
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import {createBase64PaintWorkletFactory as browserCreateBase64PaintWorkletFactory} from '\0rollup-plugin-web-worker-loader::helper::browser::createBase64AudioWorkletFactory';
2+
import {isNodeJS} from '\0rollup-plugin-web-worker-loader::helper::auto::isNodeJS';
3+
4+
export function createBase64PaintWorkletFactory(base64, sourcemapArg, enableUnicodeArg) {
5+
if (isNodeJS()) {
6+
throw new Error('rollup-plugin-web-worker-loader does not support Paint Worklet in Node.JS');
7+
}
8+
return browserCreateBase64PaintWorkletFactory(base64, sourcemapArg, enableUnicodeArg);
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {createInlinePaintWorkletFactory as browserCreateInlinePaintWorkletFactory} from '\0rollup-plugin-web-worker-loader::helper::browser::createInlinePaintWorkletFactory';
2+
import {isNodeJS} from '\0rollup-plugin-web-worker-loader::helper::auto::isNodeJS';
3+
4+
export function createInlinePaintWorkletFactory(fn, sourcemapArg) {
5+
if (isNodeJS()) {
6+
throw new Error('rollup-plugin-web-worker-loader does not support Paint Worklet in Node.JS');
7+
}
8+
return browserCreateInlinePaintWorkletFactory(fn, sourcemapArg);
9+
}
10+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {createURLPaintWorkletFactory as browserCreateURLPaintWorkletFactory} from '\0rollup-plugin-web-worker-loader::helper::browser::createURLPaintWorkletFactory';
2+
import {isNodeJS} from '\0rollup-plugin-web-worker-loader::helper::auto::isNodeJS';
3+
4+
export function createURLPaintWorkletFactory(url) {
5+
if (isNodeJS()) {
6+
throw new Error('rollup-plugin-web-worker-loader does not support Paint Worklet in Node.JS');
7+
}
8+
return browserCreateURLPaintWorkletFactory(url);
9+
}
10+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function decodeBase64(base64, enableUnicode) {
2+
var binaryString = atob(base64);
3+
if (enableUnicode) {
4+
var binaryView = new Uint8Array(binaryString.length);
5+
for (var i = 0, n = binaryString.length; i < n; ++i) {
6+
binaryView[i] = binaryString.charCodeAt(i);
7+
}
8+
return String.fromCharCode.apply(null, new Uint16Array(binaryView.buffer));
9+
}
10+
return binaryString;
11+
}
12+
13+
function createURL(base64, sourcemapArg, enableUnicodeArg) {
14+
var sourcemap = sourcemapArg === undefined ? null : sourcemapArg;
15+
var enableUnicode = enableUnicodeArg === undefined ? false : enableUnicodeArg;
16+
var source = decodeBase64(base64, enableUnicode);
17+
var start = source.indexOf('\n', 10) + 1;
18+
var body = source.substring(start) + (sourcemap ? '\/\/# sourceMappingURL=' + sourcemap : '');
19+
var blob = new Blob([body], { type: 'application/javascript' });
20+
return URL.createObjectURL(blob);
21+
}
22+
23+
export function createBase64PaintWorkletFactory(base64, sourcemapArg, enableUnicodeArg) {
24+
var url;
25+
return async function PaintWorkletFactory(options) {
26+
url = url || createURL(base64, sourcemapArg, enableUnicodeArg);
27+
return await CSS.paintWorklet.addModule(url, options);
28+
};
29+
}
30+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {funcToSource} from '\0rollup-plugin-web-worker-loader::helper::funcToSource';
2+
3+
function createURL(fn, sourcemapArg) {
4+
var lines = funcToSource(fn, sourcemapArg);
5+
var blob = new Blob(lines, { type: 'application/javascript' });
6+
return URL.createObjectURL(blob);
7+
}
8+
9+
export function createInlinePaintWorkletFactory(fn, sourcemapArg) {
10+
var url;
11+
return async function PaintWorkletFactory(options) {
12+
url = url || createURL(fn, sourcemapArg);
13+
return await CSS.paintWorklet.addModule(url, options);
14+
};
15+
}
16+

src/helper/browser/createURLAudioWorkletFactory.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export function createURLAudioWorkletrFactory(url) {
1+
export function createURLAudioWorkletFactory(url) {
22
return async function AudioWorkletFactory(audioContext, options) {
33
return await audioContext.audioWorklet.addModule(url, options);
44
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export function createURLPaintWorkletFactory(url) {
2+
return async function PaintWorkletFactory(options) {
3+
return await CSS.paintWorklet.addModule(url, options);
4+
};
5+
}
6+

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const defaultConfig = {
1414
enableUnicode: false,
1515
webWorkerPattern: /web-worker:(.+)/,
1616
audioWorkletPattern: /audio-worklet:(.+)/,
17-
// paintWorkletPattern: /paint-worklet:(.+)/,
17+
paintWorkletPattern: /paint-worklet:(.+)/,
1818
// animationWorkletPattern: /animation-worklet:(.+)/,
1919
// serviceWorkerPattern: /service-worker:(.+)/,
2020
inline: true,

src/plugin/resolveId.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ function getMatchAndType(importee, config) {
3232
match,
3333
};
3434
}
35-
// match = importee.match(config.paintWorkletPattern);
36-
// if (match) {
37-
// return {
38-
// type: "paint-worklet",
39-
// match
40-
// }
41-
// }
35+
match = importee.match(config.paintWorkletPattern);
36+
if (match) {
37+
return {
38+
type: 'paint-worklet',
39+
match,
40+
};
41+
}
4242
// match = importee.match(config.animationWorkletPattern);
4343
// if (match) {
4444
// return {

src/utils/buildWorkerCode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const kDefaultsOptions = {
88
const typeMap = {
99
'web-worker': 'Worker',
1010
'audio-worklet': 'AudioWorklet',
11-
// 'paint-worklet': 'PaintWorklet',
11+
'paint-worklet': 'PaintWorklet',
1212
// 'animation-worklet': 'AnimationWorklet',
1313
// 'service-worker': 'ServiceWorker'
1414
};

0 commit comments

Comments
 (0)