PlayFramework 2.5 中没有使用 babel 和 browserify 的映射

No mapping using babel and browserify in PlayFramework 2.5

我正在编写一个基于 git@github.com:maximebourreau/play-reactjs-es6-seed.git

种子的项目

它似乎适用于 2.3 播放框架,但我在使用 2.5 框架时遇到错误。

当我尝试加载页面时,出现以下错误:

RuntimeException: No mapping for /path/to/root/target/web/browserify/main.js

完整的堆栈跟踪在消息的底部。

这是 browserify 正在写入的文件,但 play 似乎不知道如何将其映射到 URL。我找不到任何说明文件应该写在哪里或如何添加新映射的内容。我也很乐意使用插件来进行翻译。

我应该在哪里写入文件,或者我应该如何告诉 play 如何映射文件?

我的build.sbt是

name := """myProject"""

version := "1.0-SNAPSHOT"

lazy val root = (project in file(".")).enablePlugins(PlayJava)


scalaVersion := "2.11.7"

libraryDependencies ++= Seq(
  javaJdbc,
  cache,
  javaWs,
)

val browserifyTask = taskKey[Seq[File]]("Run browserify")

val browserifyOutputDir = settingKey[File]("Browserify output directory")

browserifyOutputDir := target.value / "web" / "browserify"

browserifyTask := {
  println("Running browserify");
  val outputFile = browserifyOutputDir.value / "main.js"
  browserifyOutputDir.value.mkdirs
  "./node_modules/.bin/browserify -t [ babelify --presets [ es2015 react ] ] app/assets/javascripts/main.jsx -o "+outputFile.getPath !;
  List(outputFile)
}

sourceGenerators in Assets <+= browserifyTask

resourceDirectories in Assets += browserifyOutputDir.value

我的路线文件是

# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

# An example controller showing a sample home page
GET     /                           controllers.HomeController.index
# An example controller showing how to use dependency injection
GET     /count                      controllers.CountController.count

