question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Importing native js modules for side effects only

See original GitHub issue

There 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:closed
  • Created 3 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
sjrdcommented, Sep 6, 2020

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 the

private val dummy = RawMwcButton

you can equally write just

RawMwcButton

There is no better way to do this. Since Scala.js only reaches external imports 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 @JSImports, and for that there is no precedent.

1reaction
gzm0commented, Sep 8, 2020

Shall we close this as wontfix then? @uosis is that OK with you?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found