-
-
Notifications
You must be signed in to change notification settings - Fork 364
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Give mill the ability to re-write ESModule imports at link time (#3109)
# Motivation I'm really enjoying frontend scala, without needing to configure an entire node / npm environment. The capability to use the JS ecosystem without a bundler, is the "build primitive", that enables this. Here's a longer discussion of the motivation VirtusLab/scala-cli#1968 (comment) # Implementation arman added this to SBT here and published a library that does the heavy lifting. https://github.com/armanbilge/scalajs-importmap I followed this up in scala-cli ... VirtusLab/scala-js-cli#47 ... and am really enjoying this in the small. My larger projects are in mill though (thanks :-)!). Hence... this PR... which seeks to integrate the capability into mill. I wanted to do it in a plugin - but I couldn't see how as the call to the linker is in a private scope - so I've put it up for mill itself. I would expect this as is to pass CI with the new test. Very open to feedback. If accepted, it would be my first contribution to mill ... I'd be a little surprised if I got everything right straight out the gate - a plugin may indeed be preferable if I have not correctly understood the constraints. Pull request: #3109 --------- Co-authored-by: Tobias Roeser <le.petit.fou@web.de> Co-authored-by: Lorenzo Gabriele <lorenzolespaul@gmail.com> Co-authored-by: Romain Gilles <58700468+romain-gilles-ultra@users.noreply.github.com>
- Loading branch information
1 parent
10086ad
commit b74608f
Showing
8 changed files
with
167 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package app | ||
|
||
import scala.scalajs.js | ||
import scala.scalajs.js.annotation._ | ||
|
||
object App { | ||
def main(args: Array[String]): Unit = { | ||
println(linspace(-10.0, 10.0, 10)) | ||
} | ||
} | ||
|
||
@js.native | ||
@JSImport("@stdlib/linspace", JSImport.Default) | ||
object linspace extends js.Object { | ||
def apply(start: Double, stop: Double, num: Int): Any = js.native | ||
} |
78 changes: 78 additions & 0 deletions
78
scalajslib/test/src/mill/scalajslib/RemapEsModuleTests.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package mill.scalajslib | ||
|
||
import mill.api.Result | ||
import mill.define.Discover | ||
import mill.util.{TestEvaluator, TestUtil} | ||
import utest._ | ||
import mill.define.Target | ||
import mill.scalajslib.api._ | ||
|
||
object EsModuleRemapTests extends TestSuite { | ||
val workspacePath = TestUtil.getOutPathStatic() / "esModuleRemap" | ||
|
||
val remapTo = "https://cdn.jsdelivr.net/gh/stdlib-js/array-base-linspace@esm/index.mjs" | ||
|
||
object EsModuleRemap extends TestUtil.BaseModule { | ||
|
||
object sourceMapModule extends ScalaJSModule { | ||
override def millSourcePath = workspacePath | ||
override def scalaVersion = sys.props.getOrElse("TEST_SCALA_2_13_VERSION", ???) | ||
override def scalaJSVersion = "1.16.0" | ||
override def scalaJSSourceMap = false | ||
override def moduleKind = ModuleKind.ESModule | ||
|
||
override def scalaJSImportMap: Target[Seq[ESModuleImportMapping]] = Seq( | ||
ESModuleImportMapping.Prefix("@stdlib/linspace", remapTo) | ||
) | ||
} | ||
|
||
object OldJsModule extends ScalaJSModule { | ||
override def millSourcePath = workspacePath | ||
override def scalaVersion = sys.props.getOrElse("TEST_SCALA_2_13_VERSION", ???) | ||
override def scalaJSVersion = "1.15.0" | ||
override def scalaJSSourceMap = false | ||
override def moduleKind = ModuleKind.ESModule | ||
|
||
override def scalaJSImportMap: Target[Seq[ESModuleImportMapping]] = Seq( | ||
ESModuleImportMapping.Prefix("@stdlib/linspace", remapTo) | ||
) | ||
} | ||
|
||
override lazy val millDiscover = Discover[this.type] | ||
} | ||
|
||
val millSourcePath = os.pwd / "scalajslib" / "test" / "resources" / "esModuleRemap" | ||
|
||
val evaluator = TestEvaluator.static(EsModuleRemap) | ||
|
||
val tests: Tests = Tests { | ||
prepareWorkspace() | ||
|
||
test("should remap the esmodule") { | ||
val Right((report, _)) = | ||
evaluator(EsModuleRemap.sourceMapModule.fastLinkJS) | ||
val publicModules = report.publicModules.toSeq | ||
assert(publicModules.length == 1) | ||
val main = publicModules.head | ||
assert(main.jsFileName == "main.js") | ||
val mainPath = report.dest.path / "main.js" | ||
assert(os.exists(mainPath)) | ||
val rawJs = os.read.lines(mainPath) | ||
assert(rawJs(1).contains(remapTo)) | ||
} | ||
|
||
test("should throw for older scalaJS versions") { | ||
val Left(Result.Exception(ex, _)) = evaluator(EsModuleRemap.OldJsModule.fastLinkJS) | ||
val error = ex.getMessage | ||
assert(error == "scalaJSImportMap is not supported with Scala.js < 1.16.") | ||
} | ||
|
||
} | ||
|
||
def prepareWorkspace(): Unit = { | ||
os.remove.all(workspacePath) | ||
os.makeDir.all(workspacePath / os.up) | ||
os.copy(millSourcePath, workspacePath) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters