Skip to content

Commit

Permalink
Remap ESModule imports at link time in mdoc (#883)
Browse files Browse the repository at this point in the history
Remap ESModule imports at link time in mdoc
  • Loading branch information
Quafadas committed Sep 17, 2024
1 parent 9734ce2 commit de1f63f
Show file tree
Hide file tree
Showing 27 changed files with 11,021 additions and 197 deletions.
29 changes: 26 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ def isScala212(v: Option[(Long, Long)]): Boolean = v.exists(_._1 == 2) && v.exis
def isScala213(v: Option[(Long, Long)]): Boolean = v.exists(_._1 == 2) && v.exists(_._2 == 13)
def isScala3(v: Option[(Long, Long)]): Boolean = v.exists(_._1 == 3)

def jsoniter = List(
"com.github.plokhotnyuk.jsoniter-scala" %% "jsoniter-scala-core" % "2.13.5.2",
"com.github.plokhotnyuk.jsoniter-scala" %% "jsoniter-scala-macros" % "2.13.5.2"
)

val isScala212 = Def.setting {
VersionNumber(scalaVersion.value).matchesSemVer(SemanticSelector("2.12.x"))
}
Expand Down Expand Up @@ -248,6 +253,7 @@ lazy val mdoc = project
.excludeAll(excludePprint)
)
),
libraryDependencies ++= jsoniter,
libraryDependencies ++= List(
"com.googlecode.java-diff-utils" % "diffutils" % "1.3.0",
"io.methvin" % "directory-watcher" % "0.18.0",
Expand Down Expand Up @@ -312,6 +318,20 @@ val jsdocs = project
)
.enablePlugins(ScalaJSPlugin, ScalaJSBundlerPlugin)

val jswebsitedocs = project
.in(file("tests/websiteJs"))
.settings(
sharedSettings,
publish / skip := true,
scalaJSLinkerConfig ~= {
_.withModuleKind(ModuleKind.ESModule)
},
libraryDependencies ++= List(
"org.scala-js" %%% "scalajs-dom" % scalajsDom
)
)
.enablePlugins(ScalaJSPlugin)

lazy val worksheets = project
.in(file("tests/worksheets"))
.settings(
Expand Down Expand Up @@ -440,7 +460,10 @@ lazy val jsWorker =
.settings(
sharedSettings,
moduleName := "mdoc-js-worker",
libraryDependencies += ("org.scala-js" %% "scalajs-linker" % scalaJSVersion % Provided) cross CrossVersion.for3Use2_13
libraryDependencies ++= Seq(
"org.scala-js" %% "scalajs-linker" % scalaJSVersion % Provided cross CrossVersion.for3Use2_13,
"com.armanbilge" %% "scalajs-importmap" % "0.1.1" cross CrossVersion.for3Use2_13
)
)

lazy val js = project
Expand All @@ -449,6 +472,7 @@ lazy val js = project
.settings(
sharedSettings,
moduleName := "mdoc-js",
libraryDependencies ++= jsoniter,
Compile / unmanagedSourceDirectories ++= multiScalaDirectories("js").value
)
.dependsOn(mdoc)
Expand All @@ -471,8 +495,7 @@ lazy val docs = project
watchSources += (ThisBuild / baseDirectory).value / "docs",
Global / cancelable := true,
MdocPlugin.autoImport.mdoc := (Compile / run).evaluated,
mdocJS := Some(jsdocs),
mdocJSLibraries := (jsdocs / Compile / fullOptJS / webpack).value,
mdocJS := Some(jswebsitedocs),
MdocPlugin.mdocJSWorkerClasspath := {
val _ = (jsWorker / Compile / compile).value

Expand Down
3 changes: 1 addition & 2 deletions cli/src/main/scala/mdoc/internal/cli/MdocProperties.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ object MdocProperties {
out = getPath("out")
)
}
def default(cwd: AbsolutePath): MdocProperties = {
val path = "mdoc.properties"
def default(cwd: AbsolutePath, path: String): MdocProperties = {
Option(this.getClass.getClassLoader.getResourceAsStream(path)) match {
case Some(resource) =>
val props = new java.util.Properties()
Expand Down
25 changes: 20 additions & 5 deletions cli/src/main/scala/mdoc/internal/cli/Settings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import java.nio.file.FileSystems
import java.nio.file.Files
import java.nio.file.InvalidPathException
import java.nio.file.PathMatcher
import java.nio.file.Path
import mdoc.OnLoadContext
import mdoc.PostModifier
import mdoc.PreModifier
Expand Down Expand Up @@ -153,6 +154,14 @@ case class Settings(
@Hidden()
@Description("The pretty printer for variables")
variablePrinter: Variable => String = ReplVariablePrinter,
@Description(
"The absolute path to a file containing an import map file in this format; https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type/importmap"
)
importMapPath: Option[AbsolutePath] = None,
@Description(
"Defaults to mdoc.properties. This is the name of the properties file the CLI will read. It is assumed to be a resource on the classpath. Use the --extra-jars flag to customise the directory it is found in"
)
propertyFileName: String = "mdoc.properties",
@Hidden()
@Description("The Coursier logger used to report progress bars when downloading dependencies")
coursierLogger: coursierapi.Logger = coursierapi.Logger.progressBars()
Expand Down Expand Up @@ -261,9 +270,9 @@ object Settings { // extends MetaconfigScalametaImplicits with Decoders with Set
cwd = cwd
)
}
def default(cwd: AbsolutePath): Settings = {
def default(cwd: AbsolutePath, filename: String): Settings = {
val base = baseDefault(cwd)
val props = MdocProperties.default(cwd)
val props = MdocProperties.default(cwd, filename)
base.withProperties(props)
}

Expand Down Expand Up @@ -294,14 +303,20 @@ object Settings { // extends MetaconfigScalametaImplicits with Decoders with Set
generic.deriveDecoder[Settings](base)
}

def fromCliArgs(args: List[String], base: Settings): Configured[Settings] = {
def fromCliArgs(args: List[String], workingDirectory: Path): Configured[Settings] = {
Conf
.parseCliArgs[Settings](args)
.andThen(conf => {
val base =
Settings.default(
AbsolutePath(workingDirectory),
conf.get[String]("propertyFileName").getOrElse("mdoc.properties")
)
val cwd = conf.get[String]("cwd").map(AbsolutePath(_)(base.cwd)).getOrElse(base.cwd)
conf.as[Settings](decoder(base.copy(cwd = cwd)))
conf
.as[Settings](decoder(base.copy(cwd = cwd)))
.map(_.addSite(base.site))
})
.map(_.addSite(base.site))
}

def write(set: Settings) = ConfEncoder[Settings].write(set)
Expand Down
Loading

0 comments on commit de1f63f

Please sign in to comment.