Skip to content

Commit ede2024

Browse files
committed
Fix scala-js/scala-js#3400: Do not crash if source-map-support is unavailable by default.
The `sourceMap` option of `NodeJSEnv.Config` is now a tri-state with `Disable`, `EnableIfAvailable` and `Enable`. Only the last one crashes if `source-map-support` is not available. The default is `EnableIfAvailable`, which should be the most appropriate option for many projects.
1 parent d95880a commit ede2024

File tree

1 file changed

+50
-7
lines changed

1 file changed

+50
-7
lines changed

nodejs-env/src/main/scala/org/scalajs/jsenv/nodejs/NodeJSEnv.scala

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import org.scalajs.logging._
2323
import java.io._
2424

2525
final class NodeJSEnv(config: NodeJSEnv.Config) extends JSEnv {
26+
import NodeJSEnv._
27+
2628
def this() = this(NodeJSEnv.Config())
2729

2830
val name: String = "Node.js"
@@ -53,8 +55,11 @@ final class NodeJSEnv(config: NodeJSEnv.Config) extends JSEnv {
5355
private def initFiles: List[VirtualBinaryFile] = {
5456
val base = List(NodeJSEnv.runtimeEnv, Support.fixPercentConsole)
5557

56-
if (config.sourceMap) NodeJSEnv.installSourceMap :: base
57-
else base
58+
config.sourceMap match {
59+
case SourceMap.Disable => base
60+
case SourceMap.EnableIfAvailable => installSourceMapIfAvailable :: base
61+
case SourceMap.Enable => installSourceMap :: base
62+
}
5863
}
5964

6065
private def inputFiles(input: Input) = input match {
@@ -69,6 +74,17 @@ final class NodeJSEnv(config: NodeJSEnv.Config) extends JSEnv {
6974
object NodeJSEnv {
7075
private lazy val validator = ExternalJSRun.supports(RunConfig.Validator())
7176

77+
private lazy val installSourceMapIfAvailable = {
78+
MemVirtualBinaryFile.fromStringUTF8("sourceMapSupport.js",
79+
"""
80+
|try {
81+
| require('source-map-support').install();
82+
|} catch (e) {
83+
|};
84+
""".stripMargin
85+
)
86+
}
87+
7288
private lazy val installSourceMap = {
7389
MemVirtualBinaryFile.fromStringUTF8("sourceMapSupport.js",
7490
"require('source-map-support').install();")
@@ -117,18 +133,36 @@ object NodeJSEnv {
117133
}
118134
}
119135

136+
/** Requirements for source map support. */
137+
sealed abstract class SourceMap
138+
139+
object SourceMap {
140+
/** Disable source maps. */
141+
case object Disable extends SourceMap
142+
143+
/** Enable source maps if `source-map-support` is available. */
144+
case object EnableIfAvailable extends SourceMap
145+
146+
/** Always enable source maps.
147+
*
148+
* If `source-map-support` is not available, loading the .js code will
149+
* fail.
150+
*/
151+
case object Enable extends SourceMap
152+
}
153+
120154
final class Config private (
121155
val executable: String,
122156
val args: List[String],
123157
val env: Map[String, String],
124-
val sourceMap: Boolean
158+
val sourceMap: SourceMap
125159
) {
126160
private def this() = {
127161
this(
128162
executable = "node",
129163
args = Nil,
130164
env = Map.empty,
131-
sourceMap = true
165+
sourceMap = SourceMap.EnableIfAvailable
132166
)
133167
}
134168

@@ -141,14 +175,23 @@ object NodeJSEnv {
141175
def withEnv(env: Map[String, String]): Config =
142176
copy(env = env)
143177

144-
def withSourceMap(sourceMap: Boolean): Config =
178+
def withSourceMap(sourceMap: SourceMap): Config =
145179
copy(sourceMap = sourceMap)
146180

181+
/** Forces enabling (true) or disabling (false) source maps.
182+
*
183+
* `sourceMap = true` maps to [[SourceMap.Enable]]. `sourceMap = false`
184+
* maps to [[SourceMap.Disable]]. [[SourceMap.EnableIfAvailable]] is never
185+
* used by this method.
186+
*/
187+
def withSourceMap(sourceMap: Boolean): Config =
188+
withSourceMap(if (sourceMap) SourceMap.Enable else SourceMap.Disable)
189+
147190
private def copy(
148191
executable: String = executable,
149192
args: List[String] = args,
150193
env: Map[String, String] = env,
151-
sourceMap: Boolean = sourceMap
194+
sourceMap: SourceMap = sourceMap
152195
): Config = {
153196
new Config(executable, args, env, sourceMap)
154197
}
@@ -162,7 +205,7 @@ object NodeJSEnv {
162205
* - `executable`: `"node"`
163206
* - `args`: `Nil`
164207
* - `env`: `Map.empty`
165-
* - `sourceMap`: `true`
208+
* - `sourceMap`: [[SourceMap.EnableIfAvailable]]
166209
*/
167210
def apply(): Config = new Config()
168211
}

0 commit comments

Comments
 (0)