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

Give mill the ability to re-write ESModule imports at link time #3109

Merged
merged 31 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
599afd6
Tidy up some deprecation warninga
Quafadas Apr 3, 2024
9ea5bea
.
Quafadas Apr 5, 2024
d19c56b
.
Quafadas Apr 5, 2024
1e12f19
.
Quafadas Apr 5, 2024
ff2f434
add node
Quafadas Apr 5, 2024
5b191f4
Add ability to remap ES module imports at link time
Quafadas Apr 5, 2024
c2aa0e0
Merge branch 'main' into jsimportRemap
Quafadas Apr 5, 2024
96addd7
reformat
Quafadas Apr 5, 2024
e524bc3
Let's see if this fixes classpath issues.
Quafadas Apr 5, 2024
fccc715
formatting
Quafadas Apr 5, 2024
666fade
Remove this unncessary default as per conversation
Quafadas Apr 6, 2024
c41b15f
Update junixsocket from 2.9.0 to 2.9.1 (#3113)
lefou Apr 9, 2024
e4a672e
Update mainargs from 0.6.2 to 0.6.3 (#3114)
lefou Apr 9, 2024
247bee6
Update asm from 9.6 to 9.7 (#3112)
lefou Apr 9, 2024
c28fd8a
Update requests from 0.8.0 to 0.8.2 (#3115)
lefou Apr 9, 2024
b2d8f10
Update semanticdb-scalac from 4.9.2 to 4.9.3 (#3116)
lefou Apr 11, 2024
927e410
Update transitive commons-io from 2.15.1 to 2.16.1 (#3117)
lefou Apr 11, 2024
5023c65
Update Scala Native to 0.5.0 (#3120)
lolgab Apr 12, 2024
a68c09b
Isolate scoverage modules from their parent modules (#3118)
romain-gilles-ultra Apr 12, 2024
5e65027
.
Quafadas Apr 5, 2024
b307fe0
Propogate Module remapping from build.sc to ScalaJsModule
Quafadas Apr 15, 2024
d84925a
Merge branch 'main' into jsimportRemap
Quafadas Apr 15, 2024
e096503
dev container
Quafadas Apr 15, 2024
77903ea
Remove whitespace - reformat
Quafadas Apr 15, 2024
f8bba2b
See if this passes tests
Quafadas Apr 15, 2024
5eae119
No need to change this
Quafadas Apr 15, 2024
68b814b
reformat
Quafadas Apr 16, 2024
d44de38
Apply review suggestions
lolgab Apr 26, 2024
849afc9
Merge branch 'main' into jsimportRemap
lolgab Apr 26, 2024
87657d7
Merge pull request #1 from lolgab/jsimportRemap
Quafadas Apr 26, 2024
cdbfb80
Fix test for error message
lolgab Apr 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .devcontainer/devcontainer.json
Quafadas marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/ubuntu
{
"name": "Ubuntu",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/base:jammy",
"customizations": {
"vscode": {
"extensions": [
"scalameta.metals",
"usernamehw.errorlens",
"vscjava.vscode-java-pack",
"github.copilot",
"github.vscode-github-actions",
"github.vscode-pull-request-github",
"eamodio.gitlens"
]
}
},

// Features to add to the dev container. More info: https://containers.dev/features.
"features": {
"ghcr.io/devcontainers/features/java:1": {
"version":17
},
"ghcr.io/devcontainers/features/node:1":{
}
},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "./mill -v && ./mill __.prepareOffline && ./mill mill.bsp.BSP/install && ./mill __.compile"

// Configure tool-specific properties.
// "customizations": {},

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
4 changes: 3 additions & 1 deletion build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ 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.0"
val esModuleRemap = ivy"com.armanbilge::scalajs-importmap:0.1.1"

/** Used to manage transitive versions. */
val transitiveDeps = Seq(
Expand Down Expand Up @@ -816,7 +817,8 @@ object scalajslib extends MillStableScalaModule with BuildInfo {
Deps.Scalajs_1.scalajsEnvJsdomNodejs,
Deps.Scalajs_1.scalajsEnvExoegoJsdomNodejs,
Deps.Scalajs_1.scalajsEnvPhantomjs,
Deps.Scalajs_1.scalajsEnvSelenium
Deps.Scalajs_1.scalajsEnvSelenium,
Deps.esModuleRemap
)
}
}
Expand Down
23 changes: 18 additions & 5 deletions scalajslib/src/mill/scalajslib/ScalaJSModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ trait ScalaJSModule extends scalalib.ScalaModule { outer =>
val commonDeps = Seq(
ivy"org.scala-js::scalajs-sbt-test-adapter:${scalaJSVersion()}"
)
val maybeImportMap = scalaJSVersion() match {
case s"1.$n.$_" if n.toIntOption.exists(_ < 16) => Seq[Dep]()
case _ => Seq(ivy"com.armanbilge::scalajs-importmap:0.1.1")
Quafadas marked this conversation as resolved.
Show resolved Hide resolved
}

val envDeps = scalaJSBinaryVersion() match {
case "0.6" =>
Seq(
Expand All @@ -89,7 +94,7 @@ trait ScalaJSModule extends scalalib.ScalaModule { outer =>
// we need to use the scala-library of the currently running mill
resolveDependencies(
repositoriesTask(),
(commonDeps.iterator ++ envDeps)
(commonDeps.iterator ++ envDeps ++ maybeImportMap)
.map(Lib.depToBoundDep(_, mill.main.BuildInfo.scalaVersion, "")),
ctx = Some(T.log)
)
Expand Down Expand Up @@ -130,7 +135,8 @@ trait ScalaJSModule extends scalalib.ScalaModule { outer =>
esFeatures = esFeatures(),
moduleSplitStyle = moduleSplitStyle(),
outputPatterns = scalaJSOutputPatterns(),
minify = scalaJSMinify()
minify = scalaJSMinify(),
esModuleMap = esModuleRemap()
)
}

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

Expand All @@ -192,7 +199,8 @@ trait ScalaJSModule extends scalalib.ScalaModule { outer =>
esFeatures = esFeatures,
moduleSplitStyle = moduleSplitStyle,
outputPatterns = outputPatterns,
minify = minify
minify = minify,
esModuleMap = esModuleMap
)
}

Expand Down Expand Up @@ -266,6 +274,10 @@ trait ScalaJSModule extends scalalib.ScalaModule { outer =>

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

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

/** Whether to emit a source map. */
def scalaJSSourceMap: Target[Boolean] = T { true }

Expand Down Expand Up @@ -346,7 +358,8 @@ trait TestScalaJSModule extends ScalaJSModule with TestModule {
esFeatures = esFeatures(),
moduleSplitStyle = moduleSplitStyle(),
outputPatterns = scalaJSOutputPatterns(),
minify = scalaJSMinify()
minify = scalaJSMinify(),
esModuleMap = esModuleRemap()
)
}

Expand Down
6 changes: 4 additions & 2 deletions scalajslib/src/mill/scalajslib/worker/ScalaJSWorker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ private[scalajslib] class ScalaJSWorker extends AutoCloseable {
esFeatures: api.ESFeatures,
moduleSplitStyle: api.ModuleSplitStyle,
outputPatterns: api.OutputPatterns,
minify: Boolean
minify: Boolean,
esModuleMap: Map[String, String]
)(implicit ctx: Ctx.Home): Result[api.Report] = {
bridge(toolsClasspath).link(
runClasspath = runClasspath.iterator.map(_.path.toNIO).toSeq,
Expand All @@ -176,7 +177,8 @@ private[scalajslib] class ScalaJSWorker extends AutoCloseable {
esFeatures = toWorkerApi(esFeatures),
moduleSplitStyle = toWorkerApi(moduleSplitStyle),
outputPatterns = toWorkerApi(outputPatterns),
minify = minify
minify = minify,
esModuleMap = esModuleMap
) match {
case Right(report) => Result.Success(fromWorkerApi(report))
case Left(message) => Result.Failure(message)
Expand Down
16 changes: 16 additions & 0 deletions scalajslib/test/resources/esModuleRemap/src/app/App.scala
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ object FullOptESModuleTests extends TestSuite {

test("fullOpt with ESModule moduleKind") {
val result =
fullOptESModuleModuleEvaluator(FullOptESModuleModule.fullOptESModuleModule.fullOpt)
fullOptESModuleModuleEvaluator(FullOptESModuleModule.fullOptESModuleModule.fullLinkJS)
Quafadas marked this conversation as resolved.
Show resolved Hide resolved
assert(result.isRight)
}
}
Expand Down
6 changes: 4 additions & 2 deletions scalajslib/test/src/mill/scalajslib/MultiModuleTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import mill.eval.EvaluatorPaths
import mill.util._
import mill.scalalib._
import utest._
import mill.scalajslib.internal.ScalaJSUtils
object MultiModuleTests extends TestSuite {
val workspacePath = TestUtil.getOutPathStatic() / "multi-module"
val sourcePath = os.pwd / "scalajslib" / "test" / "resources" / "multi-module"
Expand Down Expand Up @@ -39,10 +40,11 @@ object MultiModuleTests extends TestSuite {
prepareWorkspace()

def checkOpt(optimize: Boolean) = {
val task = if (optimize) MultiModule.client.fullOpt else MultiModule.client.fastOpt
val task = if (optimize) MultiModule.client.fullLinkJS else MultiModule.client.fastLinkJS
val Right((linked, evalCount)) = evaluator(task)
val processedReport = ScalaJSUtils.getReportMainFilePathRef(linked)

val runOutput = ScalaJsUtils.runJS(linked.path)
val runOutput = ScalaJsUtils.runJS(processedReport.path)
assert(
evalCount > 0,
runOutput == "Hello from Scala.js, result is: 3\n"
Expand Down
77 changes: 77 additions & 0 deletions scalajslib/test/src/mill/scalajslib/RemapEsModuleTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package mill.scalajslib

import mill.define.Discover
import mill.util.{TestEvaluator, TestUtil}
import utest._
import mill.define.Target
import mill.scalajslib.api.ModuleKind

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 esModuleRemap: Target[Map[String, String]] = Map(
"@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 esModuleRemap: Target[Map[String, String]] = Map(
"@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(ex) = evaluator(EsModuleRemap.OldJsModule.fastLinkJS)
val error = ex.asFailing.get.toString()
assert(error.contains("will work with scalaJS 1.16 and above. You are using scalaJS 1.15.0"))
}

}

def prepareWorkspace(): Unit = {
os.remove.all(workspacePath)
os.makeDir.all(workspacePath / os.up)
os.copy(millSourcePath, workspacePath)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ private[scalajslib] trait ScalaJSWorkerApi {
esFeatures: ESFeatures,
moduleSplitStyle: ModuleSplitStyle,
outputPatterns: OutputPatterns,
minify: Boolean
minify: Boolean,
esModuleMap: Map[String, String]
): Either[String, Report]

def run(config: JsEnvConfig, report: Report): Unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import org.scalajs.testing.adapter.{TestAdapterInitializer => TAI}
import scala.collection.mutable
import scala.ref.SoftReference

import com.armanbilge.sjsimportmap.ImportMappedIRFile

class ScalaJSWorkerImpl extends ScalaJSWorkerApi {
private case class LinkerInput(
isFullLinkJS: Boolean,
Expand Down Expand Up @@ -169,7 +171,8 @@ class ScalaJSWorkerImpl extends ScalaJSWorkerApi {
esFeatures: ESFeatures,
moduleSplitStyle: ModuleSplitStyle,
outputPatterns: OutputPatterns,
minify: Boolean
minify: Boolean,
esModuleMap: Map[String, String] = Map[String, String]()
Quafadas marked this conversation as resolved.
Show resolved Hide resolved
): 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 @@ -202,21 +205,41 @@ class ScalaJSWorkerImpl extends ScalaJSWorkerApi {

val resultFuture = (for {
(irContainers, _) <- irContainersAndPathsFuture
irFiles <- irFileCacheCache.cached(irContainers)
irFiles0 <- irFileCacheCache.cached(irContainers)
irFiles = if (esModuleMap.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?"
)
}
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)))
}
irFiles0.map { ImportMappedIRFile.fromIRFile(_)(remapFct) }
}
report <-
if (useLegacy) {
// This uses the legacy linker interface, which is deprecated. The compiler will warn us about it, but the warnings are intentional in a legacy block. Suppress them.
val jsFileName = "out.js"
val jsFile = new File(dest, jsFileName).toPath()
@annotation.nowarn
var linkerOutput = LinkerOutput(PathOutputFile(jsFile))
.withJSFileURI(java.net.URI.create(jsFile.getFileName.toString))

val sourceMapNameOpt = Option.when(sourceMap)(s"${jsFile.getFileName}.map")
sourceMapNameOpt.foreach { sourceMapName =>
val sourceMapFile = jsFile.resolveSibling(sourceMapName)
@annotation.nowarn
val outFct = PathOutputFile(sourceMapFile)
linkerOutput = linkerOutput
.withSourceMap(PathOutputFile(sourceMapFile))
.withSourceMap(outFct)
.withSourceMapURI(java.net.URI.create(sourceMapFile.getFileName.toString))
}
linker.link(irFiles, moduleInitializers, linkerOutput, logger).map {
@annotation.nowarn
Quafadas marked this conversation as resolved.
Show resolved Hide resolved
val report = linker.link(irFiles, moduleInitializers, linkerOutput, logger).map {
file =>
Report(
publicModules = Seq(Report.Module(
Expand All @@ -228,6 +251,7 @@ class ScalaJSWorkerImpl extends ScalaJSWorkerApi {
dest = dest
)
}
report
} else {
val linkerOutput = PathOutputDirectory(dest.toPath())
linker.link(
Expand Down Expand Up @@ -270,20 +294,22 @@ class ScalaJSWorkerImpl extends ScalaJSWorkerApi {
else runConfig0
.withInheritErr(false)
.withInheritOut(false)
.withOnOutputStream { case (Some(processOut), Some(processErr)) =>
val sources = Seq(
(processOut, System.out, "spawnSubprocess.stdout", false, () => true),
(processErr, System.err, "spawnSubprocess.stderr", false, () => true)
)

for ((std, dest, name, checkAvailable, runningCheck) <- sources) {
val t = new Thread(
new mill.main.client.InputPumper(std, dest, checkAvailable, () => runningCheck()),
name
.withOnOutputStream {
case (Some(processOut), Some(processErr)) =>
val sources = Seq(
(processOut, System.out, "spawnSubprocess.stdout", false, () => true),
(processErr, System.err, "spawnSubprocess.stderr", false, () => true)
)
t.setDaemon(true)
t.start()
}

for ((std, dest, name, checkAvailable, runningCheck) <- sources) {
val t = new Thread(
new mill.main.client.InputPumper(std, dest, checkAvailable, () => runningCheck()),
name
)
t.setDaemon(true)
t.start()
}
case _ => ??? // should not happen
}
Run.runInterruptible(env, input, runConfig)
}
Expand Down
Loading