ASP.net MVC缩小和捆绑 - 如何缩小单个JavaScript文件 [英] ASP.net MVC Minifying and Bundling - How to minify a single JavaScript file

查看:57
本文介绍了ASP.net MVC缩小和捆绑 - 如何缩小单个JavaScript文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试使用不引人注目的JavaScript指南时,我将所有JavaScript从我的每个视图中移出并放入单独的.js文件中。

In trying to keep with unobtrusive JavaScript guidelines I moved all the JavaScript out of each of my views and into a separate .js files.

但是,在1个视图中我需要动态创建一些JavaScript变量,以便将它们插入到ViewBag中并直接呈现到视图中:

However, within 1 view I need to dynamically create some JavaScript variables so these are inserted into the ViewBag and rendered directly into the view:

    $(document).ready(function () {
        @(ViewBag.PrePop)
        @(ViewBag.PreDiscount)
    });

在我使用新的ASP.net捆绑和缩小功能之前一切正常。

This works fine until I had a go at using the new ASP.net bundling and minification feature.

将所有这些小的外部JavaScript文件捆绑在一起,然后从每个页面调用组合脚本意味着每个页面(除了包含ViewBag发出的变量的那个)都有一个变量引用错误:

Bundling all these small external JavaScript files together and then calling the combined script from every page means that every page (other than the one that contains the ViewBag emitted variables) has a variable reference error:

Uncaught ReferenceError: quotes is not defined 

所以这基本上意味着我无法捆绑这个特定的JavaScript文件。

So this basically means I can't bundle this particular JavaScript file.

但是,我想缩小它。为此,我是否只声明一个仅包含一个JavaScript文件的包:

However, I would like to minify it. To do this, do I just declare a bundle that only contains the one JavaScript file:

bundles.Add(new ScriptBundle("~/bundles/mypagescript").Include(
            "~/Scripts/mypagescript.js"));

或者还有另一种方法可以完全缩小?

Or is there another way to perform solely minification?

推荐答案

是的,目前我们并没有真正公开任何方法让您轻松调用优化只是为了缩小字符串(您可以通过<$ c的实例来实现) $ c> JsMinify.Process 如果你真的想要)ie:

Yep, currently we don't really expose any methods for you to easily call in to Optimization just to minify a string (you could do it via an instance of JsMinify.Process if you really wanted) i.e.:

string js = "//I am a comment\r\nfoo = bar;\r\nfoo = yes;";
JsMinify jsmin = new JsMinify();
BundleContext context = new BundleContext();
BundleResponse response = new BundleResponse(js, null);
response.Content = js;
jsmin.Process(context, response);
Assert.AreEqual("foo=bar,foo=yes", response.Content);

但你建议的最好(创建一个文件包)。

But what you suggest is probably best (create a bundle of one file).

这篇关于ASP.net MVC缩小和捆绑 - 如何缩小单个JavaScript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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