如何在 Play 2.3.1 模板中启用缩小的 JavaScript 文件? [英] How to enable minified JavaScript files in Play 2.3.1 templates?
问题描述
我能够在我的 Play Framework 2.3.1 应用程序中加载 sbt-uglify 1.0.3 插件.加载未压缩的 javascript 非常简单,但加载缩小版本似乎是不可能的.
I was able to load the sbt-uglify 1.0.3 plugin in my Play Framework 2.3.1 app. Loading of the non-minified javascripts is pretty straightforward, but loading the minified versions seems to be impossible.
在我的模板中,我使用了类似这样的 标签:
In my template I use <script>
tags similar to this:
<script src="@routes.Assets.at("javascripts/app.js")"></script>
在开发模式下,加载了非缩小版的 javascript 版本,这很好.在 prod 模式下(使用 activator start
),我看到 sbt-uglify 将缩小版本生成到 target/web/uglify/build
文件夹,但因为我没有更改在我的模板中的 <script>
标记行上方,加载了 javascripts 文件的非缩小版本.
In dev mode, the non-minified javascript version is loaded, which is fine. In prod mode (using activator start
) I see sbt-uglify generating the minified versions to the target/web/uglify/build
folder, but because I didn't change the above <script>
tag line in my templates, the non-minified versions of the javascripts files are loaded.
有没有办法对此类路由进行 prod-only 映射以加载缩小版本?
Is there a way to do a prod-only mapping of such routes to load the minified versions?
推荐答案
问题 反向路由器应该在生产中自动使用缩小资产 已在 Play 2.3.1 中修复,完全符合您的要求.
The issue Reverse Router should use minified assets in production automatically was fixed in Play 2.3.1 that exactly matches your requirement.
资产反向路由器的行为已更改(如果缩小)存在资产的版本,它现在返回这些版本的 URL.到禁用此行为,设置 assets.checkForMinified=true
在application.conf
.
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
,但无论如何...
NOTE It should rather read set assets.checkForMinified=false
, but anyway...
以下内容仅适用于生产模式,因此使用 activator start
而不是 run
启动应用程序或使用生成的启动脚本(在 stage
).
What follows works in production mode only so start the application with activator start
not run
or use the generated start scripts (after stage
).
在生产中使用缩小版本资产的行为应该在带有@routes.Assets.versioned
的Play版本中默认启用(不是routes.Assets.at
).
The behaviour of using minified versions of assets in production should be enabled by default in the version of Play with @routes.Assets.versioned
(not routes.Assets.at
).
它确实要求 conf/routes
中适当的路由声明是:
It does require that the appropriate route declaration in conf/routes
is:
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)
起初我发现有点不清楚的是pipelineStages
中元素的顺序以及包含sbt-rjs 在里面.
What I found a bit unclear at first was the order of elements in pipelineStages
as well as the requirement to include sbt-rjs in it.
就在我写下关于我在 Play 2.3 中的RequireJS"部分中找到的顺序的句子之后迁移指南:
And just after I'd written the sentence about the order I found in the section "RequireJS" in Play 2.3 Migration Guide:
阶段的顺序很重要.您首先要优化文件,生成它们的摘要,然后生成所有文件的 gzip 版本所得资产.
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 迁移指南的Closure Compiler"部分找到:
I've also found in Play 2.3 Migration Guide in the section "Closure Compiler":
UglifyJS 2 目前是通过 RequireJS 插件提供的(描述下一个).未来的意图是提供一个独立的 UglifyJS 2插件也适用于不使用 RequireJS 的情况.
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 插件 Javascript 缩小.
因此,下面的 pipelineStages
是有效的 - 注意顺序和 rjs
:
So, the below pipelineStages
is the working one - mind the order and 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
完成它的工作.
Don't forget to create an empty app/assets/javascripts/main.js
file to let sbt-rjs
do its job.
作为测试,我使用 activator new playApp play-scala
创建了一个 Play 应用程序,并在构建和 app/views/main.scala.html 中应用了上述更改
最终看起来如下(注意 @routes.Assets.versioned
):
As a test, I created a Play application with activator new playApp play-scala
and applied the above changes in the build as well as in app/views/main.scala.html
that ultimately looked as follows (note @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
给出(为了可读性而格式化我的):
Executing activator start
and calling curl http://localhost:9000
gives (formatting's mine for the sake of readability):
➜ 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资源消化.
Note 4302136334616ae0605d47a1932ee262-hello.min.js
and the non-JavaScript resources digested.
这篇关于如何在 Play 2.3.1 模板中启用缩小的 JavaScript 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!