Example #1
0
        public static HtmlString CompileBundleAndMinifyStyles(this HtmlHelper helper, string name, IEnumerable<string> paths)
        {
            var html = new HtmlString(string.Empty);

            if (paths != null && paths.Any())
            {
                BundleTable.EnableOptimizations = true;
                BundleTable.Bundles.IgnoreList.Clear();
                BundleTable.Bundles.FileSetOrderList.Clear();
                BundleTable.Bundles.FileExtensionReplacementList.Clear();

                var sitePath = helper.SitePath();
                var styles = paths.ToArray();
                var virtualPath = string.Format("~/styles/css/{0}", helper.UrlHelper().Encode(name));

                var bundle = new TypesetBundle(virtualPath);
                bundle.Transforms.Add(new SassCompile());
                bundle.Transforms.Add(new LessCompile());
                bundle.Transforms.Add(new CssMinify());
                bundle.IncludeLocalAndRemote(helper.ViewContext.HttpContext, helper.SitePath(), styles);
                BundleTable.Bundles.Add(bundle);

                var scriptTag = new TagBuilder("link");
                scriptTag.Attributes.Add("type", "text/css");
                scriptTag.Attributes.Add("rel", "stylesheet");
                scriptTag.Attributes.Add("href", BundleTable.Bundles.ResolveBundleUrl(virtualPath));
                html = new HtmlString(scriptTag.ToString(TagRenderMode.SelfClosing));
            }

            return html;
        }
Example #2
0
        public static HtmlString CompileBundleAndMinifyScripts(this HtmlHelper helper, string name, IEnumerable<string> paths)
        {
            var html = new HtmlString(string.Empty);

            if (paths != null && paths.Any())
            {
                BundleTable.EnableOptimizations = true;
                BundleTable.Bundles.IgnoreList.Clear();
                BundleTable.Bundles.FileSetOrderList.Clear();
                BundleTable.Bundles.FileExtensionReplacementList.Clear();

                var scripts = paths.ToArray();
                var virtualPath = string.Format("~/scripts/javascript/{0}", helper.UrlHelper().Encode(name));

                var bundle = new TypesetBundle(virtualPath);
                bundle.IncludeLocalAndRemote(helper.ViewContext.HttpContext, helper.SitePath(), scripts);
                bundle.Transforms.Add(new CoffeeScriptCompile());
                bundle.Transforms.Add(new JsMinify());
                BundleTable.Bundles.Add(bundle);

                var scriptTag = new TagBuilder("script");
                scriptTag.Attributes.Add("type", "text/javascript");
                scriptTag.Attributes.Add("src", BundleTable.Bundles.ResolveBundleUrl(virtualPath));
                html = new HtmlString(scriptTag.ToString());
            }

            return html;
        }