This PR resolves issues discovered while working on grame-cncm/fausteditor#4 whereby generated AudioWorklets would not work when using this library in projects with minifiers (as in the default vite configuration, which uses esbuild for minification).
and matches the names expected in getFaustAudioWorkletProcessor.
Second issue: mismatch between definition and use due to aliased class
The second issue was a bit trickier. One of the classes injected in the AudioWorklet is FaustWasmInstantiator, which depends on FaustMonoDspInstance. FaustMonoDspInstance is also injected into the AudioWorklet, but the names weren't matching up.
The body of FaustWasmInstantiator referred to FaustMonoDspInstance as $...
...but it was injected with the name zt_default.
There were two issues here: using export default caused the class to be aliased by another name (so that FaustDspInstance.name did not match up with the name actually used in FaustWasmInstantiator).
And, presumably to work around the behavior of another minification or bundling process, _default was appended in the generated AudioWorklet source.
The second commit resolves these by just exporting and importing FaustDspInstance directly, rather than using export default, and removing the previous _default workaround.
Remarks
This is a bit complex because of how separate AudioWorklets are from the rest of the code, and because faustwasm generates the code for them at runtime. In doing so, it relies on runtime introspection features like Function.name and Function.toString() to inject source code into AudioWorklets. These still work in the presence of tools that transform the source code at compile-time (minifiers, bundlers, transpilers...), but, much like eval(), they require care to ensure that the generated source will remain valid in the presence of such transformations - which generally preserve the correctness of code but can affect introspection features.
This PR resolves issues discovered while working on grame-cncm/fausteditor#4 whereby generated AudioWorklets would not work when using this library in projects with minifiers (as in the default vite configuration, which uses esbuild for minification).
Explanation
First issue:
dependencies
field name mismatchFaustDspGenerator
sets up dependencies in the AudioWorklet source code to be passed togetFaustAudioWorkletProcessor()
like so: https://github.com/grame-cncm/faustwasm/blob/f04942a334f13fabf56da854c083fe865db270ee/src/FaustDspGenerator.ts#L192-L197However, after minification, this would generate code like
in which the field names are also inadvertently minified, causing a mismatch with the fields expected in
getFaustAudioWorkletProcessor()
: https://github.com/grame-cncm/faustwasm/blob/f04942a334f13fabf56da854c083fe865db270ee/src/FaustAudioWorkletProcessor.ts#L50-L53and causing an error:
The first commit in this PR fixes this by making the field names explicit, so the code looks like
and matches the names expected in
getFaustAudioWorkletProcessor
.Second issue: mismatch between definition and use due to aliased class
The second issue was a bit trickier. One of the classes injected in the AudioWorklet is
FaustWasmInstantiator
, which depends onFaustMonoDspInstance
.FaustMonoDspInstance
is also injected into the AudioWorklet, but the names weren't matching up.The body of
FaustWasmInstantiator
referred toFaustMonoDspInstance
as$
......but it was injected with the name
zt_default
.There were two issues here: using
export default
caused the class to be aliased by another name (so thatFaustDspInstance.name
did not match up with the name actually used inFaustWasmInstantiator
).And, presumably to work around the behavior of another minification or bundling process,
_default
was appended in the generated AudioWorklet source.https://github.com/grame-cncm/faustwasm/blob/f04942a334f13fabf56da854c083fe865db270ee/src/FaustDspGenerator.ts#L188
The second commit resolves these by just exporting and importing
FaustDspInstance
directly, rather than usingexport default
, and removing the previous_default
workaround.Remarks
This is a bit complex because of how separate AudioWorklets are from the rest of the code, and because faustwasm generates the code for them at runtime. In doing so, it relies on runtime introspection features like
Function.name
andFunction.toString()
to inject source code into AudioWorklets. These still work in the presence of tools that transform the source code at compile-time (minifiers, bundlers, transpilers...), but, much likeeval()
, they require care to ensure that the generated source will remain valid in the presence of such transformations - which generally preserve the correctness of code but can affect introspection features.