Are there any known rules about the *order* in whi...
# webassembly
r
Are there any known rules about the order in which the independent NPM modules used with
@JsModule
are included into the bundle? I've noticed when building
wasmJs
target that the modules are generally included in alphabetical order. But it's not the case with
js
target where the order seems to be random (but can be managed a bit if I disable 'useEsModules()` and use
require()
calls instead of
@JsModule
). I have two use cases where the order is significant. I need to include one js module first, before other modules. I also need to include one
css
file last, after other modules. It's really problematic to achieve this in a shared
wasmJS
+
JS
project. My "workaround" is to publish two NPM modules with names prefixed "aaa-" and "zzz-" which are then included as first and last. But I'm not sure if this solution will "survive" future upgrades 😉
s
Kotlin/Wasm alphabetical order is not a final decision, it is a current implementation detail to ensure a stable ordering for a
Set<String>
. It could change. Proper useful stable ordering needs some additional design. It is obvious how to order multiple
@JsModule
in the same
.kt
file, but we would also need to come up with a spec for a cross-file and cross-module ordering. We currently don’t guarantee side-effects of module imports to be evaluated at all if imports from these modules are not used (even without DCE flag), and we’d probably need to specify this too. Alternatively, we could bypass this design, and provide a way to run a custom ES module before the Kotlin module. Then you can import side-effects in the ordered way:
Copy code
// before.mjs
import "first";
import "second";
// ...
import "css";
(edit: removed after.mjs) Could you file an issue with your use cases?
A stable workaround for now could be an NPM module that imports modules in an ordered way, and then re-exports them to Kotlin.
🙏 1
r