Skip to content

Commit

Permalink
Add source map support
Browse files Browse the repository at this point in the history
  • Loading branch information
mprevel authored and lucidd committed Mar 31, 2019
1 parent bf1034c commit 603740b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 15 deletions.
36 changes: 34 additions & 2 deletions examples/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,28 @@ lazy val exampleApp = project.in(file("app"))
scalaJSUseMainModuleInitializer in Test := false,
relativeSourceMaps := true,
skip in packageJSDependencies := false,

// you can customize and have a static output name for lib and dependencies
// instead of having the default files names like app-fastopt.js, ...
artifactPath in (Compile, fastOptJS) := {
(crossTarget in fastOptJS).value / "main.js"
},
artifactPath in (Compile, fullOptJS) := {
(crossTarget in fullOptJS).value / "main.js"
},
artifactPath in (Compile, packageJSDependencies) := {
(crossTarget in packageJSDependencies).value / "dependencies.js"
},
artifactPath in (Compile, packageMinifiedJSDependencies) := {
(crossTarget in packageMinifiedJSDependencies).value / "dependencies.js"
},

chromeManifest := new AppManifest {
val name = Keys.name.value
val version = Keys.version.value
val app = App(
background = Background(
scripts = Chrome.defaultScripts
scripts = List("main.js", "dependencies.js")
)
)
override val defaultLocale = Some("en")
Expand Down Expand Up @@ -71,9 +87,25 @@ lazy val extension = project.in(file("extension"))
scalaJSUseMainModuleInitializer in Test := false,
relativeSourceMaps := true,
skip in packageJSDependencies := false,

// you can customize and have a static output name for lib and dependencies
// instead of having the default files names like extension-fastopt.js, ...
artifactPath in (Compile, fastOptJS) := {
(crossTarget in fastOptJS).value / "main.js"
},
artifactPath in (Compile, fullOptJS) := {
(crossTarget in fullOptJS).value / "main.js"
},
artifactPath in (Compile, packageJSDependencies) := {
(crossTarget in packageJSDependencies).value / "dependencies.js"
},
artifactPath in (Compile, packageMinifiedJSDependencies) := {
(crossTarget in packageMinifiedJSDependencies).value / "dependencies.js"
},

chromeManifest := new ExtensionManifest {
val background = Background(
scripts = Chrome.defaultScripts
scripts = List("main.js", "dependencies.js")
)
val name = Keys.name.value
val version = Keys.version.value
Expand Down
34 changes: 25 additions & 9 deletions sbt-plugin/src/main/scala/net/lullabyte/Chrome.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import sbt._

object Chrome {

val mainFileName = "main.js"
val dependenciesFileName = "dependencies.js"
val defaultScripts = List(dependenciesFileName, mainFileName)
val manifestFileName = "manifest.json"

def i18n(msg: String): String = s"__MSG_${msg}__"

Expand All @@ -20,16 +18,34 @@ object Chrome {
}

def buildUnpackedDirectory(unpacked: File)(manifest: File, jsLib: File,
jsDeps: File, resources: Seq[File]): File = {
jsDeps: File, resources: Seq[File]): File = {

val libsAndDependencies = List(
jsLib -> unpacked / jsLib.getName,
jsDeps -> unpacked / jsDeps.getName
)

val sourceMaps = List(jsLib, jsDeps) map { sourceFile =>
val fileName = sourceFile.getName + ".map"
val originalSourceMap = sourceFile.getParentFile / fileName
originalSourceMap -> unpacked / fileName
} filter (_._1.exists())

val chromeSpecific = List(
manifest -> unpacked / manifestFileName
)

IO.createDirectory(unpacked)

resources.foreach { resource =>
IO.copyDirectory(resource, unpacked, overwrite = true, preserveLastModified = true)
}
IO.copy(List(
(jsLib, unpacked / mainFileName),
(jsDeps, unpacked / dependenciesFileName),
(manifest, unpacked / "manifest.json")
), overwrite = true, preserveLastModified = true, preserveExecutable = true)

IO.copy(
libsAndDependencies ::: sourceMaps ::: chromeSpecific,
overwrite = true, preserveLastModified = true, preserveExecutable = true
)

unpacked
}

Expand Down
9 changes: 5 additions & 4 deletions sbt-plugin/src/main/scala/net/lullabyte/ChromeSbtPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ object ChromeSbtPlugin extends AutoPlugin {
val fullOptJsLib = TaskKey[Attributed[File]]("fullOptJsLib")
val fastOptJsLib = TaskKey[Attributed[File]]("fastOptJsLib")

private val chromeDir = "chrome"

lazy val baseSettings: Seq[Def.Setting[_]] = Seq(
fastOptJsLib := (fastOptJS in Compile).value,
chromeUnpackedFast := {
Chrome.buildUnpackedDirectory(target.value / "chrome" / "unpacked-fast")(
Chrome.buildUnpackedDirectory(target.value / chromeDir / "unpacked-fast")(
(chromeGenerateManifest in Compile).value,
fastOptJsLib.value.data,
(packageJSDependencies in Compile).value,
Expand All @@ -33,15 +34,15 @@ object ChromeSbtPlugin extends AutoPlugin {
},
fullOptJsLib := (fullOptJS in Compile).value,
chromeUnpackedOpt := {
Chrome.buildUnpackedDirectory(target.value / "chrome" / "unpacked-opt")(
Chrome.buildUnpackedDirectory(target.value / chromeDir / "unpacked-opt")(
(chromeGenerateManifest in Compile).value,
fullOptJsLib.value.data,
(packageMinifiedJSDependencies in Compile).value,
(resourceDirectories in Compile).value
)
},
chromePackage := {
val out = target.value / "chrome"
val out = target.value / chromeDir
val chromeAppDir = chromeUnpackedOpt.value
val zipFile = new File(out, s"${name.value}.zip")
val excludeFileNames = Set(
Expand All @@ -52,7 +53,7 @@ object ChromeSbtPlugin extends AutoPlugin {
zipFile
},
chromeGenerateManifest := {
Chrome.generateManifest(target.value / "chrome" / "generated_manifest.json")(
Chrome.generateManifest(target.value / chromeDir / "generated_manifest.json")(
(chromeManifest in Compile).value
)
}
Expand Down

0 comments on commit 603740b

Please sign in to comment.