Skip to content

Commit

Permalink
Removed dependency on DOM in CommonMobileModule, fixed #3
Browse files Browse the repository at this point in the history
  • Loading branch information
viktor-podzigun committed Jul 14, 2020
1 parent 4cd9dbc commit d131d29
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 36 deletions.
18 changes: 14 additions & 4 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ val scommonsResourcesArtifacts: SettingKey[Seq[ModuleID]] = settingKey[Seq[Modul
val scommonsBundlesFileFilter: SettingKey[FileFilter] = settingKey[FileFilter](
"File filter of bundles files, that should be automatically generated in the webpack directory"
)

val scommonsNodeJsTestLibs: SettingKey[Seq[String]] = settingKey[Seq[String]](
"List of JavaScript files, that should be pre-pended to the test fastOptJS output, useful for module mocks"
)
```

With default values:
Expand All @@ -54,14 +58,16 @@ scommonsResourcesFileFilter :=
"*.mp4" ||
"*.mov" ||
"*.html" ||
"*.pdf"
"*.pdf",

scommonsResourcesArtifacts := Seq(
"org.scommons.react" % "scommons-react-core" % "*",
"org.scommons.client" % "scommons-client-ui" % "*"
)
),

scommonsBundlesFileFilter := NothingFilter
scommonsBundlesFileFilter := NothingFilter,

scommonsNodeJsTestLibs := Nil
```

You can extend/override the default values:
Expand All @@ -76,7 +82,11 @@ settings(

// will generate bundle.json file(s) with migrations for SQLite
// see `scommons-websql-migrations` module
scommonsBundlesFileFilter := "*.sql"
scommonsBundlesFileFilter := "*.sql",

// to substitute references to react-native modules
// with our custom mocks during test
scommonsNodeJsTestLibs := Seq("scommons.reactnative.aliases.js")
)
```

Expand Down
27 changes: 27 additions & 0 deletions src/main/scala/scommons/sbtplugin/ScommonsPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ object ScommonsPlugin extends AutoPlugin {
val scommonsBundlesFileFilter: SettingKey[FileFilter] = settingKey[FileFilter](
"File filter of bundles files, that should be automatically generated in the webpack directory"
)

val scommonsNodeJsTestLibs: SettingKey[Seq[String]] = settingKey[Seq[String]](
"List of JavaScript files, that should be pre-pended to the test fastOptJS output, useful for module mocks"
)
}

import autoImport._
Expand Down Expand Up @@ -55,11 +59,34 @@ object ScommonsPlugin extends AutoPlugin {

scommonsBundlesFileFilter := NothingFilter,

scommonsNodeJsTestLibs := Nil,

sjsStageSettings(fastOptJS, Compile),
sjsStageSettings(fullOptJS, Compile),
sjsStageSettings(fastOptJS, Test),
sjsStageSettings(fullOptJS, Test),

fastOptJS in Test := {
val logger = streams.value.log
val testLibs = scommonsNodeJsTestLibs.value
val sjsOutput = (fastOptJS in Test).value
if (testLibs.nonEmpty) {
val sjsOutputName = sjsOutput.data.name.stripSuffix(".js")
val targetDir = sjsOutput.data.getParentFile
val bundle = new File(targetDir, s"$sjsOutputName-bundle.js")

logger.info(s"Writing NodeJs test bundle\n\t$bundle")
IO.delete(bundle)
testLibs.foreach { jsFile =>
IO.write(bundle, IO.read(new File(targetDir, jsFile)), append = true)
}
IO.write(bundle, IO.read(sjsOutput.data), append = true)

Attributed(bundle)(sjsOutput.metadata)
}
else sjsOutput
},

// revert the change for clean task: https://github.com/sbt/sbt/pull/3834/files#r172686677
// to keep the logic for cleanKeepFiles and avoid the error:
// cleanKeepFiles contains directory/file that are not directly under cleanFiles
Expand Down
41 changes: 9 additions & 32 deletions src/main/scala/scommons/sbtplugin/project/CommonMobileModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import sbt._
import scommons.sbtplugin.ScommonsPlugin.autoImport._
import scommons.sbtplugin.project.CommonModule.ideExcludedDirectories

import scalajsbundler.ExternalCommand
import scalajsbundler.sbtplugin.ScalaJSBundlerPlugin
import scalajsbundler.sbtplugin.ScalaJSBundlerPlugin.autoImport._

Expand All @@ -21,12 +20,8 @@ trait CommonMobileModule extends CommonModule {
.enablePlugins(ScalaJSPlugin, ScalaJSBundlerPlugin)
.settings(CommonMobileModule.settings: _*)
.settings(
scalaJSUseMainModuleInitializer := false,
webpackBundlingMode := BundlingMode.LibraryOnly(),

webpackConfigFile in Test := Some(
baseDirectory.value / "src" / "test" / "resources" / "test.webpack.config.js"
)
// we substitute references to react-native modules with our custom mocks during test
scommonsNodeJsTestLibs := Seq("scommons.reactnative.aliases.js")
)
}

Expand Down Expand Up @@ -59,35 +54,14 @@ object CommonMobileModule {
),

scalaJSModuleKind := ModuleKind.CommonJSModule,
scalaJSUseMainModuleInitializer := false,
webpackBundlingMode := BundlingMode.LibraryOnly(),

//Opt-in @ScalaJSDefined by default
scalacOptions += "-P:scalajs:sjsDefinedByDefault",

// react-native DO NOT require DOM, but we enable it here only to trigger the webpack build
// since we substitute references to react-native module with our custom react-native-mocks module
// inside the sc-react-native-mocks.webpack.config.js
requireJsDomEnv in Test := true,
installJsdom := {
val jsdomVersion = (version in installJsdom).value

val installDir = target.value / "scalajs-bundler-jsdom"
val baseDir = baseDirectory.value
val jsdomDir = installDir / "node_modules" / "jsdom"
val log = streams.value.log
if (!jsdomDir.exists()) {
log.info(s"Installing jsdom into: ${installDir.absolutePath}")
IO.createDirectory(installDir / "node_modules")
ExternalCommand.addPackages(
baseDir,
installDir,
useYarn.value,
log,
npmExtraArgs.value,
yarnExtraArgs.value
)(s"jsdom@$jsdomVersion")
}
installDir
},
// react-native DO NOT require DOM
requireJsDomEnv in Test := false,

version in webpack := "3.5.5",

Expand All @@ -106,6 +80,9 @@ object CommonMobileModule {
"react" -> "^16.8.0",
"react-dom" -> "^16.8.0"
),
npmDevDependencies in Test ++= Seq(
"module-alias" -> "2.2.2"
),

ideExcludedDirectories ++= {
val base = baseDirectory.value
Expand Down

0 comments on commit d131d29

Please sign in to comment.