Skip to content

Commit

Permalink
fixed webjarMainResource and filter out unneeded artifacts from jar
Browse files Browse the repository at this point in the history
  • Loading branch information
lhns committed May 20, 2020
1 parent 6d236bc commit dab9cde
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 28 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
lazy val commonSettings: Seq[Setting[_]] = Seq(
organization := "de.lolhens",
version := "0.3.0-SNAPSHOT",
version := "0.3.1-SNAPSHOT",

licenses += ("Apache-2.0", url("https://www.apache.org/licenses/LICENSE-2.0")),

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package de.lolhens.sbt.scalajs.webjar

import de.lolhens.sbt.scalajs.webjar.ScalaJSWebjarPlugin.scalaJSLinkedFileTask
import de.lolhens.sbt.scalajs.webjar.ScalaJSWebjarPlugin.stagedOptJS
import de.lolhens.sbt.scalajs.webjar.WebjarPlugin.autoImport._
import sbt.Keys._
import sbt._
Expand All @@ -10,18 +10,19 @@ import scalajsbundler.sbtplugin.ScalaJSBundlerPlugin.autoImport._
object ScalaJSBundlerWebjarPlugin extends AutoPlugin {
override def requires: Plugins = ScalaJSBundlerPlugin && WebjarPlugin

override lazy val projectSettings = Seq(
Compile / npmUpdate / crossTarget := (Compile / webjarArtifacts / crossTarget).value,
override lazy val projectSettings: Seq[Setting[_]] =
Seq(
Compile / npmUpdate / crossTarget := (Compile / webjarArtifacts / crossTarget).value,

Compile / webjarMainResourceName := scalaJSLinkedFileTask(Compile / _ / artifactPath).value.name.stripSuffix(".js") + "-bundle.js",
Compile / webjarMainResourceName := stagedOptJS(Compile / _ / artifactPath).value.name.stripSuffix(".js") + "-bundle.js",

Compile / webjarArtifacts := {
val attributedFiles = scalaJSLinkedFileTask(Compile / _ / webpack).value
Compile / webjarArtifacts := {
val attributedFiles = stagedOptJS(Compile / _ / webpack).value

Seq(
attributedFiles.find(_.metadata.get(BundlerFileTypeAttr).exists(_ == BundlerFileType.ApplicationBundle)).map(_.data),
attributedFiles.find(_.metadata.get(BundlerFileTypeAttr).exists(_ == BundlerFileType.Asset)).map(_.data),
).flatMap(_.toList)
}
)
Seq(
attributedFiles.find(_.metadata.get(BundlerFileTypeAttr).exists(_ == BundlerFileType.ApplicationBundle)).map(_.data),
attributedFiles.find(_.metadata.get(BundlerFileTypeAttr).exists(_ == BundlerFileType.Asset)).map(_.data),
).flatMap(_.toList)
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ lazy val frontend = project

scalaJSUseMainModuleInitializer := true,

Compile / npmDependencies ++= Seq(
"react" -> "16.13.1",
"react-dom" -> "16.13.1"
),

Compile / fastOptJS / webpack := {
val v = (Compile / fastOptJS / webpack).value
println(v.seq.map(_.metadata.entries.toList).mkString("\n"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@ import sbt._
object ScalaJSWebjarPlugin extends AutoPlugin {
override def requires: Plugins = ScalaJSPlugin && WebjarPlugin

private[webjar] def scalaJSLinkedFileTask[A](f: TaskKey[Attributed[File]] => Def.Initialize[A]): Def.Initialize[A] =
private[webjar] def stagedOptJS[A](f: TaskKey[Attributed[File]] => Def.Initialize[A]): Def.Initialize[A] =
Def.settingDyn {
scalaJSStage.value match {
case Stage.FastOpt => f(fastOptJS)
case Stage.FullOpt => f(fullOptJS)
}
}

override lazy val projectSettings = Seq(
Compile / fastOptJS / crossTarget := (Compile / webjarArtifacts / crossTarget).value,
Compile / fullOptJS / crossTarget := (Compile / webjarArtifacts / crossTarget).value,
override lazy val projectSettings: Seq[Setting[_]] =
List(fastOptJS, fullOptJS).map { stagedOptJS =>
Compile / stagedOptJS / crossTarget := (Compile / webjarArtifacts / crossTarget).value
} ++ Seq(
Compile / webjarMainResourceName := stagedOptJS(Compile / _ / artifactPath).value.name,

Compile / webjarMainResourceName := scalaJSLinkedFileTask(Compile / _ / artifactPath).value.name,
Compile / webjarArtifacts := {
val attributedFile = (Compile / scalaJSLinkedFile).value

Compile / webjarArtifacts := {
val attributedFile = (Compile / scalaJSLinkedFile).value

Seq(
attributedFile.data,
attributedFile.metadata(scalaJSSourceMap),
)
}
)
Seq(
attributedFile.data,
attributedFile.metadata(scalaJSSourceMap),
)
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@ class WebjarProject(val self: Project) extends AnyVal {

self / Compile / webjarResourcePath := s"META-INF/resources/webjars/${(self / name).value}/${(self / version).value}",

self / Compile / webjarMainResource := (self / Compile / webjarResourcePath).value + "/" + (self / Compile / webjarMainResourceName),
self / Compile / webjarMainResource := (self / Compile / webjarResourcePath).value + "/" + (self / Compile / webjarMainResourceName).value,

self / Compile / webjarArtifacts / crossTarget := {
(Compile / classDirectory).value.toPath.resolve((self / Compile / webjarResourcePath).value).toFile
},

Compile / compile := (Compile / compile).dependsOn(self / Compile / webjarArtifacts).value,

Compile / packageBin / mappings := {
val artifacts = (self / Compile / webjarArtifacts).value
(Compile / packageBin / mappings).value.filter(mapping => artifacts.contains(mapping._1))
}
)
}
}

0 comments on commit dab9cde

Please sign in to comment.