Skip to content

Commit

Permalink
Apply review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
lolgab committed Apr 26, 2024
1 parent 68b814b commit bff2c89
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 34 deletions.
6 changes: 3 additions & 3 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ object Deps {
val scalajsEnvSelenium = ivy"org.scala-js::scalajs-env-selenium:1.1.1"
val scalajsSbtTestAdapter = ivy"org.scala-js::scalajs-sbt-test-adapter:${scalaJsVersion}"
val scalajsLinker = ivy"org.scala-js::scalajs-linker:${scalaJsVersion}"
val scalajsImportMap = ivy"com.armanbilge::scalajs-importmap:0.1.1"
}

object Scalanative_0_4 {
Expand Down Expand Up @@ -189,7 +190,6 @@ object Deps {
val fansi = ivy"com.lihaoyi::fansi:0.4.0"
val jarjarabrams = ivy"com.eed3si9n.jarjarabrams::jarjar-abrams-core:1.14.0"
val requests = ivy"com.lihaoyi::requests:0.8.2"
val esModuleRemap = ivy"com.armanbilge::scalajs-importmap:0.1.1"


/** Used to manage transitive versions. */
Expand Down Expand Up @@ -798,7 +798,7 @@ object scalajslib extends MillStableScalaModule with BuildInfo {
),
BuildInfo.Value("scalajsEnvPhantomJs", formatDep(Deps.Scalajs_1.scalajsEnvPhantomjs)),
BuildInfo.Value("scalajsEnvSelenium", formatDep(Deps.Scalajs_1.scalajsEnvSelenium)),
BuildInfo.Value("scalaJsRemapDep", formatDep(Deps.esModuleRemap))
BuildInfo.Value("scalajsImportMap", formatDep(Deps.Scalajs_1.scalajsImportMap))
)
}

