我希望 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 我希望我的资产也合并到一个文件中。
谢谢朱利安