From 0438cb7da1906703df9aa5b36e5ab12bcf85c960 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Thu, 11 Jul 2024 18:48:42 +0200 Subject: [PATCH 01/95] Remap ES Modules at link time --- build.sbt | 10 +- .../mdoc/internal/cli/MdocProperties.scala | 3 +- .../scala/mdoc/internal/cli/Settings.scala | 27 +++- docs/js.md | 4 +- .../src/main/java/ScalajsConfig.java | 12 +- .../src/main/scala/ImportMappedIRFile.scala | 30 +++++ .../main/scala/ImportMappedIRFileImpl.scala | 78 ++++++++++++ .../src/main/scala/ScalaJSWorker.scala | 25 +++- .../main/scala/ScalaJSWorkerProvider.scala | 1 - .../main/scala/mdoc/modifiers/JsConfig.scala | 16 ++- .../scala/mdoc/modifiers/JsImportMap.scala | 19 +++ .../scala/mdoc/modifiers/JsModifier.scala | 5 +- .../mdoc/modifiers/ScalajsClassloader.scala | 2 + mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala | 10 +- mdoc/src/main/scala/mdoc/Main.scala | 3 +- mdoc/src/main/scala/mdoc/MainSettings.scala | 8 +- .../unit-js/src/test/resources/importmap.json | 5 + .../src/test/scala/tests/js/JsCliSuite.scala | 117 ++++++++++++++++++ .../src/test/scala/tests/js/JsSuite.scala | 58 +++++++-- .../tests/markdown/BaseMarkdownSuite.scala | 16 +-- 20 files changed, 400 insertions(+), 49 deletions(-) create mode 100644 mdoc-js-worker/src/main/scala/ImportMappedIRFile.scala create mode 100644 mdoc-js-worker/src/main/scala/ImportMappedIRFileImpl.scala create mode 100644 mdoc-js/src/main/scala/mdoc/modifiers/JsImportMap.scala create mode 100644 tests/unit-js/src/test/resources/importmap.json diff --git a/build.sbt b/build.sbt index f833b023b..72841b11d 100644 --- a/build.sbt +++ b/build.sbt @@ -14,6 +14,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")) } @@ -250,7 +255,7 @@ lazy val mdoc = project "com.geirsson" %% "metaconfig-typesafe-config" % V.metaconfig, "com.lihaoyi" %% "fansi" % V.fansi, "com.lihaoyi" %% "pprint" % V.pprint - ) + ) ++ jsoniter ) .dependsOn(parser, runtime, cli) .enablePlugins(BuildInfoPlugin) @@ -441,7 +446,8 @@ lazy val js = project .settings( sharedSettings, moduleName := "mdoc-js", - Compile / unmanagedSourceDirectories ++= multiScalaDirectories("js").value + Compile / unmanagedSourceDirectories ++= multiScalaDirectories("js").value, + libraryDependencies ++= jsoniter ) .dependsOn(mdoc) diff --git a/cli/src/main/scala/mdoc/internal/cli/MdocProperties.scala b/cli/src/main/scala/mdoc/internal/cli/MdocProperties.scala index 7b366d8dc..aa24a5290 100644 --- a/cli/src/main/scala/mdoc/internal/cli/MdocProperties.scala +++ b/cli/src/main/scala/mdoc/internal/cli/MdocProperties.scala @@ -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() diff --git a/cli/src/main/scala/mdoc/internal/cli/Settings.scala b/cli/src/main/scala/mdoc/internal/cli/Settings.scala index e2476f6c6..75f17a700 100644 --- a/cli/src/main/scala/mdoc/internal/cli/Settings.scala +++ b/cli/src/main/scala/mdoc/internal/cli/Settings.scala @@ -32,6 +32,7 @@ import mdoc.internal.markdown.{GitHubIdGenerator, ReplVariablePrinter} import mdoc.internal.cli.CliEnrichments._ import pprint.TPrint import pprint.TPrintColors +import java.nio.file.Path class Section(val name: String) extends StaticAnnotation @@ -155,7 +156,15 @@ case class Settings( variablePrinter: Variable => String = ReplVariablePrinter, @Hidden() @Description("The Coursier logger used to report progress bars when downloading dependencies") - coursierLogger: coursierapi.Logger = coursierapi.Logger.progressBars() + coursierLogger: coursierapi.Logger = coursierapi.Logger.progressBars(), + @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" + ) + propFileName: String = "mdoc.properties" ) extends mdoc.parser.ParserSettings { val isMarkdownFileExtension = markdownExtensions.toSet @@ -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) } @@ -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]("propFileName").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) diff --git a/docs/js.md b/docs/js.md index 7d0aa8a16..94086176e 100644 --- a/docs/js.md +++ b/docs/js.md @@ -75,7 +75,9 @@ js-linker-classpath=$(coursier fetch org.scalameta:mdoc-js-worker_@SCALA_BINARY_ EOT ``` -Note: For scala 3, you may need `js-scalac-options=--scala-js` - this is currently untested. +Note: For scala 3, you may need `js-scalac-options=-scalajs` - instead of the XPlugin incantation - [Doc](https://docs.scala-lang.org/scala3/guides/migration/options-lookup.html#compiler-plugins). + + Next, create a basic Markdown files with an `mdoc:js` modifier: diff --git a/mdoc-js-interfaces/src/main/java/ScalajsConfig.java b/mdoc-js-interfaces/src/main/java/ScalajsConfig.java index f626f4ce2..b93210747 100644 --- a/mdoc-js-interfaces/src/main/java/ScalajsConfig.java +++ b/mdoc-js-interfaces/src/main/java/ScalajsConfig.java @@ -1,17 +1,20 @@ package mdoc.js.interfaces; +import java.util.Map; + public class ScalajsConfig { public ModuleType moduleType; public boolean fullOpt; public boolean sourceMap; public boolean batchMode; public boolean closureCompiler; + public Map importMap; public ScalajsConfig() { } - + public ScalajsConfig withModuleKind(ModuleType kind) { - if(kind == ModuleType.ESModule) + if (kind == ModuleType.ESModule) this.moduleType = ModuleType.ESModule; else if (kind == ModuleType.NoModule) this.moduleType = ModuleType.NoModule; @@ -20,6 +23,11 @@ else if (kind == ModuleType.CommonJSModule) return this; } + public ScalajsConfig withImportMap(Map importMap) { + this.importMap = importMap; + return this; + } + public ScalajsConfig withOptimized(boolean enabled) { this.fullOpt = enabled; return this; diff --git a/mdoc-js-worker/src/main/scala/ImportMappedIRFile.scala b/mdoc-js-worker/src/main/scala/ImportMappedIRFile.scala new file mode 100644 index 000000000..5ff419154 --- /dev/null +++ b/mdoc-js-worker/src/main/scala/ImportMappedIRFile.scala @@ -0,0 +1,30 @@ +/* + * Copyright 2023 Arman Bilge + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mdoc.js.worker + +/* + Copy pasted from here + https://github.com/armanbilge/scalajs-importmap/blob/main/importmap/src/main/scala/com/armanbilge/sjsimportmap/ImportMappedIRFile.scala + */ + +import org.scalajs.linker.interface.IRFile +import org.scalajs.linker.interface.unstable.IRFileImpl + +object ImportMappedIRFile { + def fromIRFile(ir: IRFile)(mapper: String => String): IRFile = + new ImportMappedIRFileImpl(IRFileImpl.fromIRFile(ir), mapper) +} diff --git a/mdoc-js-worker/src/main/scala/ImportMappedIRFileImpl.scala b/mdoc-js-worker/src/main/scala/ImportMappedIRFileImpl.scala new file mode 100644 index 000000000..6cb4dd23a --- /dev/null +++ b/mdoc-js-worker/src/main/scala/ImportMappedIRFileImpl.scala @@ -0,0 +1,78 @@ +/* + * Copyright 2023 Arman Bilge + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mdoc.js.worker + +/* +Copy pasted from here; +https://github.com/armanbilge/scalajs-importmap/blob/main/importmap/src/main/scala/com/armanbilge/sjsimportmap/ImportMappedIRFileImpl.scala + */ + +import org.scalajs.ir.Trees +import org.scalajs.linker.interface.unstable.IRFileImpl + +import scala.concurrent.ExecutionContext +import scala.concurrent.Future + +private final class ImportMappedIRFileImpl(impl: IRFileImpl, mapper: String => String) + extends IRFileImpl(impl.path, impl.version) { + + def entryPointsInfo(implicit ec: ExecutionContext) = + impl.entryPointsInfo + + def tree(implicit ec: ExecutionContext): Future[Trees.ClassDef] = + impl.tree.map { classDef => + if (classDef.jsNativeLoadSpec.isDefined || classDef.jsNativeMembers.nonEmpty) + Trees.ClassDef( + classDef.name, + classDef.originalName, + classDef.kind, + classDef.jsClassCaptures, + classDef.superClass, + classDef.interfaces, + classDef.jsSuperClass, + classDef.jsNativeLoadSpec.map(transform), + classDef.fields, + classDef.methods, + classDef.jsConstructor, + classDef.jsMethodProps, + classDef.jsNativeMembers.map(transform), + classDef.topLevelExportDefs + )(classDef.optimizerHints)(classDef.pos) + else classDef + } + + private[this] def transform(member: Trees.JSNativeMemberDef): Trees.JSNativeMemberDef = + Trees.JSNativeMemberDef( + member.flags, + member.name, + transform(member.jsNativeLoadSpec) + )(member.pos) + + private[this] def transform(spec: Trees.JSNativeLoadSpec): Trees.JSNativeLoadSpec = + spec match { + case importSpec: Trees.JSNativeLoadSpec.Import => transform(importSpec) + case Trees.JSNativeLoadSpec.ImportWithGlobalFallback(importSpec, globalSpec) => + Trees.JSNativeLoadSpec.ImportWithGlobalFallback(transform(importSpec), globalSpec) + case other => other + } + + private[this] def transform( + importSpec: Trees.JSNativeLoadSpec.Import + ): Trees.JSNativeLoadSpec.Import = + importSpec.copy(module = mapper(importSpec.module)) + +} diff --git a/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala b/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala index 7e431a155..5cf18f542 100644 --- a/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala +++ b/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala @@ -18,11 +18,14 @@ import org.scalajs.logging.Logger import org.scalajs.logging.Level import org.scalajs.linker.standard.MemIRFileImpl import org.scalajs.linker.interface.Semantics +import scala.collection.JavaConverters._ + class ScalaJSWorker( config: ScalajsConfig, logger: Logger ) extends ScalajsWorkerApi { case class IFile(mem: IRFile) extends ScalajsWorkerApi.IRFile + val linker = { val cfg = StandardConfig() @@ -43,6 +46,12 @@ class ScalaJSWorker( StandardImpl.clearableLinker(cfg) } + lazy val remapFct = config.importMap.asScala.toSeq.foldLeft((in: String) => in) { + case (fct, (s1, s2)) => + val fct2: (String => String) = (in => in.replace(s1, s2)) + (in => fct(fct2(in))) + } + var cachedFiles = Seq.empty[org.scalajs.linker.interface.IRFile] val cache = StandardImpl.irFileCache().newCache @@ -62,10 +71,18 @@ class ScalaJSWorker( val mem = MemOutputDirectory() val report = Await.result( linker.link( - cachedFiles.toSeq ++ - in.toSeq.collect { case IFile(o) => - o - }, + ( + cachedFiles.toSeq ++ + in.toSeq + .collect { case IFile(o) => + o + } + ).map { ir => + if (config.importMap.isEmpty) + ir + else + ImportMappedIRFile.fromIRFile(ir)(remapFct) + }, Seq.empty, mem, logger diff --git a/mdoc-js-worker/src/main/scala/ScalaJSWorkerProvider.scala b/mdoc-js-worker/src/main/scala/ScalaJSWorkerProvider.scala index 84abbe256..29fc7e347 100644 --- a/mdoc-js-worker/src/main/scala/ScalaJSWorkerProvider.scala +++ b/mdoc-js-worker/src/main/scala/ScalaJSWorkerProvider.scala @@ -24,7 +24,6 @@ class ScalaJSWorkerProvider extends i.ScalajsWorkerProvider { def trace(ex: => Throwable) = logger.trace(ex) } - new ScalaJSWorker(config, wrappedLogger) } } diff --git a/mdoc-js/src/main/scala/mdoc/modifiers/JsConfig.scala b/mdoc-js/src/main/scala/mdoc/modifiers/JsConfig.scala index 0cdc7a3f9..70437f452 100644 --- a/mdoc-js/src/main/scala/mdoc/modifiers/JsConfig.scala +++ b/mdoc-js/src/main/scala/mdoc/modifiers/JsConfig.scala @@ -7,6 +7,9 @@ import scala.meta.internal.io.PathIO import scala.meta.io.AbsolutePath import scala.meta.io.Classpath import mdoc.js.interfaces._ +import scala.collection.immutable.HashMap +import com.github.plokhotnyuk.jsoniter_scala.core._ +import mdoc.modifiers.ImportMapJsonIr.ImportMap case class JsConfig( moduleKind: ModuleType = ModuleType.NoModule, @@ -20,7 +23,8 @@ case class JsConfig( fullOpt: Boolean = true, htmlPrefix: String = "", relativeLinkPrefix: String = "", - batchMode: Boolean = false + batchMode: Boolean = false, + importMap: Map[String, String] = Map.empty ) { lazy val isCommonJS: Boolean = moduleKind == ModuleType.CommonJSModule lazy val isEsModule: Boolean = moduleKind == ModuleType.ESModule @@ -99,7 +103,15 @@ object JsConfig { } }, relativeLinkPrefix = ctx.site.getOrElse("js-relative-link-prefix", base.relativeLinkPrefix), - batchMode = ctx.site.getOrElse("js-batch-mode", "false").toBoolean + batchMode = ctx.site.getOrElse("js-batch-mode", "false").toBoolean, + importMap = ctx.settings.importMapPath match { + case None => new HashMap[String, String]() + case Some(value) => + val importMapRaw = scala.io.Source.fromFile(value.toFile).getLines().mkString("\n") + val importMapParsed = readFromString[ImportMap](importMapRaw) + println(importMapParsed) + importMapParsed.imports + } ) } } diff --git a/mdoc-js/src/main/scala/mdoc/modifiers/JsImportMap.scala b/mdoc-js/src/main/scala/mdoc/modifiers/JsImportMap.scala new file mode 100644 index 000000000..ca0f4045a --- /dev/null +++ b/mdoc-js/src/main/scala/mdoc/modifiers/JsImportMap.scala @@ -0,0 +1,19 @@ +package mdoc.modifiers + +import com.github.plokhotnyuk.jsoniter_scala.core._ +import com.github.plokhotnyuk.jsoniter_scala.macros._ + +object ImportMapJsonIr { + + type Scope = Map[String, String] + + final case class ImportMap( + val imports: Map[String, String], + val scopes: Option[Map[String, Scope]] + ) + + object ImportMap { + implicit val codec: JsonValueCodec[ImportMap] = JsonCodecMaker.make + } + +} diff --git a/mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala b/mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala index afc562bf9..e2f2c7d23 100644 --- a/mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala +++ b/mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala @@ -73,7 +73,10 @@ class JsModifier extends mdoc.PreModifier { .withModuleKind(config.moduleKind) .withSourceMap(false) .withBatchMode(config.batchMode) - .withClosureCompiler(config.fullOpt) + .withClosureCompiler( + config.fullOpt && !(config.moduleKind == ModuleType.ESModule) + ) // Closure compiler dosen't work with ESModules + .withImportMap(config.importMap.asJava) } override def onLoad(ctx: OnLoadContext): Unit = { diff --git a/mdoc-js/src/main/scala/mdoc/modifiers/ScalajsClassloader.scala b/mdoc-js/src/main/scala/mdoc/modifiers/ScalajsClassloader.scala index 5f3f6e601..f20923c14 100644 --- a/mdoc-js/src/main/scala/mdoc/modifiers/ScalajsClassloader.scala +++ b/mdoc-js/src/main/scala/mdoc/modifiers/ScalajsClassloader.scala @@ -9,6 +9,8 @@ final class FilteringClassLoader(parent: ClassLoader) extends ClassLoader(parent "scala.", "org.scalajs.linker.", "org.scalajs.logging.", + "org.scalajs.", + "org.scalajs.ir.", "sun.reflect.", "jdk.internal.reflect.", "mdoc.js." diff --git a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala index e46aa0417..fa09a5b88 100644 --- a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala +++ b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala @@ -188,7 +188,15 @@ object MdocPlugin extends AutoPlugin { classpath.mkString(java.io.File.pathSeparator) ) IO.write(props, "mdoc properties", out) - List(out) + val esVersion = props.clone().asInstanceOf[java.util.Properties] + esVersion.put("js-module-kind", "ESModule") + val esOut = managedResourceDirectories.in(Compile).value.head / "es.properties" + IO.write( + esVersion, + "mdoc esmoddule properties", + esOut + ) + List(out, esOut) } ) diff --git a/mdoc/src/main/scala/mdoc/Main.scala b/mdoc/src/main/scala/mdoc/Main.scala index b1d18bd39..f48749378 100644 --- a/mdoc/src/main/scala/mdoc/Main.scala +++ b/mdoc/src/main/scala/mdoc/Main.scala @@ -36,8 +36,7 @@ trait MainProcess { process(args, new ConsoleReporter(out), cwd) } def process(args: Array[String], reporter: Reporter, cwd: Path): Int = { - val base = Settings.default(AbsolutePath(cwd)) - val ctx = Settings.fromCliArgs(args.toList, base) + val ctx = Settings.fromCliArgs(args.toList, cwd) MainOps.process(ctx, reporter) } def process(settings: MainSettings): Int = { diff --git a/mdoc/src/main/scala/mdoc/MainSettings.scala b/mdoc/src/main/scala/mdoc/MainSettings.scala index ef71b8ee2..7758efdcb 100644 --- a/mdoc/src/main/scala/mdoc/MainSettings.scala +++ b/mdoc/src/main/scala/mdoc/MainSettings.scala @@ -23,7 +23,7 @@ final class MainSettings private ( def withArgs(args: List[String]): MainSettings = { if (args.isEmpty) this else { - Settings.fromCliArgs(args, settings) match { + Settings.fromCliArgs(args, settings.cwd.toNIO) match { case Configured.Ok(newSettings) => copy(settings = newSettings) case Configured.NotOk(error) => @@ -121,12 +121,12 @@ final class MainSettings private ( } object MainSettings { - def apply(workingDirectory: Path): MainSettings = { - val settings = Settings.default(AbsolutePath(workingDirectory)) + def apply(workingDirectory: Path, propertiesFileName: String): MainSettings = { + val settings = Settings.default(AbsolutePath(workingDirectory), propertiesFileName) val reporter = ConsoleReporter.default new MainSettings(settings, reporter) } def apply(): MainSettings = { - MainSettings(PathIO.workingDirectory.toNIO) + MainSettings(PathIO.workingDirectory.toNIO, "mdoc.properties") } } diff --git a/tests/unit-js/src/test/resources/importmap.json b/tests/unit-js/src/test/resources/importmap.json new file mode 100644 index 000000000..872b43df9 --- /dev/null +++ b/tests/unit-js/src/test/resources/importmap.json @@ -0,0 +1,5 @@ +{ + "imports": { + "@stdlib/blas": "https://cdn.jsdelivr.net/npm/@stdlib/blas@0.2.1/+esm" + } +} diff --git a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala index 131cb28cf..c5d526b16 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala @@ -1,11 +1,17 @@ package tests.js +import mdoc.internal.cli.MdocProperties import mdoc.internal.cli.Settings import scala.meta.io.Classpath import tests.markdown.StringSyntax._ import tests.cli.BaseCliSuite import scala.meta.internal.io.PathIO import tests.js.JsTests.suffix +import java.io.ByteArrayOutputStream +import java.io.PrintStream +import scala.meta.internal.io.FileIO +import scala.meta.io.AbsolutePath +import java.nio.charset.StandardCharsets class JsCliSuite extends BaseCliSuite { @@ -43,4 +49,115 @@ class JsCliSuite extends BaseCliSuite { includeOutputPath = { path => !path.toNIO.getFileName.toString.endsWith(".js") } ) + checkCli( + "basic_es", + """/docs/index1.md + |```scala mdoc:js + |println("hello world!") + |``` + |/docs/index2.md + |```scala mdoc:js + |println("hello world!") + |``` + |""".stripMargin, + s"""|/docs/index1.md + |```scala + |println("hello world!") + |``` + |
+ | + | + | + | + |/docs/index2.md + |```scala + |println("hello world!") + |``` + |
+ | + |""".stripMargin, + extraArgs = Array("--prop-file-name", "es.properties"), + includeOutputPath = { path => !path.toNIO.getFileName.toString.endsWith(".js") } + ) + + /** See MdocPlugin for where the .properties file is generated. + */ + checkCli( + "basic_es", + """/docs/index1.md + |```scala mdoc:js + |println("hello world!") + |``` + |/docs/index2.md + |```scala mdoc:js + |println("hello world!") + |``` + |""".stripMargin, + s"""|/docs/index1.md + |```scala + |println("hello world!") + |``` + |
+ | + | + | + | + |/docs/index2.md + |```scala + |println("hello world!") + |``` + |
+ | + |""".stripMargin, + extraArgs = Array("--prop-file-name", "es.properties"), + includeOutputPath = { path => !path.toNIO.getFileName.toString.endsWith(".js") } + ) + + // see the importmap.json in resources + test("import remap".only) { + val myStdout = new ByteArrayOutputStream() + tests.cli.StringFS.fromString( + """|/docs/facade.md + | make a facade + |```scala mdoc:js:shared + |import scala.scalajs.js + |import scala.scalajs.js.annotation.JSImport + | + |@js.native + |@JSImport("@stdlib/blas/base", JSImport.Namespace) + |object blas extends BlasArrayOps + | + |@js.native + |trait BlasArrayOps extends js.Object{} + |``` + |Now fake do something with the facade + |```scala mdoc:js + |println(blas) + |```""".stripMargin, + in() + ) + val args = Array[String]( + "--in", + in().toString, + "--out", + out().toString, + "--cwd", + in().syntax, + "--prop-file-name", + "es.properties", + "--import-map-path", + this.getClass.getClassLoader.getResource("importmap.json").getPath + ) + println(out()) + val code = mdoc.Main.process(args, new PrintStream(myStdout), in().toNIO) + val generatedJs = AbsolutePath(out().toString + "/docs/facade.md.js") + val content = new String(FileIO.readAllBytes(generatedJs), StandardCharsets.UTF_8) + println(content) + assert( + content.contains("https://cdn.jsdelivr.net/npm/@stdlib/blas") + ) + assertEquals(code, 0, clues(myStdout)) + + } + } diff --git a/tests/unit-js/src/test/scala/tests/js/JsSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsSuite.scala index bb7429b17..977197dad 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsSuite.scala @@ -6,15 +6,18 @@ import tests.markdown.StringSyntax._ import tests.markdown.BaseMarkdownSuite import tests.js.JsTests.suffix import tests.markdown.Compat +import scala.meta.io.AbsolutePath class JsSuite extends BaseMarkdownSuite { // NOTE(olafur) Optimization. Cache settings to reuse the Scala.js compiler instance. // By default, we create new modifiers for each unit test, which is usually fast. - override lazy val baseSettings: Settings = super.baseSettings.copy( - site = super.baseSettings.site ++ Map( - "js-opt" -> "fast" + override def baseSettings(resourcePropertyFileName: String): Settings = super + .baseSettings() + .copy( + site = super.baseSettings().site ++ Map( + "js-opt" -> "fast" + ) ) - ) check( "basic", @@ -47,8 +50,37 @@ class JsSuite extends BaseMarkdownSuite { | """.stripMargin, settings = { - baseSettings.copy( - site = baseSettings.site.updated("js-module-kind", "ESModule") + baseSettings().copy( + site = baseSettings().site.updated("js-module-kind", "ESModule") + ) + } + ) + + checkCompiles( + "es_remap_settings", + """ + |```scala mdoc:js:shared + | + |import scala.scalajs.js + |import scala.scalajs.js.annotation.JSImport + | + |@js.native + |@JSImport("@stdlib/blas/base", JSImport.Namespace) + |object blas extends BlasArrayOps + | + |@js.native + |trait BlasArrayOps extends js.Object{} + |``` + | + |```scala mdoc:js + |println(blas) + |``` + |""".stripMargin, + settings = { + baseSettings().copy( + site = baseSettings().site.updated("js-module-kind", "ESModule"), + importMapPath = + Some(AbsolutePath(this.getClass.getClassLoader.getResource("importmap.json").getPath)) ) } ) @@ -324,10 +356,10 @@ class JsSuite extends BaseMarkdownSuite { | ^ """.stripMargin, settings = { - val noScalajsDom = Classpath(baseSettings.site("js-classpath")).entries + val noScalajsDom = Classpath(baseSettings().site("js-classpath")).entries .filterNot(_.toNIO.getFileName.toString.contains("scalajs-dom")) - baseSettings.copy( - site = baseSettings.site.updated("js-classpath", Classpath(noScalajsDom).syntax) + baseSettings().copy( + site = baseSettings().site.updated("js-classpath", Classpath(noScalajsDom).syntax) ) }, compat = Map( @@ -377,8 +409,8 @@ class JsSuite extends BaseMarkdownSuite { createTempFile("mdoc-library.js"), createTempFile("mdoc-library.js.map") ) - baseSettings.copy( - site = baseSettings.site + baseSettings().copy( + site = baseSettings().site .updated("js-module-kind", "CommonJSModule") .updated("js-libraries", Classpath(libraries).syntax) ) @@ -401,8 +433,8 @@ class JsSuite extends BaseMarkdownSuite { | |""".stripMargin, settings = { - baseSettings.copy( - site = baseSettings.site.updated("js-html-header", unpkgReact) + baseSettings().copy( + site = baseSettings().site.updated("js-html-header", unpkgReact) ) } ) diff --git a/tests/unit/src/main/scala/tests/markdown/BaseMarkdownSuite.scala b/tests/unit/src/main/scala/tests/markdown/BaseMarkdownSuite.scala index 098ce473e..8cdfc0571 100644 --- a/tests/unit/src/main/scala/tests/markdown/BaseMarkdownSuite.scala +++ b/tests/unit/src/main/scala/tests/markdown/BaseMarkdownSuite.scala @@ -32,16 +32,16 @@ abstract class BaseMarkdownSuite extends tests.BaseSuite { file.write("") file } - protected def baseSettings: Settings = + protected def baseSettings(resourcePropertyFileName: String = "mdoc.properties"): Settings = Settings - .default(createTempDirectory()) + .default(createTempDirectory(), resourcePropertyFileName) .copy( site = Map( "version" -> "1.0", "boom" -> "$boom" ) ) - .withProperties(MdocProperties.default(PathIO.workingDirectory)) + .withProperties(MdocProperties.default(PathIO.workingDirectory, resourcePropertyFileName)) private val myStdout = new ByteArrayOutputStream() private def newReporter(): ConsoleReporter = { @@ -60,7 +60,7 @@ abstract class BaseMarkdownSuite extends tests.BaseSuite { name: TestOptions, original: String, expected: String, - settings: Settings = baseSettings, + settings: Settings = baseSettings(), compat: Map[Compat.ScalaVersion, String] = Map.empty )(implicit loc: munit.Location): Unit = { test(name) { @@ -68,7 +68,7 @@ abstract class BaseMarkdownSuite extends tests.BaseSuite { val context = newContext(settings, reporter) val input = Input.VirtualFile(name.name.replace(':', '-') + ".md", original) val file = InputFile.fromRelativeFilename(input.path, settings) - Markdown.toMarkdown(input, context, file, baseSettings.site, reporter, settings) + Markdown.toMarkdown(input, context, file, baseSettings().site, reporter, settings) assert(reporter.hasErrors, "Expected errors but reporter.hasErrors=false") val obtainedErrors = Compat.postProcess( fansi.Str(myStdout.toString).plainText.trimLineEnds, @@ -85,7 +85,7 @@ abstract class BaseMarkdownSuite extends tests.BaseSuite { def checkCompiles( name: TestOptions, original: String, - settings: Settings = baseSettings, + settings: Settings = baseSettings(), onOutput: String => Unit = _ => () ): Unit = { test(name) { @@ -94,7 +94,7 @@ abstract class BaseMarkdownSuite extends tests.BaseSuite { val input = Input.VirtualFile(name.name.replace(":", "-") + ".md", original) val file = InputFile.fromRelativeFilename(input.path, settings) val obtained = - Markdown.toMarkdown(input, context, file, baseSettings.site, reporter, settings) + Markdown.toMarkdown(input, context, file, baseSettings().site, reporter, settings) val colorOut = myStdout.toString() print(colorOut) val stdout = fansi.Str(colorOut).plainText @@ -108,7 +108,7 @@ abstract class BaseMarkdownSuite extends tests.BaseSuite { name: TestOptions, original: String, expected: String, - settings: Settings = baseSettings, + settings: Settings = baseSettings(), compat: Map[Compat.ScalaVersion, String] = Map.empty )(implicit loc: munit.Location): Unit = { checkCompiles( From 0e0e85956cb35edf75da73b803cdb52bd92eea4f Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Thu, 11 Jul 2024 18:48:44 +0200 Subject: [PATCH 02/95] remap esmodules at link time --- .DS_Store | Bin 0 -> 6148 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..51227d71a94e52683c4d00d01ac912a7dfa75f3b GIT binary patch literal 6148 zcmeHKI|>3p3{CuiU}I@HSMUad=n3`$7K)81_^Y?_TprDrPoXS!S|~4&yqQeiEc=Sh zMnrUeSOjyq1*=D6P-_S;VFyM%Fva+Zyp5TE&WLZbpy zfC^9nDnJE3tw46z(fHFB^FAs-1%6%u`#u!7VNGlU{nLTqBLHxKv>VnwO8|={fHkoV zL$0gG>bW2DSK<@<*e5h_zG?{ceokWPC@W?4D@!4jkV*c7e!sMHO^~d U8|ZZ8oet#BfayY`0^e5P0W;M1& literal 0 HcmV?d00001 From 54b015f2666d5b3b44d9fcc0b4b37cf29d1c9ca4 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Thu, 11 Jul 2024 19:13:07 +0200 Subject: [PATCH 03/95] . --- .../test/scala/tests/cli/CliArgsSuite.scala | 6 +-- .../scala/tests/cli/MdocPropertiesSuite.scala | 2 +- .../markdown/IndentedMarkdownSuite.scala | 10 ++-- .../tests/markdown/LinkHygieneSuite.scala | 2 +- .../markdown/MarkdownCompilerSuite.scala | 2 +- .../tests/markdown/ScastieModifierSuite.scala | 26 ++++++---- .../tests/markdown/StringModifierSuite.scala | 50 ++++++++++--------- .../tests/markdown/VariablePrinterSuite.scala | 10 ++-- 8 files changed, 57 insertions(+), 51 deletions(-) diff --git a/tests/unit/src/test/scala/tests/cli/CliArgsSuite.scala b/tests/unit/src/test/scala/tests/cli/CliArgsSuite.scala index 3430f070d..f7cb4768a 100644 --- a/tests/unit/src/test/scala/tests/cli/CliArgsSuite.scala +++ b/tests/unit/src/test/scala/tests/cli/CliArgsSuite.scala @@ -12,13 +12,13 @@ import mdoc.internal.cli.Context class CliArgsSuite extends FunSuite { private val reporter = ConsoleReporter.default - private val base = Settings.default(PathIO.workingDirectory) + // private val base = Settings.default(PathIO.workingDirectory, "mdoc.properties") def checkOK(name: String, args: List[String], onSuccess: Settings => Unit = _ => ())(implicit loc: munit.Location ): Unit = { test(name) { - val obtained = Settings.fromCliArgs(args, base).get + val obtained = Settings.fromCliArgs(args, PathIO.workingDirectory.toNIO).get onSuccess(obtained) } } @@ -28,7 +28,7 @@ class CliArgsSuite extends FunSuite { ): Unit = { test(name) { Settings - .fromCliArgs(args, base) + .fromCliArgs(args, PathIO.workingDirectory.toNIO) .andThen(s => Context.fromSettings(s, reporter)) .toEither match { case Left(obtained) => diff --git a/tests/unit/src/test/scala/tests/cli/MdocPropertiesSuite.scala b/tests/unit/src/test/scala/tests/cli/MdocPropertiesSuite.scala index 6f1f6d07c..31e4e2dff 100644 --- a/tests/unit/src/test/scala/tests/cli/MdocPropertiesSuite.scala +++ b/tests/unit/src/test/scala/tests/cli/MdocPropertiesSuite.scala @@ -7,7 +7,7 @@ import tests.markdown.Compat class MdocPropertiesSuite extends FunSuite { test("default") { - val obtained = MdocProperties.default(PathIO.workingDirectory).scalacOptions + val obtained = MdocProperties.default(PathIO.workingDirectory, "mdoc.properties").scalacOptions if (Compat.isScala3) assert(obtained.contains("-language:implicitConversions")) diff --git a/tests/unit/src/test/scala/tests/markdown/IndentedMarkdownSuite.scala b/tests/unit/src/test/scala/tests/markdown/IndentedMarkdownSuite.scala index 7316df65f..65ddac716 100644 --- a/tests/unit/src/test/scala/tests/markdown/IndentedMarkdownSuite.scala +++ b/tests/unit/src/test/scala/tests/markdown/IndentedMarkdownSuite.scala @@ -16,7 +16,7 @@ class IndentedMarkdownSuite extends BaseMarkdownSuite { | println(msg) | ``` """.stripMargin, - settings = baseSettings.copy(allowCodeFenceIndented = true) + settings = baseSettings().copy(allowCodeFenceIndented = true) ) check( @@ -33,7 +33,7 @@ class IndentedMarkdownSuite extends BaseMarkdownSuite { | println(msg) | ``` """.stripMargin, - settings = baseSettings.copy(allowCodeFenceIndented = true) + settings = baseSettings().copy(allowCodeFenceIndented = true) ) check( @@ -50,7 +50,7 @@ class IndentedMarkdownSuite extends BaseMarkdownSuite { | println(msg) | ``` """.stripMargin, - settings = baseSettings.copy(allowCodeFenceIndented = true) + settings = baseSettings().copy(allowCodeFenceIndented = true) ) check( @@ -67,7 +67,7 @@ class IndentedMarkdownSuite extends BaseMarkdownSuite { | println(msg) | ``` """.stripMargin, - settings = baseSettings.copy(allowCodeFenceIndented = true) + settings = baseSettings().copy(allowCodeFenceIndented = true) ) check( @@ -86,6 +86,6 @@ class IndentedMarkdownSuite extends BaseMarkdownSuite { | // Hello! | ``` """.stripMargin, - settings = baseSettings.copy(allowCodeFenceIndented = true) + settings = baseSettings().copy(allowCodeFenceIndented = true) ) } diff --git a/tests/unit/src/test/scala/tests/markdown/LinkHygieneSuite.scala b/tests/unit/src/test/scala/tests/markdown/LinkHygieneSuite.scala index cca95516f..7f32f40a9 100644 --- a/tests/unit/src/test/scala/tests/markdown/LinkHygieneSuite.scala +++ b/tests/unit/src/test/scala/tests/markdown/LinkHygieneSuite.scala @@ -23,7 +23,7 @@ class LinkHygieneSuite extends FunSuite { reporter.reset() val root = tests.cli.StringFS.fromString(original) val settings = Settings - .default(root) + .default(root, "mdoc.properties") .copy(reportRelativePaths = true, in = List(root), out = List(root)) val links = DocumentLinks.fromGeneratedSite(settings) LinkHygiene.report(asError, LinkHygiene.lint(links, verbose), reporter) diff --git a/tests/unit/src/test/scala/tests/markdown/MarkdownCompilerSuite.scala b/tests/unit/src/test/scala/tests/markdown/MarkdownCompilerSuite.scala index 9a3ac5f9b..a82b0c3f7 100644 --- a/tests/unit/src/test/scala/tests/markdown/MarkdownCompilerSuite.scala +++ b/tests/unit/src/test/scala/tests/markdown/MarkdownCompilerSuite.scala @@ -16,7 +16,7 @@ class MarkdownCompilerSuite extends FunSuite { private val compiler = MarkdownBuilder.default() private val reporter = ConsoleReporter.default - private val settings = Settings.default(PathIO.workingDirectory) + private val settings = Settings.default(PathIO.workingDirectory, "mdoc.properties") def checkIgnore(name: String, original: String, expected: String): Unit = test(name.ignore) {} diff --git a/tests/unit/src/test/scala/tests/markdown/ScastieModifierSuite.scala b/tests/unit/src/test/scala/tests/markdown/ScastieModifierSuite.scala index 463d9ec83..8436fa2ee 100644 --- a/tests/unit/src/test/scala/tests/markdown/ScastieModifierSuite.scala +++ b/tests/unit/src/test/scala/tests/markdown/ScastieModifierSuite.scala @@ -8,22 +8,26 @@ class ScastieModifierSuite extends BaseMarkdownSuite { private val debugClassSuffix = "test" - override val baseSettings: Settings = super.baseSettings.copy( - stringModifiers = List( - new ScastieModifier( - debugClassSuffix = Some(debugClassSuffix) + override def baseSettings(resourcePropertyFileName: String): Settings = super + .baseSettings() + .copy( + stringModifiers = List( + new ScastieModifier( + debugClassSuffix = Some(debugClassSuffix) + ) ) ) - ) - val darkThemeSettings: Settings = super.baseSettings.copy( - stringModifiers = List( - new ScastieModifier( - theme = "dark", - debugClassSuffix = Some(debugClassSuffix) + val darkThemeSettings: Settings = super + .baseSettings() + .copy( + stringModifiers = List( + new ScastieModifier( + theme = "dark", + debugClassSuffix = Some(debugClassSuffix) + ) ) ) - ) check( "inline", diff --git a/tests/unit/src/test/scala/tests/markdown/StringModifierSuite.scala b/tests/unit/src/test/scala/tests/markdown/StringModifierSuite.scala index 8118fee10..1dd4fc2f7 100644 --- a/tests/unit/src/test/scala/tests/markdown/StringModifierSuite.scala +++ b/tests/unit/src/test/scala/tests/markdown/StringModifierSuite.scala @@ -7,32 +7,34 @@ import mdoc.Reporter import mdoc.internal.cli.Settings class StringModifierSuite extends BaseMarkdownSuite { - override def baseSettings: Settings = - super.baseSettings.copy( - stringModifiers = List( - new StringModifier { - override val name: String = "hello" - override def process(info: String, code: Input, reporter: Reporter): String = { - code.text.trim + " " + info + override def baseSettings(resourcePropertyFileName: String): Settings = + super + .baseSettings() + .copy( + stringModifiers = List( + new StringModifier { + override val name: String = "hello" + override def process(info: String, code: Input, reporter: Reporter): String = { + code.text.trim + " " + info + } + }, + new StringModifier { + override val name: String = "reporter" + override def process(info: String, code: Input, reporter: Reporter): String = { + val length = code.text.trim.length + val pos = Position.Range(code, 0, length) + reporter.error(pos, "This is a message") + "reported" + } + }, + new StringModifier { + override val name: String = "exception" + override def process(info: String, code: Input, reporter: Reporter): String = { + throw new IllegalArgumentException(info) + } } - }, - new StringModifier { - override val name: String = "reporter" - override def process(info: String, code: Input, reporter: Reporter): String = { - val length = code.text.trim.length - val pos = Position.Range(code, 0, length) - reporter.error(pos, "This is a message") - "reported" - } - }, - new StringModifier { - override val name: String = "exception" - override def process(info: String, code: Input, reporter: Reporter): String = { - throw new IllegalArgumentException(info) - } - } + ) ) - ) check( "hello-world", diff --git a/tests/unit/src/test/scala/tests/markdown/VariablePrinterSuite.scala b/tests/unit/src/test/scala/tests/markdown/VariablePrinterSuite.scala index 5f7c9f5eb..671917e2c 100644 --- a/tests/unit/src/test/scala/tests/markdown/VariablePrinterSuite.scala +++ b/tests/unit/src/test/scala/tests/markdown/VariablePrinterSuite.scala @@ -20,7 +20,7 @@ class VariablePrinterSuite extends BaseMarkdownSuite { |// b: Int = 2 |``` """.stripMargin, - baseSettings + baseSettings() ) check( @@ -38,10 +38,10 @@ class VariablePrinterSuite extends BaseMarkdownSuite { |val c = 30 |``` """.stripMargin, - baseSettings + baseSettings() ) - val trailingComment = baseSettings.copy(variablePrinter = { variable => + val trailingComment = baseSettings().copy(variablePrinter = { variable => variable.runtimeValue match { case n: Int if variable.totalVariablesInStatement == 1 => s" // Number($n)" case _ => variable.toString @@ -69,7 +69,7 @@ class VariablePrinterSuite extends BaseMarkdownSuite { trailingComment ) - val lastStatementOnly = baseSettings.copy(variablePrinter = { variable => + val lastStatementOnly = baseSettings().copy(variablePrinter = { variable => if (variable.indexOfStatementInCodeFence + 1 == variable.totalStatementsInCodeFence) { variable.toString } else { @@ -97,7 +97,7 @@ class VariablePrinterSuite extends BaseMarkdownSuite { lastStatementOnly ) - val initialOffset = baseSettings.copy( + val initialOffset = baseSettings().copy( variablePrinter = new ReplVariablePrinter(leadingNewline = true, width = 25, height = 20, indent = 2) ) From dfde1319e1854f98aa0bc38059359619582d9f8a Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Thu, 11 Jul 2024 19:14:04 +0200 Subject: [PATCH 04/95] . --- tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala index c5d526b16..9172e45ae 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala @@ -152,7 +152,6 @@ class JsCliSuite extends BaseCliSuite { val code = mdoc.Main.process(args, new PrintStream(myStdout), in().toNIO) val generatedJs = AbsolutePath(out().toString + "/docs/facade.md.js") val content = new String(FileIO.readAllBytes(generatedJs), StandardCharsets.UTF_8) - println(content) assert( content.contains("https://cdn.jsdelivr.net/npm/@stdlib/blas") ) From e823704d88d97550a2ea256402ed821f830e7041 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Thu, 11 Jul 2024 19:29:34 +0200 Subject: [PATCH 05/95] Fix line no --- .../src/test/scala/tests/markdown/StringModifierSuite.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/src/test/scala/tests/markdown/StringModifierSuite.scala b/tests/unit/src/test/scala/tests/markdown/StringModifierSuite.scala index 1dd4fc2f7..9da0e46f1 100644 --- a/tests/unit/src/test/scala/tests/markdown/StringModifierSuite.scala +++ b/tests/unit/src/test/scala/tests/markdown/StringModifierSuite.scala @@ -75,7 +75,7 @@ class StringModifierSuite extends BaseMarkdownSuite { |^^^^^ |mdoc.internal.markdown.ModifierException: mdoc:exception exception |Caused by: java.lang.IllegalArgumentException: boom - | at tests.markdown.StringModifierSuite$$anon$3.process(StringModifierSuite.scala:31) + | at tests.markdown.StringModifierSuite$$anon$3.process(StringModifierSuite.scala:33) """.stripMargin ) From 2cc6eda63e9088b159f176e326ea2d39269af0e1 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Thu, 11 Jul 2024 19:30:38 +0200 Subject: [PATCH 06/95] remove only --- tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala index 9172e45ae..93a2bc3d1 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala @@ -114,7 +114,7 @@ class JsCliSuite extends BaseCliSuite { ) // see the importmap.json in resources - test("import remap".only) { + test("import remap") { val myStdout = new ByteArrayOutputStream() tests.cli.StringFS.fromString( """|/docs/facade.md From 1b1f096694b814a19758cd95c4e313926c41d782 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Thu, 11 Jul 2024 21:35:31 +0200 Subject: [PATCH 07/95] So close... --- build.sbt | 9 +- .../src/main/scala/ImportMappedIRFile.scala | 52 +++---- .../main/scala/ImportMappedIRFileImpl.scala | 134 +++++++++--------- .../src/main/scala/ScalaJSWorker.scala | 1 + .../main/scala/mdoc/modifiers/JsConfig.scala | 1 - mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala | 9 +- .../src/test/scala/tests/js/JsCliSuite.scala | 31 ---- 7 files changed, 109 insertions(+), 128 deletions(-) diff --git a/build.sbt b/build.sbt index 72841b11d..5c4961d3e 100644 --- a/build.sbt +++ b/build.sbt @@ -6,6 +6,8 @@ def scala3 = "3.3.3" def scala2Versions = List(scala212, scala213) def allScalaVersions = scala2Versions :+ scala3 + + def scalajsBinaryVersion = "1" def scalajsDom = "2.0.0" @@ -134,6 +136,7 @@ val V = new { val fs2 = "3.10.2" val metaconfig = "0.12.0" + } lazy val interfaces = project @@ -437,7 +440,11 @@ 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" % Provided cross CrossVersion.for3Use2_13 + ) + ) lazy val js = project diff --git a/mdoc-js-worker/src/main/scala/ImportMappedIRFile.scala b/mdoc-js-worker/src/main/scala/ImportMappedIRFile.scala index 5ff419154..91b749ed5 100644 --- a/mdoc-js-worker/src/main/scala/ImportMappedIRFile.scala +++ b/mdoc-js-worker/src/main/scala/ImportMappedIRFile.scala @@ -1,30 +1,30 @@ -/* - * Copyright 2023 Arman Bilge - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// /* +// * Copyright 2023 Arman Bilge +// * +// * Licensed under the Apache License, Version 2.0 (the "License"); +// * you may not use this file except in compliance with the License. +// * You may obtain a copy of the License at +// * +// * http://www.apache.org/licenses/LICENSE-2.0 +// * +// * Unless required by applicable law or agreed to in writing, software +// * distributed under the License is distributed on an "AS IS" BASIS, +// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// * See the License for the specific language governing permissions and +// * limitations under the License. +// */ -package mdoc.js.worker +// package mdoc.js.worker -/* - Copy pasted from here - https://github.com/armanbilge/scalajs-importmap/blob/main/importmap/src/main/scala/com/armanbilge/sjsimportmap/ImportMappedIRFile.scala - */ +// /* +// Copy pasted from here +// https://github.com/armanbilge/scalajs-importmap/blob/main/importmap/src/main/scala/com/armanbilge/sjsimportmap/ImportMappedIRFile.scala +// */ -import org.scalajs.linker.interface.IRFile -import org.scalajs.linker.interface.unstable.IRFileImpl +// import org.scalajs.linker.interface.IRFile +// import org.scalajs.linker.interface.unstable.IRFileImpl -object ImportMappedIRFile { - def fromIRFile(ir: IRFile)(mapper: String => String): IRFile = - new ImportMappedIRFileImpl(IRFileImpl.fromIRFile(ir), mapper) -} +// object ImportMappedIRFile { +// def fromIRFile(ir: IRFile)(mapper: String => String): IRFile = +// new ImportMappedIRFileImpl(IRFileImpl.fromIRFile(ir), mapper) +// } diff --git a/mdoc-js-worker/src/main/scala/ImportMappedIRFileImpl.scala b/mdoc-js-worker/src/main/scala/ImportMappedIRFileImpl.scala index 6cb4dd23a..6a190c8a5 100644 --- a/mdoc-js-worker/src/main/scala/ImportMappedIRFileImpl.scala +++ b/mdoc-js-worker/src/main/scala/ImportMappedIRFileImpl.scala @@ -1,78 +1,78 @@ -/* - * Copyright 2023 Arman Bilge - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// /* +// * Copyright 2023 Arman Bilge +// * +// * Licensed under the Apache License, Version 2.0 (the "License"); +// * you may not use this file except in compliance with the License. +// * You may obtain a copy of the License at +// * +// * http://www.apache.org/licenses/LICENSE-2.0 +// * +// * Unless required by applicable law or agreed to in writing, software +// * distributed under the License is distributed on an "AS IS" BASIS, +// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// * See the License for the specific language governing permissions and +// * limitations under the License. +// */ -package mdoc.js.worker +// package mdoc.js.worker -/* -Copy pasted from here; -https://github.com/armanbilge/scalajs-importmap/blob/main/importmap/src/main/scala/com/armanbilge/sjsimportmap/ImportMappedIRFileImpl.scala - */ +// /* +// Copy pasted from here; +// https://github.com/armanbilge/scalajs-importmap/blob/main/importmap/src/main/scala/com/armanbilge/sjsimportmap/ImportMappedIRFileImpl.scala +// */ -import org.scalajs.ir.Trees -import org.scalajs.linker.interface.unstable.IRFileImpl +// import org.scalajs.ir.Trees +// import org.scalajs.linker.interface.unstable.IRFileImpl -import scala.concurrent.ExecutionContext -import scala.concurrent.Future +// import scala.concurrent.ExecutionContext +// import scala.concurrent.Future -private final class ImportMappedIRFileImpl(impl: IRFileImpl, mapper: String => String) - extends IRFileImpl(impl.path, impl.version) { +// private final class ImportMappedIRFileImpl(impl: IRFileImpl, mapper: String => String) +// extends IRFileImpl(impl.path, impl.version) { - def entryPointsInfo(implicit ec: ExecutionContext) = - impl.entryPointsInfo +// def entryPointsInfo(implicit ec: ExecutionContext) = +// impl.entryPointsInfo - def tree(implicit ec: ExecutionContext): Future[Trees.ClassDef] = - impl.tree.map { classDef => - if (classDef.jsNativeLoadSpec.isDefined || classDef.jsNativeMembers.nonEmpty) - Trees.ClassDef( - classDef.name, - classDef.originalName, - classDef.kind, - classDef.jsClassCaptures, - classDef.superClass, - classDef.interfaces, - classDef.jsSuperClass, - classDef.jsNativeLoadSpec.map(transform), - classDef.fields, - classDef.methods, - classDef.jsConstructor, - classDef.jsMethodProps, - classDef.jsNativeMembers.map(transform), - classDef.topLevelExportDefs - )(classDef.optimizerHints)(classDef.pos) - else classDef - } +// def tree(implicit ec: ExecutionContext): Future[Trees.ClassDef] = +// impl.tree.map { classDef => +// if (classDef.jsNativeLoadSpec.isDefined || classDef.jsNativeMembers.nonEmpty) +// Trees.ClassDef( +// classDef.name, +// classDef.originalName, +// classDef.kind, +// classDef.jsClassCaptures, +// classDef.superClass, +// classDef.interfaces, +// classDef.jsSuperClass, +// classDef.jsNativeLoadSpec.map(transform), +// classDef.fields, +// classDef.methods, +// classDef.jsConstructor, +// classDef.jsMethodProps, +// classDef.jsNativeMembers.map(transform), +// classDef.topLevelExportDefs +// )(classDef.optimizerHints)(classDef.pos) +// else classDef +// } - private[this] def transform(member: Trees.JSNativeMemberDef): Trees.JSNativeMemberDef = - Trees.JSNativeMemberDef( - member.flags, - member.name, - transform(member.jsNativeLoadSpec) - )(member.pos) +// private[this] def transform(member: Trees.JSNativeMemberDef): Trees.JSNativeMemberDef = +// Trees.JSNativeMemberDef( +// member.flags, +// member.name, +// transform(member.jsNativeLoadSpec) +// )(member.pos) - private[this] def transform(spec: Trees.JSNativeLoadSpec): Trees.JSNativeLoadSpec = - spec match { - case importSpec: Trees.JSNativeLoadSpec.Import => transform(importSpec) - case Trees.JSNativeLoadSpec.ImportWithGlobalFallback(importSpec, globalSpec) => - Trees.JSNativeLoadSpec.ImportWithGlobalFallback(transform(importSpec), globalSpec) - case other => other - } +// private[this] def transform(spec: Trees.JSNativeLoadSpec): Trees.JSNativeLoadSpec = +// spec match { +// case importSpec: Trees.JSNativeLoadSpec.Import => transform(importSpec) +// case Trees.JSNativeLoadSpec.ImportWithGlobalFallback(importSpec, globalSpec) => +// Trees.JSNativeLoadSpec.ImportWithGlobalFallback(transform(importSpec), globalSpec) +// case other => other +// } - private[this] def transform( - importSpec: Trees.JSNativeLoadSpec.Import - ): Trees.JSNativeLoadSpec.Import = - importSpec.copy(module = mapper(importSpec.module)) +// private[this] def transform( +// importSpec: Trees.JSNativeLoadSpec.Import +// ): Trees.JSNativeLoadSpec.Import = +// importSpec.copy(module = mapper(importSpec.module)) -} +// } diff --git a/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala b/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala index 5cf18f542..9b68d85c7 100644 --- a/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala +++ b/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala @@ -19,6 +19,7 @@ import org.scalajs.logging.Level import org.scalajs.linker.standard.MemIRFileImpl import org.scalajs.linker.interface.Semantics import scala.collection.JavaConverters._ +import com.armanbilge.sjsimportmap.ImportMappedIRFile class ScalaJSWorker( config: ScalajsConfig, diff --git a/mdoc-js/src/main/scala/mdoc/modifiers/JsConfig.scala b/mdoc-js/src/main/scala/mdoc/modifiers/JsConfig.scala index 70437f452..969735a90 100644 --- a/mdoc-js/src/main/scala/mdoc/modifiers/JsConfig.scala +++ b/mdoc-js/src/main/scala/mdoc/modifiers/JsConfig.scala @@ -109,7 +109,6 @@ object JsConfig { case Some(value) => val importMapRaw = scala.io.Source.fromFile(value.toFile).getLines().mkString("\n") val importMapParsed = readFromString[ImportMap](importMapRaw) - println(importMapParsed) importMapParsed.imports } ) diff --git a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala index fa09a5b88..83584ee5d 100644 --- a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala +++ b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala @@ -157,6 +157,11 @@ object MdocPlugin extends AutoPlugin { case other => "org.scala-js" % s"scalajs-linker_$other" % sjsVersion } + val importMapDependency = binaryVersion match { + case "3" => "com.armanbilge" % "scalajs-importmap_2.13" % "0.1.1" + case other => "com.armanbilge" % s"scalajs-importmap_$other" % "0.1.1" + } + val mdocJSDependency = binaryVersion match { case "3" => "org.scalameta" % "mdoc-js-worker_3" % BuildInfo.version case other => "org.scalameta" % s"mdoc-js-worker_$other" % BuildInfo.version @@ -166,8 +171,8 @@ object MdocPlugin extends AutoPlugin { MdocJSConfiguration( scalacOptions = options.options, - compileClasspath = options.classpath, - linkerClassPath = getJars(linkerDependency) ++ workerClasspath, + compileClasspath = options.classpath , + linkerClassPath = getJars(linkerDependency) ++ workerClasspath ++ getJars(importMapDependency), moduleKind = options.moduleKind, jsLibraries = libraries ).writeTo(props) diff --git a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala index 93a2bc3d1..f6db8fb6a 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala @@ -82,36 +82,6 @@ class JsCliSuite extends BaseCliSuite { /** See MdocPlugin for where the .properties file is generated. */ - checkCli( - "basic_es", - """/docs/index1.md - |```scala mdoc:js - |println("hello world!") - |``` - |/docs/index2.md - |```scala mdoc:js - |println("hello world!") - |``` - |""".stripMargin, - s"""|/docs/index1.md - |```scala - |println("hello world!") - |``` - |
- | - | - | - | - |/docs/index2.md - |```scala - |println("hello world!") - |``` - |
- | - |""".stripMargin, - extraArgs = Array("--prop-file-name", "es.properties"), - includeOutputPath = { path => !path.toNIO.getFileName.toString.endsWith(".js") } - ) // see the importmap.json in resources test("import remap") { @@ -148,7 +118,6 @@ class JsCliSuite extends BaseCliSuite { "--import-map-path", this.getClass.getClassLoader.getResource("importmap.json").getPath ) - println(out()) val code = mdoc.Main.process(args, new PrintStream(myStdout), in().toNIO) val generatedJs = AbsolutePath(out().toString + "/docs/facade.md.js") val content = new String(FileIO.readAllBytes(generatedJs), StandardCharsets.UTF_8) From 7729ef0c63566a3ec9e991c82c377176be8f97ec Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Fri, 12 Jul 2024 19:31:10 +0200 Subject: [PATCH 08/95] Try snapshot --- build.sbt | 3 ++- mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala | 5 +++-- project/plugins.sbt | 2 ++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/build.sbt b/build.sbt index 5c4961d3e..ba6c9c39f 100644 --- a/build.sbt +++ b/build.sbt @@ -95,6 +95,7 @@ inThisBuild( ), testFrameworks := List(new TestFramework("munit.Framework")), resolvers += Resolver.sonatypeRepo("public"), + resolvers += "Sonatype OSS Snapshots" at "https://s01.oss.sonatype.org/content/repositories/snapshots", // faster publishLocal: packageDoc / publishArtifact := "true" == System.getenv("CI"), packageSrc / publishArtifact := "true" == System.getenv("CI"), @@ -442,7 +443,7 @@ lazy val jsWorker = moduleName := "mdoc-js-worker", libraryDependencies ++= Seq( "org.scala-js" %% "scalajs-linker" % scalaJSVersion % Provided cross CrossVersion.for3Use2_13, - "com.armanbilge" %% "scalajs-importmap" % "0.1.1" % Provided cross CrossVersion.for3Use2_13 + "com.armanbilge" %% "scalajs-importmap" % "0.1.1" cross CrossVersion.for3Use2_13 ) ) diff --git a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala index 83584ee5d..4b3db01bd 100644 --- a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala +++ b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala @@ -6,6 +6,7 @@ import sbt._ import scala.collection.mutable.ListBuffer object MdocPlugin extends AutoPlugin { + resolvers += "Sonatype OSS Snapshots" at "https://s01.oss.sonatype.org/content/repositories/snapshots" object autoImport { val mdoc = inputKey[Unit]( @@ -158,8 +159,8 @@ object MdocPlugin extends AutoPlugin { } val importMapDependency = binaryVersion match { - case "3" => "com.armanbilge" % "scalajs-importmap_2.13" % "0.1.1" - case other => "com.armanbilge" % s"scalajs-importmap_$other" % "0.1.1" + case "3" => "com.armanbilge" % "scalajs-importmap_2.13" % "0.1.1-4-cf4739e-SNAPSHOT" + case other => "com.armanbilge" % s"scalajs-importmap_$other" % "0.1.1-4-cf4739e-SNAPSHOT" } val mdocJSDependency = binaryVersion match { diff --git a/project/plugins.sbt b/project/plugins.sbt index 22628b2f1..e2bddae25 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -1,3 +1,5 @@ +resolvers += "Sonatype OSS Snapshots" at "https://s01.oss.sonatype.org/content/repositories/snapshots" + addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.12.0") addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.12") From fca69ab1f5a7e67b8a0d3293d9504eeb60830d86 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Fri, 12 Jul 2024 19:37:02 +0200 Subject: [PATCH 09/95] fmt --- build.sbt | 3 --- mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala | 8 +++++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/build.sbt b/build.sbt index ba6c9c39f..9e5e9b943 100644 --- a/build.sbt +++ b/build.sbt @@ -6,8 +6,6 @@ def scala3 = "3.3.3" def scala2Versions = List(scala212, scala213) def allScalaVersions = scala2Versions :+ scala3 - - def scalajsBinaryVersion = "1" def scalajsDom = "2.0.0" @@ -445,7 +443,6 @@ lazy val jsWorker = "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 diff --git a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala index 4b3db01bd..d5af552b6 100644 --- a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala +++ b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala @@ -160,7 +160,8 @@ object MdocPlugin extends AutoPlugin { val importMapDependency = binaryVersion match { case "3" => "com.armanbilge" % "scalajs-importmap_2.13" % "0.1.1-4-cf4739e-SNAPSHOT" - case other => "com.armanbilge" % s"scalajs-importmap_$other" % "0.1.1-4-cf4739e-SNAPSHOT" + case other => + "com.armanbilge" % s"scalajs-importmap_$other" % "0.1.1-4-cf4739e-SNAPSHOT" } val mdocJSDependency = binaryVersion match { @@ -172,8 +173,9 @@ object MdocPlugin extends AutoPlugin { MdocJSConfiguration( scalacOptions = options.options, - compileClasspath = options.classpath , - linkerClassPath = getJars(linkerDependency) ++ workerClasspath ++ getJars(importMapDependency), + compileClasspath = options.classpath, + linkerClassPath = + getJars(linkerDependency) ++ workerClasspath ++ getJars(importMapDependency), moduleKind = options.moduleKind, jsLibraries = libraries ).writeTo(props) From f382b3efce2ed71b5ad11f657e3809c759372973 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Fri, 12 Jul 2024 20:34:02 +0200 Subject: [PATCH 10/95] Pretty please work with a cherry on top --- build.sbt | 1 - .../src/main/scala/ImportMappedIRFile.scala | 30 ------- .../main/scala/ImportMappedIRFileImpl.scala | 78 ------------------- .../main/scala/ScalaJSWorkerProvider.scala | 1 + .../mdoc/modifiers/ScalajsClassloader.scala | 2 - mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala | 7 +- project/plugins.sbt | 2 - 7 files changed, 4 insertions(+), 117 deletions(-) delete mode 100644 mdoc-js-worker/src/main/scala/ImportMappedIRFile.scala delete mode 100644 mdoc-js-worker/src/main/scala/ImportMappedIRFileImpl.scala diff --git a/build.sbt b/build.sbt index 9e5e9b943..7bd212213 100644 --- a/build.sbt +++ b/build.sbt @@ -93,7 +93,6 @@ inThisBuild( ), testFrameworks := List(new TestFramework("munit.Framework")), resolvers += Resolver.sonatypeRepo("public"), - resolvers += "Sonatype OSS Snapshots" at "https://s01.oss.sonatype.org/content/repositories/snapshots", // faster publishLocal: packageDoc / publishArtifact := "true" == System.getenv("CI"), packageSrc / publishArtifact := "true" == System.getenv("CI"), diff --git a/mdoc-js-worker/src/main/scala/ImportMappedIRFile.scala b/mdoc-js-worker/src/main/scala/ImportMappedIRFile.scala deleted file mode 100644 index 91b749ed5..000000000 --- a/mdoc-js-worker/src/main/scala/ImportMappedIRFile.scala +++ /dev/null @@ -1,30 +0,0 @@ -// /* -// * Copyright 2023 Arman Bilge -// * -// * Licensed under the Apache License, Version 2.0 (the "License"); -// * you may not use this file except in compliance with the License. -// * You may obtain a copy of the License at -// * -// * http://www.apache.org/licenses/LICENSE-2.0 -// * -// * Unless required by applicable law or agreed to in writing, software -// * distributed under the License is distributed on an "AS IS" BASIS, -// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// * See the License for the specific language governing permissions and -// * limitations under the License. -// */ - -// package mdoc.js.worker - -// /* -// Copy pasted from here -// https://github.com/armanbilge/scalajs-importmap/blob/main/importmap/src/main/scala/com/armanbilge/sjsimportmap/ImportMappedIRFile.scala -// */ - -// import org.scalajs.linker.interface.IRFile -// import org.scalajs.linker.interface.unstable.IRFileImpl - -// object ImportMappedIRFile { -// def fromIRFile(ir: IRFile)(mapper: String => String): IRFile = -// new ImportMappedIRFileImpl(IRFileImpl.fromIRFile(ir), mapper) -// } diff --git a/mdoc-js-worker/src/main/scala/ImportMappedIRFileImpl.scala b/mdoc-js-worker/src/main/scala/ImportMappedIRFileImpl.scala deleted file mode 100644 index 6a190c8a5..000000000 --- a/mdoc-js-worker/src/main/scala/ImportMappedIRFileImpl.scala +++ /dev/null @@ -1,78 +0,0 @@ -// /* -// * Copyright 2023 Arman Bilge -// * -// * Licensed under the Apache License, Version 2.0 (the "License"); -// * you may not use this file except in compliance with the License. -// * You may obtain a copy of the License at -// * -// * http://www.apache.org/licenses/LICENSE-2.0 -// * -// * Unless required by applicable law or agreed to in writing, software -// * distributed under the License is distributed on an "AS IS" BASIS, -// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// * See the License for the specific language governing permissions and -// * limitations under the License. -// */ - -// package mdoc.js.worker - -// /* -// Copy pasted from here; -// https://github.com/armanbilge/scalajs-importmap/blob/main/importmap/src/main/scala/com/armanbilge/sjsimportmap/ImportMappedIRFileImpl.scala -// */ - -// import org.scalajs.ir.Trees -// import org.scalajs.linker.interface.unstable.IRFileImpl - -// import scala.concurrent.ExecutionContext -// import scala.concurrent.Future - -// private final class ImportMappedIRFileImpl(impl: IRFileImpl, mapper: String => String) -// extends IRFileImpl(impl.path, impl.version) { - -// def entryPointsInfo(implicit ec: ExecutionContext) = -// impl.entryPointsInfo - -// def tree(implicit ec: ExecutionContext): Future[Trees.ClassDef] = -// impl.tree.map { classDef => -// if (classDef.jsNativeLoadSpec.isDefined || classDef.jsNativeMembers.nonEmpty) -// Trees.ClassDef( -// classDef.name, -// classDef.originalName, -// classDef.kind, -// classDef.jsClassCaptures, -// classDef.superClass, -// classDef.interfaces, -// classDef.jsSuperClass, -// classDef.jsNativeLoadSpec.map(transform), -// classDef.fields, -// classDef.methods, -// classDef.jsConstructor, -// classDef.jsMethodProps, -// classDef.jsNativeMembers.map(transform), -// classDef.topLevelExportDefs -// )(classDef.optimizerHints)(classDef.pos) -// else classDef -// } - -// private[this] def transform(member: Trees.JSNativeMemberDef): Trees.JSNativeMemberDef = -// Trees.JSNativeMemberDef( -// member.flags, -// member.name, -// transform(member.jsNativeLoadSpec) -// )(member.pos) - -// private[this] def transform(spec: Trees.JSNativeLoadSpec): Trees.JSNativeLoadSpec = -// spec match { -// case importSpec: Trees.JSNativeLoadSpec.Import => transform(importSpec) -// case Trees.JSNativeLoadSpec.ImportWithGlobalFallback(importSpec, globalSpec) => -// Trees.JSNativeLoadSpec.ImportWithGlobalFallback(transform(importSpec), globalSpec) -// case other => other -// } - -// private[this] def transform( -// importSpec: Trees.JSNativeLoadSpec.Import -// ): Trees.JSNativeLoadSpec.Import = -// importSpec.copy(module = mapper(importSpec.module)) - -// } diff --git a/mdoc-js-worker/src/main/scala/ScalaJSWorkerProvider.scala b/mdoc-js-worker/src/main/scala/ScalaJSWorkerProvider.scala index 29fc7e347..84abbe256 100644 --- a/mdoc-js-worker/src/main/scala/ScalaJSWorkerProvider.scala +++ b/mdoc-js-worker/src/main/scala/ScalaJSWorkerProvider.scala @@ -24,6 +24,7 @@ class ScalaJSWorkerProvider extends i.ScalajsWorkerProvider { def trace(ex: => Throwable) = logger.trace(ex) } + new ScalaJSWorker(config, wrappedLogger) } } diff --git a/mdoc-js/src/main/scala/mdoc/modifiers/ScalajsClassloader.scala b/mdoc-js/src/main/scala/mdoc/modifiers/ScalajsClassloader.scala index f20923c14..5f3f6e601 100644 --- a/mdoc-js/src/main/scala/mdoc/modifiers/ScalajsClassloader.scala +++ b/mdoc-js/src/main/scala/mdoc/modifiers/ScalajsClassloader.scala @@ -9,8 +9,6 @@ final class FilteringClassLoader(parent: ClassLoader) extends ClassLoader(parent "scala.", "org.scalajs.linker.", "org.scalajs.logging.", - "org.scalajs.", - "org.scalajs.ir.", "sun.reflect.", "jdk.internal.reflect.", "mdoc.js." diff --git a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala index d5af552b6..fe4a6ae8a 100644 --- a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala +++ b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala @@ -6,7 +6,6 @@ import sbt._ import scala.collection.mutable.ListBuffer object MdocPlugin extends AutoPlugin { - resolvers += "Sonatype OSS Snapshots" at "https://s01.oss.sonatype.org/content/repositories/snapshots" object autoImport { val mdoc = inputKey[Unit]( @@ -159,9 +158,9 @@ object MdocPlugin extends AutoPlugin { } val importMapDependency = binaryVersion match { - case "3" => "com.armanbilge" % "scalajs-importmap_2.13" % "0.1.1-4-cf4739e-SNAPSHOT" + case "3" => "com.armanbilge" % "scalajs-importmap_2.13" % "0.1.1" case other => - "com.armanbilge" % s"scalajs-importmap_$other" % "0.1.1-4-cf4739e-SNAPSHOT" + "com.armanbilge" % s"scalajs-importmap_$other" % "0.1.1" } val mdocJSDependency = binaryVersion match { @@ -204,7 +203,7 @@ object MdocPlugin extends AutoPlugin { "mdoc esmoddule properties", esOut ) - List(out, esOut) + List(out, esOut) // Both of these are used in the JsCliSuite Integration tests } ) diff --git a/project/plugins.sbt b/project/plugins.sbt index e2bddae25..22628b2f1 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -1,5 +1,3 @@ -resolvers += "Sonatype OSS Snapshots" at "https://s01.oss.sonatype.org/content/repositories/snapshots" - addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.12.0") addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.12") From c67a83afa60b8a8a0af8ab5806e1994732b0ea4d Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Fri, 12 Jul 2024 20:51:39 +0200 Subject: [PATCH 11/95] . --- tests/unit-js/src/test/scala/tests/js/JsSuite.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit-js/src/test/scala/tests/js/JsSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsSuite.scala index 977197dad..cbd7d72f3 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsSuite.scala @@ -80,7 +80,7 @@ class JsSuite extends BaseMarkdownSuite { baseSettings().copy( site = baseSettings().site.updated("js-module-kind", "ESModule"), importMapPath = - Some(AbsolutePath(this.getClass.getClassLoader.getResource("importmap.json").getPath)) + Some(AbsolutePath(this.getClass.getClassLoader.getResource("importmap.json").getPath.toNIO)) ) } ) From 2e8ae3891b33ac07ea04ededb11d290e1c852e97 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Fri, 12 Jul 2024 20:53:40 +0200 Subject: [PATCH 12/95] fmt --- tests/unit-js/src/test/scala/tests/js/JsSuite.scala | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/unit-js/src/test/scala/tests/js/JsSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsSuite.scala index cbd7d72f3..3991f5e1c 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsSuite.scala @@ -79,8 +79,9 @@ class JsSuite extends BaseMarkdownSuite { settings = { baseSettings().copy( site = baseSettings().site.updated("js-module-kind", "ESModule"), - importMapPath = - Some(AbsolutePath(this.getClass.getClassLoader.getResource("importmap.json").getPath.toNIO)) + importMapPath = Some( + AbsolutePath(this.getClass.getClassLoader.getResource("importmap.json").getPath.toNIO) + ) ) } ) From 90060609324d40a144155d75de71c6f56b65c7c9 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Fri, 12 Jul 2024 21:00:07 +0200 Subject: [PATCH 13/95] . --- tests/unit-js/src/test/scala/tests/js/JsSuite.scala | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/unit-js/src/test/scala/tests/js/JsSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsSuite.scala index 3991f5e1c..07db6c4ba 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsSuite.scala @@ -7,6 +7,7 @@ import tests.markdown.BaseMarkdownSuite import tests.js.JsTests.suffix import tests.markdown.Compat import scala.meta.io.AbsolutePath +import java.nio.file.Paths; class JsSuite extends BaseMarkdownSuite { // NOTE(olafur) Optimization. Cache settings to reuse the Scala.js compiler instance. @@ -80,7 +81,11 @@ class JsSuite extends BaseMarkdownSuite { baseSettings().copy( site = baseSettings().site.updated("js-module-kind", "ESModule"), importMapPath = Some( - AbsolutePath(this.getClass.getClassLoader.getResource("importmap.json").getPath.toNIO) + AbsolutePath( + Paths + .get(this.getClass.getClassLoader.getResource("importmap.json").getPath()) + .toAbsolutePath() + ) ) ) } From eaa4c165e799b2ede8344314d99469294db9bde0 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Fri, 12 Jul 2024 21:08:28 +0200 Subject: [PATCH 14/95] . --- tests/unit-js/src/test/scala/tests/js/JsSuite.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit-js/src/test/scala/tests/js/JsSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsSuite.scala index 07db6c4ba..b95667c97 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsSuite.scala @@ -83,7 +83,7 @@ class JsSuite extends BaseMarkdownSuite { importMapPath = Some( AbsolutePath( Paths - .get(this.getClass.getClassLoader.getResource("importmap.json").getPath()) + .get(this.getClass.getClassLoader.getResource("importmap.json").toURI().getPath()) .toAbsolutePath() ) ) From 5376abb1f194a44f5ec7fb2591252e5ad69cede9 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Fri, 12 Jul 2024 21:18:44 +0200 Subject: [PATCH 15/95] . --- tests/unit-js/src/test/scala/tests/js/JsSuite.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit-js/src/test/scala/tests/js/JsSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsSuite.scala index b95667c97..803fd0af7 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsSuite.scala @@ -83,7 +83,7 @@ class JsSuite extends BaseMarkdownSuite { importMapPath = Some( AbsolutePath( Paths - .get(this.getClass.getClassLoader.getResource("importmap.json").toURI().getPath()) + .get(this.getClass.getClassLoader.getResource("importmap.json").toURI()) .toAbsolutePath() ) ) From 57d371032d89b627003d54fa8327cfd82a6ddfdf Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Fri, 12 Jul 2024 21:26:01 +0200 Subject: [PATCH 16/95] windows paths --- tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala index f6db8fb6a..47be0d968 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala @@ -119,7 +119,7 @@ class JsCliSuite extends BaseCliSuite { this.getClass.getClassLoader.getResource("importmap.json").getPath ) val code = mdoc.Main.process(args, new PrintStream(myStdout), in().toNIO) - val generatedJs = AbsolutePath(out().toString + "/docs/facade.md.js") + val generatedJs = AbsolutePath(out().toNIO.resolve("docs").resolve("facade.md.js")) val content = new String(FileIO.readAllBytes(generatedJs), StandardCharsets.UTF_8) assert( content.contains("https://cdn.jsdelivr.net/npm/@stdlib/blas") From eb884588fb47328659d1523c7549a862ededde35 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Fri, 12 Jul 2024 21:49:29 +0200 Subject: [PATCH 17/95] windows, finally? --- tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala index 47be0d968..772449ad0 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala @@ -12,6 +12,7 @@ import java.io.PrintStream import scala.meta.internal.io.FileIO import scala.meta.io.AbsolutePath import java.nio.charset.StandardCharsets +import java.nio.file.Paths class JsCliSuite extends BaseCliSuite { @@ -116,10 +117,11 @@ class JsCliSuite extends BaseCliSuite { "--prop-file-name", "es.properties", "--import-map-path", - this.getClass.getClassLoader.getResource("importmap.json").getPath + Paths.get(this.getClass.getClassLoader.getResource("importmap.json").toURI).toString() ) val code = mdoc.Main.process(args, new PrintStream(myStdout), in().toNIO) - val generatedJs = AbsolutePath(out().toNIO.resolve("docs").resolve("facade.md.js")) + val generatedJs = + AbsolutePath(out().toNIO.resolve("docs").resolve("facade.md.js")) val content = new String(FileIO.readAllBytes(generatedJs), StandardCharsets.UTF_8) assert( content.contains("https://cdn.jsdelivr.net/npm/@stdlib/blas") From 16e794b25849d5f51dbe06ca0ed3910307a1623d Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Tue, 16 Jul 2024 14:09:17 +0200 Subject: [PATCH 18/95] Update mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala Co-authored-by: Tomasz Godzik --- mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala b/mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala index e2f2c7d23..1a1582dfe 100644 --- a/mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala +++ b/mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala @@ -75,7 +75,7 @@ class JsModifier extends mdoc.PreModifier { .withBatchMode(config.batchMode) .withClosureCompiler( config.fullOpt && !(config.moduleKind == ModuleType.ESModule) - ) // Closure compiler dosen't work with ESModules + ) // Closure compiler doesn't work with ESModules .withImportMap(config.importMap.asJava) } From b89da7fedd183e3db7ded485439d13f98feef186 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Tue, 16 Jul 2024 14:10:14 +0200 Subject: [PATCH 19/95] Update mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala Co-authored-by: Tomasz Godzik --- mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala index fe4a6ae8a..2b8a779a0 100644 --- a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala +++ b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala @@ -159,7 +159,7 @@ object MdocPlugin extends AutoPlugin { val importMapDependency = binaryVersion match { case "3" => "com.armanbilge" % "scalajs-importmap_2.13" % "0.1.1" - case other => + case scalaBinaryVersion => "com.armanbilge" % s"scalajs-importmap_$other" % "0.1.1" } From e94ffccafb2137fb79a316d338dc0fe9f9fcc661 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Tue, 16 Jul 2024 14:10:46 +0200 Subject: [PATCH 20/95] Update cli/src/main/scala/mdoc/internal/cli/Settings.scala Co-authored-by: Tomasz Godzik --- cli/src/main/scala/mdoc/internal/cli/Settings.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/src/main/scala/mdoc/internal/cli/Settings.scala b/cli/src/main/scala/mdoc/internal/cli/Settings.scala index 75f17a700..7e44c07f0 100644 --- a/cli/src/main/scala/mdoc/internal/cli/Settings.scala +++ b/cli/src/main/scala/mdoc/internal/cli/Settings.scala @@ -164,7 +164,7 @@ case class Settings( @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" ) - propFileName: String = "mdoc.properties" + propertyFileName: String = "mdoc.properties" ) extends mdoc.parser.ParserSettings { val isMarkdownFileExtension = markdownExtensions.toSet From ba8056d5b6c4b4cf4b3e4b22ec73322cc4ee9a21 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Tue, 16 Jul 2024 14:35:27 +0200 Subject: [PATCH 21/95] Per feedback - tidy naming. Add cli args test --- .../main/scala/mdoc/internal/cli/Settings.scala | 2 +- mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala | 4 ++-- .../src/test/scala/tests/js/JsCliSuite.scala | 4 ++-- .../src/test/scala/tests/cli/CliArgsSuite.scala | 15 +++++++++++++-- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/cli/src/main/scala/mdoc/internal/cli/Settings.scala b/cli/src/main/scala/mdoc/internal/cli/Settings.scala index 7e44c07f0..932c7b181 100644 --- a/cli/src/main/scala/mdoc/internal/cli/Settings.scala +++ b/cli/src/main/scala/mdoc/internal/cli/Settings.scala @@ -310,7 +310,7 @@ object Settings { // extends MetaconfigScalametaImplicits with Decoders with Set val base = Settings.default( AbsolutePath(workingDirectory), - conf.get[String]("propFileName").getOrElse("mdoc.properties") + conf.get[String]("propertyFileName").getOrElse("mdoc.properties") ) val cwd = conf.get[String]("cwd").map(AbsolutePath(_)(base.cwd)).getOrElse(base.cwd) conf diff --git a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala index 2b8a779a0..8a47eed37 100644 --- a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala +++ b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala @@ -154,13 +154,13 @@ object MdocPlugin extends AutoPlugin { val linkerDependency = binaryVersion match { case "3" => "org.scala-js" % "scalajs-linker_2.13" % sjsVersion - case other => "org.scala-js" % s"scalajs-linker_$other" % sjsVersion + case scalaBinaryVersion => "org.scala-js" % s"scalajs-linker_$scalaBinaryVersion" % sjsVersion } val importMapDependency = binaryVersion match { case "3" => "com.armanbilge" % "scalajs-importmap_2.13" % "0.1.1" case scalaBinaryVersion => - "com.armanbilge" % s"scalajs-importmap_$other" % "0.1.1" + "com.armanbilge" % s"scalajs-importmap_$scalaBinaryVersion" % "0.1.1" } val mdocJSDependency = binaryVersion match { diff --git a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala index 772449ad0..f0f0f5b55 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala @@ -77,7 +77,7 @@ class JsCliSuite extends BaseCliSuite { |
| |""".stripMargin, - extraArgs = Array("--prop-file-name", "es.properties"), + extraArgs = Array("--property-file-name", "es.properties"), includeOutputPath = { path => !path.toNIO.getFileName.toString.endsWith(".js") } ) @@ -114,7 +114,7 @@ class JsCliSuite extends BaseCliSuite { out().toString, "--cwd", in().syntax, - "--prop-file-name", + "--property-file-name", "es.properties", "--import-map-path", Paths.get(this.getClass.getClassLoader.getResource("importmap.json").toURI).toString() diff --git a/tests/unit/src/test/scala/tests/cli/CliArgsSuite.scala b/tests/unit/src/test/scala/tests/cli/CliArgsSuite.scala index f7cb4768a..ab02b6e40 100644 --- a/tests/unit/src/test/scala/tests/cli/CliArgsSuite.scala +++ b/tests/unit/src/test/scala/tests/cli/CliArgsSuite.scala @@ -11,8 +11,7 @@ import cats.instances.set import mdoc.internal.cli.Context class CliArgsSuite extends FunSuite { - private val reporter = ConsoleReporter.default - // private val base = Settings.default(PathIO.workingDirectory, "mdoc.properties") + private val reporter = ConsoleReporter.default def checkOK(name: String, args: List[String], onSuccess: Settings => Unit = _ => ())(implicit loc: munit.Location @@ -107,6 +106,18 @@ class CliArgsSuite extends FunSuite { List("--in", tmpFile.toString, tmpDirectory.toString) ) + checkOK( + "--property-file-name", + List("--property-file-name", "es.properties", tmpFile.toString, tmpDirectory.toString), + onSuccess = conf => assertEquals(conf.propertyFileName, "es.properties") + ) + + checkOK( + "--property-file-name default", + List(tmpFile.toString, tmpDirectory.toString), + onSuccess = conf => assertEquals(conf.propertyFileName, "mdoc.properties") + ) + checkOK( "relative-by-cwd", List("--cwd", tmpDirectory.toString(), "--in", "readme.template.md", "--out", "readme.md"), From 4198a0f324c635a1a42b2ff48e656305e1de055c Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Tue, 16 Jul 2024 15:00:58 +0200 Subject: [PATCH 22/95] Feedback: Update test --- .../src/test/scala/tests/js/JsCliSuite.scala | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala index f0f0f5b55..2387d62c8 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala @@ -13,6 +13,7 @@ import scala.meta.internal.io.FileIO import scala.meta.io.AbsolutePath import java.nio.charset.StandardCharsets import java.nio.file.Paths +import scala.io.Source class JsCliSuite extends BaseCliSuite { @@ -122,9 +123,24 @@ class JsCliSuite extends BaseCliSuite { val code = mdoc.Main.process(args, new PrintStream(myStdout), in().toNIO) val generatedJs = AbsolutePath(out().toNIO.resolve("docs").resolve("facade.md.js")) - val content = new String(FileIO.readAllBytes(generatedJs), StandardCharsets.UTF_8) - assert( - content.contains("https://cdn.jsdelivr.net/npm/@stdlib/blas") + val content = Source.fromFile(generatedJs.toNIO.toUri()).getLines().toList + println(content(1)) + + /** + SP 16.07.2024 + assert(content(contains("https://cdn.jsdelivr.net/npm/@stdlib/blas")) + The line above should also be an effective test. It does not illustrate + what the remap tries to achieve, so the test below is preferred. + + However, given that the names in the test below are "generated", by scalaJS + there is a risk the test turns flaky / fails at some point in the futurem, should scalaJS + change it's with regard to variable name generation. It this test turns "flaky" + the line above could be used in that case to reduce maintenance burden. + **/ + + assertEquals( + content(1), + """import * as $i_https$003a$002f$002fcdn$002ejsdelivr$002enet$002fnpm$002f$0040stdlib$002fblas$00400$002e2$002e1$002f$002besm$002fbase from "https://cdn.jsdelivr.net/npm/@stdlib/blas@0.2.1/+esm/base";""" ) assertEquals(code, 0, clues(myStdout)) From 6adc3346112e92596170f20fb65b50baf661497c Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Tue, 16 Jul 2024 15:06:13 +0200 Subject: [PATCH 23/95] fmt --- mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala | 3 ++- .../src/test/scala/tests/js/JsCliSuite.scala | 20 +++++++++---------- .../test/scala/tests/cli/CliArgsSuite.scala | 6 +++--- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala index 8a47eed37..b7c29fed5 100644 --- a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala +++ b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala @@ -154,7 +154,8 @@ object MdocPlugin extends AutoPlugin { val linkerDependency = binaryVersion match { case "3" => "org.scala-js" % "scalajs-linker_2.13" % sjsVersion - case scalaBinaryVersion => "org.scala-js" % s"scalajs-linker_$scalaBinaryVersion" % sjsVersion + case scalaBinaryVersion => + "org.scala-js" % s"scalajs-linker_$scalaBinaryVersion" % sjsVersion } val importMapDependency = binaryVersion match { diff --git a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala index 2387d62c8..5f07a25fd 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala @@ -126,17 +126,15 @@ class JsCliSuite extends BaseCliSuite { val content = Source.fromFile(generatedJs.toNIO.toUri()).getLines().toList println(content(1)) - /** - SP 16.07.2024 - assert(content(contains("https://cdn.jsdelivr.net/npm/@stdlib/blas")) - The line above should also be an effective test. It does not illustrate - what the remap tries to achieve, so the test below is preferred. - - However, given that the names in the test below are "generated", by scalaJS - there is a risk the test turns flaky / fails at some point in the futurem, should scalaJS - change it's with regard to variable name generation. It this test turns "flaky" - the line above could be used in that case to reduce maintenance burden. - **/ + /** SP 16.07.2024 assert(content(contains("https://cdn.jsdelivr.net/npm/@stdlib/blas")) The line + * above should also be an effective test. It does not illustrate what the remap tries to + * achieve, so the test below is preferred. + * + * However, given that the names in the test below are "generated", by scalaJS there is a risk + * the test turns flaky / fails at some point in the futurem, should scalaJS change it's with + * regard to variable name generation. It this test turns "flaky" the line above could be used + * in that case to reduce maintenance burden. + */ assertEquals( content(1), diff --git a/tests/unit/src/test/scala/tests/cli/CliArgsSuite.scala b/tests/unit/src/test/scala/tests/cli/CliArgsSuite.scala index ab02b6e40..ae0f344af 100644 --- a/tests/unit/src/test/scala/tests/cli/CliArgsSuite.scala +++ b/tests/unit/src/test/scala/tests/cli/CliArgsSuite.scala @@ -11,7 +11,7 @@ import cats.instances.set import mdoc.internal.cli.Context class CliArgsSuite extends FunSuite { - private val reporter = ConsoleReporter.default + private val reporter = ConsoleReporter.default def checkOK(name: String, args: List[String], onSuccess: Settings => Unit = _ => ())(implicit loc: munit.Location @@ -108,13 +108,13 @@ class CliArgsSuite extends FunSuite { checkOK( "--property-file-name", - List("--property-file-name", "es.properties", tmpFile.toString, tmpDirectory.toString), + List("--property-file-name", "es.properties", tmpFile.toString, tmpDirectory.toString), onSuccess = conf => assertEquals(conf.propertyFileName, "es.properties") ) checkOK( "--property-file-name default", - List(tmpFile.toString, tmpDirectory.toString), + List(tmpFile.toString, tmpDirectory.toString), onSuccess = conf => assertEquals(conf.propertyFileName, "mdoc.properties") ) From 533b8b5f24bbdedcd0afd5a42b3fc99f3052e7de Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Tue, 16 Jul 2024 15:08:13 +0200 Subject: [PATCH 24/95] Rename mapping function --- mdoc-js-worker/src/main/scala/ScalaJSWorker.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala b/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala index 9b68d85c7..c5b1afca0 100644 --- a/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala +++ b/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala @@ -47,7 +47,7 @@ class ScalaJSWorker( StandardImpl.clearableLinker(cfg) } - lazy val remapFct = config.importMap.asScala.toSeq.foldLeft((in: String) => in) { + lazy val esModuleRemapFunction: String => String = config.importMap.asScala.toSeq.foldLeft((in: String) => in) { case (fct, (s1, s2)) => val fct2: (String => String) = (in => in.replace(s1, s2)) (in => fct(fct2(in))) @@ -82,7 +82,7 @@ class ScalaJSWorker( if (config.importMap.isEmpty) ir else - ImportMappedIRFile.fromIRFile(ir)(remapFct) + ImportMappedIRFile.fromIRFile(ir)(esModuleRemapFunction) }, Seq.empty, mem, From b2206d7f38063a149f8229cf02094f3a160a093d Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Tue, 16 Jul 2024 15:15:54 +0200 Subject: [PATCH 25/95] . --- mdoc-js-worker/src/main/scala/ScalaJSWorker.scala | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala b/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala index c5b1afca0..678426a93 100644 --- a/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala +++ b/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala @@ -47,11 +47,11 @@ class ScalaJSWorker( StandardImpl.clearableLinker(cfg) } - lazy val esModuleRemapFunction: String => String = config.importMap.asScala.toSeq.foldLeft((in: String) => in) { - case (fct, (s1, s2)) => + lazy val esModuleRemapFunction: String => String = + config.importMap.asScala.toSeq.foldLeft((in: String) => in) { case (fct, (s1, s2)) => val fct2: (String => String) = (in => in.replace(s1, s2)) (in => fct(fct2(in))) - } + } var cachedFiles = Seq.empty[org.scalajs.linker.interface.IRFile] From 0a1fc9b3ce786fb03382257925fa17f77428f71e Mon Sep 17 00:00:00 2001 From: Scalameta Bot Date: Mon, 22 Jul 2024 00:58:26 +0000 Subject: [PATCH 26/95] Update scalameta, testkit to 4.9.8 --- build.sbt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sbt b/build.sbt index f833b023b..b588eff45 100644 --- a/build.sbt +++ b/build.sbt @@ -114,7 +114,7 @@ lazy val sharedJavaSettings = List( ) val V = new { - val scalameta = "4.9.7" + val scalameta = "4.9.8" val munit = "1.0.0" From ff78365f55bbbf30743ce2206436870ef4dfbfcf Mon Sep 17 00:00:00 2001 From: Scalameta Bot Date: Mon, 22 Jul 2024 00:58:18 +0000 Subject: [PATCH 27/95] Update sbt, scripted-plugin to 1.10.1 --- mdoc-sbt/src/sbt-test/sbt-mdoc/basic/project/build.properties | 2 +- .../sbt-test/sbt-mdoc/extra-arguments/project/build.properties | 2 +- project/build.properties | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/mdoc-sbt/src/sbt-test/sbt-mdoc/basic/project/build.properties b/mdoc-sbt/src/sbt-test/sbt-mdoc/basic/project/build.properties index be54e7763..136f452e0 100644 --- a/mdoc-sbt/src/sbt-test/sbt-mdoc/basic/project/build.properties +++ b/mdoc-sbt/src/sbt-test/sbt-mdoc/basic/project/build.properties @@ -1 +1 @@ -sbt.version = 1.10.0 +sbt.version = 1.10.1 diff --git a/mdoc-sbt/src/sbt-test/sbt-mdoc/extra-arguments/project/build.properties b/mdoc-sbt/src/sbt-test/sbt-mdoc/extra-arguments/project/build.properties index be54e7763..136f452e0 100644 --- a/mdoc-sbt/src/sbt-test/sbt-mdoc/extra-arguments/project/build.properties +++ b/mdoc-sbt/src/sbt-test/sbt-mdoc/extra-arguments/project/build.properties @@ -1 +1 @@ -sbt.version = 1.10.0 +sbt.version = 1.10.1 diff --git a/project/build.properties b/project/build.properties index 081fdbbc7..ee4c672cd 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=1.10.0 +sbt.version=1.10.1 From aca5269a97525d0d322217ac0132f2598d05f0b5 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Mon, 22 Jul 2024 18:21:34 +0200 Subject: [PATCH 28/95] . --- docs/js.md | 20 +++++++++++++++++++ mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala | 6 ++++++ website.md | 14 +++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 website.md diff --git a/docs/js.md b/docs/js.md index 94086176e..90a35cd68 100644 --- a/docs/js.md +++ b/docs/js.md @@ -415,3 +415,23 @@ mdocVariables := Map( "js-batch-mode" -> "true" ) ``` +### Remapping imports at link time. + +It may be the case, that the library you wish to depend on, is a facade to some NPM dependancy. Setting up a bundler in mdoc isn't a simple operation, but there is an alternative. We can instead as mdoc to output ESModules (see `ModuleKind`) and then remap their imports to point at a CDN. + +To mdocs CLI, you'll need to pass this flag; + +```sh +--import-map-path full/path/to/import-map.json +``` + +`import-map.json` should look like this; + +```json +{ + "imports": { + "@easepick/bundle": "https://cdn.jsdelivr.net/npm/@easepick/bundle@1.2.1", + } +} +``` +You should then have this dependancy available to you. diff --git a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala index b7c29fed5..a4f5cef57 100644 --- a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala +++ b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala @@ -47,6 +47,11 @@ object MdocPlugin extends AutoPlugin { "If false, do not add mdoc as a library dependency this project. " + "Default value is true." ) + val esModuleImportJSMapFile = + settingKey[Option[File]]( + "File containing an import map for remapping ESModules at link time. " + + "Defaults to the toplevel docs/ directory." + ) } val mdocInternalVariables = settingKey[List[(String, String)]]( @@ -82,6 +87,7 @@ object MdocPlugin extends AutoPlugin { mdocJSWorkerClasspath := None, mdocAutoDependency := true, mdocInternalVariables := Nil, + esModuleImportJSMapFile := None, mdoc := Def.inputTaskDyn { validateSettings.value val parsed = sbt.complete.DefaultParsers.spaceDelimited("").parsed diff --git a/website.md b/website.md new file mode 100644 index 000000000..75069a9a6 --- /dev/null +++ b/website.md @@ -0,0 +1,14 @@ +# Running the website locally + +Before starting; + +1. Make sure the project compiles. +2. *Ensure you are using node v16*. As of 22.07.2024 The website will not work with higher versions of node. + +Next + +1. Switch to the "website" folder. run `npm install` +2. Copy the blog folder into the `website` folder (don't commit that) +3. Back in the project root, run `sbt '++2.12.19; docs/mdoc'` in a terminal - this will run mdoc. +4. probably in a new terminal in the "website" folder , run `npm start` + From 23c082faebe58726b526b44eac03096452538a95 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Mon, 22 Jul 2024 21:09:44 +0200 Subject: [PATCH 29/95] JS docs --- build.sbt | 17 ++- docs/js.md | 259 ++++++++++++++++++++++++++++---------------- project/plugins.sbt | 2 - 3 files changed, 171 insertions(+), 107 deletions(-) diff --git a/build.sbt b/build.sbt index 7bd212213..dd1d6af8e 100644 --- a/build.sbt +++ b/build.sbt @@ -297,18 +297,15 @@ val jsdocs = project sharedSettings, publish / skip := true, scalaJSLinkerConfig ~= { - _.withModuleKind(ModuleKind.CommonJSModule) + _.withModuleKind(ModuleKind.ESModule) }, libraryDependencies ++= List( "org.scala-js" %%% "scalajs-dom" % scalajsDom - ), - scalaJSUseMainModuleInitializer := true, - Compile / npmDependencies ++= List( - "ms" -> "2.1.1" - ), - webpackBundlingMode := BundlingMode.LibraryOnly() + // "com.raquo" %%% "laminar" % "0.17.0", + // "com.raquo" %%% "laminar-shoelace" % "0.1.0" // once this project moves to scala 3. + ) ) - .enablePlugins(ScalaJSPlugin, ScalaJSBundlerPlugin) + .enablePlugins(ScalaJSPlugin) lazy val worksheets = project .in(file("tests/worksheets")) @@ -474,7 +471,9 @@ lazy val docs = project Global / cancelable := true, MdocPlugin.autoImport.mdoc := (Compile / run).evaluated, mdocJS := Some(jsdocs), - mdocJSLibraries := (jsdocs / Compile / fullOptJS / webpack).value, + // mdocJSLibraries := (jsdocs / Compile / fullOptJS).outputFiles.map { path => + // Attributed.blank(path.toFile) + // }, MdocPlugin.mdocJSWorkerClasspath := { val _ = (jsWorker / Compile / compile).value diff --git a/docs/js.md b/docs/js.md index 90a35cd68..bb89244ef 100644 --- a/docs/js.md +++ b/docs/js.md @@ -7,13 +7,12 @@ sidebar_label: Scala.js Code fences with the `scala mdoc:js` modifier are compiled with Scala.js and run in the browser. -```scala mdoc:js:shared:invisible -import org.scalajs.dom.window._ -import jsdocs._ -``` ````md ```scala mdoc:js +import org.scalajs.dom.window._ +import jsdocs._ + val progress = new Progress() setInterval({ () => // `node` variable is a DOM element in scope. @@ -22,9 +21,14 @@ setInterval({ () => ``` ```` -```scala mdoc:js:invisible +Becomes + +```scala mdoc:js Loading progress bar... --- +import org.scalajs.dom.window._ +import jsdocs._ + val progress = new Progress() setInterval({ () => // `node` variable is a DOM element in scope. @@ -110,6 +114,20 @@ Some notes: `-Xplugin:path/to/scalajs-compiler.jar` to enable the Scala.js compiler. - The `js-classpath` field in `mdoc.properties` must include a dependency on the library `org.scala-js:scalajs-dom` +- There is a js-html-header field in `mdoc.properties` which can be used to inject custom HTML into the output. This can be used to inject scripts or stylesheets into the output. +```html +js-html-header= +``` +- There is a `--import-map` command line flag, commonly set to something like this; `--import-map-path $(pwd)/importmap.json`, which will remap ESModule imports at link time. As a reminder, `importmap.json`, could look like; +``` +{ + "imports": { + "@shoelace-style/shoelace/dist/": "https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.13.1/cdn/" + } +} +``` +which will remap the shoelace import to resolve out of a CDN - negating the need for a bundler. + ## Modifiers @@ -121,13 +139,8 @@ the rendered output. By default, each code fence is isolated from other code fences. Use the `:shared` modifier to reuse imports or variables between code fences. -```scala mdoc:js:shared:invisible -import org.scalajs.dom.window.setInterval -import scala.scalajs.js.Date -``` - -````scala mdoc:mdoc -```scala mdoc:js:shared +````md +```scala mdoc:js import org.scalajs.dom.window.setInterval import scala.scalajs.js.Date ``` @@ -138,25 +151,28 @@ setInterval(() => { ``` ```` -```scala mdoc:js -Loading :shared example... ---- -setInterval(() => { - val date = new Date().toString() - node.innerHTML = s"

Shared date $date

" -}, 1000) +Is a compile error, because the imports are not in the second block. But add the shared modifier... + +````md +```scala mdoc:js:shared +import org.scalajs.dom.window.setInterval +import scala.scalajs.js.Date ``` +```` -Without `:shared`, the example above results in a compile error. +And it works! -````scala mdoc:mdoc:crash -```scala mdoc:js +```scala mdoc:js:shared +import org.scalajs.dom.window.setInterval import scala.scalajs.js.Date ``` + ```scala mdoc:js -new Date() +setInterval(() => { + node.innerHTML = new Date().toString() +}, 1000) ``` -```` + ### `:invisible` @@ -164,7 +180,7 @@ By default, the original input code is rendered in the output page. Use `:invisible` to hide the code example from the output so that only the div is generated. -````scala mdoc:mdoc +````md ```scala mdoc:js:invisible var n = 0 org.scalajs.dom.window.setInterval(() => { @@ -173,6 +189,7 @@ org.scalajs.dom.window.setInterval(() => { }, 1000) ``` ```` +You'd have to look at the raw markdown to see the fence below... ```scala mdoc:js:invisible Loading :invisible example... @@ -184,6 +201,8 @@ setInterval(() => { }, 1000) ``` + + ### `:compile-only` Use `:compile-only` to validate that a code example compiles successfully @@ -207,7 +226,7 @@ By default, the `node` variable points to an empty div element. Prefix the code fence with custom HTML followed by a `---` separator to set the inner HTML of the `node` div. -````scala mdoc:mdoc +````md ```scala mdoc:js

I am a custom loader

--- @@ -218,8 +237,7 @@ println(node.innerHTML) ```scala mdoc:js

I am a custom loader

--- -// Open developer console to see this printed message -println(s"Loading HTML: ${node.innerHTML}") +println(node.innerHTML) ``` Replace the node's `innerHTML` to make the HTML disappear once the document has @@ -233,66 +251,33 @@ org.scalajs.dom.window.setTimeout(() => { }, 3000) ``` -## Using scalajs-bundler +## Facades -The [scalajs-bundler](https://scalacenter.github.io/scalajs-bundler/) plugin can -be used to install npm dependencies and bundle applications with webpack. - -Add the following sbt settings if you use scalajs-bundler. - -```diff - lazy val jsdocs = project - .settings( -+ webpackBundlingMode := BundlingMode.LibraryOnly() -+ scalaJSUseMainModuleInitializer := true, - npmDependencies.in(Compile) ++= List( - // ... - ), - ) - .enablePlugins(ScalaJSBundlerPlugin) +Let's imagine we want to document a (glorious) facade to [ms](https://www.npmjs.com/package/ms) with mdoc. - lazy val docs = project - .settings( - mdocJS := Some(jsdocs), -+ mdocJSLibraries := webpack.in(jsdocs, Compile, fullOptJS).value - ) - .enablePlugins(MdocPlugin) -``` - -> The `webpackBundlingMode` must be `LibraryOnly` so that the mdoc generated -> output can depend on it. - -### Bundle npm dependencies - -It's important that the main function in `jsdocs` uses the installed npm -dependencies. If the npm dependencies are not used from the `jsdocs` main -function, then webpack thinks they are unused and removes them from the bundled -output even if those dependencies are called from `mdoc:js` markdown code -fences. - -For example, to use the npm [`ms` package](https://www.npmjs.com/package/ms) -start by writing a facade using `@JSImport` - -```scala mdoc:file:tests/jsdocs/src/main/scala/jsdocs/ms.scala +```scala mdoc:js:shared +import scala.scalajs.js +import scala.scalajs.js.annotation._ + +@js.native +@JSImport("https://cdn.jsdelivr.net/npm/ms@2.1.3/+esm", JSImport.Default) +object ms extends js.Object { + def apply(s: Double): String = js.native + def apply(s: String): Double = js.native +} ``` -Next, write a main function that uses the facade. Make sure that the `jsdocs` -project contains the setting `scalaJSUseMainModuleInitializer := true`. - -```scala mdoc:file:tests/jsdocs/src/main/scala/jsdocs/Main.scala - -``` -The `ms` function can now be used from `mdoc:js`. ```scala mdoc:js val date = new scala.scalajs.js.Date() -val time = jsdocs.ms(date.getTime()) -node.innerHTML = s"Hello from npm package 'ms': $time" +val time = ms( "2 days") +val time2 = ms( 1000) +node.innerHTML = s"Hello from npm package 'ms': $time , $time2 " ``` -If the `ms` function is not referenced from the `jsdocs` main function you get a +If the `ms` function is not referenced (somehow) `jsdocs` you may get a stacktrace in the browser console like this: ```js @@ -302,25 +287,13 @@ Uncaught TypeError: $i_ms is not a function at mdoc.js:9 ``` -### Validate library.js and loader.js - -Validate that the `webpack` task provides one `*-library.js` file and one -`*-loader.js` file. +Please note that the CDN is hardcoded here as is a simple way to get started. A "real" facade would normally be constructed as ```scala -// sbt shell -> show jsdocs/fullOptJS::webpack -... -[info] * Attributed(.../jsdocs/target/scala-2.12/scalajs-bundler/main/jsdocs-opt-loader.js) -[info] * Attributed(.../jsdocs/target/scala-2.12/scalajs-bundler/main/jsdocs-opt-library.js) -... +@JSImport("ms", JSImport.Default) ``` -These files are required by mdoc in order to use the scalajs-bundler npm -dependencies. The files may be missing if you have custom webpack or -scalajs-bundler configuration. To fix this problem, you may want to try to -create a new `jsdocs` Scala.js project with minimal webpack and scalajs-bundler -configuration. +As that is where it's found in the `node_modules`. Consider remapping this import at link time, as described in the configuration section below for a (real) use case. ## Configuration @@ -417,7 +390,7 @@ mdocVariables := Map( ``` ### Remapping imports at link time. -It may be the case, that the library you wish to depend on, is a facade to some NPM dependancy. Setting up a bundler in mdoc isn't a simple operation, but there is an alternative. We can instead as mdoc to output ESModules (see `ModuleKind`) and then remap their imports to point at a CDN. +It may be the case, that the library you wish to depend on, is a facade to some NPM dependancy. Mapping the import offers an alterative to bundling, by resolving the dependancy out of a CDN. We set mdoc to output ESModules (see `ModuleKind`), and load the dependancies directly in the browser. To mdocs CLI, you'll need to pass this flag; @@ -425,13 +398,107 @@ To mdocs CLI, you'll need to pass this flag; --import-map-path full/path/to/import-map.json ``` -`import-map.json` should look like this; +`import-map.json` could look like this; ```json { - "imports": { - "@easepick/bundle": "https://cdn.jsdelivr.net/npm/@easepick/bundle@1.2.1", + "imports": + { + "@shoelace-style/shoelace/dist/": "https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.13.1/cdn/" } } ``` You should then have this dependancy available to you. + + +# Legacy + +This documentation is preserved here, but it is no longer recommend to work with scalaJS bundler. If you require a bundler you should consider the excellent [vite](https://vitejs.dev) project. It is currently unsolved how to use mdoc with vite. + + +The [scalajs-bundler](https://scalacenter.github.io/scalajs-bundler/) plugin can +be used to install npm dependencies and bundle applications with webpack. + +Add the following sbt settings if you use scalajs-bundler. + +```diff + lazy val jsdocs = project + .settings( ++ webpackBundlingMode := BundlingMode.LibraryOnly() ++ scalaJSUseMainModuleInitializer := true, + npmDependencies.in(Compile) ++= List( + // ... + ), + ) + .enablePlugins(ScalaJSBundlerPlugin) + + lazy val docs = project + .settings( + mdocJS := Some(jsdocs), ++ mdocJSLibraries := webpack.in(jsdocs, Compile, fullOptJS).value + ) + .enablePlugins(MdocPlugin) +``` + +> The `webpackBundlingMode` must be `LibraryOnly` so that the mdoc generated +> output can depend on it. + +### Bundle npm dependencies + +It's important that the main function in `jsdocs` uses the installed npm +dependencies. If the npm dependencies are not used from the `jsdocs` main +function, then webpack thinks they are unused and removes them from the bundled +output even if those dependencies are called from `mdoc:js` markdown code +fences. + +For example, to use the npm [`ms` package](https://www.npmjs.com/package/ms) +start by writing a facade using `@JSImport` + +```scala mdoc:file:tests/jsdocs/src/main/scala/jsdocs/ms.scala + +``` + +Next, write a main function that uses the facade. Make sure that the `jsdocs` +project contains the setting `scalaJSUseMainModuleInitializer := true`. + +```scala mdoc:file:tests/jsdocs/src/main/scala/jsdocs/Main.scala + +``` + +The `ms` function can now be used from `mdoc:js`. + +```scala +val date = new scala.scalajs.js.Date() +val time = jsdocs.ms(date.getTime()) +node.innerHTML = s"Hello from npm package 'ms': $time" +``` + +If the `ms` function is not referenced from the `jsdocs` main function you get a +stacktrace in the browser console like this: + +```js +Uncaught TypeError: $i_ms is not a function + at $c_Lmdocjs$.run6__Lorg_scalajs_dom_raw_Element__V (js.md.js:1180) + at js.md.js:3108 + at mdoc.js:9 +``` + +### Validate library.js and loader.js + +Validate that the `webpack` task provides one `*-library.js` file and one +`*-loader.js` file. + +```scala +// sbt shell +> show jsdocs/fullOptJS::webpack +... +[info] * Attributed(.../jsdocs/target/scala-2.12/scalajs-bundler/main/jsdocs-opt-loader.js) +[info] * Attributed(.../jsdocs/target/scala-2.12/scalajs-bundler/main/jsdocs-opt-library.js) +... +``` + +These files are required by mdoc in order to use the scalajs-bundler npm +dependencies. The files may be missing if you have custom webpack or +scalajs-bundler configuration. To fix this problem, you may want to try to +create a new `jsdocs` Scala.js project with minimal webpack and scalajs-bundler +configuration. \ No newline at end of file diff --git a/project/plugins.sbt b/project/plugins.sbt index 22628b2f1..16a755c6f 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -4,8 +4,6 @@ addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.12") addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.16.0") -addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % "0.21.1") - libraryDependencies ++= List( "org.jsoup" % "jsoup" % "1.12.1", "org.scala-sbt" %% "scripted-plugin" % sbtVersion.value From cf596600ea2d256f3bb3365f5772ccf0bfd8ced2 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Mon, 22 Jul 2024 21:10:36 +0200 Subject: [PATCH 30/95] . --- website.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website.md b/website.md index 75069a9a6..424385699 100644 --- a/website.md +++ b/website.md @@ -3,7 +3,7 @@ Before starting; 1. Make sure the project compiles. -2. *Ensure you are using node v16*. As of 22.07.2024 The website will not work with higher versions of node. +2. *Ensure you are using node v16*. (As of 22.07.2024) The website will not work with higher versions of node. Consider using nvm to manage node versions. Next From 9962de3b6516903ff847f183a3e8f405468979f3 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Mon, 22 Jul 2024 21:24:19 +0200 Subject: [PATCH 31/95] too much... put back jsdocs project --- build.sbt | 21 ++++++++++++++++++- project/plugins.sbt | 2 ++ .../src/main/scala/jsdocs/Progress.scala | 14 +++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 tests/websiteJs/src/main/scala/jsdocs/Progress.scala diff --git a/build.sbt b/build.sbt index dd1d6af8e..d7184691c 100644 --- a/build.sbt +++ b/build.sbt @@ -293,6 +293,25 @@ val tests = project val jsdocs = project .in(file("tests/jsdocs")) + .settings( + sharedSettings, + publish / skip := true, + scalaJSLinkerConfig ~= { + _.withModuleKind(ModuleKind.CommonJSModule) + }, + libraryDependencies ++= List( + "org.scala-js" %%% "scalajs-dom" % scalajsDom + ), + scalaJSUseMainModuleInitializer := true, + Compile / npmDependencies ++= List( + "ms" -> "2.1.1" + ), + webpackBundlingMode := BundlingMode.LibraryOnly() + ) + .enablePlugins(ScalaJSPlugin, ScalaJSBundlerPlugin) + +val jswebsitedocs = project + .in(file("tests/websiteJs")) .settings( sharedSettings, publish / skip := true, @@ -470,7 +489,7 @@ lazy val docs = project watchSources += (ThisBuild / baseDirectory).value / "docs", Global / cancelable := true, MdocPlugin.autoImport.mdoc := (Compile / run).evaluated, - mdocJS := Some(jsdocs), + mdocJS := Some(jswebsitedocs), // mdocJSLibraries := (jsdocs / Compile / fullOptJS).outputFiles.map { path => // Attributed.blank(path.toFile) // }, diff --git a/project/plugins.sbt b/project/plugins.sbt index 16a755c6f..22628b2f1 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -4,6 +4,8 @@ addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.12") addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.16.0") +addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % "0.21.1") + libraryDependencies ++= List( "org.jsoup" % "jsoup" % "1.12.1", "org.scala-sbt" %% "scripted-plugin" % sbtVersion.value diff --git a/tests/websiteJs/src/main/scala/jsdocs/Progress.scala b/tests/websiteJs/src/main/scala/jsdocs/Progress.scala new file mode 100644 index 000000000..f1d903876 --- /dev/null +++ b/tests/websiteJs/src/main/scala/jsdocs/Progress.scala @@ -0,0 +1,14 @@ +package jsdocs + +class Progress { + var i = -1 + val max = 20 + def tick(_unused: Int): String = { + i += 1 + val n = i % max + 1 + val hash = "#" * n + val space = " " * (max - n) + val percentage = ((n.toDouble / max) * 100).toInt + "
" + hash + "🚀" + space + s"$percentage%" + "
" + } +} From 6ca3b32db04acf0200e34d5ef2d9471a470ba80e Mon Sep 17 00:00:00 2001 From: Anton Sviridov Date: Fri, 2 Feb 2024 16:01:11 +0000 Subject: [PATCH 32/95] EvaluatedMarkdownDocument interface - Add a method to java interfaces - Implement EvaluatedMarkdownDocument class - Update Mdoc implementation (currently in worksheets, but doesn't have to be) --- .../interfaces/EvaluatedMarkdownDocument.java | 13 ++++++++ .../src/main/scala/mdoc/interfaces/Mdoc.java | 3 +- .../markdown/EvaluatedMarkdownDocument.scala | 10 ++++++ .../scala/mdoc/internal/worksheets/Mdoc.scala | 31 +++++++++++++++++++ 4 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 mdoc-interfaces/src/main/scala/mdoc/interfaces/EvaluatedMarkdownDocument.java create mode 100644 mdoc/src/main/scala/mdoc/internal/markdown/EvaluatedMarkdownDocument.scala diff --git a/mdoc-interfaces/src/main/scala/mdoc/interfaces/EvaluatedMarkdownDocument.java b/mdoc-interfaces/src/main/scala/mdoc/interfaces/EvaluatedMarkdownDocument.java new file mode 100644 index 000000000..744f8922b --- /dev/null +++ b/mdoc-interfaces/src/main/scala/mdoc/interfaces/EvaluatedMarkdownDocument.java @@ -0,0 +1,13 @@ +package mdoc.interfaces; + +import java.util.List; +import java.nio.file.Path; +import coursierapi.Dependency; +import coursierapi.Repository; + +public abstract class EvaluatedMarkdownDocument { + + public abstract List diagnostics(); + + public abstract String content(); +} diff --git a/mdoc-interfaces/src/main/scala/mdoc/interfaces/Mdoc.java b/mdoc-interfaces/src/main/scala/mdoc/interfaces/Mdoc.java index 4a9419637..0040e23f3 100644 --- a/mdoc-interfaces/src/main/scala/mdoc/interfaces/Mdoc.java +++ b/mdoc-interfaces/src/main/scala/mdoc/interfaces/Mdoc.java @@ -1,11 +1,12 @@ package mdoc.interfaces; import java.util.List; +import java.util.Map; import java.nio.file.Path; import java.io.PrintStream; public abstract class Mdoc { - + public abstract EvaluatedMarkdownDocument evaluateMarkdownDocument(String filename, String text, Map variables); public abstract EvaluatedWorksheet evaluateWorksheet(String filename, String text); public abstract EvaluatedWorksheet evaluateWorksheet(String filename, String text, String modifier); public abstract Mdoc withWorkingDirectory(Path workingDirectory); diff --git a/mdoc/src/main/scala/mdoc/internal/markdown/EvaluatedMarkdownDocument.scala b/mdoc/src/main/scala/mdoc/internal/markdown/EvaluatedMarkdownDocument.scala new file mode 100644 index 000000000..a79756963 --- /dev/null +++ b/mdoc/src/main/scala/mdoc/internal/markdown/EvaluatedMarkdownDocument.scala @@ -0,0 +1,10 @@ +package mdoc.internal.markdown + +import java.{util => ju} +import mdoc.{interfaces => i} +import java.nio.file.Path + +case class EvaluatedMarkdownDocument( + val diagnostics: ju.List[i.Diagnostic], + val content: String +) extends mdoc.interfaces.EvaluatedMarkdownDocument diff --git a/mdoc/src/main/scala/mdoc/internal/worksheets/Mdoc.scala b/mdoc/src/main/scala/mdoc/internal/worksheets/Mdoc.scala index f87cf2280..fa139c138 100644 --- a/mdoc/src/main/scala/mdoc/internal/worksheets/Mdoc.scala +++ b/mdoc/src/main/scala/mdoc/internal/worksheets/Mdoc.scala @@ -7,6 +7,7 @@ import mdoc.internal.cli.Settings import mdoc.internal.io.ConsoleReporter import mdoc.internal.markdown.MarkdownCompiler import mdoc.internal.markdown.Modifier +import mdoc.internal.markdown.EvaluatedMarkdownDocument import mdoc.internal.pos.PositionSyntax._ import mdoc.internal.worksheets.Compat._ import mdoc.{interfaces => i} @@ -17,6 +18,10 @@ import java.nio.file.Path import java.{util => ju} import scala.meta.inputs.Input import scala.meta.internal.io.PathIO +import mdoc.internal.markdown.Markdown +import mdoc.internal.cli.InputFile +import mdoc.internal.io.StoreReporter +import mdoc.internal.markdown.EvaluatedDocument class Mdoc(settings: MainSettings) extends i.Mdoc { @@ -48,6 +53,32 @@ class Mdoc(settings: MainSettings) extends i.Mdoc { } } + def evaluateMarkdownDocument( + filename: String, + text: String, + variables: ju.Map[String, String] + ): i.EvaluatedMarkdownDocument = { + val settings = this.settings.settings + val inputFile = InputFile.fromRelativeFilename(filename, settings) + val reporter = new StoreReporter() + + val result = Markdown.toMarkdown( + Input.stringToInput(text), + this.context(), + inputFile, + variables.asScala.toMap, + reporter, + settings + ) + val diagnostics = reporter.diagnostics.map(d => d: i.Diagnostic).toSeq.asJava + + EvaluatedMarkdownDocument( + diagnostics = diagnostics, + content = result + ) + + } + def evaluateWorksheet(filename: String, text: String): i.EvaluatedWorksheet = { new WorksheetProvider(settings.settings).evaluateWorksheet( Input.VirtualFile(filename, text), From 9e4c5e92414d297e073cdd0ca0869cf92ce58868 Mon Sep 17 00:00:00 2001 From: Anton Sviridov Date: Fri, 26 Jul 2024 09:07:36 +0100 Subject: [PATCH 33/95] Add basic test --- .../mdoc/internal/markdown/Instrumenter.scala | 2 +- .../EvaluatedMarkdownDocumentSuite.scala | 71 +++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 tests/worksheets/src/test/scala/tests/worksheets/EvaluatedMarkdownDocumentSuite.scala diff --git a/mdoc/src/main/scala-2/mdoc/internal/markdown/Instrumenter.scala b/mdoc/src/main/scala-2/mdoc/internal/markdown/Instrumenter.scala index 4189d0ecb..3781fb945 100644 --- a/mdoc/src/main/scala-2/mdoc/internal/markdown/Instrumenter.scala +++ b/mdoc/src/main/scala-2/mdoc/internal/markdown/Instrumenter.scala @@ -168,7 +168,7 @@ object Instrumenter { def stringLiteral(string: String): String = { import scala.meta.internal.prettyprinters._ - DoubleQuotes(string) + enquote(string, DoubleQuotes) } def wrapBody(body: String): String = { diff --git a/tests/worksheets/src/test/scala/tests/worksheets/EvaluatedMarkdownDocumentSuite.scala b/tests/worksheets/src/test/scala/tests/worksheets/EvaluatedMarkdownDocumentSuite.scala new file mode 100644 index 000000000..6d1a9e268 --- /dev/null +++ b/tests/worksheets/src/test/scala/tests/worksheets/EvaluatedMarkdownDocumentSuite.scala @@ -0,0 +1,71 @@ +package tests.interface + +import mdoc.interfaces.DiagnosticSeverity +import mdoc.interfaces.Mdoc +import mdoc.internal.CompatClassloader +import mdoc.internal.pos.PositionSyntax._ +import munit.Location +import munit.TestOptions +import tests.BaseSuite +import tests.markdown.Compat + +import java.lang.StringBuilder +import java.nio.file.Paths +import java.{util => ju} +import scala.meta.inputs.Input +import scala.meta.inputs.Position + +class EvaluatedMarkdownDocumentSuite extends BaseSuite { + + var mdoc = ju.ServiceLoader + .load(classOf[Mdoc], this.getClass().getClassLoader()) + .iterator() + .next() + .withScreenHeight(5) + .withClasspath( + CompatClassloader + .getURLs(this.getClass().getClassLoader()) + .collect { + case url + if url.toString.contains("scala3-library") || url.toString + .contains("scala-library") => + Paths.get(url.toURI()) + } + .asJava + ) + + override def afterAll(): Unit = { + mdoc.shutdown() + } + + test("basic markdown evaluation") { + val document = + """ + |# Hello world! + | + |The latest library version is `@HELLO@.@WORLD@` + | + |```scala mdoc + |println(math.abs(-25)) + |``` + """.stripMargin + + val expectedContent = + """ + |# Hello world! + | + |The latest library version is `1.2` + | + |```scala + |println(math.abs(-25)) + |// 25 + |``` + """.stripMargin + + val variables = java.util.Map.of("HELLO", "1", "WORLD", "2") + + val evaluated = mdoc.evaluateMarkdownDocument("README.md", document, variables) + + assertEquals(evaluated.content(), expectedContent) + } +} From 6f3048159e0d186e920dbb8c04d34935039adf65 Mon Sep 17 00:00:00 2001 From: Anton Sviridov Date: Fri, 26 Jul 2024 09:10:21 +0100 Subject: [PATCH 34/95] Huh --- mdoc/src/main/scala-2/mdoc/internal/markdown/Instrumenter.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mdoc/src/main/scala-2/mdoc/internal/markdown/Instrumenter.scala b/mdoc/src/main/scala-2/mdoc/internal/markdown/Instrumenter.scala index 3781fb945..4189d0ecb 100644 --- a/mdoc/src/main/scala-2/mdoc/internal/markdown/Instrumenter.scala +++ b/mdoc/src/main/scala-2/mdoc/internal/markdown/Instrumenter.scala @@ -168,7 +168,7 @@ object Instrumenter { def stringLiteral(string: String): String = { import scala.meta.internal.prettyprinters._ - enquote(string, DoubleQuotes) + DoubleQuotes(string) } def wrapBody(body: String): String = { From 1291803c80615c31540a847c09aabfa69b8b2302 Mon Sep 17 00:00:00 2001 From: Anton Sviridov Date: Fri, 26 Jul 2024 09:17:57 +0100 Subject: [PATCH 35/95] Initialise map differently --- .../tests/worksheets/EvaluatedMarkdownDocumentSuite.scala | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/worksheets/src/test/scala/tests/worksheets/EvaluatedMarkdownDocumentSuite.scala b/tests/worksheets/src/test/scala/tests/worksheets/EvaluatedMarkdownDocumentSuite.scala index 6d1a9e268..8d98bf03f 100644 --- a/tests/worksheets/src/test/scala/tests/worksheets/EvaluatedMarkdownDocumentSuite.scala +++ b/tests/worksheets/src/test/scala/tests/worksheets/EvaluatedMarkdownDocumentSuite.scala @@ -62,7 +62,9 @@ class EvaluatedMarkdownDocumentSuite extends BaseSuite { |``` """.stripMargin - val variables = java.util.Map.of("HELLO", "1", "WORLD", "2") + val variables = new java.util.HashMap[String, String]() + variables.put("HELLO", "1") + variables.put("WORLD", "2") val evaluated = mdoc.evaluateMarkdownDocument("README.md", document, variables) From 832424a89104b8fd0eb01457ee24cf360a1ac72f Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Wed, 14 Aug 2024 11:33:47 +0200 Subject: [PATCH 36/95] Update docs/js.md Co-authored-by: Tomasz Godzik --- docs/js.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/js.md b/docs/js.md index bb89244ef..192ed7f1e 100644 --- a/docs/js.md +++ b/docs/js.md @@ -253,7 +253,7 @@ org.scalajs.dom.window.setTimeout(() => { ## Facades -Let's imagine we want to document a (glorious) facade to [ms](https://www.npmjs.com/package/ms) with mdoc. +Let's imagine we want to document a (glorious) facade to [ms](https://www.npmjs.com/package/ms) with mdoc. ```scala mdoc:js:shared From 480b1edf200087248fc951cfc00eb6a5f51528e9 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Wed, 14 Aug 2024 11:34:10 +0200 Subject: [PATCH 37/95] Update docs/js.md Co-authored-by: Tomasz Godzik --- docs/js.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/js.md b/docs/js.md index 192ed7f1e..e233e3b5d 100644 --- a/docs/js.md +++ b/docs/js.md @@ -408,7 +408,7 @@ To mdocs CLI, you'll need to pass this flag; } } ``` -You should then have this dependancy available to you. +You should then have this dependency available to you. # Legacy From 28b2e1b2a380c89dbadeac0fc0a224763123263f Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Wed, 14 Aug 2024 11:34:36 +0200 Subject: [PATCH 38/95] Update docs/js.md Co-authored-by: Tomasz Godzik --- docs/js.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/js.md b/docs/js.md index e233e3b5d..21df10b39 100644 --- a/docs/js.md +++ b/docs/js.md @@ -114,7 +114,7 @@ Some notes: `-Xplugin:path/to/scalajs-compiler.jar` to enable the Scala.js compiler. - The `js-classpath` field in `mdoc.properties` must include a dependency on the library `org.scala-js:scalajs-dom` -- There is a js-html-header field in `mdoc.properties` which can be used to inject custom HTML into the output. This can be used to inject scripts or stylesheets into the output. +- There is a js-html-header field in `mdoc.properties` which can be used to inject custom HTML, scripts or stylesheets into the output. ```html js-html-header= ``` From 50b86fee87577a163c4f3c18107f1530021e51ec Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Wed, 14 Aug 2024 11:55:38 +0200 Subject: [PATCH 39/95] revert --- docs/js.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/js.md b/docs/js.md index 21df10b39..6ac59ea78 100644 --- a/docs/js.md +++ b/docs/js.md @@ -230,7 +230,8 @@ the `node` div. ```scala mdoc:js

I am a custom loader

--- -println(node.innerHTML) +// Open developer console to see this printed message +println(s"Loading HTML: ${node.innerHTML}") ``` ```` From 39b12a59f897674b1e6efdfc002f95092f78cb2f Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Wed, 14 Aug 2024 11:58:02 +0200 Subject: [PATCH 40/95] organise imports --- .../unit-js/src/test/scala/tests/js/JsCliSuite.scala | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala index 5f07a25fd..1183fc192 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala @@ -2,18 +2,19 @@ package tests.js import mdoc.internal.cli.MdocProperties import mdoc.internal.cli.Settings -import scala.meta.io.Classpath -import tests.markdown.StringSyntax._ import tests.cli.BaseCliSuite -import scala.meta.internal.io.PathIO import tests.js.JsTests.suffix +import tests.markdown.StringSyntax._ + import java.io.ByteArrayOutputStream import java.io.PrintStream -import scala.meta.internal.io.FileIO -import scala.meta.io.AbsolutePath import java.nio.charset.StandardCharsets import java.nio.file.Paths import scala.io.Source +import scala.meta.internal.io.FileIO +import scala.meta.internal.io.PathIO +import scala.meta.io.AbsolutePath +import scala.meta.io.Classpath class JsCliSuite extends BaseCliSuite { From 95bd8f6904e2bdd5086a24398f39da719c158d3f Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Wed, 14 Aug 2024 11:59:38 +0200 Subject: [PATCH 41/95] . --- docs/js.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/js.md b/docs/js.md index 6ac59ea78..3bb730a9b 100644 --- a/docs/js.md +++ b/docs/js.md @@ -238,7 +238,8 @@ println(s"Loading HTML: ${node.innerHTML}") ```scala mdoc:js

I am a custom loader

--- -println(node.innerHTML) +// Open developer console to see this printed message +println(s"Loading HTML: ${node.innerHTML}") ``` Replace the node's `innerHTML` to make the HTML disappear once the document has From bedebef7fd66bf6142e1f11e65f3c93d0f16d81f Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Wed, 14 Aug 2024 12:03:01 +0200 Subject: [PATCH 42/95] remove commented references --- build.sbt | 2 -- 1 file changed, 2 deletions(-) diff --git a/build.sbt b/build.sbt index d7184691c..066ed8d7b 100644 --- a/build.sbt +++ b/build.sbt @@ -320,8 +320,6 @@ val jswebsitedocs = project }, libraryDependencies ++= List( "org.scala-js" %%% "scalajs-dom" % scalajsDom - // "com.raquo" %%% "laminar" % "0.17.0", - // "com.raquo" %%% "laminar-shoelace" % "0.1.0" // once this project moves to scala 3. ) ) .enablePlugins(ScalaJSPlugin) From 64b7748b57aebfc5b3a290f6f863b59edff7f0a9 Mon Sep 17 00:00:00 2001 From: Scalameta Bot Date: Fri, 23 Aug 2024 00:58:30 +0000 Subject: [PATCH 43/95] Update slf4j-api to 2.0.16 --- build.sbt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sbt b/build.sbt index b588eff45..d2dbfa4f9 100644 --- a/build.sbt +++ b/build.sbt @@ -246,7 +246,7 @@ lazy val mdoc = project // live reload "io.undertow" % "undertow-core" % "2.2.24.Final", "org.jboss.xnio" % "xnio-nio" % "3.8.16.Final", - "org.slf4j" % "slf4j-api" % "2.0.13", + "org.slf4j" % "slf4j-api" % "2.0.16", "com.geirsson" %% "metaconfig-typesafe-config" % V.metaconfig, "com.lihaoyi" %% "fansi" % V.fansi, "com.lihaoyi" %% "pprint" % V.pprint From f3ed9175c72dd18f5dfd7e4c669a4e42285ee4d5 Mon Sep 17 00:00:00 2001 From: Scalameta Bot Date: Fri, 23 Aug 2024 00:58:20 +0000 Subject: [PATCH 44/95] Update scalameta, testkit to 4.9.9 --- build.sbt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sbt b/build.sbt index d2dbfa4f9..776a18dfe 100644 --- a/build.sbt +++ b/build.sbt @@ -114,7 +114,7 @@ lazy val sharedJavaSettings = List( ) val V = new { - val scalameta = "4.9.8" + val scalameta = "4.9.9" val munit = "1.0.0" From 41b8231899f9251b891c18add92d8229581f4806 Mon Sep 17 00:00:00 2001 From: Scalameta Bot Date: Fri, 23 Aug 2024 00:58:06 +0000 Subject: [PATCH 45/95] Update scalafmt-core to 3.8.3 --- .scalafmt.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.scalafmt.conf b/.scalafmt.conf index ff0eaa852..bbbd5184f 100644 --- a/.scalafmt.conf +++ b/.scalafmt.conf @@ -1,4 +1,4 @@ -version = "3.8.2" +version = "3.8.3" runner.dialect = scala213 maxColumn = 100 project.git = true From 4511c9d7631ad9d5127324f64fa8139632bbccb0 Mon Sep 17 00:00:00 2001 From: Scalameta Bot Date: Fri, 23 Aug 2024 00:57:59 +0000 Subject: [PATCH 46/95] Update munit to 1.0.1 --- build.sbt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sbt b/build.sbt index 776a18dfe..96b898809 100644 --- a/build.sbt +++ b/build.sbt @@ -116,7 +116,7 @@ lazy val sharedJavaSettings = List( val V = new { val scalameta = "4.9.9" - val munit = "1.0.0" + val munit = "1.0.1" val coursier = "1.0.19" From 4fa3cd9cf3224a8b5e88643bed0bbb41c9576191 Mon Sep 17 00:00:00 2001 From: Scalameta Bot Date: Fri, 23 Aug 2024 00:57:50 +0000 Subject: [PATCH 47/95] Update sbt-ci-release to 1.6.1 --- project/plugins.sbt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/plugins.sbt b/project/plugins.sbt index 22628b2f1..902e1bf4d 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -1,6 +1,6 @@ addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.12.0") -addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.12") +addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.6.1") addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.16.0") From c7e35199255e71037e37ee59664f462cb17bcfe6 Mon Sep 17 00:00:00 2001 From: Scalameta Bot Date: Mon, 26 Aug 2024 00:59:43 +0000 Subject: [PATCH 48/95] Update metaconfig-typesafe-config to 0.13.0 --- build.sbt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/build.sbt b/build.sbt index 96b898809..2f5e4b520 100644 --- a/build.sbt +++ b/build.sbt @@ -128,7 +128,7 @@ val V = new { val fs2 = "3.10.2" - val metaconfig = "0.12.0" + val metaconfig = "0.13.0" } lazy val interfaces = project @@ -194,7 +194,7 @@ lazy val cli = project "io.get-coursier" % "interface" % V.coursier, "com.vladsch.flexmark" % "flexmark-all" % "0.64.8", "com.lihaoyi" %% "pprint" % V.pprint, - "com.geirsson" %% "metaconfig-typesafe-config" % V.metaconfig + "org.scalameta" %% "metaconfig-typesafe-config" % V.metaconfig ), libraryDependencies ++= crossSetting( scalaVersion.value, @@ -247,7 +247,7 @@ lazy val mdoc = project "io.undertow" % "undertow-core" % "2.2.24.Final", "org.jboss.xnio" % "xnio-nio" % "3.8.16.Final", "org.slf4j" % "slf4j-api" % "2.0.16", - "com.geirsson" %% "metaconfig-typesafe-config" % V.metaconfig, + "org.scalameta" %% "metaconfig-typesafe-config" % V.metaconfig, "com.lihaoyi" %% "fansi" % V.fansi, "com.lihaoyi" %% "pprint" % V.pprint ) From 64ee2f1c7cca1ad6ba07bbe63899d45191101a88 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Fri, 30 Aug 2024 10:29:17 +0200 Subject: [PATCH 49/95] some feedback --- build.sbt | 10 ++++------ .../main/scala/mdoc/internal/cli/Settings.scala | 14 ++++++++------ docs/js.md | 14 +++++++------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/build.sbt b/build.sbt index 066ed8d7b..2979a0e39 100644 --- a/build.sbt +++ b/build.sbt @@ -246,6 +246,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", @@ -256,7 +257,7 @@ lazy val mdoc = project "com.geirsson" %% "metaconfig-typesafe-config" % V.metaconfig, "com.lihaoyi" %% "fansi" % V.fansi, "com.lihaoyi" %% "pprint" % V.pprint - ) ++ jsoniter + ) ) .dependsOn(parser, runtime, cli) .enablePlugins(BuildInfoPlugin) @@ -464,8 +465,8 @@ lazy val js = project .settings( sharedSettings, moduleName := "mdoc-js", - Compile / unmanagedSourceDirectories ++= multiScalaDirectories("js").value, - libraryDependencies ++= jsoniter + libraryDependencies ++= jsoniter, + Compile / unmanagedSourceDirectories ++= multiScalaDirectories("js").value ) .dependsOn(mdoc) @@ -488,9 +489,6 @@ lazy val docs = project Global / cancelable := true, MdocPlugin.autoImport.mdoc := (Compile / run).evaluated, mdocJS := Some(jswebsitedocs), - // mdocJSLibraries := (jsdocs / Compile / fullOptJS).outputFiles.map { path => - // Attributed.blank(path.toFile) - // }, MdocPlugin.mdocJSWorkerClasspath := { val _ = (jsWorker / Compile / compile).value diff --git a/cli/src/main/scala/mdoc/internal/cli/Settings.scala b/cli/src/main/scala/mdoc/internal/cli/Settings.scala index 932c7b181..025fd5d14 100644 --- a/cli/src/main/scala/mdoc/internal/cli/Settings.scala +++ b/cli/src/main/scala/mdoc/internal/cli/Settings.scala @@ -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 @@ -32,7 +33,6 @@ import mdoc.internal.markdown.{GitHubIdGenerator, ReplVariablePrinter} import mdoc.internal.cli.CliEnrichments._ import pprint.TPrint import pprint.TPrintColors -import java.nio.file.Path class Section(val name: String) extends StaticAnnotation @@ -154,9 +154,6 @@ case class Settings( @Hidden() @Description("The pretty printer for variables") variablePrinter: Variable => String = ReplVariablePrinter, - @Hidden() - @Description("The Coursier logger used to report progress bars when downloading dependencies") - coursierLogger: coursierapi.Logger = coursierapi.Logger.progressBars(), @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" ) @@ -164,11 +161,16 @@ case class Settings( @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" + propertyFileName: String = mdocProperties, + @Hidden() + @Description("The Coursier logger used to report progress bars when downloading dependencies") + coursierLogger: coursierapi.Logger = coursierapi.Logger.progressBars() ) extends mdoc.parser.ParserSettings { val isMarkdownFileExtension = markdownExtensions.toSet + val mdocProperties = "mdoc.properties" + val outputByInput = in .zip(out) .iterator @@ -310,7 +312,7 @@ object Settings { // extends MetaconfigScalametaImplicits with Decoders with Set val base = Settings.default( AbsolutePath(workingDirectory), - conf.get[String]("propertyFileName").getOrElse("mdoc.properties") + conf.get[String]("propertyFileName").getOrElse(mdocProperties) ) val cwd = conf.get[String]("cwd").map(AbsolutePath(_)(base.cwd)).getOrElse(base.cwd) conf diff --git a/docs/js.md b/docs/js.md index 3bb730a9b..8f0c04edc 100644 --- a/docs/js.md +++ b/docs/js.md @@ -79,7 +79,7 @@ js-linker-classpath=$(coursier fetch org.scalameta:mdoc-js-worker_@SCALA_BINARY_ EOT ``` -Note: For scala 3, you may need `js-scalac-options=-scalajs` - instead of the XPlugin incantation - [Doc](https://docs.scala-lang.org/scala3/guides/migration/options-lookup.html#compiler-plugins). +Note: For scala 3, you may need `js-scalac-options=-scalajs` - instead of the `-XPlugin` flag - [Doc](https://docs.scala-lang.org/scala3/guides/migration/options-lookup.html#compiler-plugins). @@ -118,7 +118,7 @@ Some notes: ```html js-html-header= ``` -- There is a `--import-map` command line flag, commonly set to something like this; `--import-map-path $(pwd)/importmap.json`, which will remap ESModule imports at link time. As a reminder, `importmap.json`, could look like; +- There's an `--import-map` command line flag, commonly set to ; `--import-map-path $(pwd)/importmap.json`, which will remap ESModule imports at link time. As a reminder `importmap.json` could look like: ``` { "imports": { @@ -279,7 +279,7 @@ val time2 = ms( 1000) node.innerHTML = s"Hello from npm package 'ms': $time , $time2 " ``` -If the `ms` function is not referenced (somehow) `jsdocs` you may get a +If the `ms` function is not referenced from `jsdocs` you may get a stacktrace in the browser console like this: ```js @@ -390,9 +390,9 @@ mdocVariables := Map( "js-batch-mode" -> "true" ) ``` -### Remapping imports at link time. +### Remapping imports at link time -It may be the case, that the library you wish to depend on, is a facade to some NPM dependancy. Mapping the import offers an alterative to bundling, by resolving the dependancy out of a CDN. We set mdoc to output ESModules (see `ModuleKind`), and load the dependancies directly in the browser. +It may be the case that the library you wish to depend on is a facade to some NPM dependency. Mapping the import offers an alterative to bundling, by resolving the dependency out of a CDN. We set mdoc to output ESModules (see `ModuleKind`), and load the dependancies directly in the browser. To mdocs CLI, you'll need to pass this flag; @@ -415,13 +415,13 @@ You should then have this dependency available to you. # Legacy -This documentation is preserved here, but it is no longer recommend to work with scalaJS bundler. If you require a bundler you should consider the excellent [vite](https://vitejs.dev) project. It is currently unsolved how to use mdoc with vite. +This documentation is preserved here, but it is no longer recommend to work with scalaJS bundler. If you require a bundler you should consider the excellent [vite](https://vitejs.dev) project - integration with vite is a potential improvement for mdoc. The [scalajs-bundler](https://scalacenter.github.io/scalajs-bundler/) plugin can be used to install npm dependencies and bundle applications with webpack. -Add the following sbt settings if you use scalajs-bundler. +Add the following sbt settings if you use scalajs-bundler: ```diff lazy val jsdocs = project From 432cddae22f4bce6b8363cad92b5161ae6f18b58 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Fri, 30 Aug 2024 11:44:15 +0200 Subject: [PATCH 50/95] Parse from stream --- .../scala/mdoc/internal/cli/Settings.scala | 6 ++-- .../main/scala/mdoc/modifiers/JsConfig.scala | 28 +++++++++++++++---- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/cli/src/main/scala/mdoc/internal/cli/Settings.scala b/cli/src/main/scala/mdoc/internal/cli/Settings.scala index 025fd5d14..c286756e5 100644 --- a/cli/src/main/scala/mdoc/internal/cli/Settings.scala +++ b/cli/src/main/scala/mdoc/internal/cli/Settings.scala @@ -161,7 +161,7 @@ case class Settings( @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 = mdocProperties, + propertyFileName: String = "mdoc.properties", @Hidden() @Description("The Coursier logger used to report progress bars when downloading dependencies") coursierLogger: coursierapi.Logger = coursierapi.Logger.progressBars() @@ -169,8 +169,6 @@ case class Settings( val isMarkdownFileExtension = markdownExtensions.toSet - val mdocProperties = "mdoc.properties" - val outputByInput = in .zip(out) .iterator @@ -312,7 +310,7 @@ object Settings { // extends MetaconfigScalametaImplicits with Decoders with Set val base = Settings.default( AbsolutePath(workingDirectory), - conf.get[String]("propertyFileName").getOrElse(mdocProperties) + conf.get[String]("propertyFileName").getOrElse("mdoc.properties") ) val cwd = conf.get[String]("cwd").map(AbsolutePath(_)(base.cwd)).getOrElse(base.cwd) conf diff --git a/mdoc-js/src/main/scala/mdoc/modifiers/JsConfig.scala b/mdoc-js/src/main/scala/mdoc/modifiers/JsConfig.scala index 969735a90..aee8ed878 100644 --- a/mdoc-js/src/main/scala/mdoc/modifiers/JsConfig.scala +++ b/mdoc-js/src/main/scala/mdoc/modifiers/JsConfig.scala @@ -1,15 +1,16 @@ package mdoc.modifiers +import java.io.{FileInputStream, InputStreamReader, BufferedReader, IOException} import mdoc.OnLoadContext import mdoc.PostProcessContext import mdoc.internal.pos.PositionSyntax._ +import mdoc.modifiers.ImportMapJsonIr.ImportMap +import mdoc.js.interfaces._ import scala.meta.internal.io.PathIO import scala.meta.io.AbsolutePath import scala.meta.io.Classpath -import mdoc.js.interfaces._ import scala.collection.immutable.HashMap -import com.github.plokhotnyuk.jsoniter_scala.core._ -import mdoc.modifiers.ImportMapJsonIr.ImportMap +import com.github.plokhotnyuk.jsoniter_scala.{core => jsoniter} case class JsConfig( moduleKind: ModuleType = ModuleType.NoModule, @@ -107,9 +108,24 @@ object JsConfig { importMap = ctx.settings.importMapPath match { case None => new HashMap[String, String]() case Some(value) => - val importMapRaw = scala.io.Source.fromFile(value.toFile).getLines().mkString("\n") - val importMapParsed = readFromString[ImportMap](importMapRaw) - importMapParsed.imports + var fileInputStream: FileInputStream = null + try { + fileInputStream = new FileInputStream(value.toFile) + val importMapParsed = jsoniter.readFromStream[ImportMap](fileInputStream) + importMapParsed.imports + } catch { + case e: IOException => + e.printStackTrace() + throw e + } finally { + if (fileInputStream != null) { + try { + fileInputStream.close() + } catch { + case e: IOException => e.printStackTrace() + } + } + } } ) } From b63b5880c9f590c807191d417b42a740a5bbe4c7 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Fri, 30 Aug 2024 11:46:08 +0200 Subject: [PATCH 51/95] remove semicolon --- tests/unit-js/src/test/scala/tests/js/JsSuite.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit-js/src/test/scala/tests/js/JsSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsSuite.scala index 803fd0af7..22f6e32ef 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsSuite.scala @@ -7,7 +7,7 @@ import tests.markdown.BaseMarkdownSuite import tests.js.JsTests.suffix import tests.markdown.Compat import scala.meta.io.AbsolutePath -import java.nio.file.Paths; +import java.nio.file.Paths class JsSuite extends BaseMarkdownSuite { // NOTE(olafur) Optimization. Cache settings to reuse the Scala.js compiler instance. From 7ad139a3348f4bb8d29d30c0beed6638840dcb42 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Fri, 30 Aug 2024 11:47:40 +0200 Subject: [PATCH 52/95] synatz --- mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala b/mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala index 1a1582dfe..63ecdefc2 100644 --- a/mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala +++ b/mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala @@ -74,7 +74,7 @@ class JsModifier extends mdoc.PreModifier { .withSourceMap(false) .withBatchMode(config.batchMode) .withClosureCompiler( - config.fullOpt && !(config.moduleKind == ModuleType.ESModule) + config.fullOpt && config.moduleKind != ModuleType.ESModule ) // Closure compiler doesn't work with ESModules .withImportMap(config.importMap.asJava) } From 25059704313f3c2e1b5b60a99ad96afb043f2fd7 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Thu, 11 Jul 2024 18:48:42 +0200 Subject: [PATCH 53/95] Remap ES Modules at link time --- build.sbt | 10 +- .../mdoc/internal/cli/MdocProperties.scala | 3 +- .../scala/mdoc/internal/cli/Settings.scala | 27 +++- docs/js.md | 4 +- .../src/main/java/ScalajsConfig.java | 12 +- .../src/main/scala/ImportMappedIRFile.scala | 30 +++++ .../main/scala/ImportMappedIRFileImpl.scala | 78 ++++++++++++ .../src/main/scala/ScalaJSWorker.scala | 25 +++- .../main/scala/ScalaJSWorkerProvider.scala | 1 - .../main/scala/mdoc/modifiers/JsConfig.scala | 16 ++- .../scala/mdoc/modifiers/JsImportMap.scala | 19 +++ .../scala/mdoc/modifiers/JsModifier.scala | 5 +- .../mdoc/modifiers/ScalajsClassloader.scala | 2 + mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala | 10 +- mdoc/src/main/scala/mdoc/Main.scala | 3 +- mdoc/src/main/scala/mdoc/MainSettings.scala | 8 +- .../unit-js/src/test/resources/importmap.json | 5 + .../src/test/scala/tests/js/JsCliSuite.scala | 117 ++++++++++++++++++ .../src/test/scala/tests/js/JsSuite.scala | 58 +++++++-- .../tests/markdown/BaseMarkdownSuite.scala | 16 +-- 20 files changed, 400 insertions(+), 49 deletions(-) create mode 100644 mdoc-js-worker/src/main/scala/ImportMappedIRFile.scala create mode 100644 mdoc-js-worker/src/main/scala/ImportMappedIRFileImpl.scala create mode 100644 mdoc-js/src/main/scala/mdoc/modifiers/JsImportMap.scala create mode 100644 tests/unit-js/src/test/resources/importmap.json diff --git a/build.sbt b/build.sbt index 2f5e4b520..47f2d849a 100644 --- a/build.sbt +++ b/build.sbt @@ -14,6 +14,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")) } @@ -250,7 +255,7 @@ lazy val mdoc = project "org.scalameta" %% "metaconfig-typesafe-config" % V.metaconfig, "com.lihaoyi" %% "fansi" % V.fansi, "com.lihaoyi" %% "pprint" % V.pprint - ) + ) ++ jsoniter ) .dependsOn(parser, runtime, cli) .enablePlugins(BuildInfoPlugin) @@ -441,7 +446,8 @@ lazy val js = project .settings( sharedSettings, moduleName := "mdoc-js", - Compile / unmanagedSourceDirectories ++= multiScalaDirectories("js").value + Compile / unmanagedSourceDirectories ++= multiScalaDirectories("js").value, + libraryDependencies ++= jsoniter ) .dependsOn(mdoc) diff --git a/cli/src/main/scala/mdoc/internal/cli/MdocProperties.scala b/cli/src/main/scala/mdoc/internal/cli/MdocProperties.scala index 7b366d8dc..aa24a5290 100644 --- a/cli/src/main/scala/mdoc/internal/cli/MdocProperties.scala +++ b/cli/src/main/scala/mdoc/internal/cli/MdocProperties.scala @@ -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() diff --git a/cli/src/main/scala/mdoc/internal/cli/Settings.scala b/cli/src/main/scala/mdoc/internal/cli/Settings.scala index e2476f6c6..75f17a700 100644 --- a/cli/src/main/scala/mdoc/internal/cli/Settings.scala +++ b/cli/src/main/scala/mdoc/internal/cli/Settings.scala @@ -32,6 +32,7 @@ import mdoc.internal.markdown.{GitHubIdGenerator, ReplVariablePrinter} import mdoc.internal.cli.CliEnrichments._ import pprint.TPrint import pprint.TPrintColors +import java.nio.file.Path class Section(val name: String) extends StaticAnnotation @@ -155,7 +156,15 @@ case class Settings( variablePrinter: Variable => String = ReplVariablePrinter, @Hidden() @Description("The Coursier logger used to report progress bars when downloading dependencies") - coursierLogger: coursierapi.Logger = coursierapi.Logger.progressBars() + coursierLogger: coursierapi.Logger = coursierapi.Logger.progressBars(), + @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" + ) + propFileName: String = "mdoc.properties" ) extends mdoc.parser.ParserSettings { val isMarkdownFileExtension = markdownExtensions.toSet @@ -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) } @@ -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]("propFileName").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) diff --git a/docs/js.md b/docs/js.md index 7d0aa8a16..94086176e 100644 --- a/docs/js.md +++ b/docs/js.md @@ -75,7 +75,9 @@ js-linker-classpath=$(coursier fetch org.scalameta:mdoc-js-worker_@SCALA_BINARY_ EOT ``` -Note: For scala 3, you may need `js-scalac-options=--scala-js` - this is currently untested. +Note: For scala 3, you may need `js-scalac-options=-scalajs` - instead of the XPlugin incantation - [Doc](https://docs.scala-lang.org/scala3/guides/migration/options-lookup.html#compiler-plugins). + + Next, create a basic Markdown files with an `mdoc:js` modifier: diff --git a/mdoc-js-interfaces/src/main/java/ScalajsConfig.java b/mdoc-js-interfaces/src/main/java/ScalajsConfig.java index f626f4ce2..b93210747 100644 --- a/mdoc-js-interfaces/src/main/java/ScalajsConfig.java +++ b/mdoc-js-interfaces/src/main/java/ScalajsConfig.java @@ -1,17 +1,20 @@ package mdoc.js.interfaces; +import java.util.Map; + public class ScalajsConfig { public ModuleType moduleType; public boolean fullOpt; public boolean sourceMap; public boolean batchMode; public boolean closureCompiler; + public Map importMap; public ScalajsConfig() { } - + public ScalajsConfig withModuleKind(ModuleType kind) { - if(kind == ModuleType.ESModule) + if (kind == ModuleType.ESModule) this.moduleType = ModuleType.ESModule; else if (kind == ModuleType.NoModule) this.moduleType = ModuleType.NoModule; @@ -20,6 +23,11 @@ else if (kind == ModuleType.CommonJSModule) return this; } + public ScalajsConfig withImportMap(Map importMap) { + this.importMap = importMap; + return this; + } + public ScalajsConfig withOptimized(boolean enabled) { this.fullOpt = enabled; return this; diff --git a/mdoc-js-worker/src/main/scala/ImportMappedIRFile.scala b/mdoc-js-worker/src/main/scala/ImportMappedIRFile.scala new file mode 100644 index 000000000..5ff419154 --- /dev/null +++ b/mdoc-js-worker/src/main/scala/ImportMappedIRFile.scala @@ -0,0 +1,30 @@ +/* + * Copyright 2023 Arman Bilge + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mdoc.js.worker + +/* + Copy pasted from here + https://github.com/armanbilge/scalajs-importmap/blob/main/importmap/src/main/scala/com/armanbilge/sjsimportmap/ImportMappedIRFile.scala + */ + +import org.scalajs.linker.interface.IRFile +import org.scalajs.linker.interface.unstable.IRFileImpl + +object ImportMappedIRFile { + def fromIRFile(ir: IRFile)(mapper: String => String): IRFile = + new ImportMappedIRFileImpl(IRFileImpl.fromIRFile(ir), mapper) +} diff --git a/mdoc-js-worker/src/main/scala/ImportMappedIRFileImpl.scala b/mdoc-js-worker/src/main/scala/ImportMappedIRFileImpl.scala new file mode 100644 index 000000000..6cb4dd23a --- /dev/null +++ b/mdoc-js-worker/src/main/scala/ImportMappedIRFileImpl.scala @@ -0,0 +1,78 @@ +/* + * Copyright 2023 Arman Bilge + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mdoc.js.worker + +/* +Copy pasted from here; +https://github.com/armanbilge/scalajs-importmap/blob/main/importmap/src/main/scala/com/armanbilge/sjsimportmap/ImportMappedIRFileImpl.scala + */ + +import org.scalajs.ir.Trees +import org.scalajs.linker.interface.unstable.IRFileImpl + +import scala.concurrent.ExecutionContext +import scala.concurrent.Future + +private final class ImportMappedIRFileImpl(impl: IRFileImpl, mapper: String => String) + extends IRFileImpl(impl.path, impl.version) { + + def entryPointsInfo(implicit ec: ExecutionContext) = + impl.entryPointsInfo + + def tree(implicit ec: ExecutionContext): Future[Trees.ClassDef] = + impl.tree.map { classDef => + if (classDef.jsNativeLoadSpec.isDefined || classDef.jsNativeMembers.nonEmpty) + Trees.ClassDef( + classDef.name, + classDef.originalName, + classDef.kind, + classDef.jsClassCaptures, + classDef.superClass, + classDef.interfaces, + classDef.jsSuperClass, + classDef.jsNativeLoadSpec.map(transform), + classDef.fields, + classDef.methods, + classDef.jsConstructor, + classDef.jsMethodProps, + classDef.jsNativeMembers.map(transform), + classDef.topLevelExportDefs + )(classDef.optimizerHints)(classDef.pos) + else classDef + } + + private[this] def transform(member: Trees.JSNativeMemberDef): Trees.JSNativeMemberDef = + Trees.JSNativeMemberDef( + member.flags, + member.name, + transform(member.jsNativeLoadSpec) + )(member.pos) + + private[this] def transform(spec: Trees.JSNativeLoadSpec): Trees.JSNativeLoadSpec = + spec match { + case importSpec: Trees.JSNativeLoadSpec.Import => transform(importSpec) + case Trees.JSNativeLoadSpec.ImportWithGlobalFallback(importSpec, globalSpec) => + Trees.JSNativeLoadSpec.ImportWithGlobalFallback(transform(importSpec), globalSpec) + case other => other + } + + private[this] def transform( + importSpec: Trees.JSNativeLoadSpec.Import + ): Trees.JSNativeLoadSpec.Import = + importSpec.copy(module = mapper(importSpec.module)) + +} diff --git a/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala b/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala index 7e431a155..5cf18f542 100644 --- a/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala +++ b/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala @@ -18,11 +18,14 @@ import org.scalajs.logging.Logger import org.scalajs.logging.Level import org.scalajs.linker.standard.MemIRFileImpl import org.scalajs.linker.interface.Semantics +import scala.collection.JavaConverters._ + class ScalaJSWorker( config: ScalajsConfig, logger: Logger ) extends ScalajsWorkerApi { case class IFile(mem: IRFile) extends ScalajsWorkerApi.IRFile + val linker = { val cfg = StandardConfig() @@ -43,6 +46,12 @@ class ScalaJSWorker( StandardImpl.clearableLinker(cfg) } + lazy val remapFct = config.importMap.asScala.toSeq.foldLeft((in: String) => in) { + case (fct, (s1, s2)) => + val fct2: (String => String) = (in => in.replace(s1, s2)) + (in => fct(fct2(in))) + } + var cachedFiles = Seq.empty[org.scalajs.linker.interface.IRFile] val cache = StandardImpl.irFileCache().newCache @@ -62,10 +71,18 @@ class ScalaJSWorker( val mem = MemOutputDirectory() val report = Await.result( linker.link( - cachedFiles.toSeq ++ - in.toSeq.collect { case IFile(o) => - o - }, + ( + cachedFiles.toSeq ++ + in.toSeq + .collect { case IFile(o) => + o + } + ).map { ir => + if (config.importMap.isEmpty) + ir + else + ImportMappedIRFile.fromIRFile(ir)(remapFct) + }, Seq.empty, mem, logger diff --git a/mdoc-js-worker/src/main/scala/ScalaJSWorkerProvider.scala b/mdoc-js-worker/src/main/scala/ScalaJSWorkerProvider.scala index 84abbe256..29fc7e347 100644 --- a/mdoc-js-worker/src/main/scala/ScalaJSWorkerProvider.scala +++ b/mdoc-js-worker/src/main/scala/ScalaJSWorkerProvider.scala @@ -24,7 +24,6 @@ class ScalaJSWorkerProvider extends i.ScalajsWorkerProvider { def trace(ex: => Throwable) = logger.trace(ex) } - new ScalaJSWorker(config, wrappedLogger) } } diff --git a/mdoc-js/src/main/scala/mdoc/modifiers/JsConfig.scala b/mdoc-js/src/main/scala/mdoc/modifiers/JsConfig.scala index 0cdc7a3f9..70437f452 100644 --- a/mdoc-js/src/main/scala/mdoc/modifiers/JsConfig.scala +++ b/mdoc-js/src/main/scala/mdoc/modifiers/JsConfig.scala @@ -7,6 +7,9 @@ import scala.meta.internal.io.PathIO import scala.meta.io.AbsolutePath import scala.meta.io.Classpath import mdoc.js.interfaces._ +import scala.collection.immutable.HashMap +import com.github.plokhotnyuk.jsoniter_scala.core._ +import mdoc.modifiers.ImportMapJsonIr.ImportMap case class JsConfig( moduleKind: ModuleType = ModuleType.NoModule, @@ -20,7 +23,8 @@ case class JsConfig( fullOpt: Boolean = true, htmlPrefix: String = "", relativeLinkPrefix: String = "", - batchMode: Boolean = false + batchMode: Boolean = false, + importMap: Map[String, String] = Map.empty ) { lazy val isCommonJS: Boolean = moduleKind == ModuleType.CommonJSModule lazy val isEsModule: Boolean = moduleKind == ModuleType.ESModule @@ -99,7 +103,15 @@ object JsConfig { } }, relativeLinkPrefix = ctx.site.getOrElse("js-relative-link-prefix", base.relativeLinkPrefix), - batchMode = ctx.site.getOrElse("js-batch-mode", "false").toBoolean + batchMode = ctx.site.getOrElse("js-batch-mode", "false").toBoolean, + importMap = ctx.settings.importMapPath match { + case None => new HashMap[String, String]() + case Some(value) => + val importMapRaw = scala.io.Source.fromFile(value.toFile).getLines().mkString("\n") + val importMapParsed = readFromString[ImportMap](importMapRaw) + println(importMapParsed) + importMapParsed.imports + } ) } } diff --git a/mdoc-js/src/main/scala/mdoc/modifiers/JsImportMap.scala b/mdoc-js/src/main/scala/mdoc/modifiers/JsImportMap.scala new file mode 100644 index 000000000..ca0f4045a --- /dev/null +++ b/mdoc-js/src/main/scala/mdoc/modifiers/JsImportMap.scala @@ -0,0 +1,19 @@ +package mdoc.modifiers + +import com.github.plokhotnyuk.jsoniter_scala.core._ +import com.github.plokhotnyuk.jsoniter_scala.macros._ + +object ImportMapJsonIr { + + type Scope = Map[String, String] + + final case class ImportMap( + val imports: Map[String, String], + val scopes: Option[Map[String, Scope]] + ) + + object ImportMap { + implicit val codec: JsonValueCodec[ImportMap] = JsonCodecMaker.make + } + +} diff --git a/mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala b/mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala index afc562bf9..e2f2c7d23 100644 --- a/mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala +++ b/mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala @@ -73,7 +73,10 @@ class JsModifier extends mdoc.PreModifier { .withModuleKind(config.moduleKind) .withSourceMap(false) .withBatchMode(config.batchMode) - .withClosureCompiler(config.fullOpt) + .withClosureCompiler( + config.fullOpt && !(config.moduleKind == ModuleType.ESModule) + ) // Closure compiler dosen't work with ESModules + .withImportMap(config.importMap.asJava) } override def onLoad(ctx: OnLoadContext): Unit = { diff --git a/mdoc-js/src/main/scala/mdoc/modifiers/ScalajsClassloader.scala b/mdoc-js/src/main/scala/mdoc/modifiers/ScalajsClassloader.scala index 5f3f6e601..f20923c14 100644 --- a/mdoc-js/src/main/scala/mdoc/modifiers/ScalajsClassloader.scala +++ b/mdoc-js/src/main/scala/mdoc/modifiers/ScalajsClassloader.scala @@ -9,6 +9,8 @@ final class FilteringClassLoader(parent: ClassLoader) extends ClassLoader(parent "scala.", "org.scalajs.linker.", "org.scalajs.logging.", + "org.scalajs.", + "org.scalajs.ir.", "sun.reflect.", "jdk.internal.reflect.", "mdoc.js." diff --git a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala index e46aa0417..fa09a5b88 100644 --- a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala +++ b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala @@ -188,7 +188,15 @@ object MdocPlugin extends AutoPlugin { classpath.mkString(java.io.File.pathSeparator) ) IO.write(props, "mdoc properties", out) - List(out) + val esVersion = props.clone().asInstanceOf[java.util.Properties] + esVersion.put("js-module-kind", "ESModule") + val esOut = managedResourceDirectories.in(Compile).value.head / "es.properties" + IO.write( + esVersion, + "mdoc esmoddule properties", + esOut + ) + List(out, esOut) } ) diff --git a/mdoc/src/main/scala/mdoc/Main.scala b/mdoc/src/main/scala/mdoc/Main.scala index b1d18bd39..f48749378 100644 --- a/mdoc/src/main/scala/mdoc/Main.scala +++ b/mdoc/src/main/scala/mdoc/Main.scala @@ -36,8 +36,7 @@ trait MainProcess { process(args, new ConsoleReporter(out), cwd) } def process(args: Array[String], reporter: Reporter, cwd: Path): Int = { - val base = Settings.default(AbsolutePath(cwd)) - val ctx = Settings.fromCliArgs(args.toList, base) + val ctx = Settings.fromCliArgs(args.toList, cwd) MainOps.process(ctx, reporter) } def process(settings: MainSettings): Int = { diff --git a/mdoc/src/main/scala/mdoc/MainSettings.scala b/mdoc/src/main/scala/mdoc/MainSettings.scala index ef71b8ee2..7758efdcb 100644 --- a/mdoc/src/main/scala/mdoc/MainSettings.scala +++ b/mdoc/src/main/scala/mdoc/MainSettings.scala @@ -23,7 +23,7 @@ final class MainSettings private ( def withArgs(args: List[String]): MainSettings = { if (args.isEmpty) this else { - Settings.fromCliArgs(args, settings) match { + Settings.fromCliArgs(args, settings.cwd.toNIO) match { case Configured.Ok(newSettings) => copy(settings = newSettings) case Configured.NotOk(error) => @@ -121,12 +121,12 @@ final class MainSettings private ( } object MainSettings { - def apply(workingDirectory: Path): MainSettings = { - val settings = Settings.default(AbsolutePath(workingDirectory)) + def apply(workingDirectory: Path, propertiesFileName: String): MainSettings = { + val settings = Settings.default(AbsolutePath(workingDirectory), propertiesFileName) val reporter = ConsoleReporter.default new MainSettings(settings, reporter) } def apply(): MainSettings = { - MainSettings(PathIO.workingDirectory.toNIO) + MainSettings(PathIO.workingDirectory.toNIO, "mdoc.properties") } } diff --git a/tests/unit-js/src/test/resources/importmap.json b/tests/unit-js/src/test/resources/importmap.json new file mode 100644 index 000000000..872b43df9 --- /dev/null +++ b/tests/unit-js/src/test/resources/importmap.json @@ -0,0 +1,5 @@ +{ + "imports": { + "@stdlib/blas": "https://cdn.jsdelivr.net/npm/@stdlib/blas@0.2.1/+esm" + } +} diff --git a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala index 131cb28cf..c5d526b16 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala @@ -1,11 +1,17 @@ package tests.js +import mdoc.internal.cli.MdocProperties import mdoc.internal.cli.Settings import scala.meta.io.Classpath import tests.markdown.StringSyntax._ import tests.cli.BaseCliSuite import scala.meta.internal.io.PathIO import tests.js.JsTests.suffix +import java.io.ByteArrayOutputStream +import java.io.PrintStream +import scala.meta.internal.io.FileIO +import scala.meta.io.AbsolutePath +import java.nio.charset.StandardCharsets class JsCliSuite extends BaseCliSuite { @@ -43,4 +49,115 @@ class JsCliSuite extends BaseCliSuite { includeOutputPath = { path => !path.toNIO.getFileName.toString.endsWith(".js") } ) + checkCli( + "basic_es", + """/docs/index1.md + |```scala mdoc:js + |println("hello world!") + |``` + |/docs/index2.md + |```scala mdoc:js + |println("hello world!") + |``` + |""".stripMargin, + s"""|/docs/index1.md + |```scala + |println("hello world!") + |``` + |
+ | + | + | + | + |/docs/index2.md + |```scala + |println("hello world!") + |``` + |
+ | + |""".stripMargin, + extraArgs = Array("--prop-file-name", "es.properties"), + includeOutputPath = { path => !path.toNIO.getFileName.toString.endsWith(".js") } + ) + + /** See MdocPlugin for where the .properties file is generated. + */ + checkCli( + "basic_es", + """/docs/index1.md + |```scala mdoc:js + |println("hello world!") + |``` + |/docs/index2.md + |```scala mdoc:js + |println("hello world!") + |``` + |""".stripMargin, + s"""|/docs/index1.md + |```scala + |println("hello world!") + |``` + |
+ | + | + | + | + |/docs/index2.md + |```scala + |println("hello world!") + |``` + |
+ | + |""".stripMargin, + extraArgs = Array("--prop-file-name", "es.properties"), + includeOutputPath = { path => !path.toNIO.getFileName.toString.endsWith(".js") } + ) + + // see the importmap.json in resources + test("import remap".only) { + val myStdout = new ByteArrayOutputStream() + tests.cli.StringFS.fromString( + """|/docs/facade.md + | make a facade + |```scala mdoc:js:shared + |import scala.scalajs.js + |import scala.scalajs.js.annotation.JSImport + | + |@js.native + |@JSImport("@stdlib/blas/base", JSImport.Namespace) + |object blas extends BlasArrayOps + | + |@js.native + |trait BlasArrayOps extends js.Object{} + |``` + |Now fake do something with the facade + |```scala mdoc:js + |println(blas) + |```""".stripMargin, + in() + ) + val args = Array[String]( + "--in", + in().toString, + "--out", + out().toString, + "--cwd", + in().syntax, + "--prop-file-name", + "es.properties", + "--import-map-path", + this.getClass.getClassLoader.getResource("importmap.json").getPath + ) + println(out()) + val code = mdoc.Main.process(args, new PrintStream(myStdout), in().toNIO) + val generatedJs = AbsolutePath(out().toString + "/docs/facade.md.js") + val content = new String(FileIO.readAllBytes(generatedJs), StandardCharsets.UTF_8) + println(content) + assert( + content.contains("https://cdn.jsdelivr.net/npm/@stdlib/blas") + ) + assertEquals(code, 0, clues(myStdout)) + + } + } diff --git a/tests/unit-js/src/test/scala/tests/js/JsSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsSuite.scala index bb7429b17..977197dad 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsSuite.scala @@ -6,15 +6,18 @@ import tests.markdown.StringSyntax._ import tests.markdown.BaseMarkdownSuite import tests.js.JsTests.suffix import tests.markdown.Compat +import scala.meta.io.AbsolutePath class JsSuite extends BaseMarkdownSuite { // NOTE(olafur) Optimization. Cache settings to reuse the Scala.js compiler instance. // By default, we create new modifiers for each unit test, which is usually fast. - override lazy val baseSettings: Settings = super.baseSettings.copy( - site = super.baseSettings.site ++ Map( - "js-opt" -> "fast" + override def baseSettings(resourcePropertyFileName: String): Settings = super + .baseSettings() + .copy( + site = super.baseSettings().site ++ Map( + "js-opt" -> "fast" + ) ) - ) check( "basic", @@ -47,8 +50,37 @@ class JsSuite extends BaseMarkdownSuite { | """.stripMargin, settings = { - baseSettings.copy( - site = baseSettings.site.updated("js-module-kind", "ESModule") + baseSettings().copy( + site = baseSettings().site.updated("js-module-kind", "ESModule") + ) + } + ) + + checkCompiles( + "es_remap_settings", + """ + |```scala mdoc:js:shared + | + |import scala.scalajs.js + |import scala.scalajs.js.annotation.JSImport + | + |@js.native + |@JSImport("@stdlib/blas/base", JSImport.Namespace) + |object blas extends BlasArrayOps + | + |@js.native + |trait BlasArrayOps extends js.Object{} + |``` + | + |```scala mdoc:js + |println(blas) + |``` + |""".stripMargin, + settings = { + baseSettings().copy( + site = baseSettings().site.updated("js-module-kind", "ESModule"), + importMapPath = + Some(AbsolutePath(this.getClass.getClassLoader.getResource("importmap.json").getPath)) ) } ) @@ -324,10 +356,10 @@ class JsSuite extends BaseMarkdownSuite { | ^ """.stripMargin, settings = { - val noScalajsDom = Classpath(baseSettings.site("js-classpath")).entries + val noScalajsDom = Classpath(baseSettings().site("js-classpath")).entries .filterNot(_.toNIO.getFileName.toString.contains("scalajs-dom")) - baseSettings.copy( - site = baseSettings.site.updated("js-classpath", Classpath(noScalajsDom).syntax) + baseSettings().copy( + site = baseSettings().site.updated("js-classpath", Classpath(noScalajsDom).syntax) ) }, compat = Map( @@ -377,8 +409,8 @@ class JsSuite extends BaseMarkdownSuite { createTempFile("mdoc-library.js"), createTempFile("mdoc-library.js.map") ) - baseSettings.copy( - site = baseSettings.site + baseSettings().copy( + site = baseSettings().site .updated("js-module-kind", "CommonJSModule") .updated("js-libraries", Classpath(libraries).syntax) ) @@ -401,8 +433,8 @@ class JsSuite extends BaseMarkdownSuite { | |""".stripMargin, settings = { - baseSettings.copy( - site = baseSettings.site.updated("js-html-header", unpkgReact) + baseSettings().copy( + site = baseSettings().site.updated("js-html-header", unpkgReact) ) } ) diff --git a/tests/unit/src/main/scala/tests/markdown/BaseMarkdownSuite.scala b/tests/unit/src/main/scala/tests/markdown/BaseMarkdownSuite.scala index 098ce473e..8cdfc0571 100644 --- a/tests/unit/src/main/scala/tests/markdown/BaseMarkdownSuite.scala +++ b/tests/unit/src/main/scala/tests/markdown/BaseMarkdownSuite.scala @@ -32,16 +32,16 @@ abstract class BaseMarkdownSuite extends tests.BaseSuite { file.write("") file } - protected def baseSettings: Settings = + protected def baseSettings(resourcePropertyFileName: String = "mdoc.properties"): Settings = Settings - .default(createTempDirectory()) + .default(createTempDirectory(), resourcePropertyFileName) .copy( site = Map( "version" -> "1.0", "boom" -> "$boom" ) ) - .withProperties(MdocProperties.default(PathIO.workingDirectory)) + .withProperties(MdocProperties.default(PathIO.workingDirectory, resourcePropertyFileName)) private val myStdout = new ByteArrayOutputStream() private def newReporter(): ConsoleReporter = { @@ -60,7 +60,7 @@ abstract class BaseMarkdownSuite extends tests.BaseSuite { name: TestOptions, original: String, expected: String, - settings: Settings = baseSettings, + settings: Settings = baseSettings(), compat: Map[Compat.ScalaVersion, String] = Map.empty )(implicit loc: munit.Location): Unit = { test(name) { @@ -68,7 +68,7 @@ abstract class BaseMarkdownSuite extends tests.BaseSuite { val context = newContext(settings, reporter) val input = Input.VirtualFile(name.name.replace(':', '-') + ".md", original) val file = InputFile.fromRelativeFilename(input.path, settings) - Markdown.toMarkdown(input, context, file, baseSettings.site, reporter, settings) + Markdown.toMarkdown(input, context, file, baseSettings().site, reporter, settings) assert(reporter.hasErrors, "Expected errors but reporter.hasErrors=false") val obtainedErrors = Compat.postProcess( fansi.Str(myStdout.toString).plainText.trimLineEnds, @@ -85,7 +85,7 @@ abstract class BaseMarkdownSuite extends tests.BaseSuite { def checkCompiles( name: TestOptions, original: String, - settings: Settings = baseSettings, + settings: Settings = baseSettings(), onOutput: String => Unit = _ => () ): Unit = { test(name) { @@ -94,7 +94,7 @@ abstract class BaseMarkdownSuite extends tests.BaseSuite { val input = Input.VirtualFile(name.name.replace(":", "-") + ".md", original) val file = InputFile.fromRelativeFilename(input.path, settings) val obtained = - Markdown.toMarkdown(input, context, file, baseSettings.site, reporter, settings) + Markdown.toMarkdown(input, context, file, baseSettings().site, reporter, settings) val colorOut = myStdout.toString() print(colorOut) val stdout = fansi.Str(colorOut).plainText @@ -108,7 +108,7 @@ abstract class BaseMarkdownSuite extends tests.BaseSuite { name: TestOptions, original: String, expected: String, - settings: Settings = baseSettings, + settings: Settings = baseSettings(), compat: Map[Compat.ScalaVersion, String] = Map.empty )(implicit loc: munit.Location): Unit = { checkCompiles( From cc9a3c94d1590f9705379904ddc9d6932f3a980c Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Thu, 11 Jul 2024 18:48:44 +0200 Subject: [PATCH 54/95] remap esmodules at link time --- .DS_Store | Bin 0 -> 6148 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..51227d71a94e52683c4d00d01ac912a7dfa75f3b GIT binary patch literal 6148 zcmeHKI|>3p3{CuiU}I@HSMUad=n3`$7K)81_^Y?_TprDrPoXS!S|~4&yqQeiEc=Sh zMnrUeSOjyq1*=D6P-_S;VFyM%Fva+Zyp5TE&WLZbpy zfC^9nDnJE3tw46z(fHFB^FAs-1%6%u`#u!7VNGlU{nLTqBLHxKv>VnwO8|={fHkoV zL$0gG>bW2DSK<@<*e5h_zG?{ceokWPC@W?4D@!4jkV*c7e!sMHO^~d U8|ZZ8oet#BfayY`0^e5P0W;M1& literal 0 HcmV?d00001 From 93e344011b1366c11bbe1beb318993f519040b57 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Thu, 11 Jul 2024 19:13:07 +0200 Subject: [PATCH 55/95] . --- .../test/scala/tests/cli/CliArgsSuite.scala | 6 +-- .../scala/tests/cli/MdocPropertiesSuite.scala | 2 +- .../markdown/IndentedMarkdownSuite.scala | 10 ++-- .../tests/markdown/LinkHygieneSuite.scala | 2 +- .../markdown/MarkdownCompilerSuite.scala | 2 +- .../tests/markdown/ScastieModifierSuite.scala | 26 ++++++---- .../tests/markdown/StringModifierSuite.scala | 50 ++++++++++--------- .../tests/markdown/VariablePrinterSuite.scala | 10 ++-- 8 files changed, 57 insertions(+), 51 deletions(-) diff --git a/tests/unit/src/test/scala/tests/cli/CliArgsSuite.scala b/tests/unit/src/test/scala/tests/cli/CliArgsSuite.scala index 3430f070d..f7cb4768a 100644 --- a/tests/unit/src/test/scala/tests/cli/CliArgsSuite.scala +++ b/tests/unit/src/test/scala/tests/cli/CliArgsSuite.scala @@ -12,13 +12,13 @@ import mdoc.internal.cli.Context class CliArgsSuite extends FunSuite { private val reporter = ConsoleReporter.default - private val base = Settings.default(PathIO.workingDirectory) + // private val base = Settings.default(PathIO.workingDirectory, "mdoc.properties") def checkOK(name: String, args: List[String], onSuccess: Settings => Unit = _ => ())(implicit loc: munit.Location ): Unit = { test(name) { - val obtained = Settings.fromCliArgs(args, base).get + val obtained = Settings.fromCliArgs(args, PathIO.workingDirectory.toNIO).get onSuccess(obtained) } } @@ -28,7 +28,7 @@ class CliArgsSuite extends FunSuite { ): Unit = { test(name) { Settings - .fromCliArgs(args, base) + .fromCliArgs(args, PathIO.workingDirectory.toNIO) .andThen(s => Context.fromSettings(s, reporter)) .toEither match { case Left(obtained) => diff --git a/tests/unit/src/test/scala/tests/cli/MdocPropertiesSuite.scala b/tests/unit/src/test/scala/tests/cli/MdocPropertiesSuite.scala index 6f1f6d07c..31e4e2dff 100644 --- a/tests/unit/src/test/scala/tests/cli/MdocPropertiesSuite.scala +++ b/tests/unit/src/test/scala/tests/cli/MdocPropertiesSuite.scala @@ -7,7 +7,7 @@ import tests.markdown.Compat class MdocPropertiesSuite extends FunSuite { test("default") { - val obtained = MdocProperties.default(PathIO.workingDirectory).scalacOptions + val obtained = MdocProperties.default(PathIO.workingDirectory, "mdoc.properties").scalacOptions if (Compat.isScala3) assert(obtained.contains("-language:implicitConversions")) diff --git a/tests/unit/src/test/scala/tests/markdown/IndentedMarkdownSuite.scala b/tests/unit/src/test/scala/tests/markdown/IndentedMarkdownSuite.scala index 7316df65f..65ddac716 100644 --- a/tests/unit/src/test/scala/tests/markdown/IndentedMarkdownSuite.scala +++ b/tests/unit/src/test/scala/tests/markdown/IndentedMarkdownSuite.scala @@ -16,7 +16,7 @@ class IndentedMarkdownSuite extends BaseMarkdownSuite { | println(msg) | ``` """.stripMargin, - settings = baseSettings.copy(allowCodeFenceIndented = true) + settings = baseSettings().copy(allowCodeFenceIndented = true) ) check( @@ -33,7 +33,7 @@ class IndentedMarkdownSuite extends BaseMarkdownSuite { | println(msg) | ``` """.stripMargin, - settings = baseSettings.copy(allowCodeFenceIndented = true) + settings = baseSettings().copy(allowCodeFenceIndented = true) ) check( @@ -50,7 +50,7 @@ class IndentedMarkdownSuite extends BaseMarkdownSuite { | println(msg) | ``` """.stripMargin, - settings = baseSettings.copy(allowCodeFenceIndented = true) + settings = baseSettings().copy(allowCodeFenceIndented = true) ) check( @@ -67,7 +67,7 @@ class IndentedMarkdownSuite extends BaseMarkdownSuite { | println(msg) | ``` """.stripMargin, - settings = baseSettings.copy(allowCodeFenceIndented = true) + settings = baseSettings().copy(allowCodeFenceIndented = true) ) check( @@ -86,6 +86,6 @@ class IndentedMarkdownSuite extends BaseMarkdownSuite { | // Hello! | ``` """.stripMargin, - settings = baseSettings.copy(allowCodeFenceIndented = true) + settings = baseSettings().copy(allowCodeFenceIndented = true) ) } diff --git a/tests/unit/src/test/scala/tests/markdown/LinkHygieneSuite.scala b/tests/unit/src/test/scala/tests/markdown/LinkHygieneSuite.scala index cca95516f..7f32f40a9 100644 --- a/tests/unit/src/test/scala/tests/markdown/LinkHygieneSuite.scala +++ b/tests/unit/src/test/scala/tests/markdown/LinkHygieneSuite.scala @@ -23,7 +23,7 @@ class LinkHygieneSuite extends FunSuite { reporter.reset() val root = tests.cli.StringFS.fromString(original) val settings = Settings - .default(root) + .default(root, "mdoc.properties") .copy(reportRelativePaths = true, in = List(root), out = List(root)) val links = DocumentLinks.fromGeneratedSite(settings) LinkHygiene.report(asError, LinkHygiene.lint(links, verbose), reporter) diff --git a/tests/unit/src/test/scala/tests/markdown/MarkdownCompilerSuite.scala b/tests/unit/src/test/scala/tests/markdown/MarkdownCompilerSuite.scala index 9a3ac5f9b..a82b0c3f7 100644 --- a/tests/unit/src/test/scala/tests/markdown/MarkdownCompilerSuite.scala +++ b/tests/unit/src/test/scala/tests/markdown/MarkdownCompilerSuite.scala @@ -16,7 +16,7 @@ class MarkdownCompilerSuite extends FunSuite { private val compiler = MarkdownBuilder.default() private val reporter = ConsoleReporter.default - private val settings = Settings.default(PathIO.workingDirectory) + private val settings = Settings.default(PathIO.workingDirectory, "mdoc.properties") def checkIgnore(name: String, original: String, expected: String): Unit = test(name.ignore) {} diff --git a/tests/unit/src/test/scala/tests/markdown/ScastieModifierSuite.scala b/tests/unit/src/test/scala/tests/markdown/ScastieModifierSuite.scala index 463d9ec83..8436fa2ee 100644 --- a/tests/unit/src/test/scala/tests/markdown/ScastieModifierSuite.scala +++ b/tests/unit/src/test/scala/tests/markdown/ScastieModifierSuite.scala @@ -8,22 +8,26 @@ class ScastieModifierSuite extends BaseMarkdownSuite { private val debugClassSuffix = "test" - override val baseSettings: Settings = super.baseSettings.copy( - stringModifiers = List( - new ScastieModifier( - debugClassSuffix = Some(debugClassSuffix) + override def baseSettings(resourcePropertyFileName: String): Settings = super + .baseSettings() + .copy( + stringModifiers = List( + new ScastieModifier( + debugClassSuffix = Some(debugClassSuffix) + ) ) ) - ) - val darkThemeSettings: Settings = super.baseSettings.copy( - stringModifiers = List( - new ScastieModifier( - theme = "dark", - debugClassSuffix = Some(debugClassSuffix) + val darkThemeSettings: Settings = super + .baseSettings() + .copy( + stringModifiers = List( + new ScastieModifier( + theme = "dark", + debugClassSuffix = Some(debugClassSuffix) + ) ) ) - ) check( "inline", diff --git a/tests/unit/src/test/scala/tests/markdown/StringModifierSuite.scala b/tests/unit/src/test/scala/tests/markdown/StringModifierSuite.scala index 8118fee10..1dd4fc2f7 100644 --- a/tests/unit/src/test/scala/tests/markdown/StringModifierSuite.scala +++ b/tests/unit/src/test/scala/tests/markdown/StringModifierSuite.scala @@ -7,32 +7,34 @@ import mdoc.Reporter import mdoc.internal.cli.Settings class StringModifierSuite extends BaseMarkdownSuite { - override def baseSettings: Settings = - super.baseSettings.copy( - stringModifiers = List( - new StringModifier { - override val name: String = "hello" - override def process(info: String, code: Input, reporter: Reporter): String = { - code.text.trim + " " + info + override def baseSettings(resourcePropertyFileName: String): Settings = + super + .baseSettings() + .copy( + stringModifiers = List( + new StringModifier { + override val name: String = "hello" + override def process(info: String, code: Input, reporter: Reporter): String = { + code.text.trim + " " + info + } + }, + new StringModifier { + override val name: String = "reporter" + override def process(info: String, code: Input, reporter: Reporter): String = { + val length = code.text.trim.length + val pos = Position.Range(code, 0, length) + reporter.error(pos, "This is a message") + "reported" + } + }, + new StringModifier { + override val name: String = "exception" + override def process(info: String, code: Input, reporter: Reporter): String = { + throw new IllegalArgumentException(info) + } } - }, - new StringModifier { - override val name: String = "reporter" - override def process(info: String, code: Input, reporter: Reporter): String = { - val length = code.text.trim.length - val pos = Position.Range(code, 0, length) - reporter.error(pos, "This is a message") - "reported" - } - }, - new StringModifier { - override val name: String = "exception" - override def process(info: String, code: Input, reporter: Reporter): String = { - throw new IllegalArgumentException(info) - } - } + ) ) - ) check( "hello-world", diff --git a/tests/unit/src/test/scala/tests/markdown/VariablePrinterSuite.scala b/tests/unit/src/test/scala/tests/markdown/VariablePrinterSuite.scala index 5f7c9f5eb..671917e2c 100644 --- a/tests/unit/src/test/scala/tests/markdown/VariablePrinterSuite.scala +++ b/tests/unit/src/test/scala/tests/markdown/VariablePrinterSuite.scala @@ -20,7 +20,7 @@ class VariablePrinterSuite extends BaseMarkdownSuite { |// b: Int = 2 |``` """.stripMargin, - baseSettings + baseSettings() ) check( @@ -38,10 +38,10 @@ class VariablePrinterSuite extends BaseMarkdownSuite { |val c = 30 |``` """.stripMargin, - baseSettings + baseSettings() ) - val trailingComment = baseSettings.copy(variablePrinter = { variable => + val trailingComment = baseSettings().copy(variablePrinter = { variable => variable.runtimeValue match { case n: Int if variable.totalVariablesInStatement == 1 => s" // Number($n)" case _ => variable.toString @@ -69,7 +69,7 @@ class VariablePrinterSuite extends BaseMarkdownSuite { trailingComment ) - val lastStatementOnly = baseSettings.copy(variablePrinter = { variable => + val lastStatementOnly = baseSettings().copy(variablePrinter = { variable => if (variable.indexOfStatementInCodeFence + 1 == variable.totalStatementsInCodeFence) { variable.toString } else { @@ -97,7 +97,7 @@ class VariablePrinterSuite extends BaseMarkdownSuite { lastStatementOnly ) - val initialOffset = baseSettings.copy( + val initialOffset = baseSettings().copy( variablePrinter = new ReplVariablePrinter(leadingNewline = true, width = 25, height = 20, indent = 2) ) From 58349ce35391b42c19dc600914071b98302dd8da Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Thu, 11 Jul 2024 19:14:04 +0200 Subject: [PATCH 56/95] . --- tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala index c5d526b16..9172e45ae 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala @@ -152,7 +152,6 @@ class JsCliSuite extends BaseCliSuite { val code = mdoc.Main.process(args, new PrintStream(myStdout), in().toNIO) val generatedJs = AbsolutePath(out().toString + "/docs/facade.md.js") val content = new String(FileIO.readAllBytes(generatedJs), StandardCharsets.UTF_8) - println(content) assert( content.contains("https://cdn.jsdelivr.net/npm/@stdlib/blas") ) From 27806fb92e07cea9ff9c1548e90df91f8c899519 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Thu, 11 Jul 2024 19:29:34 +0200 Subject: [PATCH 57/95] Fix line no --- .../src/test/scala/tests/markdown/StringModifierSuite.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/src/test/scala/tests/markdown/StringModifierSuite.scala b/tests/unit/src/test/scala/tests/markdown/StringModifierSuite.scala index 1dd4fc2f7..9da0e46f1 100644 --- a/tests/unit/src/test/scala/tests/markdown/StringModifierSuite.scala +++ b/tests/unit/src/test/scala/tests/markdown/StringModifierSuite.scala @@ -75,7 +75,7 @@ class StringModifierSuite extends BaseMarkdownSuite { |^^^^^ |mdoc.internal.markdown.ModifierException: mdoc:exception exception |Caused by: java.lang.IllegalArgumentException: boom - | at tests.markdown.StringModifierSuite$$anon$3.process(StringModifierSuite.scala:31) + | at tests.markdown.StringModifierSuite$$anon$3.process(StringModifierSuite.scala:33) """.stripMargin ) From 596d7c0a0cd278c41a71613063af73ed369a343f Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Thu, 11 Jul 2024 19:30:38 +0200 Subject: [PATCH 58/95] remove only --- tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala index 9172e45ae..93a2bc3d1 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala @@ -114,7 +114,7 @@ class JsCliSuite extends BaseCliSuite { ) // see the importmap.json in resources - test("import remap".only) { + test("import remap") { val myStdout = new ByteArrayOutputStream() tests.cli.StringFS.fromString( """|/docs/facade.md From 96dabd0c67372758a45095804d9061d351bf0969 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Thu, 11 Jul 2024 21:35:31 +0200 Subject: [PATCH 59/95] So close... --- build.sbt | 5 +- .../src/main/scala/ImportMappedIRFile.scala | 52 +++---- .../main/scala/ImportMappedIRFileImpl.scala | 134 +++++++++--------- .../src/main/scala/ScalaJSWorker.scala | 1 + .../main/scala/mdoc/modifiers/JsConfig.scala | 1 - mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala | 9 +- .../src/test/scala/tests/js/JsCliSuite.scala | 31 ---- 7 files changed, 105 insertions(+), 128 deletions(-) diff --git a/build.sbt b/build.sbt index 47f2d849a..324bfa84e 100644 --- a/build.sbt +++ b/build.sbt @@ -437,7 +437,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" % Provided cross CrossVersion.for3Use2_13 + ) ) lazy val js = project diff --git a/mdoc-js-worker/src/main/scala/ImportMappedIRFile.scala b/mdoc-js-worker/src/main/scala/ImportMappedIRFile.scala index 5ff419154..91b749ed5 100644 --- a/mdoc-js-worker/src/main/scala/ImportMappedIRFile.scala +++ b/mdoc-js-worker/src/main/scala/ImportMappedIRFile.scala @@ -1,30 +1,30 @@ -/* - * Copyright 2023 Arman Bilge - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// /* +// * Copyright 2023 Arman Bilge +// * +// * Licensed under the Apache License, Version 2.0 (the "License"); +// * you may not use this file except in compliance with the License. +// * You may obtain a copy of the License at +// * +// * http://www.apache.org/licenses/LICENSE-2.0 +// * +// * Unless required by applicable law or agreed to in writing, software +// * distributed under the License is distributed on an "AS IS" BASIS, +// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// * See the License for the specific language governing permissions and +// * limitations under the License. +// */ -package mdoc.js.worker +// package mdoc.js.worker -/* - Copy pasted from here - https://github.com/armanbilge/scalajs-importmap/blob/main/importmap/src/main/scala/com/armanbilge/sjsimportmap/ImportMappedIRFile.scala - */ +// /* +// Copy pasted from here +// https://github.com/armanbilge/scalajs-importmap/blob/main/importmap/src/main/scala/com/armanbilge/sjsimportmap/ImportMappedIRFile.scala +// */ -import org.scalajs.linker.interface.IRFile -import org.scalajs.linker.interface.unstable.IRFileImpl +// import org.scalajs.linker.interface.IRFile +// import org.scalajs.linker.interface.unstable.IRFileImpl -object ImportMappedIRFile { - def fromIRFile(ir: IRFile)(mapper: String => String): IRFile = - new ImportMappedIRFileImpl(IRFileImpl.fromIRFile(ir), mapper) -} +// object ImportMappedIRFile { +// def fromIRFile(ir: IRFile)(mapper: String => String): IRFile = +// new ImportMappedIRFileImpl(IRFileImpl.fromIRFile(ir), mapper) +// } diff --git a/mdoc-js-worker/src/main/scala/ImportMappedIRFileImpl.scala b/mdoc-js-worker/src/main/scala/ImportMappedIRFileImpl.scala index 6cb4dd23a..6a190c8a5 100644 --- a/mdoc-js-worker/src/main/scala/ImportMappedIRFileImpl.scala +++ b/mdoc-js-worker/src/main/scala/ImportMappedIRFileImpl.scala @@ -1,78 +1,78 @@ -/* - * Copyright 2023 Arman Bilge - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// /* +// * Copyright 2023 Arman Bilge +// * +// * Licensed under the Apache License, Version 2.0 (the "License"); +// * you may not use this file except in compliance with the License. +// * You may obtain a copy of the License at +// * +// * http://www.apache.org/licenses/LICENSE-2.0 +// * +// * Unless required by applicable law or agreed to in writing, software +// * distributed under the License is distributed on an "AS IS" BASIS, +// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// * See the License for the specific language governing permissions and +// * limitations under the License. +// */ -package mdoc.js.worker +// package mdoc.js.worker -/* -Copy pasted from here; -https://github.com/armanbilge/scalajs-importmap/blob/main/importmap/src/main/scala/com/armanbilge/sjsimportmap/ImportMappedIRFileImpl.scala - */ +// /* +// Copy pasted from here; +// https://github.com/armanbilge/scalajs-importmap/blob/main/importmap/src/main/scala/com/armanbilge/sjsimportmap/ImportMappedIRFileImpl.scala +// */ -import org.scalajs.ir.Trees -import org.scalajs.linker.interface.unstable.IRFileImpl +// import org.scalajs.ir.Trees +// import org.scalajs.linker.interface.unstable.IRFileImpl -import scala.concurrent.ExecutionContext -import scala.concurrent.Future +// import scala.concurrent.ExecutionContext +// import scala.concurrent.Future -private final class ImportMappedIRFileImpl(impl: IRFileImpl, mapper: String => String) - extends IRFileImpl(impl.path, impl.version) { +// private final class ImportMappedIRFileImpl(impl: IRFileImpl, mapper: String => String) +// extends IRFileImpl(impl.path, impl.version) { - def entryPointsInfo(implicit ec: ExecutionContext) = - impl.entryPointsInfo +// def entryPointsInfo(implicit ec: ExecutionContext) = +// impl.entryPointsInfo - def tree(implicit ec: ExecutionContext): Future[Trees.ClassDef] = - impl.tree.map { classDef => - if (classDef.jsNativeLoadSpec.isDefined || classDef.jsNativeMembers.nonEmpty) - Trees.ClassDef( - classDef.name, - classDef.originalName, - classDef.kind, - classDef.jsClassCaptures, - classDef.superClass, - classDef.interfaces, - classDef.jsSuperClass, - classDef.jsNativeLoadSpec.map(transform), - classDef.fields, - classDef.methods, - classDef.jsConstructor, - classDef.jsMethodProps, - classDef.jsNativeMembers.map(transform), - classDef.topLevelExportDefs - )(classDef.optimizerHints)(classDef.pos) - else classDef - } +// def tree(implicit ec: ExecutionContext): Future[Trees.ClassDef] = +// impl.tree.map { classDef => +// if (classDef.jsNativeLoadSpec.isDefined || classDef.jsNativeMembers.nonEmpty) +// Trees.ClassDef( +// classDef.name, +// classDef.originalName, +// classDef.kind, +// classDef.jsClassCaptures, +// classDef.superClass, +// classDef.interfaces, +// classDef.jsSuperClass, +// classDef.jsNativeLoadSpec.map(transform), +// classDef.fields, +// classDef.methods, +// classDef.jsConstructor, +// classDef.jsMethodProps, +// classDef.jsNativeMembers.map(transform), +// classDef.topLevelExportDefs +// )(classDef.optimizerHints)(classDef.pos) +// else classDef +// } - private[this] def transform(member: Trees.JSNativeMemberDef): Trees.JSNativeMemberDef = - Trees.JSNativeMemberDef( - member.flags, - member.name, - transform(member.jsNativeLoadSpec) - )(member.pos) +// private[this] def transform(member: Trees.JSNativeMemberDef): Trees.JSNativeMemberDef = +// Trees.JSNativeMemberDef( +// member.flags, +// member.name, +// transform(member.jsNativeLoadSpec) +// )(member.pos) - private[this] def transform(spec: Trees.JSNativeLoadSpec): Trees.JSNativeLoadSpec = - spec match { - case importSpec: Trees.JSNativeLoadSpec.Import => transform(importSpec) - case Trees.JSNativeLoadSpec.ImportWithGlobalFallback(importSpec, globalSpec) => - Trees.JSNativeLoadSpec.ImportWithGlobalFallback(transform(importSpec), globalSpec) - case other => other - } +// private[this] def transform(spec: Trees.JSNativeLoadSpec): Trees.JSNativeLoadSpec = +// spec match { +// case importSpec: Trees.JSNativeLoadSpec.Import => transform(importSpec) +// case Trees.JSNativeLoadSpec.ImportWithGlobalFallback(importSpec, globalSpec) => +// Trees.JSNativeLoadSpec.ImportWithGlobalFallback(transform(importSpec), globalSpec) +// case other => other +// } - private[this] def transform( - importSpec: Trees.JSNativeLoadSpec.Import - ): Trees.JSNativeLoadSpec.Import = - importSpec.copy(module = mapper(importSpec.module)) +// private[this] def transform( +// importSpec: Trees.JSNativeLoadSpec.Import +// ): Trees.JSNativeLoadSpec.Import = +// importSpec.copy(module = mapper(importSpec.module)) -} +// } diff --git a/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala b/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala index 5cf18f542..9b68d85c7 100644 --- a/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala +++ b/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala @@ -19,6 +19,7 @@ import org.scalajs.logging.Level import org.scalajs.linker.standard.MemIRFileImpl import org.scalajs.linker.interface.Semantics import scala.collection.JavaConverters._ +import com.armanbilge.sjsimportmap.ImportMappedIRFile class ScalaJSWorker( config: ScalajsConfig, diff --git a/mdoc-js/src/main/scala/mdoc/modifiers/JsConfig.scala b/mdoc-js/src/main/scala/mdoc/modifiers/JsConfig.scala index 70437f452..969735a90 100644 --- a/mdoc-js/src/main/scala/mdoc/modifiers/JsConfig.scala +++ b/mdoc-js/src/main/scala/mdoc/modifiers/JsConfig.scala @@ -109,7 +109,6 @@ object JsConfig { case Some(value) => val importMapRaw = scala.io.Source.fromFile(value.toFile).getLines().mkString("\n") val importMapParsed = readFromString[ImportMap](importMapRaw) - println(importMapParsed) importMapParsed.imports } ) diff --git a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala index fa09a5b88..83584ee5d 100644 --- a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala +++ b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala @@ -157,6 +157,11 @@ object MdocPlugin extends AutoPlugin { case other => "org.scala-js" % s"scalajs-linker_$other" % sjsVersion } + val importMapDependency = binaryVersion match { + case "3" => "com.armanbilge" % "scalajs-importmap_2.13" % "0.1.1" + case other => "com.armanbilge" % s"scalajs-importmap_$other" % "0.1.1" + } + val mdocJSDependency = binaryVersion match { case "3" => "org.scalameta" % "mdoc-js-worker_3" % BuildInfo.version case other => "org.scalameta" % s"mdoc-js-worker_$other" % BuildInfo.version @@ -166,8 +171,8 @@ object MdocPlugin extends AutoPlugin { MdocJSConfiguration( scalacOptions = options.options, - compileClasspath = options.classpath, - linkerClassPath = getJars(linkerDependency) ++ workerClasspath, + compileClasspath = options.classpath , + linkerClassPath = getJars(linkerDependency) ++ workerClasspath ++ getJars(importMapDependency), moduleKind = options.moduleKind, jsLibraries = libraries ).writeTo(props) diff --git a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala index 93a2bc3d1..f6db8fb6a 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala @@ -82,36 +82,6 @@ class JsCliSuite extends BaseCliSuite { /** See MdocPlugin for where the .properties file is generated. */ - checkCli( - "basic_es", - """/docs/index1.md - |```scala mdoc:js - |println("hello world!") - |``` - |/docs/index2.md - |```scala mdoc:js - |println("hello world!") - |``` - |""".stripMargin, - s"""|/docs/index1.md - |```scala - |println("hello world!") - |``` - |
- | - | - | - | - |/docs/index2.md - |```scala - |println("hello world!") - |``` - |
- | - |""".stripMargin, - extraArgs = Array("--prop-file-name", "es.properties"), - includeOutputPath = { path => !path.toNIO.getFileName.toString.endsWith(".js") } - ) // see the importmap.json in resources test("import remap") { @@ -148,7 +118,6 @@ class JsCliSuite extends BaseCliSuite { "--import-map-path", this.getClass.getClassLoader.getResource("importmap.json").getPath ) - println(out()) val code = mdoc.Main.process(args, new PrintStream(myStdout), in().toNIO) val generatedJs = AbsolutePath(out().toString + "/docs/facade.md.js") val content = new String(FileIO.readAllBytes(generatedJs), StandardCharsets.UTF_8) From c4ebc980d6e25b05c434e91dc09700de865a6750 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Fri, 12 Jul 2024 19:31:10 +0200 Subject: [PATCH 60/95] Try snapshot --- build.sbt | 3 ++- mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala | 5 +++-- project/plugins.sbt | 2 ++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/build.sbt b/build.sbt index 324bfa84e..7477ed23d 100644 --- a/build.sbt +++ b/build.sbt @@ -93,6 +93,7 @@ inThisBuild( ), testFrameworks := List(new TestFramework("munit.Framework")), resolvers += Resolver.sonatypeRepo("public"), + resolvers += "Sonatype OSS Snapshots" at "https://s01.oss.sonatype.org/content/repositories/snapshots", // faster publishLocal: packageDoc / publishArtifact := "true" == System.getenv("CI"), packageSrc / publishArtifact := "true" == System.getenv("CI"), @@ -439,7 +440,7 @@ lazy val jsWorker = moduleName := "mdoc-js-worker", libraryDependencies ++= Seq( "org.scala-js" %% "scalajs-linker" % scalaJSVersion % Provided cross CrossVersion.for3Use2_13, - "com.armanbilge" %% "scalajs-importmap" % "0.1.1" % Provided cross CrossVersion.for3Use2_13 + "com.armanbilge" %% "scalajs-importmap" % "0.1.1" cross CrossVersion.for3Use2_13 ) ) diff --git a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala index 83584ee5d..4b3db01bd 100644 --- a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala +++ b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala @@ -6,6 +6,7 @@ import sbt._ import scala.collection.mutable.ListBuffer object MdocPlugin extends AutoPlugin { + resolvers += "Sonatype OSS Snapshots" at "https://s01.oss.sonatype.org/content/repositories/snapshots" object autoImport { val mdoc = inputKey[Unit]( @@ -158,8 +159,8 @@ object MdocPlugin extends AutoPlugin { } val importMapDependency = binaryVersion match { - case "3" => "com.armanbilge" % "scalajs-importmap_2.13" % "0.1.1" - case other => "com.armanbilge" % s"scalajs-importmap_$other" % "0.1.1" + case "3" => "com.armanbilge" % "scalajs-importmap_2.13" % "0.1.1-4-cf4739e-SNAPSHOT" + case other => "com.armanbilge" % s"scalajs-importmap_$other" % "0.1.1-4-cf4739e-SNAPSHOT" } val mdocJSDependency = binaryVersion match { diff --git a/project/plugins.sbt b/project/plugins.sbt index 902e1bf4d..a1799e607 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -1,3 +1,5 @@ +resolvers += "Sonatype OSS Snapshots" at "https://s01.oss.sonatype.org/content/repositories/snapshots" + addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.12.0") addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.6.1") From eb73393da95396b638641bcbc8612296bcc2a968 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Fri, 12 Jul 2024 19:37:02 +0200 Subject: [PATCH 61/95] fmt --- mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala index 4b3db01bd..d5af552b6 100644 --- a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala +++ b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala @@ -160,7 +160,8 @@ object MdocPlugin extends AutoPlugin { val importMapDependency = binaryVersion match { case "3" => "com.armanbilge" % "scalajs-importmap_2.13" % "0.1.1-4-cf4739e-SNAPSHOT" - case other => "com.armanbilge" % s"scalajs-importmap_$other" % "0.1.1-4-cf4739e-SNAPSHOT" + case other => + "com.armanbilge" % s"scalajs-importmap_$other" % "0.1.1-4-cf4739e-SNAPSHOT" } val mdocJSDependency = binaryVersion match { @@ -172,8 +173,9 @@ object MdocPlugin extends AutoPlugin { MdocJSConfiguration( scalacOptions = options.options, - compileClasspath = options.classpath , - linkerClassPath = getJars(linkerDependency) ++ workerClasspath ++ getJars(importMapDependency), + compileClasspath = options.classpath, + linkerClassPath = + getJars(linkerDependency) ++ workerClasspath ++ getJars(importMapDependency), moduleKind = options.moduleKind, jsLibraries = libraries ).writeTo(props) From a7f4f02e5690dfd8c42d1d24ce51b7fdebd150b9 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Fri, 12 Jul 2024 20:34:02 +0200 Subject: [PATCH 62/95] Pretty please work with a cherry on top --- build.sbt | 1 - .../src/main/scala/ImportMappedIRFile.scala | 30 ------- .../main/scala/ImportMappedIRFileImpl.scala | 78 ------------------- .../main/scala/ScalaJSWorkerProvider.scala | 1 + .../mdoc/modifiers/ScalajsClassloader.scala | 2 - mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala | 7 +- project/plugins.sbt | 2 - 7 files changed, 4 insertions(+), 117 deletions(-) delete mode 100644 mdoc-js-worker/src/main/scala/ImportMappedIRFile.scala delete mode 100644 mdoc-js-worker/src/main/scala/ImportMappedIRFileImpl.scala diff --git a/build.sbt b/build.sbt index 7477ed23d..2a413832c 100644 --- a/build.sbt +++ b/build.sbt @@ -93,7 +93,6 @@ inThisBuild( ), testFrameworks := List(new TestFramework("munit.Framework")), resolvers += Resolver.sonatypeRepo("public"), - resolvers += "Sonatype OSS Snapshots" at "https://s01.oss.sonatype.org/content/repositories/snapshots", // faster publishLocal: packageDoc / publishArtifact := "true" == System.getenv("CI"), packageSrc / publishArtifact := "true" == System.getenv("CI"), diff --git a/mdoc-js-worker/src/main/scala/ImportMappedIRFile.scala b/mdoc-js-worker/src/main/scala/ImportMappedIRFile.scala deleted file mode 100644 index 91b749ed5..000000000 --- a/mdoc-js-worker/src/main/scala/ImportMappedIRFile.scala +++ /dev/null @@ -1,30 +0,0 @@ -// /* -// * Copyright 2023 Arman Bilge -// * -// * Licensed under the Apache License, Version 2.0 (the "License"); -// * you may not use this file except in compliance with the License. -// * You may obtain a copy of the License at -// * -// * http://www.apache.org/licenses/LICENSE-2.0 -// * -// * Unless required by applicable law or agreed to in writing, software -// * distributed under the License is distributed on an "AS IS" BASIS, -// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// * See the License for the specific language governing permissions and -// * limitations under the License. -// */ - -// package mdoc.js.worker - -// /* -// Copy pasted from here -// https://github.com/armanbilge/scalajs-importmap/blob/main/importmap/src/main/scala/com/armanbilge/sjsimportmap/ImportMappedIRFile.scala -// */ - -// import org.scalajs.linker.interface.IRFile -// import org.scalajs.linker.interface.unstable.IRFileImpl - -// object ImportMappedIRFile { -// def fromIRFile(ir: IRFile)(mapper: String => String): IRFile = -// new ImportMappedIRFileImpl(IRFileImpl.fromIRFile(ir), mapper) -// } diff --git a/mdoc-js-worker/src/main/scala/ImportMappedIRFileImpl.scala b/mdoc-js-worker/src/main/scala/ImportMappedIRFileImpl.scala deleted file mode 100644 index 6a190c8a5..000000000 --- a/mdoc-js-worker/src/main/scala/ImportMappedIRFileImpl.scala +++ /dev/null @@ -1,78 +0,0 @@ -// /* -// * Copyright 2023 Arman Bilge -// * -// * Licensed under the Apache License, Version 2.0 (the "License"); -// * you may not use this file except in compliance with the License. -// * You may obtain a copy of the License at -// * -// * http://www.apache.org/licenses/LICENSE-2.0 -// * -// * Unless required by applicable law or agreed to in writing, software -// * distributed under the License is distributed on an "AS IS" BASIS, -// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// * See the License for the specific language governing permissions and -// * limitations under the License. -// */ - -// package mdoc.js.worker - -// /* -// Copy pasted from here; -// https://github.com/armanbilge/scalajs-importmap/blob/main/importmap/src/main/scala/com/armanbilge/sjsimportmap/ImportMappedIRFileImpl.scala -// */ - -// import org.scalajs.ir.Trees -// import org.scalajs.linker.interface.unstable.IRFileImpl - -// import scala.concurrent.ExecutionContext -// import scala.concurrent.Future - -// private final class ImportMappedIRFileImpl(impl: IRFileImpl, mapper: String => String) -// extends IRFileImpl(impl.path, impl.version) { - -// def entryPointsInfo(implicit ec: ExecutionContext) = -// impl.entryPointsInfo - -// def tree(implicit ec: ExecutionContext): Future[Trees.ClassDef] = -// impl.tree.map { classDef => -// if (classDef.jsNativeLoadSpec.isDefined || classDef.jsNativeMembers.nonEmpty) -// Trees.ClassDef( -// classDef.name, -// classDef.originalName, -// classDef.kind, -// classDef.jsClassCaptures, -// classDef.superClass, -// classDef.interfaces, -// classDef.jsSuperClass, -// classDef.jsNativeLoadSpec.map(transform), -// classDef.fields, -// classDef.methods, -// classDef.jsConstructor, -// classDef.jsMethodProps, -// classDef.jsNativeMembers.map(transform), -// classDef.topLevelExportDefs -// )(classDef.optimizerHints)(classDef.pos) -// else classDef -// } - -// private[this] def transform(member: Trees.JSNativeMemberDef): Trees.JSNativeMemberDef = -// Trees.JSNativeMemberDef( -// member.flags, -// member.name, -// transform(member.jsNativeLoadSpec) -// )(member.pos) - -// private[this] def transform(spec: Trees.JSNativeLoadSpec): Trees.JSNativeLoadSpec = -// spec match { -// case importSpec: Trees.JSNativeLoadSpec.Import => transform(importSpec) -// case Trees.JSNativeLoadSpec.ImportWithGlobalFallback(importSpec, globalSpec) => -// Trees.JSNativeLoadSpec.ImportWithGlobalFallback(transform(importSpec), globalSpec) -// case other => other -// } - -// private[this] def transform( -// importSpec: Trees.JSNativeLoadSpec.Import -// ): Trees.JSNativeLoadSpec.Import = -// importSpec.copy(module = mapper(importSpec.module)) - -// } diff --git a/mdoc-js-worker/src/main/scala/ScalaJSWorkerProvider.scala b/mdoc-js-worker/src/main/scala/ScalaJSWorkerProvider.scala index 29fc7e347..84abbe256 100644 --- a/mdoc-js-worker/src/main/scala/ScalaJSWorkerProvider.scala +++ b/mdoc-js-worker/src/main/scala/ScalaJSWorkerProvider.scala @@ -24,6 +24,7 @@ class ScalaJSWorkerProvider extends i.ScalajsWorkerProvider { def trace(ex: => Throwable) = logger.trace(ex) } + new ScalaJSWorker(config, wrappedLogger) } } diff --git a/mdoc-js/src/main/scala/mdoc/modifiers/ScalajsClassloader.scala b/mdoc-js/src/main/scala/mdoc/modifiers/ScalajsClassloader.scala index f20923c14..5f3f6e601 100644 --- a/mdoc-js/src/main/scala/mdoc/modifiers/ScalajsClassloader.scala +++ b/mdoc-js/src/main/scala/mdoc/modifiers/ScalajsClassloader.scala @@ -9,8 +9,6 @@ final class FilteringClassLoader(parent: ClassLoader) extends ClassLoader(parent "scala.", "org.scalajs.linker.", "org.scalajs.logging.", - "org.scalajs.", - "org.scalajs.ir.", "sun.reflect.", "jdk.internal.reflect.", "mdoc.js." diff --git a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala index d5af552b6..fe4a6ae8a 100644 --- a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala +++ b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala @@ -6,7 +6,6 @@ import sbt._ import scala.collection.mutable.ListBuffer object MdocPlugin extends AutoPlugin { - resolvers += "Sonatype OSS Snapshots" at "https://s01.oss.sonatype.org/content/repositories/snapshots" object autoImport { val mdoc = inputKey[Unit]( @@ -159,9 +158,9 @@ object MdocPlugin extends AutoPlugin { } val importMapDependency = binaryVersion match { - case "3" => "com.armanbilge" % "scalajs-importmap_2.13" % "0.1.1-4-cf4739e-SNAPSHOT" + case "3" => "com.armanbilge" % "scalajs-importmap_2.13" % "0.1.1" case other => - "com.armanbilge" % s"scalajs-importmap_$other" % "0.1.1-4-cf4739e-SNAPSHOT" + "com.armanbilge" % s"scalajs-importmap_$other" % "0.1.1" } val mdocJSDependency = binaryVersion match { @@ -204,7 +203,7 @@ object MdocPlugin extends AutoPlugin { "mdoc esmoddule properties", esOut ) - List(out, esOut) + List(out, esOut) // Both of these are used in the JsCliSuite Integration tests } ) diff --git a/project/plugins.sbt b/project/plugins.sbt index a1799e607..902e1bf4d 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -1,5 +1,3 @@ -resolvers += "Sonatype OSS Snapshots" at "https://s01.oss.sonatype.org/content/repositories/snapshots" - addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.12.0") addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.6.1") From 2872fb9c4ef3275a8a2d7200d95289eeb37833bc Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Fri, 12 Jul 2024 20:51:39 +0200 Subject: [PATCH 63/95] . --- tests/unit-js/src/test/scala/tests/js/JsSuite.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit-js/src/test/scala/tests/js/JsSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsSuite.scala index 977197dad..cbd7d72f3 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsSuite.scala @@ -80,7 +80,7 @@ class JsSuite extends BaseMarkdownSuite { baseSettings().copy( site = baseSettings().site.updated("js-module-kind", "ESModule"), importMapPath = - Some(AbsolutePath(this.getClass.getClassLoader.getResource("importmap.json").getPath)) + Some(AbsolutePath(this.getClass.getClassLoader.getResource("importmap.json").getPath.toNIO)) ) } ) From 0d2ed3abd7ab4ea0d16b39ab163b3b1f18030ae3 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Fri, 12 Jul 2024 20:53:40 +0200 Subject: [PATCH 64/95] fmt --- tests/unit-js/src/test/scala/tests/js/JsSuite.scala | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/unit-js/src/test/scala/tests/js/JsSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsSuite.scala index cbd7d72f3..3991f5e1c 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsSuite.scala @@ -79,8 +79,9 @@ class JsSuite extends BaseMarkdownSuite { settings = { baseSettings().copy( site = baseSettings().site.updated("js-module-kind", "ESModule"), - importMapPath = - Some(AbsolutePath(this.getClass.getClassLoader.getResource("importmap.json").getPath.toNIO)) + importMapPath = Some( + AbsolutePath(this.getClass.getClassLoader.getResource("importmap.json").getPath.toNIO) + ) ) } ) From 33e3f31c59ebedeb59296309f0439acfe9d704bb Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Fri, 12 Jul 2024 21:00:07 +0200 Subject: [PATCH 65/95] . --- tests/unit-js/src/test/scala/tests/js/JsSuite.scala | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/unit-js/src/test/scala/tests/js/JsSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsSuite.scala index 3991f5e1c..07db6c4ba 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsSuite.scala @@ -7,6 +7,7 @@ import tests.markdown.BaseMarkdownSuite import tests.js.JsTests.suffix import tests.markdown.Compat import scala.meta.io.AbsolutePath +import java.nio.file.Paths; class JsSuite extends BaseMarkdownSuite { // NOTE(olafur) Optimization. Cache settings to reuse the Scala.js compiler instance. @@ -80,7 +81,11 @@ class JsSuite extends BaseMarkdownSuite { baseSettings().copy( site = baseSettings().site.updated("js-module-kind", "ESModule"), importMapPath = Some( - AbsolutePath(this.getClass.getClassLoader.getResource("importmap.json").getPath.toNIO) + AbsolutePath( + Paths + .get(this.getClass.getClassLoader.getResource("importmap.json").getPath()) + .toAbsolutePath() + ) ) ) } From a1f5559215d329481478ba8f0aee147a02af4489 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Fri, 12 Jul 2024 21:08:28 +0200 Subject: [PATCH 66/95] . --- tests/unit-js/src/test/scala/tests/js/JsSuite.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit-js/src/test/scala/tests/js/JsSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsSuite.scala index 07db6c4ba..b95667c97 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsSuite.scala @@ -83,7 +83,7 @@ class JsSuite extends BaseMarkdownSuite { importMapPath = Some( AbsolutePath( Paths - .get(this.getClass.getClassLoader.getResource("importmap.json").getPath()) + .get(this.getClass.getClassLoader.getResource("importmap.json").toURI().getPath()) .toAbsolutePath() ) ) From dc9b6ef5d2253933bbb5dbd11c5a9ba0f2dbd455 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Fri, 12 Jul 2024 21:18:44 +0200 Subject: [PATCH 67/95] . --- tests/unit-js/src/test/scala/tests/js/JsSuite.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit-js/src/test/scala/tests/js/JsSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsSuite.scala index b95667c97..803fd0af7 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsSuite.scala @@ -83,7 +83,7 @@ class JsSuite extends BaseMarkdownSuite { importMapPath = Some( AbsolutePath( Paths - .get(this.getClass.getClassLoader.getResource("importmap.json").toURI().getPath()) + .get(this.getClass.getClassLoader.getResource("importmap.json").toURI()) .toAbsolutePath() ) ) From 6df4338cf5a436deb17b1d0f4314e89484a2d3f6 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Fri, 12 Jul 2024 21:26:01 +0200 Subject: [PATCH 68/95] windows paths --- tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala index f6db8fb6a..47be0d968 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala @@ -119,7 +119,7 @@ class JsCliSuite extends BaseCliSuite { this.getClass.getClassLoader.getResource("importmap.json").getPath ) val code = mdoc.Main.process(args, new PrintStream(myStdout), in().toNIO) - val generatedJs = AbsolutePath(out().toString + "/docs/facade.md.js") + val generatedJs = AbsolutePath(out().toNIO.resolve("docs").resolve("facade.md.js")) val content = new String(FileIO.readAllBytes(generatedJs), StandardCharsets.UTF_8) assert( content.contains("https://cdn.jsdelivr.net/npm/@stdlib/blas") From 3a4765c3b054f14e26c77c74b3e42c1fe9c9c2e5 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Fri, 12 Jul 2024 21:49:29 +0200 Subject: [PATCH 69/95] windows, finally? --- tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala index 47be0d968..772449ad0 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala @@ -12,6 +12,7 @@ import java.io.PrintStream import scala.meta.internal.io.FileIO import scala.meta.io.AbsolutePath import java.nio.charset.StandardCharsets +import java.nio.file.Paths class JsCliSuite extends BaseCliSuite { @@ -116,10 +117,11 @@ class JsCliSuite extends BaseCliSuite { "--prop-file-name", "es.properties", "--import-map-path", - this.getClass.getClassLoader.getResource("importmap.json").getPath + Paths.get(this.getClass.getClassLoader.getResource("importmap.json").toURI).toString() ) val code = mdoc.Main.process(args, new PrintStream(myStdout), in().toNIO) - val generatedJs = AbsolutePath(out().toNIO.resolve("docs").resolve("facade.md.js")) + val generatedJs = + AbsolutePath(out().toNIO.resolve("docs").resolve("facade.md.js")) val content = new String(FileIO.readAllBytes(generatedJs), StandardCharsets.UTF_8) assert( content.contains("https://cdn.jsdelivr.net/npm/@stdlib/blas") From cb1e50cba3b916259140c0cf97376d943b35c18d Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Tue, 16 Jul 2024 14:09:17 +0200 Subject: [PATCH 70/95] Update mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala Co-authored-by: Tomasz Godzik --- mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala b/mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala index e2f2c7d23..1a1582dfe 100644 --- a/mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala +++ b/mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala @@ -75,7 +75,7 @@ class JsModifier extends mdoc.PreModifier { .withBatchMode(config.batchMode) .withClosureCompiler( config.fullOpt && !(config.moduleKind == ModuleType.ESModule) - ) // Closure compiler dosen't work with ESModules + ) // Closure compiler doesn't work with ESModules .withImportMap(config.importMap.asJava) } From 316f7415a4e9559296c51bfcc8c0519bfcf7177e Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Tue, 16 Jul 2024 14:10:14 +0200 Subject: [PATCH 71/95] Update mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala Co-authored-by: Tomasz Godzik --- mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala index fe4a6ae8a..2b8a779a0 100644 --- a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala +++ b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala @@ -159,7 +159,7 @@ object MdocPlugin extends AutoPlugin { val importMapDependency = binaryVersion match { case "3" => "com.armanbilge" % "scalajs-importmap_2.13" % "0.1.1" - case other => + case scalaBinaryVersion => "com.armanbilge" % s"scalajs-importmap_$other" % "0.1.1" } From 682b5b8b36ec16a4dcfd92b920e3bc1aadb8e1b5 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Tue, 16 Jul 2024 14:10:46 +0200 Subject: [PATCH 72/95] Update cli/src/main/scala/mdoc/internal/cli/Settings.scala Co-authored-by: Tomasz Godzik --- cli/src/main/scala/mdoc/internal/cli/Settings.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/src/main/scala/mdoc/internal/cli/Settings.scala b/cli/src/main/scala/mdoc/internal/cli/Settings.scala index 75f17a700..7e44c07f0 100644 --- a/cli/src/main/scala/mdoc/internal/cli/Settings.scala +++ b/cli/src/main/scala/mdoc/internal/cli/Settings.scala @@ -164,7 +164,7 @@ case class Settings( @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" ) - propFileName: String = "mdoc.properties" + propertyFileName: String = "mdoc.properties" ) extends mdoc.parser.ParserSettings { val isMarkdownFileExtension = markdownExtensions.toSet From f3363c83f586cc0194723450fb8bc0d0de09f28a Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Tue, 16 Jul 2024 14:35:27 +0200 Subject: [PATCH 73/95] Per feedback - tidy naming. Add cli args test --- .../main/scala/mdoc/internal/cli/Settings.scala | 2 +- mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala | 4 ++-- .../src/test/scala/tests/js/JsCliSuite.scala | 4 ++-- .../src/test/scala/tests/cli/CliArgsSuite.scala | 15 +++++++++++++-- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/cli/src/main/scala/mdoc/internal/cli/Settings.scala b/cli/src/main/scala/mdoc/internal/cli/Settings.scala index 7e44c07f0..932c7b181 100644 --- a/cli/src/main/scala/mdoc/internal/cli/Settings.scala +++ b/cli/src/main/scala/mdoc/internal/cli/Settings.scala @@ -310,7 +310,7 @@ object Settings { // extends MetaconfigScalametaImplicits with Decoders with Set val base = Settings.default( AbsolutePath(workingDirectory), - conf.get[String]("propFileName").getOrElse("mdoc.properties") + conf.get[String]("propertyFileName").getOrElse("mdoc.properties") ) val cwd = conf.get[String]("cwd").map(AbsolutePath(_)(base.cwd)).getOrElse(base.cwd) conf diff --git a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala index 2b8a779a0..8a47eed37 100644 --- a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala +++ b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala @@ -154,13 +154,13 @@ object MdocPlugin extends AutoPlugin { val linkerDependency = binaryVersion match { case "3" => "org.scala-js" % "scalajs-linker_2.13" % sjsVersion - case other => "org.scala-js" % s"scalajs-linker_$other" % sjsVersion + case scalaBinaryVersion => "org.scala-js" % s"scalajs-linker_$scalaBinaryVersion" % sjsVersion } val importMapDependency = binaryVersion match { case "3" => "com.armanbilge" % "scalajs-importmap_2.13" % "0.1.1" case scalaBinaryVersion => - "com.armanbilge" % s"scalajs-importmap_$other" % "0.1.1" + "com.armanbilge" % s"scalajs-importmap_$scalaBinaryVersion" % "0.1.1" } val mdocJSDependency = binaryVersion match { diff --git a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala index 772449ad0..f0f0f5b55 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala @@ -77,7 +77,7 @@ class JsCliSuite extends BaseCliSuite { |
| |""".stripMargin, - extraArgs = Array("--prop-file-name", "es.properties"), + extraArgs = Array("--property-file-name", "es.properties"), includeOutputPath = { path => !path.toNIO.getFileName.toString.endsWith(".js") } ) @@ -114,7 +114,7 @@ class JsCliSuite extends BaseCliSuite { out().toString, "--cwd", in().syntax, - "--prop-file-name", + "--property-file-name", "es.properties", "--import-map-path", Paths.get(this.getClass.getClassLoader.getResource("importmap.json").toURI).toString() diff --git a/tests/unit/src/test/scala/tests/cli/CliArgsSuite.scala b/tests/unit/src/test/scala/tests/cli/CliArgsSuite.scala index f7cb4768a..ab02b6e40 100644 --- a/tests/unit/src/test/scala/tests/cli/CliArgsSuite.scala +++ b/tests/unit/src/test/scala/tests/cli/CliArgsSuite.scala @@ -11,8 +11,7 @@ import cats.instances.set import mdoc.internal.cli.Context class CliArgsSuite extends FunSuite { - private val reporter = ConsoleReporter.default - // private val base = Settings.default(PathIO.workingDirectory, "mdoc.properties") + private val reporter = ConsoleReporter.default def checkOK(name: String, args: List[String], onSuccess: Settings => Unit = _ => ())(implicit loc: munit.Location @@ -107,6 +106,18 @@ class CliArgsSuite extends FunSuite { List("--in", tmpFile.toString, tmpDirectory.toString) ) + checkOK( + "--property-file-name", + List("--property-file-name", "es.properties", tmpFile.toString, tmpDirectory.toString), + onSuccess = conf => assertEquals(conf.propertyFileName, "es.properties") + ) + + checkOK( + "--property-file-name default", + List(tmpFile.toString, tmpDirectory.toString), + onSuccess = conf => assertEquals(conf.propertyFileName, "mdoc.properties") + ) + checkOK( "relative-by-cwd", List("--cwd", tmpDirectory.toString(), "--in", "readme.template.md", "--out", "readme.md"), From 6c6f6da8a1f2486bb7a7a9aab5288ad92c3f0e3a Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Tue, 16 Jul 2024 15:00:58 +0200 Subject: [PATCH 74/95] Feedback: Update test --- .../src/test/scala/tests/js/JsCliSuite.scala | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala index f0f0f5b55..2387d62c8 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala @@ -13,6 +13,7 @@ import scala.meta.internal.io.FileIO import scala.meta.io.AbsolutePath import java.nio.charset.StandardCharsets import java.nio.file.Paths +import scala.io.Source class JsCliSuite extends BaseCliSuite { @@ -122,9 +123,24 @@ class JsCliSuite extends BaseCliSuite { val code = mdoc.Main.process(args, new PrintStream(myStdout), in().toNIO) val generatedJs = AbsolutePath(out().toNIO.resolve("docs").resolve("facade.md.js")) - val content = new String(FileIO.readAllBytes(generatedJs), StandardCharsets.UTF_8) - assert( - content.contains("https://cdn.jsdelivr.net/npm/@stdlib/blas") + val content = Source.fromFile(generatedJs.toNIO.toUri()).getLines().toList + println(content(1)) + + /** + SP 16.07.2024 + assert(content(contains("https://cdn.jsdelivr.net/npm/@stdlib/blas")) + The line above should also be an effective test. It does not illustrate + what the remap tries to achieve, so the test below is preferred. + + However, given that the names in the test below are "generated", by scalaJS + there is a risk the test turns flaky / fails at some point in the futurem, should scalaJS + change it's with regard to variable name generation. It this test turns "flaky" + the line above could be used in that case to reduce maintenance burden. + **/ + + assertEquals( + content(1), + """import * as $i_https$003a$002f$002fcdn$002ejsdelivr$002enet$002fnpm$002f$0040stdlib$002fblas$00400$002e2$002e1$002f$002besm$002fbase from "https://cdn.jsdelivr.net/npm/@stdlib/blas@0.2.1/+esm/base";""" ) assertEquals(code, 0, clues(myStdout)) From cf3ef94fc4ffa1068f8f469049fa1160cf51c6a0 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Tue, 16 Jul 2024 15:06:13 +0200 Subject: [PATCH 75/95] fmt --- mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala | 3 ++- .../src/test/scala/tests/js/JsCliSuite.scala | 20 +++++++++---------- .../test/scala/tests/cli/CliArgsSuite.scala | 6 +++--- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala index 8a47eed37..b7c29fed5 100644 --- a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala +++ b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala @@ -154,7 +154,8 @@ object MdocPlugin extends AutoPlugin { val linkerDependency = binaryVersion match { case "3" => "org.scala-js" % "scalajs-linker_2.13" % sjsVersion - case scalaBinaryVersion => "org.scala-js" % s"scalajs-linker_$scalaBinaryVersion" % sjsVersion + case scalaBinaryVersion => + "org.scala-js" % s"scalajs-linker_$scalaBinaryVersion" % sjsVersion } val importMapDependency = binaryVersion match { diff --git a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala index 2387d62c8..5f07a25fd 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala @@ -126,17 +126,15 @@ class JsCliSuite extends BaseCliSuite { val content = Source.fromFile(generatedJs.toNIO.toUri()).getLines().toList println(content(1)) - /** - SP 16.07.2024 - assert(content(contains("https://cdn.jsdelivr.net/npm/@stdlib/blas")) - The line above should also be an effective test. It does not illustrate - what the remap tries to achieve, so the test below is preferred. - - However, given that the names in the test below are "generated", by scalaJS - there is a risk the test turns flaky / fails at some point in the futurem, should scalaJS - change it's with regard to variable name generation. It this test turns "flaky" - the line above could be used in that case to reduce maintenance burden. - **/ + /** SP 16.07.2024 assert(content(contains("https://cdn.jsdelivr.net/npm/@stdlib/blas")) The line + * above should also be an effective test. It does not illustrate what the remap tries to + * achieve, so the test below is preferred. + * + * However, given that the names in the test below are "generated", by scalaJS there is a risk + * the test turns flaky / fails at some point in the futurem, should scalaJS change it's with + * regard to variable name generation. It this test turns "flaky" the line above could be used + * in that case to reduce maintenance burden. + */ assertEquals( content(1), diff --git a/tests/unit/src/test/scala/tests/cli/CliArgsSuite.scala b/tests/unit/src/test/scala/tests/cli/CliArgsSuite.scala index ab02b6e40..ae0f344af 100644 --- a/tests/unit/src/test/scala/tests/cli/CliArgsSuite.scala +++ b/tests/unit/src/test/scala/tests/cli/CliArgsSuite.scala @@ -11,7 +11,7 @@ import cats.instances.set import mdoc.internal.cli.Context class CliArgsSuite extends FunSuite { - private val reporter = ConsoleReporter.default + private val reporter = ConsoleReporter.default def checkOK(name: String, args: List[String], onSuccess: Settings => Unit = _ => ())(implicit loc: munit.Location @@ -108,13 +108,13 @@ class CliArgsSuite extends FunSuite { checkOK( "--property-file-name", - List("--property-file-name", "es.properties", tmpFile.toString, tmpDirectory.toString), + List("--property-file-name", "es.properties", tmpFile.toString, tmpDirectory.toString), onSuccess = conf => assertEquals(conf.propertyFileName, "es.properties") ) checkOK( "--property-file-name default", - List(tmpFile.toString, tmpDirectory.toString), + List(tmpFile.toString, tmpDirectory.toString), onSuccess = conf => assertEquals(conf.propertyFileName, "mdoc.properties") ) From 7e76eb94b88b4f052c21c5cccfdd39145a09aecf Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Tue, 16 Jul 2024 15:08:13 +0200 Subject: [PATCH 76/95] Rename mapping function --- mdoc-js-worker/src/main/scala/ScalaJSWorker.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala b/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala index 9b68d85c7..c5b1afca0 100644 --- a/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala +++ b/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala @@ -47,7 +47,7 @@ class ScalaJSWorker( StandardImpl.clearableLinker(cfg) } - lazy val remapFct = config.importMap.asScala.toSeq.foldLeft((in: String) => in) { + lazy val esModuleRemapFunction: String => String = config.importMap.asScala.toSeq.foldLeft((in: String) => in) { case (fct, (s1, s2)) => val fct2: (String => String) = (in => in.replace(s1, s2)) (in => fct(fct2(in))) @@ -82,7 +82,7 @@ class ScalaJSWorker( if (config.importMap.isEmpty) ir else - ImportMappedIRFile.fromIRFile(ir)(remapFct) + ImportMappedIRFile.fromIRFile(ir)(esModuleRemapFunction) }, Seq.empty, mem, From 7e97011b04ea4ec392549f4852cdfdaa591ed454 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Tue, 16 Jul 2024 15:15:54 +0200 Subject: [PATCH 77/95] . --- mdoc-js-worker/src/main/scala/ScalaJSWorker.scala | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala b/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala index c5b1afca0..678426a93 100644 --- a/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala +++ b/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala @@ -47,11 +47,11 @@ class ScalaJSWorker( StandardImpl.clearableLinker(cfg) } - lazy val esModuleRemapFunction: String => String = config.importMap.asScala.toSeq.foldLeft((in: String) => in) { - case (fct, (s1, s2)) => + lazy val esModuleRemapFunction: String => String = + config.importMap.asScala.toSeq.foldLeft((in: String) => in) { case (fct, (s1, s2)) => val fct2: (String => String) = (in => in.replace(s1, s2)) (in => fct(fct2(in))) - } + } var cachedFiles = Seq.empty[org.scalajs.linker.interface.IRFile] From e7b140eb0d60af43c669adc69a83fb899d7b481a Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Mon, 22 Jul 2024 18:21:34 +0200 Subject: [PATCH 78/95] . --- docs/js.md | 20 +++++++++++++++++++ mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala | 6 ++++++ website.md | 14 +++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 website.md diff --git a/docs/js.md b/docs/js.md index 94086176e..90a35cd68 100644 --- a/docs/js.md +++ b/docs/js.md @@ -415,3 +415,23 @@ mdocVariables := Map( "js-batch-mode" -> "true" ) ``` +### Remapping imports at link time. + +It may be the case, that the library you wish to depend on, is a facade to some NPM dependancy. Setting up a bundler in mdoc isn't a simple operation, but there is an alternative. We can instead as mdoc to output ESModules (see `ModuleKind`) and then remap their imports to point at a CDN. + +To mdocs CLI, you'll need to pass this flag; + +```sh +--import-map-path full/path/to/import-map.json +``` + +`import-map.json` should look like this; + +```json +{ + "imports": { + "@easepick/bundle": "https://cdn.jsdelivr.net/npm/@easepick/bundle@1.2.1", + } +} +``` +You should then have this dependancy available to you. diff --git a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala index b7c29fed5..a4f5cef57 100644 --- a/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala +++ b/mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala @@ -47,6 +47,11 @@ object MdocPlugin extends AutoPlugin { "If false, do not add mdoc as a library dependency this project. " + "Default value is true." ) + val esModuleImportJSMapFile = + settingKey[Option[File]]( + "File containing an import map for remapping ESModules at link time. " + + "Defaults to the toplevel docs/ directory." + ) } val mdocInternalVariables = settingKey[List[(String, String)]]( @@ -82,6 +87,7 @@ object MdocPlugin extends AutoPlugin { mdocJSWorkerClasspath := None, mdocAutoDependency := true, mdocInternalVariables := Nil, + esModuleImportJSMapFile := None, mdoc := Def.inputTaskDyn { validateSettings.value val parsed = sbt.complete.DefaultParsers.spaceDelimited("").parsed diff --git a/website.md b/website.md new file mode 100644 index 000000000..75069a9a6 --- /dev/null +++ b/website.md @@ -0,0 +1,14 @@ +# Running the website locally + +Before starting; + +1. Make sure the project compiles. +2. *Ensure you are using node v16*. As of 22.07.2024 The website will not work with higher versions of node. + +Next + +1. Switch to the "website" folder. run `npm install` +2. Copy the blog folder into the `website` folder (don't commit that) +3. Back in the project root, run `sbt '++2.12.19; docs/mdoc'` in a terminal - this will run mdoc. +4. probably in a new terminal in the "website" folder , run `npm start` + From 03ea40eb979d8047bcb64fa061cff4acb5c33496 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Mon, 22 Jul 2024 21:09:44 +0200 Subject: [PATCH 79/95] JS docs --- build.sbt | 17 ++- docs/js.md | 259 ++++++++++++++++++++++++++++---------------- project/plugins.sbt | 2 - 3 files changed, 171 insertions(+), 107 deletions(-) diff --git a/build.sbt b/build.sbt index 2a413832c..0bc04a0cf 100644 --- a/build.sbt +++ b/build.sbt @@ -296,18 +296,15 @@ val jsdocs = project sharedSettings, publish / skip := true, scalaJSLinkerConfig ~= { - _.withModuleKind(ModuleKind.CommonJSModule) + _.withModuleKind(ModuleKind.ESModule) }, libraryDependencies ++= List( "org.scala-js" %%% "scalajs-dom" % scalajsDom - ), - scalaJSUseMainModuleInitializer := true, - Compile / npmDependencies ++= List( - "ms" -> "2.1.1" - ), - webpackBundlingMode := BundlingMode.LibraryOnly() + // "com.raquo" %%% "laminar" % "0.17.0", + // "com.raquo" %%% "laminar-shoelace" % "0.1.0" // once this project moves to scala 3. + ) ) - .enablePlugins(ScalaJSPlugin, ScalaJSBundlerPlugin) + .enablePlugins(ScalaJSPlugin) lazy val worksheets = project .in(file("tests/worksheets")) @@ -473,7 +470,9 @@ lazy val docs = project Global / cancelable := true, MdocPlugin.autoImport.mdoc := (Compile / run).evaluated, mdocJS := Some(jsdocs), - mdocJSLibraries := (jsdocs / Compile / fullOptJS / webpack).value, + // mdocJSLibraries := (jsdocs / Compile / fullOptJS).outputFiles.map { path => + // Attributed.blank(path.toFile) + // }, MdocPlugin.mdocJSWorkerClasspath := { val _ = (jsWorker / Compile / compile).value diff --git a/docs/js.md b/docs/js.md index 90a35cd68..bb89244ef 100644 --- a/docs/js.md +++ b/docs/js.md @@ -7,13 +7,12 @@ sidebar_label: Scala.js Code fences with the `scala mdoc:js` modifier are compiled with Scala.js and run in the browser. -```scala mdoc:js:shared:invisible -import org.scalajs.dom.window._ -import jsdocs._ -``` ````md ```scala mdoc:js +import org.scalajs.dom.window._ +import jsdocs._ + val progress = new Progress() setInterval({ () => // `node` variable is a DOM element in scope. @@ -22,9 +21,14 @@ setInterval({ () => ``` ```` -```scala mdoc:js:invisible +Becomes + +```scala mdoc:js Loading progress bar... --- +import org.scalajs.dom.window._ +import jsdocs._ + val progress = new Progress() setInterval({ () => // `node` variable is a DOM element in scope. @@ -110,6 +114,20 @@ Some notes: `-Xplugin:path/to/scalajs-compiler.jar` to enable the Scala.js compiler. - The `js-classpath` field in `mdoc.properties` must include a dependency on the library `org.scala-js:scalajs-dom` +- There is a js-html-header field in `mdoc.properties` which can be used to inject custom HTML into the output. This can be used to inject scripts or stylesheets into the output. +```html +js-html-header= +``` +- There is a `--import-map` command line flag, commonly set to something like this; `--import-map-path $(pwd)/importmap.json`, which will remap ESModule imports at link time. As a reminder, `importmap.json`, could look like; +``` +{ + "imports": { + "@shoelace-style/shoelace/dist/": "https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.13.1/cdn/" + } +} +``` +which will remap the shoelace import to resolve out of a CDN - negating the need for a bundler. + ## Modifiers @@ -121,13 +139,8 @@ the rendered output. By default, each code fence is isolated from other code fences. Use the `:shared` modifier to reuse imports or variables between code fences. -```scala mdoc:js:shared:invisible -import org.scalajs.dom.window.setInterval -import scala.scalajs.js.Date -``` - -````scala mdoc:mdoc -```scala mdoc:js:shared +````md +```scala mdoc:js import org.scalajs.dom.window.setInterval import scala.scalajs.js.Date ``` @@ -138,25 +151,28 @@ setInterval(() => { ``` ```` -```scala mdoc:js -Loading :shared example... ---- -setInterval(() => { - val date = new Date().toString() - node.innerHTML = s"

Shared date $date

" -}, 1000) +Is a compile error, because the imports are not in the second block. But add the shared modifier... + +````md +```scala mdoc:js:shared +import org.scalajs.dom.window.setInterval +import scala.scalajs.js.Date ``` +```` -Without `:shared`, the example above results in a compile error. +And it works! -````scala mdoc:mdoc:crash -```scala mdoc:js +```scala mdoc:js:shared +import org.scalajs.dom.window.setInterval import scala.scalajs.js.Date ``` + ```scala mdoc:js -new Date() +setInterval(() => { + node.innerHTML = new Date().toString() +}, 1000) ``` -```` + ### `:invisible` @@ -164,7 +180,7 @@ By default, the original input code is rendered in the output page. Use `:invisible` to hide the code example from the output so that only the div is generated. -````scala mdoc:mdoc +````md ```scala mdoc:js:invisible var n = 0 org.scalajs.dom.window.setInterval(() => { @@ -173,6 +189,7 @@ org.scalajs.dom.window.setInterval(() => { }, 1000) ``` ```` +You'd have to look at the raw markdown to see the fence below... ```scala mdoc:js:invisible Loading :invisible example... @@ -184,6 +201,8 @@ setInterval(() => { }, 1000) ``` + + ### `:compile-only` Use `:compile-only` to validate that a code example compiles successfully @@ -207,7 +226,7 @@ By default, the `node` variable points to an empty div element. Prefix the code fence with custom HTML followed by a `---` separator to set the inner HTML of the `node` div. -````scala mdoc:mdoc +````md ```scala mdoc:js

I am a custom loader

--- @@ -218,8 +237,7 @@ println(node.innerHTML) ```scala mdoc:js

I am a custom loader

--- -// Open developer console to see this printed message -println(s"Loading HTML: ${node.innerHTML}") +println(node.innerHTML) ``` Replace the node's `innerHTML` to make the HTML disappear once the document has @@ -233,66 +251,33 @@ org.scalajs.dom.window.setTimeout(() => { }, 3000) ``` -## Using scalajs-bundler +## Facades -The [scalajs-bundler](https://scalacenter.github.io/scalajs-bundler/) plugin can -be used to install npm dependencies and bundle applications with webpack. - -Add the following sbt settings if you use scalajs-bundler. - -```diff - lazy val jsdocs = project - .settings( -+ webpackBundlingMode := BundlingMode.LibraryOnly() -+ scalaJSUseMainModuleInitializer := true, - npmDependencies.in(Compile) ++= List( - // ... - ), - ) - .enablePlugins(ScalaJSBundlerPlugin) +Let's imagine we want to document a (glorious) facade to [ms](https://www.npmjs.com/package/ms) with mdoc. - lazy val docs = project - .settings( - mdocJS := Some(jsdocs), -+ mdocJSLibraries := webpack.in(jsdocs, Compile, fullOptJS).value - ) - .enablePlugins(MdocPlugin) -``` - -> The `webpackBundlingMode` must be `LibraryOnly` so that the mdoc generated -> output can depend on it. - -### Bundle npm dependencies - -It's important that the main function in `jsdocs` uses the installed npm -dependencies. If the npm dependencies are not used from the `jsdocs` main -function, then webpack thinks they are unused and removes them from the bundled -output even if those dependencies are called from `mdoc:js` markdown code -fences. - -For example, to use the npm [`ms` package](https://www.npmjs.com/package/ms) -start by writing a facade using `@JSImport` - -```scala mdoc:file:tests/jsdocs/src/main/scala/jsdocs/ms.scala +```scala mdoc:js:shared +import scala.scalajs.js +import scala.scalajs.js.annotation._ + +@js.native +@JSImport("https://cdn.jsdelivr.net/npm/ms@2.1.3/+esm", JSImport.Default) +object ms extends js.Object { + def apply(s: Double): String = js.native + def apply(s: String): Double = js.native +} ``` -Next, write a main function that uses the facade. Make sure that the `jsdocs` -project contains the setting `scalaJSUseMainModuleInitializer := true`. - -```scala mdoc:file:tests/jsdocs/src/main/scala/jsdocs/Main.scala - -``` -The `ms` function can now be used from `mdoc:js`. ```scala mdoc:js val date = new scala.scalajs.js.Date() -val time = jsdocs.ms(date.getTime()) -node.innerHTML = s"Hello from npm package 'ms': $time" +val time = ms( "2 days") +val time2 = ms( 1000) +node.innerHTML = s"Hello from npm package 'ms': $time , $time2 " ``` -If the `ms` function is not referenced from the `jsdocs` main function you get a +If the `ms` function is not referenced (somehow) `jsdocs` you may get a stacktrace in the browser console like this: ```js @@ -302,25 +287,13 @@ Uncaught TypeError: $i_ms is not a function at mdoc.js:9 ``` -### Validate library.js and loader.js - -Validate that the `webpack` task provides one `*-library.js` file and one -`*-loader.js` file. +Please note that the CDN is hardcoded here as is a simple way to get started. A "real" facade would normally be constructed as ```scala -// sbt shell -> show jsdocs/fullOptJS::webpack -... -[info] * Attributed(.../jsdocs/target/scala-2.12/scalajs-bundler/main/jsdocs-opt-loader.js) -[info] * Attributed(.../jsdocs/target/scala-2.12/scalajs-bundler/main/jsdocs-opt-library.js) -... +@JSImport("ms", JSImport.Default) ``` -These files are required by mdoc in order to use the scalajs-bundler npm -dependencies. The files may be missing if you have custom webpack or -scalajs-bundler configuration. To fix this problem, you may want to try to -create a new `jsdocs` Scala.js project with minimal webpack and scalajs-bundler -configuration. +As that is where it's found in the `node_modules`. Consider remapping this import at link time, as described in the configuration section below for a (real) use case. ## Configuration @@ -417,7 +390,7 @@ mdocVariables := Map( ``` ### Remapping imports at link time. -It may be the case, that the library you wish to depend on, is a facade to some NPM dependancy. Setting up a bundler in mdoc isn't a simple operation, but there is an alternative. We can instead as mdoc to output ESModules (see `ModuleKind`) and then remap their imports to point at a CDN. +It may be the case, that the library you wish to depend on, is a facade to some NPM dependancy. Mapping the import offers an alterative to bundling, by resolving the dependancy out of a CDN. We set mdoc to output ESModules (see `ModuleKind`), and load the dependancies directly in the browser. To mdocs CLI, you'll need to pass this flag; @@ -425,13 +398,107 @@ To mdocs CLI, you'll need to pass this flag; --import-map-path full/path/to/import-map.json ``` -`import-map.json` should look like this; +`import-map.json` could look like this; ```json { - "imports": { - "@easepick/bundle": "https://cdn.jsdelivr.net/npm/@easepick/bundle@1.2.1", + "imports": + { + "@shoelace-style/shoelace/dist/": "https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.13.1/cdn/" } } ``` You should then have this dependancy available to you. + + +# Legacy + +This documentation is preserved here, but it is no longer recommend to work with scalaJS bundler. If you require a bundler you should consider the excellent [vite](https://vitejs.dev) project. It is currently unsolved how to use mdoc with vite. + + +The [scalajs-bundler](https://scalacenter.github.io/scalajs-bundler/) plugin can +be used to install npm dependencies and bundle applications with webpack. + +Add the following sbt settings if you use scalajs-bundler. + +```diff + lazy val jsdocs = project + .settings( ++ webpackBundlingMode := BundlingMode.LibraryOnly() ++ scalaJSUseMainModuleInitializer := true, + npmDependencies.in(Compile) ++= List( + // ... + ), + ) + .enablePlugins(ScalaJSBundlerPlugin) + + lazy val docs = project + .settings( + mdocJS := Some(jsdocs), ++ mdocJSLibraries := webpack.in(jsdocs, Compile, fullOptJS).value + ) + .enablePlugins(MdocPlugin) +``` + +> The `webpackBundlingMode` must be `LibraryOnly` so that the mdoc generated +> output can depend on it. + +### Bundle npm dependencies + +It's important that the main function in `jsdocs` uses the installed npm +dependencies. If the npm dependencies are not used from the `jsdocs` main +function, then webpack thinks they are unused and removes them from the bundled +output even if those dependencies are called from `mdoc:js` markdown code +fences. + +For example, to use the npm [`ms` package](https://www.npmjs.com/package/ms) +start by writing a facade using `@JSImport` + +```scala mdoc:file:tests/jsdocs/src/main/scala/jsdocs/ms.scala + +``` + +Next, write a main function that uses the facade. Make sure that the `jsdocs` +project contains the setting `scalaJSUseMainModuleInitializer := true`. + +```scala mdoc:file:tests/jsdocs/src/main/scala/jsdocs/Main.scala + +``` + +The `ms` function can now be used from `mdoc:js`. + +```scala +val date = new scala.scalajs.js.Date() +val time = jsdocs.ms(date.getTime()) +node.innerHTML = s"Hello from npm package 'ms': $time" +``` + +If the `ms` function is not referenced from the `jsdocs` main function you get a +stacktrace in the browser console like this: + +```js +Uncaught TypeError: $i_ms is not a function + at $c_Lmdocjs$.run6__Lorg_scalajs_dom_raw_Element__V (js.md.js:1180) + at js.md.js:3108 + at mdoc.js:9 +``` + +### Validate library.js and loader.js + +Validate that the `webpack` task provides one `*-library.js` file and one +`*-loader.js` file. + +```scala +// sbt shell +> show jsdocs/fullOptJS::webpack +... +[info] * Attributed(.../jsdocs/target/scala-2.12/scalajs-bundler/main/jsdocs-opt-loader.js) +[info] * Attributed(.../jsdocs/target/scala-2.12/scalajs-bundler/main/jsdocs-opt-library.js) +... +``` + +These files are required by mdoc in order to use the scalajs-bundler npm +dependencies. The files may be missing if you have custom webpack or +scalajs-bundler configuration. To fix this problem, you may want to try to +create a new `jsdocs` Scala.js project with minimal webpack and scalajs-bundler +configuration. \ No newline at end of file diff --git a/project/plugins.sbt b/project/plugins.sbt index 902e1bf4d..3ceef3ac3 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -4,8 +4,6 @@ addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.6.1") addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.16.0") -addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % "0.21.1") - libraryDependencies ++= List( "org.jsoup" % "jsoup" % "1.12.1", "org.scala-sbt" %% "scripted-plugin" % sbtVersion.value From bc0b4a83b0a98bf9dce71163a25c0edd6a56c770 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Mon, 22 Jul 2024 21:10:36 +0200 Subject: [PATCH 80/95] . --- website.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website.md b/website.md index 75069a9a6..424385699 100644 --- a/website.md +++ b/website.md @@ -3,7 +3,7 @@ Before starting; 1. Make sure the project compiles. -2. *Ensure you are using node v16*. As of 22.07.2024 The website will not work with higher versions of node. +2. *Ensure you are using node v16*. (As of 22.07.2024) The website will not work with higher versions of node. Consider using nvm to manage node versions. Next From 11f882dfeac0dcbe7f5002c5a54e5afc68a83f64 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Mon, 22 Jul 2024 21:24:19 +0200 Subject: [PATCH 81/95] too much... put back jsdocs project --- build.sbt | 21 ++++++++++++++++++- project/plugins.sbt | 2 ++ .../src/main/scala/jsdocs/Progress.scala | 14 +++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 tests/websiteJs/src/main/scala/jsdocs/Progress.scala diff --git a/build.sbt b/build.sbt index 0bc04a0cf..a3e70d428 100644 --- a/build.sbt +++ b/build.sbt @@ -292,6 +292,25 @@ val tests = project val jsdocs = project .in(file("tests/jsdocs")) + .settings( + sharedSettings, + publish / skip := true, + scalaJSLinkerConfig ~= { + _.withModuleKind(ModuleKind.CommonJSModule) + }, + libraryDependencies ++= List( + "org.scala-js" %%% "scalajs-dom" % scalajsDom + ), + scalaJSUseMainModuleInitializer := true, + Compile / npmDependencies ++= List( + "ms" -> "2.1.1" + ), + webpackBundlingMode := BundlingMode.LibraryOnly() + ) + .enablePlugins(ScalaJSPlugin, ScalaJSBundlerPlugin) + +val jswebsitedocs = project + .in(file("tests/websiteJs")) .settings( sharedSettings, publish / skip := true, @@ -469,7 +488,7 @@ lazy val docs = project watchSources += (ThisBuild / baseDirectory).value / "docs", Global / cancelable := true, MdocPlugin.autoImport.mdoc := (Compile / run).evaluated, - mdocJS := Some(jsdocs), + mdocJS := Some(jswebsitedocs), // mdocJSLibraries := (jsdocs / Compile / fullOptJS).outputFiles.map { path => // Attributed.blank(path.toFile) // }, diff --git a/project/plugins.sbt b/project/plugins.sbt index 3ceef3ac3..902e1bf4d 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -4,6 +4,8 @@ addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.6.1") addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.16.0") +addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % "0.21.1") + libraryDependencies ++= List( "org.jsoup" % "jsoup" % "1.12.1", "org.scala-sbt" %% "scripted-plugin" % sbtVersion.value diff --git a/tests/websiteJs/src/main/scala/jsdocs/Progress.scala b/tests/websiteJs/src/main/scala/jsdocs/Progress.scala new file mode 100644 index 000000000..f1d903876 --- /dev/null +++ b/tests/websiteJs/src/main/scala/jsdocs/Progress.scala @@ -0,0 +1,14 @@ +package jsdocs + +class Progress { + var i = -1 + val max = 20 + def tick(_unused: Int): String = { + i += 1 + val n = i % max + 1 + val hash = "#" * n + val space = " " * (max - n) + val percentage = ((n.toDouble / max) * 100).toInt + "
" + hash + "🚀" + space + s"$percentage%" + "
" + } +} From 6afbc6d2767b789570bf381193d763fbf7580c79 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Wed, 14 Aug 2024 11:33:47 +0200 Subject: [PATCH 82/95] Update docs/js.md Co-authored-by: Tomasz Godzik --- docs/js.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/js.md b/docs/js.md index bb89244ef..192ed7f1e 100644 --- a/docs/js.md +++ b/docs/js.md @@ -253,7 +253,7 @@ org.scalajs.dom.window.setTimeout(() => { ## Facades -Let's imagine we want to document a (glorious) facade to [ms](https://www.npmjs.com/package/ms) with mdoc. +Let's imagine we want to document a (glorious) facade to [ms](https://www.npmjs.com/package/ms) with mdoc. ```scala mdoc:js:shared From 1089279308d37e8ecfcd58f325aed9676428c777 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Wed, 14 Aug 2024 11:34:10 +0200 Subject: [PATCH 83/95] Update docs/js.md Co-authored-by: Tomasz Godzik --- docs/js.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/js.md b/docs/js.md index 192ed7f1e..e233e3b5d 100644 --- a/docs/js.md +++ b/docs/js.md @@ -408,7 +408,7 @@ To mdocs CLI, you'll need to pass this flag; } } ``` -You should then have this dependancy available to you. +You should then have this dependency available to you. # Legacy From 81bfd4112c9f5efc73e2a375e67c81f49d963431 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Wed, 14 Aug 2024 11:34:36 +0200 Subject: [PATCH 84/95] Update docs/js.md Co-authored-by: Tomasz Godzik --- docs/js.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/js.md b/docs/js.md index e233e3b5d..21df10b39 100644 --- a/docs/js.md +++ b/docs/js.md @@ -114,7 +114,7 @@ Some notes: `-Xplugin:path/to/scalajs-compiler.jar` to enable the Scala.js compiler. - The `js-classpath` field in `mdoc.properties` must include a dependency on the library `org.scala-js:scalajs-dom` -- There is a js-html-header field in `mdoc.properties` which can be used to inject custom HTML into the output. This can be used to inject scripts or stylesheets into the output. +- There is a js-html-header field in `mdoc.properties` which can be used to inject custom HTML, scripts or stylesheets into the output. ```html js-html-header= ``` From 26758009588fe774bbdb2d11eb8bebc5f24be510 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Wed, 14 Aug 2024 11:55:38 +0200 Subject: [PATCH 85/95] revert --- docs/js.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/js.md b/docs/js.md index 21df10b39..6ac59ea78 100644 --- a/docs/js.md +++ b/docs/js.md @@ -230,7 +230,8 @@ the `node` div. ```scala mdoc:js

I am a custom loader

--- -println(node.innerHTML) +// Open developer console to see this printed message +println(s"Loading HTML: ${node.innerHTML}") ``` ```` From 895714590938fab850af0b550fbc5af6e2b5242e Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Wed, 14 Aug 2024 11:58:02 +0200 Subject: [PATCH 86/95] organise imports --- .../unit-js/src/test/scala/tests/js/JsCliSuite.scala | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala index 5f07a25fd..1183fc192 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsCliSuite.scala @@ -2,18 +2,19 @@ package tests.js import mdoc.internal.cli.MdocProperties import mdoc.internal.cli.Settings -import scala.meta.io.Classpath -import tests.markdown.StringSyntax._ import tests.cli.BaseCliSuite -import scala.meta.internal.io.PathIO import tests.js.JsTests.suffix +import tests.markdown.StringSyntax._ + import java.io.ByteArrayOutputStream import java.io.PrintStream -import scala.meta.internal.io.FileIO -import scala.meta.io.AbsolutePath import java.nio.charset.StandardCharsets import java.nio.file.Paths import scala.io.Source +import scala.meta.internal.io.FileIO +import scala.meta.internal.io.PathIO +import scala.meta.io.AbsolutePath +import scala.meta.io.Classpath class JsCliSuite extends BaseCliSuite { From 225e92b9d7a861a9530706f8caa3dd788ab4e9c2 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Wed, 14 Aug 2024 11:59:38 +0200 Subject: [PATCH 87/95] . --- docs/js.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/js.md b/docs/js.md index 6ac59ea78..3bb730a9b 100644 --- a/docs/js.md +++ b/docs/js.md @@ -238,7 +238,8 @@ println(s"Loading HTML: ${node.innerHTML}") ```scala mdoc:js

I am a custom loader

--- -println(node.innerHTML) +// Open developer console to see this printed message +println(s"Loading HTML: ${node.innerHTML}") ``` Replace the node's `innerHTML` to make the HTML disappear once the document has From fbd4a986aa771dad1d94a0292ffcc940f78f17d4 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Wed, 14 Aug 2024 12:03:01 +0200 Subject: [PATCH 88/95] remove commented references --- build.sbt | 2 -- 1 file changed, 2 deletions(-) diff --git a/build.sbt b/build.sbt index a3e70d428..98ccd19d0 100644 --- a/build.sbt +++ b/build.sbt @@ -319,8 +319,6 @@ val jswebsitedocs = project }, libraryDependencies ++= List( "org.scala-js" %%% "scalajs-dom" % scalajsDom - // "com.raquo" %%% "laminar" % "0.17.0", - // "com.raquo" %%% "laminar-shoelace" % "0.1.0" // once this project moves to scala 3. ) ) .enablePlugins(ScalaJSPlugin) From 62038dbbe08d1107478a7b8b2d07bf6cbe18702e Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Fri, 30 Aug 2024 10:29:17 +0200 Subject: [PATCH 89/95] some feedback --- build.sbt | 10 ++++------ .../main/scala/mdoc/internal/cli/Settings.scala | 14 ++++++++------ docs/js.md | 14 +++++++------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/build.sbt b/build.sbt index 98ccd19d0..82229d5cf 100644 --- a/build.sbt +++ b/build.sbt @@ -245,6 +245,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", @@ -255,7 +256,7 @@ lazy val mdoc = project "org.scalameta" %% "metaconfig-typesafe-config" % V.metaconfig, "com.lihaoyi" %% "fansi" % V.fansi, "com.lihaoyi" %% "pprint" % V.pprint - ) ++ jsoniter + ) ) .dependsOn(parser, runtime, cli) .enablePlugins(BuildInfoPlugin) @@ -463,8 +464,8 @@ lazy val js = project .settings( sharedSettings, moduleName := "mdoc-js", - Compile / unmanagedSourceDirectories ++= multiScalaDirectories("js").value, - libraryDependencies ++= jsoniter + libraryDependencies ++= jsoniter, + Compile / unmanagedSourceDirectories ++= multiScalaDirectories("js").value ) .dependsOn(mdoc) @@ -487,9 +488,6 @@ lazy val docs = project Global / cancelable := true, MdocPlugin.autoImport.mdoc := (Compile / run).evaluated, mdocJS := Some(jswebsitedocs), - // mdocJSLibraries := (jsdocs / Compile / fullOptJS).outputFiles.map { path => - // Attributed.blank(path.toFile) - // }, MdocPlugin.mdocJSWorkerClasspath := { val _ = (jsWorker / Compile / compile).value diff --git a/cli/src/main/scala/mdoc/internal/cli/Settings.scala b/cli/src/main/scala/mdoc/internal/cli/Settings.scala index 932c7b181..025fd5d14 100644 --- a/cli/src/main/scala/mdoc/internal/cli/Settings.scala +++ b/cli/src/main/scala/mdoc/internal/cli/Settings.scala @@ -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 @@ -32,7 +33,6 @@ import mdoc.internal.markdown.{GitHubIdGenerator, ReplVariablePrinter} import mdoc.internal.cli.CliEnrichments._ import pprint.TPrint import pprint.TPrintColors -import java.nio.file.Path class Section(val name: String) extends StaticAnnotation @@ -154,9 +154,6 @@ case class Settings( @Hidden() @Description("The pretty printer for variables") variablePrinter: Variable => String = ReplVariablePrinter, - @Hidden() - @Description("The Coursier logger used to report progress bars when downloading dependencies") - coursierLogger: coursierapi.Logger = coursierapi.Logger.progressBars(), @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" ) @@ -164,11 +161,16 @@ case class Settings( @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" + propertyFileName: String = mdocProperties, + @Hidden() + @Description("The Coursier logger used to report progress bars when downloading dependencies") + coursierLogger: coursierapi.Logger = coursierapi.Logger.progressBars() ) extends mdoc.parser.ParserSettings { val isMarkdownFileExtension = markdownExtensions.toSet + val mdocProperties = "mdoc.properties" + val outputByInput = in .zip(out) .iterator @@ -310,7 +312,7 @@ object Settings { // extends MetaconfigScalametaImplicits with Decoders with Set val base = Settings.default( AbsolutePath(workingDirectory), - conf.get[String]("propertyFileName").getOrElse("mdoc.properties") + conf.get[String]("propertyFileName").getOrElse(mdocProperties) ) val cwd = conf.get[String]("cwd").map(AbsolutePath(_)(base.cwd)).getOrElse(base.cwd) conf diff --git a/docs/js.md b/docs/js.md index 3bb730a9b..8f0c04edc 100644 --- a/docs/js.md +++ b/docs/js.md @@ -79,7 +79,7 @@ js-linker-classpath=$(coursier fetch org.scalameta:mdoc-js-worker_@SCALA_BINARY_ EOT ``` -Note: For scala 3, you may need `js-scalac-options=-scalajs` - instead of the XPlugin incantation - [Doc](https://docs.scala-lang.org/scala3/guides/migration/options-lookup.html#compiler-plugins). +Note: For scala 3, you may need `js-scalac-options=-scalajs` - instead of the `-XPlugin` flag - [Doc](https://docs.scala-lang.org/scala3/guides/migration/options-lookup.html#compiler-plugins). @@ -118,7 +118,7 @@ Some notes: ```html js-html-header= ``` -- There is a `--import-map` command line flag, commonly set to something like this; `--import-map-path $(pwd)/importmap.json`, which will remap ESModule imports at link time. As a reminder, `importmap.json`, could look like; +- There's an `--import-map` command line flag, commonly set to ; `--import-map-path $(pwd)/importmap.json`, which will remap ESModule imports at link time. As a reminder `importmap.json` could look like: ``` { "imports": { @@ -279,7 +279,7 @@ val time2 = ms( 1000) node.innerHTML = s"Hello from npm package 'ms': $time , $time2 " ``` -If the `ms` function is not referenced (somehow) `jsdocs` you may get a +If the `ms` function is not referenced from `jsdocs` you may get a stacktrace in the browser console like this: ```js @@ -390,9 +390,9 @@ mdocVariables := Map( "js-batch-mode" -> "true" ) ``` -### Remapping imports at link time. +### Remapping imports at link time -It may be the case, that the library you wish to depend on, is a facade to some NPM dependancy. Mapping the import offers an alterative to bundling, by resolving the dependancy out of a CDN. We set mdoc to output ESModules (see `ModuleKind`), and load the dependancies directly in the browser. +It may be the case that the library you wish to depend on is a facade to some NPM dependency. Mapping the import offers an alterative to bundling, by resolving the dependency out of a CDN. We set mdoc to output ESModules (see `ModuleKind`), and load the dependancies directly in the browser. To mdocs CLI, you'll need to pass this flag; @@ -415,13 +415,13 @@ You should then have this dependency available to you. # Legacy -This documentation is preserved here, but it is no longer recommend to work with scalaJS bundler. If you require a bundler you should consider the excellent [vite](https://vitejs.dev) project. It is currently unsolved how to use mdoc with vite. +This documentation is preserved here, but it is no longer recommend to work with scalaJS bundler. If you require a bundler you should consider the excellent [vite](https://vitejs.dev) project - integration with vite is a potential improvement for mdoc. The [scalajs-bundler](https://scalacenter.github.io/scalajs-bundler/) plugin can be used to install npm dependencies and bundle applications with webpack. -Add the following sbt settings if you use scalajs-bundler. +Add the following sbt settings if you use scalajs-bundler: ```diff lazy val jsdocs = project From 2a79c0edfc4db3aadc7fd14827f210fbe43e1a96 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Fri, 30 Aug 2024 11:44:15 +0200 Subject: [PATCH 90/95] Parse from stream --- .../scala/mdoc/internal/cli/Settings.scala | 6 ++-- .../main/scala/mdoc/modifiers/JsConfig.scala | 28 +++++++++++++++---- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/cli/src/main/scala/mdoc/internal/cli/Settings.scala b/cli/src/main/scala/mdoc/internal/cli/Settings.scala index 025fd5d14..c286756e5 100644 --- a/cli/src/main/scala/mdoc/internal/cli/Settings.scala +++ b/cli/src/main/scala/mdoc/internal/cli/Settings.scala @@ -161,7 +161,7 @@ case class Settings( @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 = mdocProperties, + propertyFileName: String = "mdoc.properties", @Hidden() @Description("The Coursier logger used to report progress bars when downloading dependencies") coursierLogger: coursierapi.Logger = coursierapi.Logger.progressBars() @@ -169,8 +169,6 @@ case class Settings( val isMarkdownFileExtension = markdownExtensions.toSet - val mdocProperties = "mdoc.properties" - val outputByInput = in .zip(out) .iterator @@ -312,7 +310,7 @@ object Settings { // extends MetaconfigScalametaImplicits with Decoders with Set val base = Settings.default( AbsolutePath(workingDirectory), - conf.get[String]("propertyFileName").getOrElse(mdocProperties) + conf.get[String]("propertyFileName").getOrElse("mdoc.properties") ) val cwd = conf.get[String]("cwd").map(AbsolutePath(_)(base.cwd)).getOrElse(base.cwd) conf diff --git a/mdoc-js/src/main/scala/mdoc/modifiers/JsConfig.scala b/mdoc-js/src/main/scala/mdoc/modifiers/JsConfig.scala index 969735a90..aee8ed878 100644 --- a/mdoc-js/src/main/scala/mdoc/modifiers/JsConfig.scala +++ b/mdoc-js/src/main/scala/mdoc/modifiers/JsConfig.scala @@ -1,15 +1,16 @@ package mdoc.modifiers +import java.io.{FileInputStream, InputStreamReader, BufferedReader, IOException} import mdoc.OnLoadContext import mdoc.PostProcessContext import mdoc.internal.pos.PositionSyntax._ +import mdoc.modifiers.ImportMapJsonIr.ImportMap +import mdoc.js.interfaces._ import scala.meta.internal.io.PathIO import scala.meta.io.AbsolutePath import scala.meta.io.Classpath -import mdoc.js.interfaces._ import scala.collection.immutable.HashMap -import com.github.plokhotnyuk.jsoniter_scala.core._ -import mdoc.modifiers.ImportMapJsonIr.ImportMap +import com.github.plokhotnyuk.jsoniter_scala.{core => jsoniter} case class JsConfig( moduleKind: ModuleType = ModuleType.NoModule, @@ -107,9 +108,24 @@ object JsConfig { importMap = ctx.settings.importMapPath match { case None => new HashMap[String, String]() case Some(value) => - val importMapRaw = scala.io.Source.fromFile(value.toFile).getLines().mkString("\n") - val importMapParsed = readFromString[ImportMap](importMapRaw) - importMapParsed.imports + var fileInputStream: FileInputStream = null + try { + fileInputStream = new FileInputStream(value.toFile) + val importMapParsed = jsoniter.readFromStream[ImportMap](fileInputStream) + importMapParsed.imports + } catch { + case e: IOException => + e.printStackTrace() + throw e + } finally { + if (fileInputStream != null) { + try { + fileInputStream.close() + } catch { + case e: IOException => e.printStackTrace() + } + } + } } ) } From 007f49469efc7d135332b90310e9873998637246 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Fri, 30 Aug 2024 11:46:08 +0200 Subject: [PATCH 91/95] remove semicolon --- tests/unit-js/src/test/scala/tests/js/JsSuite.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit-js/src/test/scala/tests/js/JsSuite.scala b/tests/unit-js/src/test/scala/tests/js/JsSuite.scala index 803fd0af7..22f6e32ef 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsSuite.scala @@ -7,7 +7,7 @@ import tests.markdown.BaseMarkdownSuite import tests.js.JsTests.suffix import tests.markdown.Compat import scala.meta.io.AbsolutePath -import java.nio.file.Paths; +import java.nio.file.Paths class JsSuite extends BaseMarkdownSuite { // NOTE(olafur) Optimization. Cache settings to reuse the Scala.js compiler instance. From 12cdf90c5f6fcae348adbfb02bef9693dcb2688f Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Fri, 30 Aug 2024 11:47:40 +0200 Subject: [PATCH 92/95] synatz --- mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala b/mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala index 1a1582dfe..63ecdefc2 100644 --- a/mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala +++ b/mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala @@ -74,7 +74,7 @@ class JsModifier extends mdoc.PreModifier { .withSourceMap(false) .withBatchMode(config.batchMode) .withClosureCompiler( - config.fullOpt && !(config.moduleKind == ModuleType.ESModule) + config.fullOpt && config.moduleKind != ModuleType.ESModule ) // Closure compiler doesn't work with ESModules .withImportMap(config.importMap.asJava) } From fefb8f6ecce2a1d1fc0ff41fee24c86df5feda79 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Mon, 16 Sep 2024 18:04:46 +0200 Subject: [PATCH 93/95] Remove DS_Store, resolve more feedback. --- .DS_Store | Bin 6148 -> 0 bytes .../src/main/scala/ScalaJSWorker.scala | 13 ++++++------- 2 files changed, 6 insertions(+), 7 deletions(-) delete mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 51227d71a94e52683c4d00d01ac912a7dfa75f3b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKI|>3p3{CuiU}I@HSMUad=n3`$7K)81_^Y?_TprDrPoXS!S|~4&yqQeiEc=Sh zMnrUeSOjyq1*=D6P-_S;VFyM%Fva+Zyp5TE&WLZbpy zfC^9nDnJE3tw46z(fHFB^FAs-1%6%u`#u!7VNGlU{nLTqBLHxKv>VnwO8|={fHkoV zL$0gG>bW2DSK<@<*e5h_zG?{ceokWPC@W?4D@!4jkV*c7e!sMHO^~d U8|ZZ8oet#BfayY`0^e5P0W;M1& diff --git a/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala b/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala index 678426a93..04d1da590 100644 --- a/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala +++ b/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala @@ -47,12 +47,16 @@ class ScalaJSWorker( StandardImpl.clearableLinker(cfg) } - lazy val esModuleRemapFunction: String => String = + def esModuleRemapFunction: String => String = config.importMap.asScala.toSeq.foldLeft((in: String) => in) { case (fct, (s1, s2)) => val fct2: (String => String) = (in => in.replace(s1, s2)) (in => fct(fct2(in))) } + val remapImportsInIRFile: IRFile => IRFile = + if (config.importMap.isEmpty) identity + else ImportMappedIRFile.fromIRFile(_)(esModuleRemapFunction) + var cachedFiles = Seq.empty[org.scalajs.linker.interface.IRFile] val cache = StandardImpl.irFileCache().newCache @@ -78,12 +82,7 @@ class ScalaJSWorker( .collect { case IFile(o) => o } - ).map { ir => - if (config.importMap.isEmpty) - ir - else - ImportMappedIRFile.fromIRFile(ir)(esModuleRemapFunction) - }, + ).map { remapImportsInIRFile }, Seq.empty, mem, logger From b40cabb77df0c4b041ce363df7faa1f4195bd251 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Mon, 16 Sep 2024 18:07:07 +0200 Subject: [PATCH 94/95] Update docs/js.md Co-authored-by: Tomasz Godzik --- docs/js.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/js.md b/docs/js.md index 8f0c04edc..88285d1e5 100644 --- a/docs/js.md +++ b/docs/js.md @@ -151,7 +151,7 @@ setInterval(() => { ``` ```` -Is a compile error, because the imports are not in the second block. But add the shared modifier... +Is a compile error, because the imports are not in the second block. But if you add the shared modifier: ````md ```scala mdoc:js:shared From 0301afa644a5f8e9eefff7f0a98e9dea85572051 Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Mon, 16 Sep 2024 18:07:28 +0200 Subject: [PATCH 95/95] Update docs/js.md Co-authored-by: Tomasz Godzik --- docs/js.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/js.md b/docs/js.md index 88285d1e5..ec064734a 100644 --- a/docs/js.md +++ b/docs/js.md @@ -189,7 +189,7 @@ org.scalajs.dom.window.setInterval(() => { }, 1000) ``` ```` -You'd have to look at the raw markdown to see the fence below... +You'd have to look at the raw markdown to see the fence below. ```scala mdoc:js:invisible Loading :invisible example...