# Map static resources from the /public folder to the /assets URL path
GET     /assets/*file               controllers.Assets.versioned(path="/public", file: Asset)


POST    /assessment                 controllers.AsyncController.assessment

#
# Play can't find files in node_modules in 2.5+
#GET     /node_modules/*file         controllers.NodeModulesController.at(file)
#GET     /node_modules/*file         controllers.NodeModulesController.at(file: String)
GET     /node_modules/*file         controllers.NodeModulesController.versioned(file: String)
#GET     /node_modules/*file         controllers.Assets.versioned(path="/node_modules", file:Asset)

堆栈跟踪是

 scala.sys.package$.error(package.scala:27)
 sbt.Mapper$$anonfun$fail.apply(PathMapper.scala:37)
 sbt.Mapper$$anonfun$fail.apply(PathMapper.scala:37)
 sbt.Alternatives$$anon$$anonfun$$bar$$anonfun$apply.apply(PathMapper.scala:117)
 sbt.Alternatives$$anon$$anonfun$$bar$$anonfun$apply.apply(PathMapper.scala:117)
 scala.Option.orElse(Option.scala:257)
 sbt.Alternatives$$anon$$anonfun$$bar.apply(PathMapper.scala:117)
 sbt.Alternatives$$anon$$anonfun$$bar.apply(PathMapper.scala:117)
 sbt.PathFinder$$anonfun$pair.apply(Path.scala:135)
 sbt.PathFinder$$anonfun$pair.apply(Path.scala:135)
 scala.collection.TraversableLike$$anonfun$flatMap.apply(TraversableLike.scala:251)
 scala.collection.TraversableLike$$anonfun$flatMap.apply(TraversableLike.scala:251)
 scala.collection.mutable.ResizableArray$class.foreach(ResizableArray.scala:59)
 scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:47)
 scala.collection.TraversableLike$class.flatMap(TraversableLike.scala:251)
 scala.collection.AbstractTraversable.flatMap(Traversable.scala:105)
 sbt.PathFinder.pair(Path.scala:135)
 com.typesafe.sbt.jse.SbtJsTask$$anonfun$jsSourceFileTask$$anonfun$$anonfun$$anonfun$apply.apply(SbtJsTask.scala:292)
 com.typesafe.sbt.jse.SbtJsTask$$anonfun$jsSourceFileTask$$anonfun$$anonfun$$anonfun$apply.apply(SbtJsTask.scala:286)
 com.typesafe.sbt.web.SbtWeb$.withActorRefFactory(SbtWeb.scala:595)
 com.typesafe.sbt.jse.SbtJsTask$$anonfun$jsSourceFileTask$$anonfun$$anonfun.apply(SbtJsTask.scala:285)
 com.typesafe.sbt.jse.SbtJsTask$$anonfun$jsSourceFileTask$$anonfun$$anonfun.apply(SbtJsTask.scala:284)
 scala.collection.immutable.Stream$$anonfun$map.apply(Stream.scala:376)
 scala.collection.immutable.Stream$$anonfun$map.apply(Stream.scala:376)
 scala.collection.immutable.Stream$Cons.tail(Stream.scala:1085)
 scala.collection.immutable.Stream$Cons.tail(Stream.scala:1077)
 scala.collection.immutable.Stream.foldLeft(Stream.scala:563)
 scala.concurrent.Future$.sequence(Future.scala:491)
 com.typesafe.sbt.jse.SbtJsTask$$anonfun$jsSourceFileTask$$anonfun.apply(SbtJsTask.scala:303)
 com.typesafe.sbt.jse.SbtJsTask$$anonfun$jsSourceFileTask$$anonfun.apply(SbtJsTask.scala:272)
 com.typesafe.sbt.web.incremental.package$.syncIncremental(package.scala:228)
 com.typesafe.sbt.jse.SbtJsTask$$anonfun$jsSourceFileTask.apply(SbtJsTask.scala:271)
 com.typesafe.sbt.jse.SbtJsTask$$anonfun$jsSourceFileTask.apply(SbtJsTask.scala:257)
 scala.Function1$$anonfun$compose.apply(Function1.scala:47)
 sbt.$tilde$greater$$anonfun$$u2219.apply(TypeFunctions.scala:40)
 sbt.std.Transform$$anon.work(System.scala:63)
 sbt.Execute$$anonfun$submit$$anonfun$apply.apply(Execute.scala:228)
 sbt.Execute$$anonfun$submit$$anonfun$apply.apply(Execute.scala:228)
 sbt.ErrorHandling$.wideConvert(ErrorHandling.scala:17)
 sbt.Execute.work(Execute.scala:237)
 sbt.Execute$$anonfun$submit.apply(Execute.scala:228)
 sbt.Execute$$anonfun$submit.apply(Execute.scala:228)
 sbt.ConcurrentRestrictions$$anon$$anonfun.apply(ConcurrentRestrictions.scala:159)
 sbt.CompletionService$$anon.call(CompletionService.scala:28)
 java.util.concurrent.FutureTask.run(FutureTask.java:266)
 java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
 java.util.concurrent.FutureTask.run(FutureTask.java:266)
 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
 java.lang.Thread.run(Thread.java:745)

您必须在 build.sbt

中更改两行
List(outputFile)  to Nil
and
resourceDirectories in Asset += browserifyOutputDir.value
to
unmanagedResources in Assets += baseDirectory.value / "target/web/browserify/main.js"

例如:

browserifyTask := {
  println("Running browserify");
  val outputFile = browserifyOutputDir.value / "main.js"
  browserifyOutputDir.value.mkdirs
  "./node_modules/.bin/browserify -t [ babelify --presets [ es2015 react ] ] app/assets/javascripts/main.jsx -o "+outputFile.getPath !;
  Nil
}

sourceGenerators in Assets <+= browserifyTask
unmanagedResources in Assets += baseDirectory.value / "target/web/browserify/main.js"

我 运行 遇到了相同的错误消息和几乎相同的堆栈跟踪, 从同一个 git 仓库开始一个项目后:git@github.com:maximebourreau/play-reactjs-es6-seed.git

repo 的最新版本使用 play 2.5 并且运行良好,所以 我觉得玩版没问题。

我注意到错误是在某处提到了 [sbt-jshint], 并从 project/plugins.sbt 中删除这一行对我来说解决了问题:

addSbtPlugin("com.typesafe.sbt" % "sbt-jshint" % "1.0.3")