简体   繁体   English

如何使用 Pug 缩小/丑化内联 JavaScript?

[英]How can I have Minify/uglify inline JavaScript using Pug?

I am using Pug for a web page that I am building.我正在将 Pug 用于我正在构建的网页。 At the end of a module I have a script.在模块的末尾,我有一个script. tag (the regular script tag cannot be used because it isn't compatible with jQuery):标记(不能使用常规script标记,因为它与 jQuery 不兼容):

script.

    // load more videos
    $("#btn-more").click(() => {
        $.get(`#{lang}/videos?quantity=#{numVideos + 2}`)
            .done((videos) => {
                $(videos).ready(() => {
                    $("#videos").replaceWith(videos)
                })
            })
    })

Which ends up producing code that is not minified:最终生成未缩小的代码:

<script>// load more videos
$("#btn-more").click(() => {
    $.get(`en/videos?quantity=6`)
        .done((videos) => {
            $(videos).ready(() => {
                $("#videos").replaceWith(videos)
            })
        })
})</script>

Is there a way to have Pug minify the code?有没有办法让 Pug 缩小代码? I have not figured out how to use filters (Uglify JS) on the script.我还没有弄清楚如何在script. tag.标签。

Script: uglify-js脚本:uglify-js

// load more videos
$("#btn-more").click(() => {
    $.get(`#{lang}/videos?quantity=#{numVideos + 2}`)
        .done((videos) => {
            $(videos).ready(() => {
                $("#videos").replaceWith(videos)
            })
        })
})
  1. First make sure that JSTransformer Uglify JS is installed首先确保安装了JSTransformer Uglify JS
npm i jstransformer-uglify-js
  1. Now, you should be able to render the following template with :uglify-js filter.现在,您应该能够使用:uglify-js过滤器呈现以下模板。
script: :uglify-js
    $("#btn-more").click(() => {
        $.get(`#{lang}/videos?quantity=#{numVideos + 2}`)
            .done((videos) => {
                $(videos).ready(() => {
                    $("#videos").replaceWith(videos)
                })
            })
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM