Skip to content
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .mill-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.4-native
1.1.7-native
42 changes: 40 additions & 2 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,2 +1,40 @@
version = "3.9.7"
runner.dialect = scala213
version = "3.9.9"
runner.dialect = scala3
# maxColumn = 120

rewrite {
rules = [Imports, SortModifiers, AvoidInfix, RedundantBraces, RedundantParens, PreferCurlyFors]
imports {
expand = true
sort = scalastyle
groups = [
["build\\..*"]
["io.github.nafg.millbundler\\..*", "millbundler\\..*", "nafg.millbundler\\..*"]
["[a-z0-9]*\\..*"]
["scala\\..*"]
["java\\..*"]
["mill\\..*", "os\\..*"]
]
}
redundantBraces {
stringInterpolation = true
generalExpressions = true
ifElseExpressions = true
methodBodies = true
}
scala3 {
convertToNewSyntax = true
removeOptionalBraces = yes
insertEndMarkerMinLines = 10
}
}

newlines {
source = keep
afterCurlyLambdaParams = squash
beforeCurlyLambdaParams = multilineWithCaseOnly
implicitParamListModifierPrefer = after
topLevelStatementBlankLines = [
{ blanks = 1 }
]
}
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import $ivy.`io.github.nafg.millbundler::jsdeps::0.1.0`, io.github.nafg.millbund


object frontend extends ScalaJSNpmModule {
override def scalaVersion = "3.7.2"
override def scalaJSVersion = "1.19.0"
override def scalaVersion = "3.8.4"
override def scalaJSVersion = "1.22.0"
override def jsDeps =
super.jsDeps() ++
JsDeps(
Expand Down Expand Up @@ -55,8 +55,15 @@ object frontend extends ScalaJSRollupModule {
}
```

You can then run `mill frontend.devBundle` or `mill frontend.prodBundle` to bundle your Scala.js code into a single
JavaScript file.
You can then run `mill frontend.devBundle` or `mill frontend.prodBundle` to bundle your Scala.js code.

When the Scala.js linker produces a single public module, bundling writes one output file (`bundleFilename`, default
`out-bundle.js`). When the linker produces multiple public modules, pass all entry files to the bundler automatically
via `getReportMainFilePath`; outputs are written using `outputEntryFileNames` (default `out-bundle-[name].js`), where
`[name]` is derived from each input file name.

Override `bundleFilename` / `outputEntryFileNames` to customize output naming. For Rollup, `rollupOutputName` sets the
IIFE global name when bundling a single entry.

### Testing

Expand Down
34 changes: 15 additions & 19 deletions build.mill
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ import mill.scalalib.scalafmt.*
import mill.util.VcsVersion


trait BaseScalaModule extends ScalaModule {
override def scalaVersion = "3.7.2"
trait BaseScalaModule extends ScalaModule:
override def scalaVersion = "3.8.4"
override def zincReportCachedProblems = true
}

trait BasePublishModule
extends BaseScalaModule
with SonatypeCentralPublishModule
with ScalafmtModule {
override def scalaVersion = "3.7.2"
with ScalafmtModule:
override def scalaVersion = "3.8.4"

override def publishVersion: T[String] =
VcsVersion
Expand All @@ -42,32 +41,29 @@ trait BasePublishModule
override def scalacOptions = Seq("-Ywarn-unused", "-deprecation")

override def mvnDeps = super.mvnDeps() ++ Seq(
mvn"com.lihaoyi::mill-libs:${millVersion}"
mvn"com.lihaoyi::mill-libs:$millVersion"
)
}

object test_common extends BaseScalaModule {
end BasePublishModule

object test_common extends BaseScalaModule:
override def moduleDeps = Seq(jsdeps)

override def mvnDeps = Seq(
mvn"com.lihaoyi::mill-testkit:${millVersion}",
mvn"org.scalameta::munit::1.1.1"
mvn"com.lihaoyi::mill-testkit:$millVersion",
mvn"org.scalameta::munit::1.3.3"
)
}

object jsdeps extends BasePublishModule {
object test extends ScalaTests with TestModule.Munit {
object jsdeps extends BasePublishModule:

object test extends ScalaTests with TestModule.Munit:
override def moduleDeps = super.moduleDeps :+ test_common
}
}

//noinspection ScalaUnusedSymbol
object millbundler extends BasePublishModule {
object millbundler extends BasePublishModule:
override def moduleDeps = Seq(jsdeps)
override def mvnDeps = Seq(mvn"com.lihaoyi::geny:1.1.1")

object test extends ScalaTests with TestModule.Munit {
object test extends ScalaTests with TestModule.Munit:
override def moduleDeps = super.moduleDeps :+ test_common
override def testParallelism = true
}
}
28 changes: 16 additions & 12 deletions jsdeps/src/io/github/nafg/millbundler/jsdeps/JsDeps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@ case class JsDeps(
/** Javascript source files, as a map from path to contents
*/
jsSources: Map[String, String] = Map.empty
) {
):

def ++(that: JsDeps): JsDeps =
JsDeps(
dependencies ++ that.dependencies,
devDependencies ++ that.devDependencies,
jsSources ++ that.jsSources
)
}
object JsDeps {

end JsDeps

object JsDeps:

def apply(dependencies: (String, String)*): JsDeps =
JsDeps(dependencies = dependencies.toMap)

Expand All @@ -36,17 +40,15 @@ object JsDeps {
in: InputStream,
buffer: Array[Byte] = new Array[Byte](8192),
out: ByteArrayOutputStream = new ByteArrayOutputStream
): String = {
): String =
val byteCount = in.read(buffer)
if (byteCount < 0)
out.toString
else {
if byteCount < 0 then out.toString
else
out.write(buffer, 0, byteCount)
readAllBytes(in, buffer, out)
}
}
end readAllBytes

def fromJar(jar: os.Path): Seq[JsDeps] = {
def fromJar(jar: os.Path): Seq[JsDeps] =
val stream = new ZipInputStream(
new BufferedInputStream(new FileInputStream(jar.toIO))
)
Expand Down Expand Up @@ -80,5 +82,7 @@ object JsDeps {
.toList
finally
stream.close()
}
}
end try
end fromJar

end JsDeps
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import mill.scalajslib.ScalaJSModule
import os.Path

//noinspection ScalaUnusedSymbol,ScalaWeakerAccess
trait ScalaJSDepsModule extends ScalaJSModule {
trait ScalaJSDepsModule extends ScalaJSModule:

/** JS dependencies, explicitly defined for this module
*/
Expand All @@ -15,9 +15,10 @@ trait ScalaJSDepsModule extends ScalaJSModule {
/** JS dependencies of moduleDeps, transitively.
*/
def transitiveJsDeps = Task.sequence(
recursiveModuleDeps.collect { case mod: ScalaJSDepsModule =>
mod.allJsDeps
// case mod: ScalaJsModule => mod.recursiveModuleDeps
recursiveModuleDeps.collect {
case mod: ScalaJSDepsModule =>
mod.allJsDeps
// case mod: ScalaJsModule => mod.recursiveModuleDeps
}
)

Expand All @@ -35,4 +36,5 @@ trait ScalaJSDepsModule extends ScalaJSModule {
final def allJsDeps: Task.Simple[JsDeps] = Task {
JsDeps.combine(mvnJsDeps() ++ transitiveJsDeps() :+ jsDeps())
}
}

end ScalaJSDepsModule
49 changes: 25 additions & 24 deletions jsdeps/src/io/github/nafg/millbundler/jsdeps/ScalaJSNpmModule.scala
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
package io.github.nafg.millbundler.jsdeps

import mill.*
import mill.api.BuildCtx
import mill.scalajslib.TestScalaJSModule
import mill.scalajslib.api.JsEnvConfig
import mill.api.BuildCtx

//noinspection ScalaUnusedSymbol,ScalaWeakerAccess
trait ScalaJSNpmModule extends ScalaJSDepsModule {
trait ScalaJSNpmModule extends ScalaJSDepsModule:

def npmCommand = Task {
if (System.getProperty("os.name").toLowerCase.contains("windows"))
if System.getProperty("os.name").toLowerCase.contains("windows") then
"npm.cmd"
else
"npm"
else "npm"
}

def npmInstallCommand =
Task { Seq(npmCommand(), "install", "--force") }
Task(Seq(npmCommand(), "install", "--force"))

protected def packageJson(deps: JsDeps) =
ujson.Obj(
"dependencies" -> deps.dependencies,
"devDependencies" -> deps.devDependencies
)
protected def packageJson: Task[JsDeps => ujson.Obj] = Task.Anon {
(deps: JsDeps) =>
ujson.Obj(
"dependencies" -> deps.dependencies,
"devDependencies" -> deps.devDependencies
)
}

def npmInstall = Task {
val pkgJson = packageJson(allJsDeps())
val pkgJson = packageJson()(allJsDeps())

os.write.over(Task.dest / "package.json", pkgJson.render(2) + "\n")

try
os.call(npmInstallCommand(), cwd = Task.dest)
catch {
try os.call(npmInstallCommand(), cwd = Task.dest)
catch
case e: Exception =>
throw new RuntimeException("Error running npm install", e)
}

PathRef(Task.dest)
}
Expand All @@ -57,23 +57,24 @@ trait ScalaJSNpmModule extends ScalaJSDepsModule {
os.call(Seq("npm", "view", pkg, "dist-tags.latest")).out.trim()
)
val updates = latestVersions.filter(x => x._2 != x._3)
if (updates.nonEmpty) {
if updates.nonEmpty then
logger.info(
s"$BOLD$proj $RESET${updates.length} new npm package versions"
)
for ((pkg, cur, latest) <- updates)
for (pkg, cur, latest) <- updates do
logger.info(
s" $BOLD${pad(pkg, maxPkgLen)}$RESET ${pad(cur, maxVerLen)} -> $latest"
)
}
}
}

end ScalaJSNpmModule

//noinspection ScalaWeakerAccess
object ScalaJSNpmModule {
trait Test extends TestScalaJSModule with ScalaJSNpmModule {
object ScalaJSNpmModule:

trait Test extends TestScalaJSModule with ScalaJSNpmModule:

override def jsEnvConfig = Task {
val path = npmInstall().path / "node_modules"
JsEnvConfig.NodeJs(env = Map("NODE_PATH" -> path.toString))
}
}
}
11 changes: 6 additions & 5 deletions jsdeps/test/resources/simple/src/Tests.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import io.github.nafg.scalajs.facades.reactphonenumberinput._
import munit.Assertions._
import io.github.nafg.scalajs.facades.reactphonenumberinput.*
import munit.Assertions.*

import scala.scalajs.js
import scala.scalajs.js.annotation._
import scala.scalajs.js.annotation.*

class Tests extends munit.FunSuite:

class Tests extends munit.FunSuite {
test("ReactPhoneNumberInput is defined (simple)") {
assert(!js.isUndefined(ReactPhoneNumberInput.raw))
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package io.github.nafg.millbundler.jsdeps

import io.github.nafg.millbundler.testcommon.BaseSuite

import mill.api.Discover
import mill.util.TokenReaders.*

class JsDepsSuite extends BaseSuite {
class JsDepsSuite extends BaseSuite:

test("JsDeps") {
object build extends BaseBuild with ScalaJSNpmModule {
object test extends BaseTestModule with ScalaJSNpmModule.Test {
object build extends BaseBuild with ScalaJSNpmModule:
object test extends BaseTestModule with ScalaJSNpmModule.Test:
lazy val millDiscover = Discover[this.type]
}

lazy val millDiscover = Discover[test.type]
}

checkTestResults(build.test, "simple")
}
}

end JsDepsSuite
6 changes: 3 additions & 3 deletions millbundler/src/io/github/nafg/millbundler/BundleParams.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.github.nafg.millbundler

case class BundleParams(inputFiles: Seq[os.Path], opt: Boolean)
case class BundleParams(inputFiles: Iterable[os.Path], opt: Boolean)

object BundleParams:

object BundleParams {
def apply(inputFile: os.Path, opt: Boolean): BundleParams =
BundleParams(Seq(inputFile), opt)
}
Loading
Loading