0

我希望 Play 将 GZipped JS 和 CSS 发送到浏览器。在 Build.scala 中,我添加了

 val gzipAssets = TaskKey[Seq[File]]("gzip-assets", "GZIP all assets")
 lazy val gzipAssetsSetting = gzipAssets <<= gzipAssetsTask
 lazy val gzipAssetsTask = (gzippableAssets, streams) map {
  case (finder: PathFinder, s: TaskStreams) => {
    finder.get.map { file =>
      val gzTarget = new File(file.getAbsolutePath + ".gz")
      IO.gzip(file, gzTarget)
      s.log.info("Compressed " + file.getAbsolutePath + " " + file.length / 1000 + " k     => " + gzTarget.getName + " " + gzTarget.length / 1000 + " k")
      gzTarget
    }
  }
}

val main = PlayProject(appName, appVersion, appDependencies, mainLang = JAVA).settings(
    // Twitter Bootstrap v2.0.1 compilation (https://plus.google.com/u/0/108788785914419775677/posts/QgyUF9cXPkv)
    lessEntryPoints <<= (sourceDirectory in Compile)(base => ((base / "assets" / "stylesheets" / "twitterbootstrap" / "styles.less"))),

    // set up gzip of assets
    gzippableAssets <<= (resourceManaged in (ThisProject))(dir => ((dir ** "*.js") +++ (dir ** "*.css"))),
    gzipAssetsSetting,
    playPackageEverything <<= playPackageEverything dependsOn gzipAssets


).settings( ...

当我 dist 时,它会在目标中生成我的文件:

[信息] 压缩 /Users/wimha/Documents/**/target/scala-2.9.1/resource_managed/main/public/stylesheets/twitterbootstrap/styles.min.css 183 k => styles.min.css.gz 27 k

但是,在产品中,该文件不可用:

加载资源失败:服务器响应状态为 404(未找到 )http://ec2-54-228-70-193.eu-west-1.compute.amazonaws.com/assets/stylesheets/twitterbootstrap/styles .min.css.gz

我有 2 个问题:

  • 为什么文件不可用?
  • 你有更好的解决方案吗?也许有一个模块?除了 GreenScript,我没有发现任何有趣的东西,但它仅适用于 Play 1.x 我希望我的资产也合并到一个文件中。

谢谢朱利安

4

1 回答 1

1

我浪费了几个小时寻找相同问题的解决方案。

试试这个: http: //play.lighthouseapp.com/projects/82401/tickets/841-gzip-assets。无论.gz您是运行play dist还是play stage.

鉴于 Play 已经足够智能,可以在相应的 .gz 文件存在时提供压缩后的资产,因此在执行“stage”或“dist”时对资产进行 gzip 压缩是有意义的。一些反向代理可以 gzip 资源,但这要么效率较低(如果为每个请求完成),要么更复杂(如果资源被缓存,并且最终可能会过时)。

这是使用 Build.scala 的半生不熟的解决方案:

val gzippableAssets = SettingKey[PathFinder]("gzippable-assets", "Defines the files to gzip")
val gzipAssets = TaskKey[Seq[File]]("gzip-assets", "gzip all assets")
lazy val gzipAssetsSetting = gzipAssets <<= gzipAssetsTask dependsOn (copyResources in Compile)
lazy val gzipAssetsTask = (gzippableAssets, streams) map {
  case (finder: PathFinder, s: TaskStreams) => {
    var count = 0
    var files = finder.get.map { file =>
      val gzTarget = new File(file.getAbsolutePath + ".gz")
      IO.gzip(file, gzTarget)
      count += 1;
      gzTarget
    }
    s.log.info("Compressed " + count + " asset(s)")
    files
  }
}

val main = PlayProject(appName, appVersion, appDependencies, mainLang = JAVA).settings(
    ...
    gzippableAssets <<= (classDirectory in Compile)(dir => (dir ** ("*.js" || "*.css" || "*.html"))),
    gzipAssetsSetting,
    playPackageEverything <<= playPackageEverything dependsOn gzipAssets
)
于 2013-08-08T15:50:04.980 回答