Example #1
0
        public static MvcHtmlString RenderJsBundle(this HtmlHelper html, string bundlePath, BundleOptions options = BundleOptions.Minified)
        {
            if (string.IsNullOrEmpty(bundlePath))
            {
                return(MvcHtmlString.Empty);
            }

            if (!CachePaths())
            {
                BundleCache.SafeClear();
            }

            return(BundleCache.GetOrAdd(bundlePath, str =>
            {
                var filePath = MapPath(bundlePath);

                var baseUrl = VirtualPathUtility.GetDirectory(bundlePath);

                if (options == BundleOptions.Combined)
                {
                    return html.Js(bundlePath.Replace(".bundle", ""), options);
                }
                if (options == BundleOptions.MinifiedAndCombined)
                {
                    return html.Js(bundlePath.Replace(".js.bundle", ".min.js"), options);
                }

                var jsFiles = File.ReadAllLines(filePath);

                var scripts = new StringBuilder();
                foreach (var file in jsFiles)
                {
                    if (file.StartsWith("#"))
                    {
                        continue;
                    }

                    var jsFile = file.Trim()
                                 .Replace(".coffee", ".js")
                                 .Replace(".ls", ".js");

                    var jsSrc = Path.Combine(baseUrl, jsFile);

                    scripts.AppendLine(
                        html.Js(jsSrc, options).ToString()
                        );
                }

                return scripts.ToString().ToMvcHtmlString();
            }));
        }