Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #115: Add ESModule support #116

Merged
merged 2 commits into from
Sep 27, 2020
Merged

Fix #115: Add ESModule support #116

merged 2 commits into from
Sep 27, 2020

Conversation

mushtaq
Copy link
Contributor

@mushtaq mushtaq commented Sep 21, 2020

No description provided.

@gzm0
Copy link
Contributor

gzm0 commented Sep 21, 2020

Thank you for your contribution. Please sign the Scala CLA: https://www.lightbend.com/contribute/cla/scala in order for us to be able to accept your contribution.

@gzm0
Copy link
Contributor

gzm0 commented Sep 21, 2020

I'm aware this isn't (unfortunately) explicitly written in the readme here, but most (if not all) of the Scala.js main repo contribution guidelines apply.

@mushtaq
Copy link
Contributor Author

mushtaq commented Sep 21, 2020

@gzm0 I just signed the CLA.

I will go over the guidelines. Will appreciate if you could points a few missing things in the PR to get me started.

Copy link
Contributor

@gzm0 gzm0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test. At least a superficial one examining the code paths, ideally one that actually loads another module. Note that this needs a webserver, so it should probably go in seleniumJSHttpEnvTest.

Please put the commit message in the following form:

Fix #115: Add ESModule support

.gitignore Outdated Show resolved Hide resolved
@mushtaq
Copy link
Contributor Author

mushtaq commented Sep 22, 2020

Thanks for the inputs @gzm0.
Will look into adding a test over the weekend.

@mushtaq mushtaq force-pushed the esmodules branch 2 times, most recently from 60d10b2 to 0c455f6 Compare September 22, 2020 12:40
@mushtaq
Copy link
Contributor Author

mushtaq commented Sep 22, 2020

@gzm0 I attempted writing a module import test by mapping JSImport to a CDN entry. Will that be ok?

@mushtaq
Copy link
Contributor Author

mushtaq commented Sep 23, 2020

Done with the version upgrade and ESModule setting on by default.

Copy link
Contributor

@gzm0 gzm0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only minor nits. LG otherwise.

Once done, please put the changes in two commits: one with the version changes, one with the fix for module support.

In the one with the version changes, please put a short explanation into the commit body, why you are not re-using scalaJSVersions. (feel free to reference http://www.scala-js.org/news/2020/07/02/announcing-scalajs-1.1.1/).

build.sbt Outdated Show resolved Hide resolved
As mentioned in the [ScalaJS 1.1.1 release](http://www.scala-js.org/news/2020/07/02/announcing-scalajs-1.1.1), ScalaJS version in `scalajs-js-envs` should be harcoded instead of deriving it from `scalaJSVersion`. `scalajs-js-envs` is extracted out of the ScalaJS repository and will follow its own publishing cadence.
@mushtaq
Copy link
Contributor Author

mushtaq commented Sep 27, 2020

I have incorporated all the suggestions so far. Thank you.

@gzm0 gzm0 changed the title Add ESModule support Fix #115: Add ESModule support Sep 27, 2020
@gzm0
Copy link
Contributor

gzm0 commented Sep 27, 2020

@sjrd further comments on this?

@sjrd
Copy link
Member

sjrd commented Sep 27, 2020

I have nothing else to add :)

val scriptTags =
scripts.map(path => s"<script src='${path.toString}'></script>")
private def htmlPage(setupJsPath: Path, input: Seq[Input], materializer: FileMaterializer): String = {
val setupJs = makeTag(setupJsPath, "text/javascript", materializer)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum, am I misunderstanding this now, or would this copy the setup script twice?

If you are looking for ways to consolidate code and the fact that the setup script is not a Path is problematic, I think it would be OK to add Jimfs as a dependency and make in-memory Paths. There is precedent for this in the Node.js env: https://github.com/scala-js/scala-js-js-envs/blob/v1.1.1/nodejs-env/src/main/scala/org/scalajs/jsenv/nodejs/ComSupport.scala#L247

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Let me know if I got it right.

val page = m.materialize("scalajsRun.html", htmlPage(allScriptURLs))

val setupJsPath = JSSetup.setupFile(enableCom)
val page = m.materialize("scalajsRun.html", htmlPage(setupJsPath, input, m))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point, you can just pass this as additional input:

val fullInput = Input.Script(JSSetup.setupFile(enableCom)) :: input
/* snip */ htmlPage(fullInput, m) /* snip */

That'll also simplify htmlPage.

""".stripMargin
def setupFile(enableCom: Boolean): Path = {
Files.write(
Jimfs.newFileSystem().getPath("setup.js"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scala.js style guide demands that this be indented by 4 spaces (continuation line).

Consider:

val code = {
  s"""
  /* snip */
  """.stripMargin
}

Files.write(
    Jimfs.newFileSystem().getPath("/setup.js"),
    code.getBytes(StandardCharset.UTF_8))

To avoid having to indent the huge string literal too much.

| </body>
|</html>
""".stripMargin
}

private def makeTag(path: Path, tpe: String, materializer: FileMaterializer): String = {
val url = materializer.materialize(path)
s"<script defer type='$tpe' src='${url.toString}'></script>"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also just noticed this: You can drop the toString (and hence the braces). It's implied by the s interpolator.

@gzm0 gzm0 merged commit bb39519 into scala-js:master Sep 27, 2020
@gzm0
Copy link
Contributor

gzm0 commented Sep 27, 2020

Thank you!

@mushtaq
Copy link
Contributor Author

mushtaq commented Sep 28, 2020

@gzm0 Any chance of getting a new release soon?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants