如何在Play 2.3.1模板中启用缩小的JavaScript文件? [英] How to enable minified JavaScript files in Play 2.3.1 templates?

查看:110
本文介绍了如何在Play 2.3.1模板中启用缩小的JavaScript文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够在我的Play Framework 2.3.1应用程序中加载sbt-uglify 1.0.3插件。加载非缩小的javascripts非常简单,但加载缩小版本似乎是不可能的。

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.

在我的模板中,我使用< script> ; 与此类似的标签:

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映射以加载缩小版本?

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.

根据播放2.3.1更改日志

According to Play 2.3.1 Changelog:


Assets反向路由器的行为已经改变,如果存在缩小的
版本的资产,它现在返回一个相反的URL。要
禁用此行为,请在
application.conf 中设置 assets.checkForMinified = true

注意它应该读取 set assets.checkForMinified = false ,但无论如何......

NOTE It should rather read set assets.checkForMinified=false, but anyway...

以下内容适用于生产模式,因此只需使用启动应用程序激活器启动不是运行或使用生成的启动脚本(在阶段之后)。

What follows works in production mode only so start the application with activator start not run or use the generated start scripts (after stage).

默认情况下,应在Play with @routes版本中启用在生产中使用缩小版资产的行为。 Assets.versioned (不是 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.

在我写完关于播放2.3迁移指南


阶段的顺序很重要。您首先要优化
文件,生成它们的摘要,然后生成所有
结果资产的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迁移指南中找到了:

I've also found in Play 2.3 Migration Guide in the section "Closure Compiler":


UglifyJS 2目前通过RequireJS插件提供(接下来描述为
)。未来的目的是在不使用RequireJS的情况下提供独立的UglifyJS 2
插件。

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.

全部首先回答播放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.

作为测试,我创建了一个带有的Play应用程序激活器新的playApp play-scala 并在构建中以及 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>

执行激活器启动并调用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