如何使用bundle transformer来最小化CSS或JS

本文关键字:CSS JS 最小化 何使用 bundle transformer | 更新日期: 2023-09-27 17:49:33

我想使用bundle transformer来捆绑和最小化我的css集合,但无论如何我尝试它不工作

这是我的代码,请给我一些建议,谢谢。

        var nullBuilder = new NullBuilder();
        var cssTransformer = new StyleTransformer(new CleanCssMinifier());
        var nullOrderer = new NullOrderer();
        BundleResolver.Current = new CustomBundleResolver();
        var commonStylesBundle = new Bundle("~/bundles/css/site");
        commonStylesBundle.IncludeDirectory("~/Content/sass", "*.css");
        commonStylesBundle.Builder = nullBuilder;
        commonStylesBundle.Transforms.Add(cssTransformer);
        commonStylesBundle.Orderer = nullOrderer;
        bundles.Add(commonStylesBundle);

它总是输出这样的HTML:

<link href="/Content/sass/card.css?bundleVirtualPath=%7e%2fbundles%2fcss%2fsite" rel="stylesheet"/>
<link href="/Content/sass/header.css?bundleVirtualPath=%7e%2fbundles%2fcss%2fsite" rel="stylesheet"/>
<link href="/Content/sass/login.css?bundleVirtualPath=%7e%2fbundles%2fcss%2fsite" rel="stylesheet"/>

如何使用bundle transformer来最小化CSS或JS

您需要在配置中将调试模式设置为false。或者在代码中执行以下操作-

BundleTable.EnableOptimizations = true;