From 21ee699990ad9009898013139326d5b263391f6f Mon Sep 17 00:00:00 2001 From: Domantas Petrauskas Date: Sat, 7 May 2022 10:18:34 +0300 Subject: [PATCH] Implement pnpm, fix tests --- .github/workflows/ci.yml | 5 +++ .../scala/scalajsbundler/PackageManager.scala | 43 +++++++++++++------ .../sbtplugin/PackageJsonTasks.scala | 3 +- .../custom-test-config/build.sbt | 2 +- .../build.sbt | 2 +- .../sbt-scalajs-bundler/npm/build.sbt | 11 +++++ .../npm/project/plugins.sbt | 8 ++++ .../npm/src/main/scala/example/Main.scala | 7 +++ .../src/sbt-test/sbt-scalajs-bundler/npm/test | 2 + .../sbt-scalajs-bundler/pnpm/build.sbt | 11 +++++ .../pnpm/project/plugins.sbt | 8 ++++ .../pnpm/src/main/scala/example/Main.scala | 7 +++ .../sbt-test/sbt-scalajs-bundler/pnpm/test | 2 + .../sharedconfig/build.sbt | 2 +- .../sbt-scalajs-bundler/static/build.sbt | 2 +- .../webpack-assets/build.sbt | 2 +- .../webpack-dev-server/build.sbt | 2 + .../yarn-interactive/build.sbt | 6 +-- .../sbt-scalajs-bundler/yarn-interactive/test | 1 + 19 files changed, 103 insertions(+), 23 deletions(-) create mode 100644 sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/npm/build.sbt create mode 100644 sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/npm/project/plugins.sbt create mode 100644 sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/npm/src/main/scala/example/Main.scala create mode 100644 sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/npm/test create mode 100644 sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/pnpm/build.sbt create mode 100644 sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/pnpm/project/plugins.sbt create mode 100644 sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/pnpm/src/main/scala/example/Main.scala create mode 100644 sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/pnpm/test diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ffed4906..601b64e7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,6 +15,11 @@ jobs: with: java-version: "adopt@1.11" - uses: coursier/cache-action@v5 + - uses: actions/setup-node@v3 + with: + node-version: 16.14.2 + - name: Setup yarn + run: npm install -g yarn@1.22.15 - name: Unit tests run: sbt test - name: Scripted tests diff --git a/sbt-scalajs-bundler/src/main/scala/scalajsbundler/PackageManager.scala b/sbt-scalajs-bundler/src/main/scala/scalajsbundler/PackageManager.scala index 2477db6b..ef22d1dd 100644 --- a/sbt-scalajs-bundler/src/main/scala/scalajsbundler/PackageManager.scala +++ b/sbt-scalajs-bundler/src/main/scala/scalajsbundler/PackageManager.scala @@ -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` @@ -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) +} diff --git a/sbt-scalajs-bundler/src/main/scala/scalajsbundler/sbtplugin/PackageJsonTasks.scala b/sbt-scalajs-bundler/src/main/scala/scalajsbundler/sbtplugin/PackageJsonTasks.scala index d613833a..dd7650a8 100644 --- a/sbt-scalajs-bundler/src/main/scala/scalajsbundler/sbtplugin/PackageJsonTasks.scala +++ b/sbt-scalajs-bundler/src/main/scala/scalajsbundler/sbtplugin/PackageJsonTasks.scala @@ -43,7 +43,8 @@ object PackageJsonTasks { fullClasspath.map(_.data.name).toString, webpackVersion, webpackDevServerVersion, - webpackCliVersion + webpackCliVersion, + packageManager.toString ).mkString(",") val packageJsonFile = targetDir / "package.json" diff --git a/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/custom-test-config/build.sbt b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/custom-test-config/build.sbt index 13a4e1b8..c9064f5f 100644 --- a/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/custom-test-config/build.sbt +++ b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/custom-test-config/build.sbt @@ -25,7 +25,7 @@ requireJsDomEnv in Test := true webpackBundlingMode := BundlingMode.LibraryAndApplication() -useYarn := true +packageManager := scalajsbundler.Yarn() npmDependencies in Compile ++= Seq( "react" -> reactJS, diff --git a/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/global-namespace-with-jsdom-unit-testing/build.sbt b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/global-namespace-with-jsdom-unit-testing/build.sbt index e3e58a59..148c49d4 100644 --- a/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/global-namespace-with-jsdom-unit-testing/build.sbt +++ b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/global-namespace-with-jsdom-unit-testing/build.sbt @@ -28,6 +28,6 @@ requireJsDomEnv in Test := true version in installJsdom := "12.0.0" -useYarn := true +packageManager := scalajsbundler.Yarn() ivyLoggingLevel in ThisBuild := UpdateLogging.Quiet diff --git a/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/npm/build.sbt b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/npm/build.sbt new file mode 100644 index 00000000..1d34eafe --- /dev/null +++ b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/npm/build.sbt @@ -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 diff --git a/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/npm/project/plugins.sbt b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/npm/project/plugins.sbt new file mode 100644 index 00000000..77ac06f1 --- /dev/null +++ b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/npm/project/plugins.sbt @@ -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 diff --git a/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/npm/src/main/scala/example/Main.scala b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/npm/src/main/scala/example/Main.scala new file mode 100644 index 00000000..5e8ae3ed --- /dev/null +++ b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/npm/src/main/scala/example/Main.scala @@ -0,0 +1,7 @@ +package example + +object Main { + def main(args: Array[String]): Unit = { + println("npm main") + } +} diff --git a/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/npm/test b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/npm/test new file mode 100644 index 00000000..90e9ebc4 --- /dev/null +++ b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/npm/test @@ -0,0 +1,2 @@ +> fastOptJS::webpack +$ exists package-lock.json \ No newline at end of file diff --git a/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/pnpm/build.sbt b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/pnpm/build.sbt new file mode 100644 index 00000000..ebb01429 --- /dev/null +++ b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/pnpm/build.sbt @@ -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 diff --git a/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/pnpm/project/plugins.sbt b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/pnpm/project/plugins.sbt new file mode 100644 index 00000000..77ac06f1 --- /dev/null +++ b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/pnpm/project/plugins.sbt @@ -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 diff --git a/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/pnpm/src/main/scala/example/Main.scala b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/pnpm/src/main/scala/example/Main.scala new file mode 100644 index 00000000..6aab86d7 --- /dev/null +++ b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/pnpm/src/main/scala/example/Main.scala @@ -0,0 +1,7 @@ +package example + +object Main { + def main(args: Array[String]): Unit = { + println("pnpm main") + } +} diff --git a/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/pnpm/test b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/pnpm/test new file mode 100644 index 00000000..6a170d44 --- /dev/null +++ b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/pnpm/test @@ -0,0 +1,2 @@ +> fastOptJS::webpack +$ exists pnpm-lock.yaml \ No newline at end of file diff --git a/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/sharedconfig/build.sbt b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/sharedconfig/build.sbt index d4d2b773..d66db05e 100644 --- a/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/sharedconfig/build.sbt +++ b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/sharedconfig/build.sbt @@ -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)) } diff --git a/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/static/build.sbt b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/static/build.sbt index 5498802e..7ec94fd7 100644 --- a/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/static/build.sbt +++ b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/static/build.sbt @@ -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)) } diff --git a/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/webpack-assets/build.sbt b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/webpack-assets/build.sbt index 3d57c472..fac5cf48 100644 --- a/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/webpack-assets/build.sbt +++ b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/webpack-assets/build.sbt @@ -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)) } diff --git a/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/webpack-dev-server/build.sbt b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/webpack-dev-server/build.sbt index 24e22cd6..b398f3ff 100644 --- a/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/webpack-dev-server/build.sbt +++ b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/webpack-dev-server/build.sbt @@ -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 diff --git a/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/yarn-interactive/build.sbt b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/yarn-interactive/build.sbt index 8ab99487..5e14c4d6 100644 --- a/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/yarn-interactive/build.sbt +++ b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/yarn-interactive/build.sbt @@ -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) diff --git a/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/yarn-interactive/test b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/yarn-interactive/test index ddbbec9e..19a17428 100644 --- a/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/yarn-interactive/test +++ b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/yarn-interactive/test @@ -1 +1,2 @@ > fastOptJS::webpack +$ exists yarn.lock \ No newline at end of file