Importing native js modules for side effects only
See original GitHub issueThere does not appear to be a way to do the JS equivalent of import 'foo';
in Scala.js. Our workaround is this pattern:
@js.native
@JSImport("@material/mwc-button", JSImport.Default)
object RawMwcButton extends js.Object
private val dummy = RawMwcButton
If we don’t have the dummy variable, object gets optimized out and import never happens.
Is there / should there be a better way to do this?
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
import - JavaScript - MDN Web Docs - Mozilla
Import an entire module for side effects only, without importing anything. This runs the module's global code, but doesn't actually import ...
Read more >es6 import for side effects meaning - javascript - Stack Overflow
A module with side-effects is one that changes the scope in other ways then returning something, and it's effects are not always predictable, ......
Read more >Side effects in ES modules with vanilla JS - Go Make Things
Side effects in ES modules with vanilla JS. Yesterday, we learned how to renaming imports and exports with ES modules.
Read more >Everything you never wanted to know about side effects
A side effect, in an ECMAScript module context, is code that performs some externally-visible behaviour (that is, behaviour which is visible ...
Read more >Import Modules for Side Effects - chan.dev
Modules — in JavaScript — can be imported strictly for their side effects. import ". ... On the module side, simply write code...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You don’t necessarily need a dummy variable. It’s OK to only refer to the object as a statement, for example in your
main
or in the constructor body of a class/object that relies on that functionality. In other words, wherever you put theyou can equally write just
There is no better way to do this. Since Scala.js only reaches external
import
s for things that are themselves reachable, you need to make sure that the object is somehow reachable.AFAICT, it’s also not really possible to change Scala.js to make this any different. Much of the specification relies on having no real entry points besides
main
methods and exports. We do have the notion of static initializers, which are “out of the blue” entry points, so there is precedent for adding other kinds of entry points. But adding a different kind@JSImport
that would automatically reach its target module if it is on the classpath would behave very differently from the other@JSImport
s, and for that there is no precedent.Shall we close this as wontfix then? @uosis is that OK with you?