diff --git a/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala b/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala index 033523a6e..7e431a155 100644 --- a/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala +++ b/mdoc-js-worker/src/main/scala/ScalaJSWorker.scala @@ -18,13 +18,11 @@ import org.scalajs.logging.Logger import org.scalajs.logging.Level import org.scalajs.linker.standard.MemIRFileImpl import org.scalajs.linker.interface.Semantics - class ScalaJSWorker( config: ScalajsConfig, logger: Logger ) extends ScalajsWorkerApi { case class IFile(mem: IRFile) extends ScalajsWorkerApi.IRFile - val linker = { val cfg = StandardConfig() diff --git a/mdoc-js/src/main/resources/mdoc_esmodule.js b/mdoc-js/src/main/resources/mdoc_esmodule.js new file mode 100644 index 000000000..79dedcb97 --- /dev/null +++ b/mdoc-js/src/main/resources/mdoc_esmodule.js @@ -0,0 +1,17 @@ +(function (global) { + function findDivs() { + return Array.from(global.document.querySelectorAll("div[data-mdoc-js]")); + } + + function loadAll(scope) { + findDivs().forEach(function (el) { + const id = el.getAttribute("id").replace("-html-", "_js_"); + const moduleName = el.getAttribute("data-mdoc-module-name"); + import(moduleName).then(function (module) { + module[id](el); + }); + }); + } + + loadAll(global); +})(window); diff --git a/mdoc-js/src/main/resources/mdoc.js b/mdoc-js/src/main/resources/mdoc_nomodule.js similarity index 100% rename from mdoc-js/src/main/resources/mdoc.js rename to mdoc-js/src/main/resources/mdoc_nomodule.js diff --git a/mdoc-js/src/main/scala/mdoc/modifiers/JsConfig.scala b/mdoc-js/src/main/scala/mdoc/modifiers/JsConfig.scala index 8cc56cd2b..0cdc7a3f9 100644 --- a/mdoc-js/src/main/scala/mdoc/modifiers/JsConfig.scala +++ b/mdoc-js/src/main/scala/mdoc/modifiers/JsConfig.scala @@ -22,7 +22,8 @@ case class JsConfig( relativeLinkPrefix: String = "", batchMode: Boolean = false ) { - def isCommonJS: Boolean = moduleKind == ModuleType.CommonJSModule + lazy val isCommonJS: Boolean = moduleKind == ModuleType.CommonJSModule + lazy val isEsModule: Boolean = moduleKind == ModuleType.ESModule def libraryScripts( outjsfile: AbsolutePath, ctx: PostProcessContext @@ -37,7 +38,12 @@ case class JsConfig( if (filename.endsWith(".js")) { lib.copyTo(out) val src = out.toRelativeLinkFrom(ctx.outputFile, relativeLinkPrefix) - List(s"""""") + isEsModule match { + case true => + List(s"""""") + case false => + List(s"""""") + } } else if (filename.endsWith(".js.map")) { lib.copyTo(out) Nil diff --git a/mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala b/mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala index 881fd6c5d..afc562bf9 100644 --- a/mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala +++ b/mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala @@ -24,6 +24,9 @@ import scala.concurrent.Future import java.nio.file.Paths import mdoc.js.interfaces._ import java.util.ServiceLoader +import mdoc.js.interfaces.ModuleType.CommonJSModule +import mdoc.js.interfaces.ModuleType.ESModule +import mdoc.js.interfaces.ModuleType.NoModule class JsModifier extends mdoc.PreModifier { override val name = "js" @@ -173,15 +176,30 @@ class JsModifier extends mdoc.PreModifier { val outjsfile = resolveOutputJsFile(inputFile) outjsfile.write(new String(content)) val outmdoc = outjsfile.resolveSibling(_ => "mdoc.js") - outmdoc.write(Resources.readPath("/mdoc.js")) + val selectMocTemplate = config.moduleKind match { + case CommonJSModule => "/mdoc_nomodule.js" + case ESModule => "/mdoc_esmodule.js" + case NoModule => "/mdoc_nomodule.js" + } + outmdoc.write(Resources.readPath(selectMocTemplate)) val relfile = outjsfile.toRelativeLinkFrom(ctx.outputFile, config.relativeLinkPrefix) val relmdoc = outmdoc.toRelativeLinkFrom(ctx.outputFile, config.relativeLinkPrefix) - new CodeBuilder() - .println(config.htmlHeader) - .lines(config.libraryScripts(outjsfile, ctx)) - .println(s"""""") - .println(s"""""") - .toString + config.moduleKind match { + case NoModule | CommonJSModule => + new CodeBuilder() + .println(config.htmlHeader) + .lines(config.libraryScripts(outjsfile, ctx)) + .println(s"""""") + .println(s"""""") + .toString + case ESModule => + new CodeBuilder() + .println(config.htmlHeader) + .lines(config.libraryScripts(outjsfile, ctx)) + .println(s"""""") + .println(s"""""") + .toString + } } } } @@ -250,6 +268,8 @@ class JsModifier extends mdoc.PreModifier { .toString } + val outModule = ctx.outputFile.toRelative(ctx.outDirectory) + runs += code new CodeBuilder() .printIf(!mods.isInvisible, s"```scala") @@ -257,7 +277,14 @@ class JsModifier extends mdoc.PreModifier { .printIf(remainingMods.isEmpty && !mods.isInvisible, s"\n") .printIf(!mods.isInvisible, s"${input.text}\n```") .printIf(!mods.isInvisible, s"\n") - .printlnIf(mods.isEntrypoint, s"""
$body
""") + .printlnIf( + mods.isEntrypoint && !config.isEsModule, + s"""
$body
""" + ) + .printlnIf( + mods.isEntrypoint && config.isEsModule, + s"""
$body
""" + ) .toString } } 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 56adba9c8..bb7429b17 100644 --- a/tests/unit-js/src/test/scala/tests/js/JsSuite.scala +++ b/tests/unit-js/src/test/scala/tests/js/JsSuite.scala @@ -32,6 +32,27 @@ class JsSuite extends BaseMarkdownSuite { """.stripMargin ) + check( + "basic_es", + """ + |```scala mdoc:js + |println("hello world!") + |``` + |""".stripMargin, + """|```scala + |println("hello world!") + |``` + |
+ | + | + """.stripMargin, + settings = { + baseSettings.copy( + site = baseSettings.site.updated("js-module-kind", "ESModule") + ) + } + ) + check( "extra_fences", """