Skip to content

Commit

Permalink
Merge pull request #41 from sjrd/fix-jsenvinput-with-commonjsname
Browse files Browse the repository at this point in the history
Fix #40: Generate valid path names for `require-$name.js` files.
  • Loading branch information
sjrd authored May 22, 2020
2 parents 1342837 + 18e76ab commit a97eba0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ object JSDependenciesPlugin extends AutoPlugin {

}

private lazy val memFS = Jimfs.newFileSystem()

import autoImport._

/** Collect certain file types from a classpath.
Expand Down Expand Up @@ -131,6 +129,7 @@ object JSDependenciesPlugin extends AutoPlugin {
import java.util.zip._

val jarPath = jar.getPath
val memFS = Jimfs.newFileSystem()

val stream =
new ZipInputStream(new BufferedInputStream(new FileInputStream(jar)))
Expand Down Expand Up @@ -217,7 +216,7 @@ object JSDependenciesPlugin extends AutoPlugin {

private val tmpSuffixRE = """[a-zA-Z0-9-_.]*$""".r

private def tmpFile(path: String, in: InputStream): URI = {
private def tmpFile(path: String, in: InputStream): File = {
try {
/* - createTempFile requires a prefix of at least 3 chars
* - we use a safe part of the path as suffix so the extension stays (some
Expand All @@ -228,15 +227,15 @@ object JSDependenciesPlugin extends AutoPlugin {
val f = File.createTempFile("tmp-", suffix)
f.deleteOnExit()
Files.copy(in, f.toPath(), StandardCopyOption.REPLACE_EXISTING)
f.toURI()
f
} finally {
in.close()
}
}

private def materialize(file: Path): URI = {
private def materialize(file: Path): File = {
try {
file.toFile().toURI()
file.toFile()
} catch {
case _: UnsupportedOperationException =>
tmpFile(file.toString(), new BufferedInputStream(Files.newInputStream(file)))
Expand Down Expand Up @@ -401,12 +400,25 @@ object JSDependenciesPlugin extends AutoPlugin {
*/
val libs = env match {
case _: org.scalajs.jsenv.nodejs.NodeJSEnv =>
val memFS = Jimfs.newFileSystem()
val usedNames = mutable.Set.empty[String]

def getFileNameFor(commonJSName: String): String = {
var name = commonJSName
var suffix = 0
while (!usedNames.add(name)) {
suffix += 1
name = s"${commonJSName}_$suffix"
}
s"require-$name.js"
}

for (dep <- deps) yield {
dep.info.commonJSName.fold {
dep.lib
} { commonJSName =>
val fname = materialize(dep.lib).toASCIIString
Files.write(memFS.getPath(s"require-$fname"),
val fname = materialize(dep.lib).getAbsolutePath
Files.write(memFS.getPath(getFileNameFor(commonJSName)),
s"""$commonJSName = require("${escapeJS(fname)}");""".getBytes(StandardCharsets.UTF_8))
}
}
Expand Down
14 changes: 13 additions & 1 deletion sbt-plugin-test/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ addCommandAlias("testAll",
";jsDependenciesTest/packageJSDependencies" +
";jsDependenciesTest/packageMinifiedJSDependencies" +
";jsDependenciesTest/regressionTestForIssue2243" +
";jsNoDependenciesTest/regressionTestForIssue2243")
";jsNoDependenciesTest/regressionTestForIssue2243" +
";jsDependenciesRunTest/run")

val regressionTestForIssue2243 = TaskKey[Unit]("regressionTestForIssue2243",
"", KeyRanks.BTask)
Expand Down Expand Up @@ -114,3 +115,14 @@ lazy val jsNoDependenciesTest = withRegretionTestForIssue2243(
project.
enablePlugins(ScalaJSPlugin, JSDependenciesPlugin)
)

lazy val jsDependenciesRunTest =
project.
enablePlugins(ScalaJSPlugin, JSDependenciesPlugin).
settings(
scalaJSUseMainModuleInitializer := true,
jsDependencies ++= Seq(
// #40 Make sure we can run in the presence of a commonJSName
"org.webjars" % "mustachejs" % "0.8.2" / "mustache.js" commonJSName "Mustache",
),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package test

import scala.scalajs.js

object Test {
def main(args: Array[String]): Unit = {
println("Hello world")
assert(js.typeOf(js.Dynamic.global.Mustache) == "object")
assert((js.Dynamic.global.Mustache.version: Any) == "0.8.1")
}
}

0 comments on commit a97eba0

Please sign in to comment.