Skip to content

Commit

Permalink
Remove Scala.js module esbuild configuration (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrdom authored Feb 14, 2024
1 parent 7ca4b6c commit 56f9a8f
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import sbt.Keys.*
import scalajsesbuild.ScalaJSEsbuildPlugin.autoImport.esbuildBundle
import scalajsesbuild.ScalaJSEsbuildPlugin.autoImport.esbuildBundleScript
import scalajsesbuild.ScalaJSEsbuildPlugin.autoImport.esbuildInstall
import scalajsesbuild.ScalaJSEsbuildPlugin.autoImport.esbuildScalaJSModuleConfigurations
import scalajsesbuild.ScalaJSEsbuildWebPlugin.autoImport.esbuildBundleHtmlEntryPoints
import scalajsesbuild.ScalaJSEsbuildWebPlugin.autoImport.esbuildServeScript
import scalajsesbuild.ScalaJSEsbuildWebPlugin.autoImport.esbuildServeStart
Expand Down Expand Up @@ -57,7 +56,6 @@ object ScalaJSEsbuildElectronPlugin extends AutoPlugin {
inConfig(Test)(perConfigSettings)

private lazy val perConfigSettings: Seq[Setting[?]] = Seq(
esbuildScalaJSModuleConfigurations := Map.empty,
jsEnvInput := jsEnvInputTask.value,
run := Def.taskDyn {
val stageTask = scalaJSStage.value.stageTask
Expand Down Expand Up @@ -154,7 +152,7 @@ object ScalaJSEsbuildElectronPlugin extends AutoPlugin {
|${EsbuildWebScripts.transformHtmlEntryPoints}
|
|bundle(
| ${EsbuildScalaJSModuleConfiguration.EsbuildPlatform.Node.jsValue},
| ${EsbuildPlatform.Node.jsValue},
| $nodeEntryPointsJsArray,
| $relativeOutputDirectoryJs,
| null,
Expand All @@ -165,7 +163,7 @@ object ScalaJSEsbuildElectronPlugin extends AutoPlugin {
|);
|
|const metaFilePromise = bundle(
| ${EsbuildScalaJSModuleConfiguration.EsbuildPlatform.Browser.jsValue},
| ${EsbuildPlatform.Browser.jsValue},
| $rendererModuleEntryPointsJsArray,
| $relativeOutputDirectoryJs,
| 'assets',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import scalajsesbuild.ScalaJSEsbuildPlugin.autoImport.esbuildBundleScript
import scalajsesbuild.ScalaJSEsbuildPlugin.autoImport.esbuildCompile
import scalajsesbuild.ScalaJSEsbuildPlugin.autoImport.esbuildInstall
import scalajsesbuild.ScalaJSEsbuildPlugin.autoImport.esbuildRunner
import scalajsesbuild.ScalaJSEsbuildPlugin.autoImport.esbuildScalaJSModuleConfigurations
import java.nio.file.Path

import scala.sys.process.*
Expand Down Expand Up @@ -102,13 +101,7 @@ object ScalaJSEsbuildWebPlugin extends AutoPlugin {
Seq(
stageTask / esbuildBundleScript := {
val stageTaskReport = stageTask.value.data
val moduleConfigurations = esbuildScalaJSModuleConfigurations.value
val entryPoints =
extractEntryPointsByPlatform(stageTaskReport, moduleConfigurations)
.getOrElse(
EsbuildScalaJSModuleConfiguration.EsbuildPlatform.Browser,
Set.empty
)
val entryPoints = jsFileNames(stageTaskReport)
val entryPointsJsArray =
entryPoints.map("'" + _ + "'").mkString("[", ",", "]")
val targetDirectory = (esbuildInstall / crossTarget).value
Expand Down Expand Up @@ -148,7 +141,7 @@ object ScalaJSEsbuildWebPlugin extends AutoPlugin {
|${EsbuildWebScripts.transformHtmlEntryPoints}
|
|const metaFilePromise = bundle(
| ${EsbuildScalaJSModuleConfiguration.EsbuildPlatform.Browser.jsValue},
| ${EsbuildPlatform.Browser.jsValue},
| $entryPointsJsArray,
| $relativeOutputDirectoryJs,
| 'assets',
Expand All @@ -169,13 +162,7 @@ object ScalaJSEsbuildWebPlugin extends AutoPlugin {
},
stageTask / esbuildServeScript := {
val stageTaskReport = stageTask.value.data
val moduleConfigurations = esbuildScalaJSModuleConfigurations.value
val entryPoints =
extractEntryPointsByPlatform(stageTaskReport, moduleConfigurations)
.getOrElse(
EsbuildScalaJSModuleConfiguration.EsbuildPlatform.Browser,
Set.empty
)
val entryPoints = jsFileNames(stageTaskReport)
val entryPointsJsArray =
entryPoints.map("'" + _ + "'").mkString("[", ",", "]")
val targetDirectory = (esbuildInstall / crossTarget).value
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -129,37 +129,4 @@ object EsbuildScripts {
|};
|""".stripMargin
}

private[scalajsesbuild] def bundleByPlatform = {
// language=JS
"""const bundleByPlatform = async (
| entryPointsByPlatform,
| outDirectory,
| outputFilesDirectory,
| hashOutputFiles,
| minify
|) => {
| return await Promise
| .all(
| Object.keys(entryPointsByPlatform).reduce((acc, platform) => {
| const platformMetafilePromise =
| bundle(
| platform,
| entryPointsByPlatform[platform],
| outDirectory,
| outputFilesDirectory,
| hashOutputFiles,
| minify
| )
| .then((metafile) => {
| return {[platform]: metafile};
| });
| acc.push(platformMetafilePromise);
| return acc;
| }, [])
| )
| .then((results) => results.reduce((acc, result) => ({...acc, ...result}) , {}));
|};
|""".stripMargin
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ object ScalaJSEsbuildPlugin extends AutoPlugin {
taskKey(
"Compiles module and copies output to target directory"
)
val esbuildScalaJSModuleConfigurations
: TaskKey[Map[String, EsbuildScalaJSModuleConfiguration]] = taskKey(
"esbuild configurations for Scala.js modules"
)
val esbuildBundleScript: TaskKey[String] = taskKey(
"esbuild script used for bundling"
) // TODO consider doing the writing of the script upon call of this task, then use FileChanges to track changes to the script
Expand Down Expand Up @@ -84,21 +80,6 @@ object ScalaJSEsbuildPlugin extends AutoPlugin {
inConfig(Test)(perConfigSettings)

private lazy val perConfigSettings: Seq[Setting[?]] = Seq(
esbuildScalaJSModuleConfigurations := {
val moduleKind = scalaJSLinkerConfig.value.moduleKind
val scalaJSModuleConfiguration = new EsbuildScalaJSModuleConfiguration(
platform = moduleKind match {
case ModuleKind.CommonJSModule =>
EsbuildScalaJSModuleConfiguration.EsbuildPlatform.Node
case _ =>
EsbuildScalaJSModuleConfiguration.EsbuildPlatform.Browser
}
)
val modules = scalaJSModuleInitializers.value
modules
.map(module => module.moduleID -> scalaJSModuleConfiguration)
.toMap
},
esbuildInstall / crossTarget := {
crossTarget.value /
"esbuild" /
Expand Down Expand Up @@ -200,13 +181,9 @@ object ScalaJSEsbuildPlugin extends AutoPlugin {
stageTask / esbuildBundle / crossTarget := (esbuildInstall / crossTarget).value / "out",
stageTask / esbuildBundleScript := {
val stageTaskReport = stageTask.value.data
val moduleConfigurations = esbuildScalaJSModuleConfigurations.value
val entryPointsByPlatform =
extractEntryPointsByPlatform(stageTaskReport, moduleConfigurations)
val entryPointsByPlatformJs = "{" + entryPointsByPlatform
.foldLeft("") { case (acc, (platform, entryPoints)) =>
acc + s"${platform.jsValue}:${entryPoints.map("'" + _ + "'").mkString("[", ",", "]")}"
} + "}"
val entryPoints = jsFileNames(stageTaskReport)
val entryPointsJs =
s"${entryPoints.map("'" + _ + "'").mkString("[", ",", "]")}"
val targetDirectory = (esbuildInstall / crossTarget).value
val outputDirectory =
(stageTask / esbuildBundle / crossTarget).value
Expand All @@ -226,20 +203,24 @@ object ScalaJSEsbuildPlugin extends AutoPlugin {
true
}

val moduleKind = scalaJSLinkerConfig.value.moduleKind
val platformJs = EsbuildPlatform(moduleKind).jsValue

// language=JS
s"""
|${EsbuildScripts.esbuildOptions}
|
|${EsbuildScripts.bundle}
|
|${EsbuildScripts.bundleByPlatform}
|
|bundleByPlatform(
| $entryPointsByPlatformJs,
|bundle(
| $platformJs,
| $entryPointsJs,
| $relativeOutputDirectoryJs,
| null,
| false,
| $minify
| $minify,
| null,
| null
|);
|""".stripMargin
},
Expand Down
52 changes: 16 additions & 36 deletions sbt-scalajs-esbuild/src/main/scala/scalajsesbuild/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import org.scalajs.sbtplugin.ScalaJSPlugin.autoImport.fastLinkJS
import org.scalajs.sbtplugin.ScalaJSPlugin.autoImport.fullLinkJS
import org.scalajs.sbtplugin.ScalaJSPlugin.autoImport.scalaJSStage
import sbt.*
import sbt.Keys.configuration
import sbt.Keys.crossTarget
import scalajsesbuild.ScalaJSEsbuildPlugin.autoImport.esbuildBundle
import scalajsesbuild.ScalaJSEsbuildPlugin.autoImport.esbuildFastLinkJSWrapper
Expand All @@ -34,44 +33,10 @@ package object scalajsesbuild {
}
}

private[scalajsesbuild] def extractEntryPointsByPlatform(
report: Report,
moduleConfigurations: Map[String, EsbuildScalaJSModuleConfiguration]
) = {
report match {
case report: unstable.ReportImpl =>
report.publicModules
.foldLeft(
Map.empty[EsbuildScalaJSModuleConfiguration.EsbuildPlatform, Set[
String
]]
) { case (acc, publicModule) =>
val platform = moduleConfigurations
.getOrElse(
publicModule.moduleID,
sys.error(
s"esbuild module configuration missing for moduleID [${publicModule.moduleID}]"
)
)
.platform
acc.updated(
platform,
acc.getOrElse(platform, Set.empty) + publicModule.jsFileName
)
}
case unhandled =>
sys.error(s"Unhandled report type [$unhandled]")
}
}

private[scalajsesbuild] def jsFileNames(report: Report) = {
report match {
case report: unstable.ReportImpl =>
val jsFileNames = report.publicModules
.map { publicModule =>
publicModule.jsFileName
}
jsFileNames
report.publicModules.map(publicModule => publicModule.jsFileName).toSet
case unhandled =>
sys.error(s"Unhandled report type [$unhandled]")
}
Expand Down Expand Up @@ -150,4 +115,19 @@ package object scalajsesbuild {
Seq(Input.Script(path))
}
}

private[scalajsesbuild] sealed trait EsbuildPlatform {
def jsValue: String = s"'${toString.toLowerCase}'"
}
private[scalajsesbuild] object EsbuildPlatform {
case object Browser extends EsbuildPlatform
case object Node extends EsbuildPlatform

def apply(moduleKind: ModuleKind): EsbuildPlatform = {
moduleKind match {
case ModuleKind.CommonJSModule => Node
case _ => Browser
}
}
}
}

0 comments on commit 56f9a8f

Please sign in to comment.