Skip to content

Commit 757956f

Browse files
committed
Add jsNoOpt option and directive
1 parent 92c2187 commit 757956f

File tree

12 files changed

+128
-23
lines changed

12 files changed

+128
-23
lines changed

modules/build/src/test/scala/scala/build/tests/BuildTests.scala

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -958,23 +958,23 @@ abstract class BuildTests(server: Boolean) extends TestUtil.ScalaCliBuildSuite {
958958
(modeStr, bloopMode) <-
959959
Seq("fastLinkJs" -> LinkerMode.Debug, "fullLinkJs" -> LinkerMode.Release)
960960
if server
961-
}
961+
} do {
962962
test(s"bloop config for $modeStr") {
963963
val testInputs = TestInputs(
964964
os.rel / "Simple.scala" ->
965965
"""//> using platform js
966966
|def foo(): String = "foo"
967967
|""".stripMargin
968968
)
969-
val fastLinkBuildOptions = defaultOptions.copy(
969+
val jsLinkBuildOptions = defaultOptions.copy(
970970
scalaOptions = defaultOptions.scalaOptions.copy(
971971
scalaVersion = None
972972
),
973973
scalaJsOptions = defaultOptions.scalaJsOptions.copy(
974974
mode = ScalaJsMode(Some(modeStr))
975975
)
976976
)
977-
testInputs.withBuild(fastLinkBuildOptions, buildThreads, bloopConfigOpt) {
977+
testInputs.withBuild(jsLinkBuildOptions, buildThreads, bloopConfigOpt) {
978978
(_, _, maybeBuild) =>
979979
maybeBuild match {
980980
case Right(b: Build.Successful) =>
@@ -984,4 +984,31 @@ abstract class BuildTests(server: Boolean) extends TestUtil.ScalaCliBuildSuite {
984984

985985
}
986986
}
987+
988+
test(s"bloop config for noOpt and $modeStr") {
989+
val testInputs = TestInputs(
990+
os.rel / "Simple.scala" ->
991+
"""//> using platform js
992+
|def foo(): String = "foo"
993+
|""".stripMargin
994+
)
995+
val noOptBuildOptions = defaultOptions.copy(
996+
scalaOptions = defaultOptions.scalaOptions.copy(
997+
scalaVersion = None
998+
),
999+
scalaJsOptions = defaultOptions.scalaJsOptions.copy(
1000+
mode = ScalaJsMode(Some(modeStr)),
1001+
noOpt = Some(true)
1002+
)
1003+
)
1004+
testInputs.withBuild(noOptBuildOptions, buildThreads, bloopConfigOpt) {
1005+
(_, _, maybeBuild) =>
1006+
maybeBuild match {
1007+
case Right(b: Build.Successful) =>
1008+
assert(b.project.scalaJsOptions.exists(_.mode == LinkerMode.Debug))
1009+
case _ => fail("Build failed")
1010+
}
1011+
}
1012+
}
1013+
}
9871014
}

modules/build/src/test/scala/scala/build/tests/SourcesTests.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ class SourcesTests extends TestUtil.ScalaCliBuildSuite {
494494
os.rel / "something.sc" ->
495495
"""//> using jsVersion "1.8.0"
496496
|//> using jsMode "mode"
497+
|//> using jsNoOpt
497498
|//> using jsModuleKind "commonjs"
498499
|//> using jsCheckIr true
499500
|//> using jsEmitSourceMaps true
@@ -533,7 +534,8 @@ class SourcesTests extends TestUtil.ScalaCliBuildSuite {
533534
jsOptions.moduleKindStr == Some("commonjs"),
534535
jsOptions.checkIr == Some(true),
535536
jsOptions.emitSourceMaps == true,
536-
jsOptions.dom == Some(true)
537+
jsOptions.dom == Some(true),
538+
jsOptions.noOpt == Some(true)
537539
)
538540
expect(
539541
jsConfig.moduleKind == ScalaJsLinkerConfig.ModuleKind.CommonJSModule,

modules/cli/src/main/scala/scala/cli/commands/shared/ScalaJsOptions.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ final case class ScalaJsOptions(
2424
@HelpMessage("The Scala.js mode, for `fastLinkJS` use one of [`dev`, `fastLinkJS` or `fast`], for `fullLinkJS` use one of [`release`, `fullLinkJS`, `full`]")
2525
jsMode: Option[String] = None,
2626

27+
@Group(HelpGroup.ScalaJs.toString)
28+
@HelpMessage("Disable optimalisation for Scala.js, overrides `--js-mode`")
29+
@Tag(tags.implementation)
30+
@Hidden
31+
jsNoOpt: Option[Boolean] = None,
32+
2733
@HelpMessage("The Scala.js module kind: commonjs/common, esmodule/es, nomodule/none")
2834
@Group(HelpGroup.ScalaJs.toString)
2935
@Tag(tags.should)

modules/cli/src/main/scala/scala/cli/commands/shared/SharedOptions.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,8 @@ final case class SharedOptions(
244244
avoidLetsAndConsts = jsAvoidLetsAndConsts,
245245
moduleSplitStyleStr = jsModuleSplitStyle,
246246
smallModuleForPackage = jsSmallModuleForPackage,
247-
esVersionStr = jsEsVersion
247+
esVersionStr = jsEsVersion,
248+
noOpt = jsNoOpt
248249
)
249250
}
250251

modules/directives/src/main/scala/scala/build/preprocessing/directives/ScalaJs.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import scala.cli.commands.SpecificationLevel
1616
|
1717
|`//> using jsMode` _value_
1818
|
19+
|`//> using jsNoOpt` _true|false_
20+
|
1921
|`//> using jsModuleKind` _value_
2022
|
2123
|`//> using jsSmallModuleForPackage` _value1_ _value2_ …
@@ -45,6 +47,7 @@ import scala.cli.commands.SpecificationLevel
4547
final case class ScalaJs(
4648
jsVersion: Option[String] = None,
4749
jsMode: Option[String] = None,
50+
jsNoOpt: Option[Boolean] = None,
4851
jsModuleKind: Option[String] = None,
4952
jsCheckIr: Option[Boolean] = None,
5053
jsEmitSourceMaps: Option[Boolean] = None,
@@ -72,7 +75,8 @@ final case class ScalaJs(
7275
avoidClasses = jsAvoidClasses,
7376
avoidLetsAndConsts = jsAvoidLetsAndConsts,
7477
moduleSplitStyleStr = jsModuleSplitStyleStr,
75-
esVersionStr = jsEsVersionStr
78+
esVersionStr = jsEsVersionStr,
79+
noOpt = jsNoOpt
7680
)
7781
BuildOptions(
7882
scalaJsOptions = scalaJsOptions

modules/integration/src/test/scala/scala/cli/integration/RunScalaJsTestDefinitions.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,27 @@ trait RunScalaJsTestDefinitions { _: RunTestDefinitions =>
3333
test(s"simple script JS in fullLinkJs mode") {
3434
val output = simpleJsTestOutput("--js-mode", "fullLinkJs", "-v", "-v", "-v")
3535
expect(output.contains("--fullOpt"))
36+
3637
expect(!output.contains("--fastOpt"))
3738
expect(!output.contains("--noOpt"))
3839
}
3940

4041
test(s"simple script JS in fastLinkJs mode") {
4142
val output = simpleJsTestOutput("--js-mode", "fastLinkJs", "-v", "-v", "-v")
4243
expect(output.contains("--fastOpt"))
44+
4345
expect(!output.contains("--fullOpt"))
4446
expect(!output.contains("--noOpt"))
4547
}
4648

49+
test(s"simple script JS with noOpt") {
50+
val output = simpleJsTestOutput("--js-mode", "fullLinkJs", "--js-no-opt", "-v", "-v", "-v")
51+
expect(output.contains("--noOpt"))
52+
53+
expect(!output.contains("--fastOpt"))
54+
expect(!output.contains("--fullOpt"))
55+
}
56+
4757
test("without node on the PATH") {
4858
val fileName = "simple.sc"
4959
val message = "Hello"

modules/options/src/main/scala/scala/build/options/ScalaJsOptions.scala

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,18 @@ final case class ScalaJsOptions(
2626
esVersionStr: Option[String] = None,
2727
noOpt: Option[Boolean] = None
2828
) {
29-
30-
def fullOpt: Either[UnrecognizedJsOptModeError, Boolean] = mode.fullOpt
29+
def fullOpt: Either[UnrecognizedJsOptModeError, Boolean] =
30+
if (mode.isValid)
31+
if (noOpt.contains(true))
32+
Right(false)
33+
else
34+
Right(mode.nameOpt.exists(ScalaJsMode.validFullLinkAliases.contains))
35+
else
36+
Left(UnrecognizedJsOptModeError(
37+
mode.nameOpt.getOrElse("None"), // shouldn't happen since None is valid
38+
ScalaJsMode.validFullLinkAliases.toSeq,
39+
ScalaJsMode.validFastLinkAliases.toSeq
40+
))
3141
def platformSuffix: String =
3242
"sjs" + ScalaVersion.jsBinary(finalVersion).getOrElse(finalVersion)
3343
def jsDependencies(scalaVersion: String): Seq[AnyDependency] =
@@ -95,7 +105,7 @@ final case class ScalaJsOptions(
95105
def finalVersion = version.map(_.trim).filter(_.nonEmpty).getOrElse(Constants.scalaJsVersion)
96106

97107
private def configUnsafe(logger: Logger): Either[BuildException, BloopConfig.JsConfig] = for {
98-
isFullOpt <- mode.fullOpt
108+
isFullOpt <- fullOpt
99109
} yield {
100110
val kind = moduleKind(logger) match {
101111
case ScalaJsLinkerConfig.ModuleKind.CommonJSModule => BloopConfig.ModuleKindJS.CommonJSModule
@@ -144,28 +154,22 @@ final case class ScalaJsOptions(
144154
}
145155

146156
case class ScalaJsMode(nameOpt: Option[String] = None) {
147-
private val validFullLinkAliases = Set(
157+
lazy val isValid = nameOpt.isEmpty ||
158+
nameOpt.exists(
159+
ScalaJsMode.validFullLinkAliases.union(ScalaJsMode.validFastLinkAliases).contains
160+
)
161+
}
162+
object ScalaJsMode {
163+
val validFullLinkAliases = Set(
148164
"release",
149165
"fullLinkJs",
150166
"full"
151167
)
152-
private val validFastLinkAliases = Set(
168+
val validFastLinkAliases = Set(
153169
"dev",
154170
"fastLinkJs",
155171
"fast"
156172
)
157-
158-
def fullOpt: Either[UnrecognizedJsOptModeError, Boolean] =
159-
if (
160-
nameOpt.isEmpty || nameOpt.exists(validFullLinkAliases.union(validFastLinkAliases).contains)
161-
)
162-
Right(nameOpt.exists(validFullLinkAliases.contains))
163-
else
164-
Left(UnrecognizedJsOptModeError(
165-
nameOpt.get,
166-
validFullLinkAliases.toSeq,
167-
validFastLinkAliases.toSeq
168-
))
169173
}
170174

171175
object ScalaJsOptions {

website/docs/reference/cli-options.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,6 +1257,11 @@ The Scala.js version (1.14.0 by default).
12571257

12581258
The Scala.js mode, for `fastLinkJS` use one of [`dev`, `fastLinkJS` or `fast`], for `fullLinkJS` use one of [`release`, `fullLinkJS`, `full`]
12591259

1260+
### `--js-no-opt`
1261+
1262+
[Internal]
1263+
Disable optimalisation for Scala.js, overrides `--js-mode`
1264+
12601265
### `--js-module-kind`
12611266

12621267
The Scala.js module kind: commonjs/common, esmodule/es, nomodule/none

website/docs/reference/directives.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,8 @@ Add Scala.js options
373373

374374
`//> using jsMode` _value_
375375

376+
`//> using jsNoOpt` _true|false_
377+
376378
`//> using jsModuleKind` _value_
377379

378380
`//> using jsSmallModuleForPackage` _value1_ _value2_

website/docs/reference/scala-command/cli-options.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,12 @@ The Scala.js version (1.14.0 by default).
714714

715715
The Scala.js mode, for `fastLinkJS` use one of [`dev`, `fastLinkJS` or `fast`], for `fullLinkJS` use one of [`release`, `fullLinkJS`, `full`]
716716

717+
### `--js-no-opt`
718+
719+
`IMPLEMENTATION specific` per Scala Runner specification
720+
721+
Disable optimalisation for Scala.js, overrides `--js-mode`
722+
717723
### `--js-module-kind`
718724

719725
`SHOULD have` per Scala Runner specification

website/docs/reference/scala-command/directives.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ Add Scala.js options
259259

260260
`//> using jsMode` _value_
261261

262+
`//> using jsNoOpt` _true|false_
263+
262264
`//> using jsModuleKind` _value_
263265

264266
`//> using jsSmallModuleForPackage` _value1_ _value2_

website/docs/reference/scala-command/runner-specification.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,10 @@ Aliases: `-q`
358358

359359
Use progress bars
360360

361+
**--js-no-opt**
362+
363+
Disable optimalisation for Scala.js, overrides `--js-mode`
364+
361365
**--js-allow-big-ints-for-longs**
362366

363367
Primitive Longs *may* be compiled as primitive JavaScript bigints
@@ -1085,6 +1089,10 @@ Aliases: `-q`
10851089

10861090
Use progress bars
10871091

1092+
**--js-no-opt**
1093+
1094+
Disable optimalisation for Scala.js, overrides `--js-mode`
1095+
10881096
**--js-allow-big-ints-for-longs**
10891097

10901098
Primitive Longs *may* be compiled as primitive JavaScript bigints
@@ -1638,6 +1646,10 @@ Aliases: `-q`
16381646

16391647
Use progress bars
16401648

1649+
**--js-no-opt**
1650+
1651+
Disable optimalisation for Scala.js, overrides `--js-mode`
1652+
16411653
**--js-allow-big-ints-for-longs**
16421654

16431655
Primitive Longs *may* be compiled as primitive JavaScript bigints
@@ -2221,6 +2233,10 @@ Aliases: `-q`
22212233

22222234
Use progress bars
22232235

2236+
**--js-no-opt**
2237+
2238+
Disable optimalisation for Scala.js, overrides `--js-mode`
2239+
22242240
**--js-allow-big-ints-for-longs**
22252241

22262242
Primitive Longs *may* be compiled as primitive JavaScript bigints
@@ -2813,6 +2829,10 @@ Aliases: `-q`
28132829

28142830
Use progress bars
28152831

2832+
**--js-no-opt**
2833+
2834+
Disable optimalisation for Scala.js, overrides `--js-mode`
2835+
28162836
**--js-allow-big-ints-for-longs**
28172837

28182838
Primitive Longs *may* be compiled as primitive JavaScript bigints
@@ -3363,6 +3383,10 @@ Aliases: `-q`
33633383

33643384
Use progress bars
33653385

3386+
**--js-no-opt**
3387+
3388+
Disable optimalisation for Scala.js, overrides `--js-mode`
3389+
33663390
**--js-allow-big-ints-for-longs**
33673391

33683392
Primitive Longs *may* be compiled as primitive JavaScript bigints
@@ -3988,6 +4012,10 @@ Aliases: `-q`
39884012

39894013
Use progress bars
39904014

4015+
**--js-no-opt**
4016+
4017+
Disable optimalisation for Scala.js, overrides `--js-mode`
4018+
39914019
**--js-allow-big-ints-for-longs**
39924020

39934021
Primitive Longs *may* be compiled as primitive JavaScript bigints
@@ -4620,6 +4648,10 @@ Aliases: `-q`
46204648

46214649
Use progress bars
46224650

4651+
**--js-no-opt**
4652+
4653+
Disable optimalisation for Scala.js, overrides `--js-mode`
4654+
46234655
**--js-allow-big-ints-for-longs**
46244656

46254657
Primitive Longs *may* be compiled as primitive JavaScript bigints
@@ -5507,6 +5539,10 @@ Aliases: `-q`
55075539

55085540
Use progress bars
55095541

5542+
**--js-no-opt**
5543+
5544+
Disable optimalisation for Scala.js, overrides `--js-mode`
5545+
55105546
**--js-allow-big-ints-for-longs**
55115547

55125548
Primitive Longs *may* be compiled as primitive JavaScript bigints

0 commit comments

Comments
 (0)