本文介绍了如何在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.

根据。

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.

在我写完关于:

我还在封闭编译器部分的中找到了:

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

全部首先回答。

因此,下面的 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文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 13:19