scalacenter / scalajs-bundler

https://scalacenter.github.io/scalajs-bundler
Other
237 stars 101 forks source link

[WIP] Add ES modules support #312

Closed vhiairrassary closed 3 years ago

vhiairrassary commented 5 years ago

Fixes https://github.com/scalacenter/scalajs-bundler/issues/293

Allow to use ES modules as well as CommonJS module.

WIP: BundlingMode.LibraryOnly() does not work yet as with:

<script src="[name]-fastopt-library.js"></script>
<script src="[name]-fastopt-loader.js"></script>
<script src="[name]-fastopt.js"></script>

the browser will not recognize the import syntax in the last script.

Adding the attribute type="module" is not enough: if [name]-fastopt.js contains something like import * as $i_jquery from "jquery"; then the following error is thrown:

Uncaught TypeError: Failed to resolve module specifier "jquery". Relative references must start with either "/", "./", or "../".
julienrf commented 5 years ago

BundlingMode.LibraryOnly() does not work

I’m not sure what we can do about that. By design, the LibraryOnly mode can only work with a module kind that is understood by web browsers. We could just document that the LibraryOnly mode only works with CommonJSModule.

a1russell commented 4 years ago

Any update on this?

jeroentervoorde commented 4 years ago

I updated the PR against current master. It seems to work fine except for this warning when trying to configure ES modules in fullOptJS ):

"The module kind in scalaJSLinkerConfig in (learnreact, compile, fullOptJS) is different than the one in(learnreact, compile)`. Some things will go wrong."

In build.sbt I used: scalaJSLinkerConfig in fullOptJS ~= (_.withModuleKind(ModuleKind.ESModule))

Not sure what to do about that. We should be able to configure ES modules in fullOptJS only i guess as it does not work will BundlingMode.LibraryOnly()

My reason to enable this is fullOptJS is the smaller bundler size. It generates a 25% smaller output file. Probably because of the better DCE for ES modules in webpack.

Updated PR. https://github.com/scalacenter/scalajs-bundler/pull/336

vhiairrassary commented 3 years ago

Closing in favor of https://github.com/scalacenter/scalajs-bundler/pull/336