-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Loading script to AudioWorklet #1093
Comments
Maybe it needs to be added to Line 19 in 0140dce
|
@ronkot What happens currently? Does |
@mischnic |
This correctly detects the calls, but the emitted worklet file contains the hmr runtime. diff --git a/src/assets/JSAsset.js b/src/assets/JSAsset.js
index 7261c35..6fffbb5 100644
--- a/src/assets/JSAsset.js
+++ b/src/assets/JSAsset.js
@@ -16,7 +16,7 @@ const IMPORT_RE = /\b(?:import\b|export\b|require\s*\()/;
const GLOBAL_RE = /\b(?:process|__dirname|__filename|global|Buffer)\b/;
const FS_RE = /\breadFileSync\b/;
const SW_RE = /\bnavigator\s*\.\s*serviceWorker\s*\.\s*register\s*\(/;
-const WORKER_RE = /\bnew\s*Worker\s*\(/;
+const WORKER_RE = /\bnew\s*Worker\s*\(|audioWorklet\.addModule\s*\(/;
class JSAsset extends Asset {
constructor(name, pkg, options) {
diff --git a/src/visitors/dependencies.js b/src/visitors/dependencies.js
index e697db7..4652f81 100644
--- a/src/visitors/dependencies.js
+++ b/src/visitors/dependencies.js
@@ -61,6 +61,13 @@ module.exports = {
return;
}
+ if(types.isStringLiteral(args[0]) && types.isMemberExpression(callee) &&
+ callee.object.property && callee.object.property.name === "audioWorklet" &&
+ callee.property.name === "addModule") {
+ addURLDependency(asset, args[0]);
+ return;
+ }
+
const isRegisterServiceWorker =
types.isStringLiteral(args[0]) &&
matchesPattern(callee, serviceWorkerPattern); |
Any news on this? Could I help somehow - by providing a minimal working example for example? Also, I'd be happy to hear if there's some temporary hack that I could use. |
I would be interested in some basic guidance about this as well. My thoughts are for now to inline the script as multiline string, convert it to a This looses all the potential transpilation wins parcel provides but since these scripts are meant to be lean and mean, they probably need a tailored transpilation approach anyway (ala |
@mischnic Using your patches suggested above, does the hmr runtime added to the Audio Worklet script cause to script to fail, or is the hmr code functional in the AudioWorklet context (which doesn't seem all that bad...) ? (Or has this not been tested -- in which case I can try) |
I don't recall the exact error.
HMR won't work because you can register a
|
@mischnic Thanks! all makes sense |
This'd be solved by #2306. If you want to try a hackish workaround today, you might try my |
That's not needed for this, normal WebWorkers work fine with Parcel. |
Any news on this ? |
Now, there are some more worklet types with CSS Houdini |
@mischnic could either implicitly detect |
A pipeline that returns a URL to a worker is harder to do.... |
@mischnic so if I understand things correctly, your suggested fix works as long as I use |
Yes (so the approach from #1093 (comment) but for v2 and using #5430) |
Does this look correct? // @parcel/transformer-js/src/visitors/dependencies.js
if (types.isStringLiteral(args[0]) && types.isMemberExpression(callee) &&
callee.object.property && callee.object.property.name === "audioWorklet" &&
callee.property.name === "addModule") {
addURLDependency(asset, ast, args[0], {
env: {
context: 'worker',
outputFormat:
isModule && asset.env.scopeHoist ? 'esmodule' : undefined,
},
meta: {
webworker: true,
},
});
return;
} Pinging @dioptre |
I thought that |
Can we detect |
That is not a problem, but how would you differentiate this from class Foo {
addModule(data) {
...
}
}
let worklet = new Foo();
worklet.addModule(....) (= it's not really possible to determine if
This breaks once you there are shared bundles in the worker, because I also found that adding an import to the worklet results in a |
Yes I've seen some other bundler wrap the contents in an Personally, I wish this was a little bit more declarative and explicit, I really like the As long as we had SOME solution for now to be unblocked that is flexible for everyone's weird scenario would be great, we can try to get the automagic working later (and possibly integrate with native apis better if supported) |
Would that be because the import is not treated as a runtime entrypoint so it's expected to have some sort of export? |
I think detecting Although I guess some weird obscure library dependency could break :( I would clearly document it prominently and see who complains lol. |
I'm testing this out: // @parcel/transformer-js/src/visitors/dependencies.js
// ...
CallExpression: {
// ...
let isWorkletAddModule =
types.isMemberExpression(callee) &&
types.matchesPattern(callee, 'Worklet.addModule') && // (W
args.length >= 1 &&
types.isStringLiteral(args[0]) &&
!isInFalsyBranch(ancestors);
if (isWorkletAddModule) {
let isModule = false;
if (types.isObjectExpression(args[1])) {
let prop = args[1].properties.find(v =>
types.isIdentifier(v.key, { name: 'type' }),
);
if (prop && types.isStringLiteral(prop.value))
isModule = prop.value.value === 'module';
}
addURLDependency(asset, ast, args[0], {
env: {
context: 'web-worker',
outputFormat:
isModule && asset.env.shouldScopeHoist ? 'esmodule' : undefined,
},
meta: {
webworker: true,
},
});
return;
}
// ... |
As I've said, your version of detecting I've pushed an experiment from some time ago for supporting |
Oh that's awesome! I understand that reservation. I will play with it, luckily my worklet code is not complex enough to have any dependencies (yet) |
Okay I've checked out your branch, reinstalled xcode, linked everything, went into my project and linked in the packages you modified in your worker-url branch (core, transformer-worker/worklet, config-default) and built my project. Unfortunately I'm seeing this error:
In my code I have the following:
|
@mischnic do I need to link every single parcel package when using this fork? Or just a specific subset? |
The easiest way would probably be just copying the transformers into your project (if you're using a monorepo anyway), because there aren't any changes to Parcel core anyway. |
I'm looking at your diff https://github.com/parcel-bundler/parcel/compare/worker-url it looks like you do modify the core but I'll try copying the packages over |
Parcel keeps trying to reinstall packages when I run it and it's clearing out the copied over packages. I don't really know of a quicker way to implement this other than getting it into a nightly release. |
I can't beleive I'm saying this but inlining it worked for me: |
❓Question:
I'm trying to use new AudioWorklet feature (enabled by default in Chrome 66 ->). Part of the process is to load a
WorkletProcessorScript
to a web worker from my main js file like this:So I'd need to keep
my-worklet-processor.js
as a separate file. Maybe this is already possible, but I can't find a way to do this. Any clues how to solve this? Big thanks! 🙏There's not yet good documentation on AudioWorklets, but here and here are some examples I've been using.
🌍 Your Environment
The text was updated successfully, but these errors were encountered: