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

Fix audio worklet import #143

Merged
merged 9 commits into from
Dec 23, 2024
Prev Previous commit
Next Next commit
feat: allow loading audio worklets from node_modules
b-ma committed Dec 23, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 4c09a79b6adfb9dbe4129519210e3c09251911b6
19 changes: 14 additions & 5 deletions js/AudioWorklet.js
Original file line number Diff line number Diff line change
@@ -40,7 +40,11 @@ const resolveModule = async (moduleUrl) => {
let absPathname = null;

if (existsSync(moduleUrl)) {
absPathname = path.join(process.cwd(), moduleUrl);
if (path.isAbsolute(moduleUrl)) {
absPathname = moduleUrl;
} else { // moduleUrl is relative to process.cwd();
absPathname = path.join(process.cwd(), moduleUrl);
}
} else if (moduleUrl.startsWith('http')) {
try {
const res = await fetch(moduleUrl);
@@ -56,7 +60,6 @@ const resolveModule = async (moduleUrl) => {
throw new DOMException(`Failed to execute 'addModule' on 'AudioWorklet': ${err.message}`, 'AbortError');
}
} else {
// get caller site from error stack trace
const callerSite = caller(2);

if (callerSite.startsWith('http')) { // this branch exists for wpt where caller site is an url
@@ -78,14 +81,20 @@ const resolveModule = async (moduleUrl) => {
throw new DOMException(`Failed to execute 'addModule' on 'AudioWorklet': ${err.message}`, 'AbortError');
}
} else {
const dirname = callerSite.substr(0, callerSite.lastIndexOf(path.sep));
// filesystem, relative to caller site or in node_modules
const dirname = callerSite.substring(0, callerSite.lastIndexOf(path.sep));
const absDirname = dirname.replace('file://', '');
const pathname = path.join(absDirname, moduleUrl);

if (existsSync(pathname)) {
if (existsSync(pathname)) { // relative to caller site
absPathname = pathname;
} else {
throw new DOMException(`Failed to execute 'addModule' on 'AudioWorklet': Cannot resolve module ${moduleUrl}`, 'AbortError');
try {
// try resolve according to process.cwd()
absPathname = require.resolve(moduleUrl, { paths: [process.cwd()] });
} catch (err) {
throw new DOMException(`Failed to execute 'addModule' on 'AudioWorklet': Cannot resolve module ${moduleUrl}`, 'AbortError');
}
}
}
}