Skip to content

Commit

Permalink
Implement pnpm, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrdom committed May 8, 2022
1 parent 8f5ec3c commit 21ee699
Show file tree
Hide file tree
Showing 19 changed files with 103 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ jobs:
with:
java-version: "[email protected]"
- uses: coursier/cache-action@v5
- uses: actions/setup-node@v3
with:
node-version: 16.14.2
- name: Setup yarn
run: npm install -g [email protected]
- name: Unit tests
run: sbt test
- name: Scripted tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import sbt._
import scalajsbundler.util.Commands
import scalajsbundler.util.JSON

/**
* Attempts to smoothen platform-specific differences when invoking commands.
*
* @param name Name of the command to run
*/
abstract class PackageManager(val name: String, val installCommand: String, val installArgs: Seq[String]) {
abstract class PackageManager {

val name: String
val installCommand: String
val installArgs: Seq[String]

/**
* Runs the command `cmd`
Expand Down Expand Up @@ -118,29 +117,47 @@ trait LockFileSupport {
}

case class Npm(
name: String = "npm",
lockFileName: String = "package-lock.json",
override val installCommand: String = "install",
override val installArgs: Seq[String] = Seq.empty,
installCommand: String = "install",
installArgs: Seq[String] = Seq.empty,
addPackagesCommand: String = "install",
override val addPackagesArgs: Seq[String] = Seq.empty,
) extends PackageManager("npm", "install", installArgs)
addPackagesArgs: Seq[String] = Seq.empty,
) extends PackageManager
with LockFileSupport
with AddPackagesSupport {
override val packageJsonContents: Map[String, JSON] = Map.empty
}

case class Yarn(
version: String,
name: String = "yarn",
version: Option[String] = None,
lockFileName: String = "yarn.lock",
override val installCommand: String = "install",
override val installArgs: Seq[String] = Yarn.DefaultArgs,
addPackagesCommand: String = "add",
override val addPackagesArgs: Seq[String] = Yarn.DefaultArgs
) extends PackageManager("yarn", "install", installArgs)
) extends PackageManager
with LockFileSupport
with AddPackagesSupport {
override val packageJsonContents: Map[String, JSON] = Map("packageManager" -> JSON.str(s"yarn@$version"))
override val packageJsonContents: Map[String, JSON] =
version.map(v => Map("packageManager" -> JSON.str(s"$name@$v"))).getOrElse(Map.empty)
}
object Yarn {
val DefaultArgs: Seq[String] = Seq("--non-interactive", "--mutex", "network")
}

case class Pnpm(
name: String = "pnpm",
version: Option[String] = None,
lockFileName: String = "pnpm-lock.yaml",
override val installCommand: String = "install",
override val installArgs: Seq[String] = Seq.empty,
addPackagesCommand: String = "add",
override val addPackagesArgs: Seq[String] = Seq.empty
) extends PackageManager
with LockFileSupport
with AddPackagesSupport {
override val packageJsonContents: Map[String, JSON] =
version.map(v => Map("packageManager" -> JSON.str(s"$name@$v"))).getOrElse(Map.empty)
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ object PackageJsonTasks {
fullClasspath.map(_.data.name).toString,
webpackVersion,
webpackDevServerVersion,
webpackCliVersion
webpackCliVersion,
packageManager.toString
).mkString(",")

val packageJsonFile = targetDir / "package.json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ requireJsDomEnv in Test := true

webpackBundlingMode := BundlingMode.LibraryAndApplication()

useYarn := true
packageManager := scalajsbundler.Yarn()

npmDependencies in Compile ++= Seq(
"react" -> reactJS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ requireJsDomEnv in Test := true

version in installJsdom := "12.0.0"

useYarn := true
packageManager := scalajsbundler.Yarn()

ivyLoggingLevel in ThisBuild := UpdateLogging.Quiet
11 changes: 11 additions & 0 deletions sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/npm/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
scalaVersion := "2.12.8"

packageManager := scalajsbundler.Npm()

scalaJSUseMainModuleInitializer := true

npmDependencies in Compile += "neat" -> "2.1.0"

enablePlugins(ScalaJSBundlerPlugin)

ivyLoggingLevel in ThisBuild := UpdateLogging.Quiet
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
val scalaJSVersion = sys.props.getOrElse("scalajs.version", sys.error("'scalajs.version' environment variable is not defined"))
val scalaJSBundlerVersion = sys.props.getOrElse("plugin.version", sys.error("'plugin.version' environment variable is not set"))

addSbtPlugin("org.scala-js" % "sbt-scalajs" % scalaJSVersion)

addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % scalaJSBundlerVersion)

ivyLoggingLevel in ThisBuild := UpdateLogging.Quiet
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package example

object Main {
def main(args: Array[String]): Unit = {
println("npm main")
}
}
2 changes: 2 additions & 0 deletions sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/npm/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
> fastOptJS::webpack
$ exists package-lock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
scalaVersion := "2.12.8"

packageManager := scalajsbundler.Pnpm(version = Some("7.0.1"))

scalaJSUseMainModuleInitializer := true

npmDependencies in Compile += "neat" -> "2.1.0"

enablePlugins(ScalaJSBundlerPlugin)

ivyLoggingLevel in ThisBuild := UpdateLogging.Quiet
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
val scalaJSVersion = sys.props.getOrElse("scalajs.version", sys.error("'scalajs.version' environment variable is not defined"))
val scalaJSBundlerVersion = sys.props.getOrElse("plugin.version", sys.error("'plugin.version' environment variable is not set"))

addSbtPlugin("org.scala-js" % "sbt-scalajs" % scalaJSVersion)

addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % scalaJSBundlerVersion)

ivyLoggingLevel in ThisBuild := UpdateLogging.Quiet
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package example

object Main {
def main(args: Array[String]): Unit = {
println("pnpm main")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
> fastOptJS::webpack
$ exists pnpm-lock.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ requireJsDomEnv in Test := true

webpackBundlingMode in fastOptJS := BundlingMode.LibraryAndApplication()

useYarn := true
packageManager := scalajsbundler.Yarn()

// HtmlUnit does not support ECMAScript 2015
scalaJSLinkerConfig ~= { _.withESFeatures(_.withUseECMAScript2015(false)) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ version in installJsdom := "16.4.0"

webpackBundlingMode := BundlingMode.LibraryAndApplication()

useYarn := true
packageManager := scalajsbundler.Yarn()

// HtmlUnit does not support ECMAScript 2015
scalaJSLinkerConfig ~= { _.withESFeatures(_.withUseECMAScript2015(false)) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ npmDevDependencies in Compile += "mini-css-extract-plugin" -> "1.3.4"

webpackDevServerPort := 7357

useYarn := true
packageManager := scalajsbundler.Yarn()

// HtmlUnit does not support ECMAScript 2015
scalaJSLinkerConfig ~= { _.withESFeatures(_.withUseECMAScript2015(false)) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ webpackConfigFile := Some(baseDirectory.value / "webpack.config.js")

(npmDevDependencies in Compile) += ("html-webpack-plugin" -> "5.2.0")

webpackCliVersion := "4.9.0"

webpackDevServerPort := 7357

// HtmlUnit does not support ECMAScript 2015
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
scalaVersion := "2.12.8"

useYarn := true

yarnExtraArgs in Compile := Seq("--silent")
packageManager := scalajsbundler.Yarn(installArgs = Seq("--silent"), addPackagesArgs = Seq("--silent"))

scalaJSUseMainModuleInitializer := true

npmDependencies in Compile += "neat" -> "1.1.2"
npmDependencies in Compile += "neat" -> "2.1.0"

enablePlugins(ScalaJSBundlerPlugin)

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
> fastOptJS::webpack
$ exists yarn.lock

0 comments on commit 21ee699

Please sign in to comment.