Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audio Worklet support #1

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/helper/browser/createBase64WorkerFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ function createURL(base64, sourcemapArg, enableUnicodeArg) {

export function createBase64WorkerFactory(base64, sourcemapArg, enableUnicodeArg) {
var url;
return function WorkerFactory(options) {
return async function WorkerFactory(audioContext, options) {
url = url || createURL(base64, sourcemapArg, enableUnicodeArg);
return new Worker(url, options);
return await audioContext.audioWorklet.addModule(url, options);
};
}
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ const defaultConfig = {
preserveSource: false,
preserveFileNames: false,
enableUnicode: false,
pattern: /web-worker:(.+)/,
// Other work[er|let]s to keep track of:
// CSS Houdini Animation worket & Layout worklet
webWorkerPattern: /web-worker:(.+)/,
audioWorkletPattern: /audio-worklet:(.+)/,
paintWorkletPattern: /paint-worklet:(.+)/,
serviceWorkerPattern: /service-worker:(.+)/,
inline: true,
forceInline: false,
external: undefined,
Expand Down
125 changes: 78 additions & 47 deletions src/plugin/resolveId.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,63 +17,94 @@ function resolveModule(name, paths, extensions) {
return null;
}

function resolveTypedId(type, theMatch, state, config, importer) {
const name = theMatch[theMatch.length - 1];
if (!state.idMap.has(name)) {
let target = null;
if (importer) {
const folder = path.dirname(importer);
const paths = require.resolve.paths(importer);
paths.push(folder);
target = resolveModule(name, paths, config.extensions);
} else if (path.isAbsolute(name)) {
target = name;
}

if (target) {
const prefixed = `\0rollup-plugin-worker-loader::module:${config.forceInline ? `:${state.forceInlineCounter++}:` : ''}${target}`;
if (!state.idMap.has(prefixed)) {
const inputOptions = Object.assign({}, state.options, {
input: target,
});

let workerName;
if (config.preserveFileNames) {
const extension = path.extname(target);
workerName = path.basename(target, extension);
if (!state.outFiles.has(workerName)) {
state.outFiles.set(workerName, 0);
} else {
const duplicateCount = state.outFiles.get(workerName);
state.outFiles.set(workerName, duplicateCount + 1);
workerName += duplicateCount + 1;
}
} else {
workerName = `${type}-${state.idMap.size}`;
}

state.idMap.set(prefixed, {
workerID: `${workerName}.js`,
chunk: null,
inputOptions,
target,
type,
});
}

if (!state.exclude.has(prefixed)) {
return prefixed;
}
return target;
}
}
return null;
}

function resolveId(state, config, importee, importer) {
const match = importee.match(config.pattern);
if (importee.startsWith('\0rollup-plugin-web-worker-loader::helper')) {
if (config.forceInline) {
return `\0${state.forceInlineCounter++}::${importee.substr(1)}`;
}
return importee;
} else if (match && match.length) {
const name = match[match.length - 1];
if (!state.idMap.has(name)) {
let target = null;
if (importer) {
const folder = path.dirname(importer);
const paths = require.resolve.paths(importer);
paths.push(folder);
target = resolveModule(name, paths, config.extensions);
} else if (path.isAbsolute(name)) {
target = name;
}
}

if (target) {
const prefixed = `\0rollup-plugin-worker-loader::module:${config.forceInline ? `:${state.forceInlineCounter++}:` : ''}${target}`;
if (!state.idMap.has(prefixed)) {
const inputOptions = Object.assign({}, state.options, {
input: target,
});

let workerName;
if (config.preserveFileNames) {
const extension = path.extname(target);
workerName = path.basename(target, extension);
if (!state.outFiles.has(workerName)) {
state.outFiles.set(workerName, 0);
} else {
const duplicateCount = state.outFiles.get(workerName);
state.outFiles.set(workerName, duplicateCount + 1);
workerName += duplicateCount + 1;
}
} else {
workerName = `web-worker-${state.idMap.size}`;
}
const {
webWorkerPattern,
audioWorkletPattern,
paintWorkletPattern,
serviceWorkerPattern,
} = config;

state.idMap.set(prefixed, {
workerID: `${workerName}.js`,
chunk: null,
inputOptions,
target,
});
}
const webWorkerMatch = importee.match(webWorkerPattern);
if (webWorkerMatch) {
return resolveTypedId('web-worker', webWorkerMatch, state, config, importer);
}

if (!state.exclude.has(prefixed)) {
return prefixed;
}
return target;
}
}
const audioWorkletMatch = importee.match(audioWorkletPattern);
if (audioWorkletMatch) {
return resolveTypedId('audio-worklet', audioWorkletMatch, state, config, importer);
}

const paintWorkletMatch = importee.match(paintWorkletPattern);
if (paintWorkletMatch) {
return resolveTypedId('paint-worklet', paintWorkletMatch, state, config, importer);
}

const serviceWorkerMatch = importee.match(serviceWorkerPattern);
if (serviceWorkerMatch) {
return resolveTypedId('service-worker', serviceWorkerMatch, state, config, importer);
}

return null;
}

Expand Down