我能够在我的 Play Framework 2.3.1 应用程序中加载 sbt-uglify 1.0.3 插件。加载非缩小版 javascript 非常简单,但加载缩小版似乎是不可能的。
在我的模板中,我使用 <script>
与此类似的标签:
<script src="@routes.Assets.at("javascripts/app.js")"></script>
在开发模式下,加载非缩小的 javascript 版本,这很好。在生产模式下(使用 activator start
)我看到 sbt-uglify 生成缩小版本到 target/web/uglify/build
文件夹,但因为我没有更改上面的 <script>
在我的模板中标记行,加载 javascripts 文件的非缩小版本。
有没有办法对此类路由进行 prod-only 映射以加载缩小版本?
最佳答案
问题Reverse Router should use minified assets in production automatically已在 Play 2.3.1 中修复,完全符合您的要求。
根据 Play 2.3.1 Changelog :
The behaviour of the Assets reverse router has changed, if minified versions of assets exist, it now returns a URL for those instead. To disable this behaviour, set
assets.checkForMinified=true
inapplication.conf
.
注意它应该读作set assets.checkForMinified=false
,但无论如何......
以下内容仅适用于生产模式,因此使用activator start
而非run
启动应用程序或使用生成的启动脚本(在阶段
).
在 production 中使用 Assets 的缩小版本的行为应该默认在 Play 版本中启用 @routes.Assets.versioned
(不是 routes .Assets.at
).
它确实要求在 conf/routes
中适当的路由声明是:
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)
起初我发现有点不清楚的是 pipelineStages
中元素的顺序以及包含 sbt-rjs 的要求。
就在我写完关于订单的句子之后,我在 Play 2.3 Migration Guide 的“RequireJS”部分找到了:
The order of stages is significant. You first want to optimize the files, produce digests of them and then produce gzip versions of all resultant assets.
我还在 Play 2.3 Migration Guide 中找到了在“闭包编译器”部分:
UglifyJS 2 is presently provided via the RequireJS plugin (described next). The intent in future is to provide a standalone UglifyJS 2 plugin also for situations where RequireJS is not used.
一切都始于 Play 2.3 sbt-web plugin Javascript minification 的答案.
因此,下面的 pipelineStages
是有效的 - 请注意顺序和 rjs
:
pipelineStages := Seq(rjs, uglify, digest, gzip)
project/plugins.sbt
使用如下:
resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.3.5")
addSbtPlugin("com.typesafe.sbt" % "sbt-digest" % "1.0.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-uglify" % "1.0.3")
addSbtPlugin("com.typesafe.sbt" % "sbt-gzip" % "1.0.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-rjs" % "1.0.6")
不要忘记创建一个空的 app/assets/javascripts/main.js
文件让 sbt-rjs
完成它的工作。
作为测试,我使用 activator new playApp play-scala
创建了一个 Play 应用程序,并在构建和 app/views/main.scala.html 中应用了上述更改
最终看起来如下(注意 @routes.Assets.versioned
):
@(title: String)(content: Html)
<!DOCTYPE html>
<html>
<head>
<title>@title</title>
<link rel="stylesheet" media="screen" href="@routes.Assets.versioned("stylesheets/main.css")">
<link rel="shortcut icon" type="image/png" href="@routes.Assets.versioned("images/favicon.png")">
<script src="@routes.Assets.versioned("javascripts/hello.js")" type="text/javascript"></script>
</head>
<body>
@content
</body>
</html>
执行 activator start
并调用 curl http://localhost:9000
给出(为了便于阅读,我的格式):
➜ play-uglify curl http://localhost:9000
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Play</title>
<link rel="stylesheet" media="screen" href="/assets/stylesheets/d41d8cd98f00b204e9800998ecf8427e-main.css">
<link rel="shortcut icon" type="image/png" href="/assets/images/84a01dc6c53f0d2a58a2f7ff9e17a294-favicon.png">
<script src="/assets/javascripts/4302136334616ae0605d47a1932ee262-hello.min.js" type="text/javascript"></script>
</head>
<body>
<h1>Your new application is ready.</h1>
</body>
</html>
注意 4302136334616ae0605d47a1932ee262-hello.min.js
和消化的非 JavaScript 资源。
关于javascript - 如何在 Play 2.3.1 模板中启用缩小的 JavaScript 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24581606/