Expand All @@ -820,7 +820,7 @@ object scalajslib extends MillStableScalaModule with BuildInfo {
Deps.Scalajs_1.scalajsEnvExoegoJsdomNodejs,
Deps.Scalajs_1.scalajsEnvPhantomjs,
Deps.Scalajs_1.scalajsEnvSelenium,
Deps.esModuleRemap
Deps.Scalajs_1.scalajsImportMap
)
}
}
Expand Down
25 changes: 11 additions & 14 deletions scalajslib/src/mill/scalajslib/ScalaJSModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,10 @@ trait ScalaJSModule extends scalalib.ScalaModule { outer =>
val commonDeps = Seq(
ivy"org.scala-js::scalajs-sbt-test-adapter:${scalaJSVersion()}"
)
val maybeImportMap = (scalaJSVersion(), esModuleRemap()) match {
case (s"1.$n.$_", _) if n.toIntOption.exists(_ < 16) => Seq[Dep]()
case (_, importMap) =>
if (importMap.isEmpty)
Seq[Dep]()
else
Seq(ivy"${ScalaJSBuildInfo.scalaJsRemapDep}")
val scalajsImportMapDeps = scalaJSVersion() match {
case s"1.$n.$_" if n.toIntOption.exists(_ >= 16) && scalaJSImportMap().nonEmpty =>
Seq(ivy"${ScalaJSBuildInfo.scalajsImportMap}")
case _ => Seq.empty[Dep]
}

val envDeps = scalaJSBinaryVersion() match {
Expand All @@ -98,7 +95,7 @@ trait ScalaJSModule extends scalalib.ScalaModule { outer =>
// we need to use the scala-library of the currently running mill
resolveDependencies(
repositoriesTask(),
(commonDeps.iterator ++ envDeps ++ maybeImportMap)
(commonDeps.iterator ++ envDeps ++ scalajsImportMapDeps)
.map(Lib.depToBoundDep(_, mill.main.BuildInfo.scalaVersion, "")),
ctx = Some(T.log)
)
Expand Down Expand Up @@ -140,7 +137,7 @@ trait ScalaJSModule extends scalalib.ScalaModule { outer =>
moduleSplitStyle = moduleSplitStyle(),
outputPatterns = scalaJSOutputPatterns(),
minify = scalaJSMinify(),
esModuleMap = esModuleRemap()
importMap = scalaJSImportMap()
)
}

Expand Down Expand Up @@ -183,7 +180,7 @@ trait ScalaJSModule extends scalalib.ScalaModule { outer =>
moduleSplitStyle: ModuleSplitStyle,
outputPatterns: OutputPatterns,
minify: Boolean,
esModuleMap: Map[String, String]
importMap: Seq[ESModuleImportMapping]
)(implicit ctx: mill.api.Ctx): Result[Report] = {
val outputPath = ctx.dest

Expand All @@ -204,7 +201,7 @@ trait ScalaJSModule extends scalalib.ScalaModule { outer =>
moduleSplitStyle = moduleSplitStyle,
outputPatterns = outputPatterns,
minify = minify,
esModuleMap = esModuleMap
importMap = importMap
)
}

Expand Down Expand Up @@ -278,8 +275,8 @@ trait ScalaJSModule extends scalalib.ScalaModule { outer =>

def scalaJSOptimizer: Target[Boolean] = T { true }

def esModuleRemap: Target[Map[String, String]] = T {
Map.empty[String, String]
def scalaJSImportMap: Target[Seq[ESModuleImportMapping]] = T {
Seq.empty[ESModuleImportMapping]
}

/** Whether to emit a source map. */
Expand Down Expand Up @@ -363,7 +360,7 @@ trait TestScalaJSModule extends ScalaJSModule with TestModule {
moduleSplitStyle = moduleSplitStyle(),
outputPatterns = scalaJSOutputPatterns(),
minify = scalaJSMinify(),
esModuleMap = esModuleRemap()
importMap = scalaJSImportMap()
)
}

Expand Down
8 changes: 8 additions & 0 deletions scalajslib/src/mill/scalajslib/api/ScalaJSApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,11 @@ object OutputPatterns {

implicit val rw: RW[OutputPatterns] = macroRW[OutputPatterns]
}

sealed trait ESModuleImportMapping
object ESModuleImportMapping {
case class Prefix(prefix: String, replacement: String) extends ESModuleImportMapping

implicit def rwPrefix: RW[Prefix] = macroRW
implicit def rw: RW[ESModuleImportMapping] = macroRW
}
11 changes: 9 additions & 2 deletions scalajslib/src/mill/scalajslib/worker/ScalaJSWorker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ private[scalajslib] class ScalaJSWorker extends AutoCloseable {
)
}

private def toWorkerApi(importMap: api.ESModuleImportMapping): workerApi.ESModuleImportMapping = {
importMap match {
case api.ESModuleImportMapping.Prefix(prefix, replacement) =>
workerApi.ESModuleImportMapping.Prefix(prefix, replacement)
}
}

def link(
toolsClasspath: Agg[mill.PathRef],
runClasspath: Agg[mill.PathRef],
Expand All @@ -162,7 +169,7 @@ private[scalajslib] class ScalaJSWorker extends AutoCloseable {
moduleSplitStyle: api.ModuleSplitStyle,
outputPatterns: api.OutputPatterns,
minify: Boolean,
esModuleMap: Map[String, String]
importMap: Seq[api.ESModuleImportMapping]
)(implicit ctx: Ctx.Home): Result[api.Report] = {
bridge(toolsClasspath).link(
runClasspath = runClasspath.iterator.map(_.path.toNIO).toSeq,
Expand All @@ -178,7 +185,7 @@ private[scalajslib] class ScalaJSWorker extends AutoCloseable {
moduleSplitStyle = toWorkerApi(moduleSplitStyle),
outputPatterns = toWorkerApi(outputPatterns),
minify = minify,
esModuleMap = esModuleMap
importMap = importMap.map(toWorkerApi)
) match {
case Right(report) => Result.Success(fromWorkerApi(report))
case Left(message) => Result.Failure(message)
Expand Down
10 changes: 5 additions & 5 deletions scalajslib/test/src/mill/scalajslib/RemapEsModuleTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import mill.define.Discover
import mill.util.{TestEvaluator, TestUtil}
import utest._
import mill.define.Target
import mill.scalajslib.api.ModuleKind
import mill.scalajslib.api._

object EsModuleRemapTests extends TestSuite {
val workspacePath = TestUtil.getOutPathStatic() / "esModuleRemap"
Expand All @@ -20,8 +20,8 @@ object EsModuleRemapTests extends TestSuite {
override def scalaJSSourceMap = false
override def moduleKind = ModuleKind.ESModule

override def esModuleRemap: Target[Map[String, String]] = Map(
"@stdlib/linspace" -> remapTo
override def scalaJSImportMap: Target[Seq[ESModuleImportMapping]] = Seq(
ESModuleImportMapping.Prefix("@stdlib/linspace", remapTo)
)
}

Expand All @@ -32,8 +32,8 @@ object EsModuleRemapTests extends TestSuite {
override def scalaJSSourceMap = false
override def moduleKind = ModuleKind.ESModule

override def esModuleRemap: Target[Map[String, String]] = Map(
"@stdlib/linspace" -> remapTo
override def scalaJSImportMap: Target[Seq[ESModuleImportMapping]] = Seq(
ESModuleImportMapping.Prefix("@stdlib/linspace", remapTo)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ private[scalajslib] trait ScalaJSWorkerApi {
moduleSplitStyle: ModuleSplitStyle,
outputPatterns: OutputPatterns,
minify: Boolean,
esModuleMap: Map[String, String]
importMap: Seq[ESModuleImportMapping]
): Either[String, Report]

def run(config: JsEnvConfig, report: Report): Unit
Expand Down Expand Up @@ -123,3 +123,8 @@ private[scalajslib] final case class OutputPatterns(
jsFileURI: String,
sourceMapURI: String
)

private[scalajslib] sealed trait ESModuleImportMapping
private[scalajslib] object ESModuleImportMapping {
case class Prefix(prefix: String, replacement: String) extends ESModuleImportMapping
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class ScalaJSWorkerImpl extends ScalaJSWorkerApi {
moduleSplitStyle: ModuleSplitStyle,
outputPatterns: OutputPatterns,
minify: Boolean,
esModuleMap: Map[String, String]
importMap: Seq[ESModuleImportMapping]
): Either[String, Report] = {
// On Scala.js 1.2- we want to use the legacy mode either way since
// the new mode is not supported and in tests we always use legacy = false
Expand Down Expand Up @@ -206,19 +206,22 @@ class ScalaJSWorkerImpl extends ScalaJSWorkerApi {
val resultFuture = (for {
(irContainers, _) <- irContainersAndPathsFuture
irFiles0 <- irFileCacheCache.cached(irContainers)
irFiles = if (esModuleMap.isEmpty) {
irFiles = if (importMap.isEmpty) {
irFiles0
} else {
if (!minorIsGreaterThanOrEqual(16)) {
throw new Exception(
s"Remapping EsModule imports will work with scalaJS 1.16 and above. You are using scalaJS ${ScalaJSVersions.current} - consider upgrading?"
)
throw new Exception("scalaJSImportMap is not supported with Scala.js < 1.16.")
}
val remapFct = esModuleMap.toSeq.foldLeft((in: String) => in) { case (fct, (s1, s2)) =>
val fct2: (String => String) = (in => in.replace(s1, s2))
(in => fct(fct2(in)))
val remapFunction = (rawImport: String) => {
importMap
.collectFirst {
case ESModuleImportMapping.Prefix(prefix, replacement)
if rawImport.startsWith(prefix) =>
s"$replacement${rawImport.stripPrefix(prefix)}"
}
.getOrElse(rawImport)
}
irFiles0.map { ImportMappedIRFile.fromIRFile(_)(remapFct) }
irFiles0.map { ImportMappedIRFile.fromIRFile(_)(remapFunction) }
}
report <-
if (useLegacy) {
Expand Down

0 comments on commit bff2c89

Please sign in to comment.