/// <summary> /// Generate all script parts /// </summary> /// <param name="urlHelper">URL Helper</param> /// <param name="location">A location of the script element</param> /// <param name="bundleFiles">A value indicating whether to bundle script elements</param> /// <returns>Generated string</returns> public virtual string RenderScripts(IUrlHelper urlHelper, ResourceLocation location) { if (!_scriptParts.ContainsKey(location) || _scriptParts[location] == null) { return(string.Empty); } if (!_scriptParts.Any()) { return(string.Empty); } if (_seoSettings.EnableJsBundling) { var partsToBundle = _scriptParts[location] .Where(x => !x.ExcludeFromBundle) .Distinct() .ToArray(); var partsToDontBundle = _scriptParts[location] .Where(x => x.ExcludeFromBundle) .Distinct() .ToArray(); var result = new StringBuilder(); //parts to bundle if (partsToBundle.Any()) { //ensure \bundles directory exists _fileProvider.CreateDirectory(_fileProvider.GetAbsolutePath(BundleFolder)); var bundle = new Bundle(); foreach (var item in partsToBundle) { string src = $"wwwroot{urlHelper.Content(item.Path)}"; bundle.InputFiles.Add(src); } //output file var outputFileName = GetBundleFileName(partsToBundle.Select(x => x.Path).ToArray()); bundle.OutputFileName = $"wwwroot/{BundleFolder}/{outputFileName}.js"; //save var configFilePath = $"{_hostingEnvironment.ContentRootPath}\\{outputFileName}.json"; bundle.FileName = configFilePath; lock (s_lock) { var cacheKey = string.Format(ScriptsCacheKey, outputFileName); var shouldRebuild = _cacheManager.Get(cacheKey, RecheckBundledFilesPeriod, () => true); if (shouldRebuild) { //process _processor.Process(configFilePath, new List <Bundle> { bundle }); _cacheManager.Set(cacheKey, false, RecheckBundledFilesPeriod); } } //render result.AppendFormat("<script src=\"{0}\"></script>", urlHelper.Content($"~/{BundleFolder}/" + outputFileName + ".min.js")); result.Append(Environment.NewLine); } //parts to not bundle foreach (var item in partsToDontBundle) { result.AppendFormat("<script src=\"{0}\"></script>", urlHelper.Content(item.Path)); result.Append(Environment.NewLine); } return(result.ToString()); } else { //bundling is disabled var result = new StringBuilder(); foreach (var item in _scriptParts[location].Distinct()) { result.AppendFormat("<script src=\"{0}\"></script>", urlHelper.Content(item.Path)); result.Append(Environment.NewLine); } return(result.ToString()); } }
/// <summary> /// Generate all script parts /// </summary> /// <param name="urlHelper">URL Helper</param> /// <param name="location">A location of the script element</param> /// <param name="bundleFiles">A value indicating whether to bundle script elements</param> /// <returns>Generated string</returns> public virtual string RenderScripts(IUrlHelper urlHelper, ResourceLocation location) { if (!_scriptParts.ContainsKey(location) || _scriptParts[location] == null) { return(""); } if (!_scriptParts.Any()) { return(""); } if (_seoSettings.EnableJsBundling) { var partsToBundle = _scriptParts[location] .Where(x => !x.ExcludeFromBundle) .Distinct() .ToArray(); var partsToDontBundle = _scriptParts[location] .Where(x => x.ExcludeFromBundle) .Distinct() .ToArray(); var result = new StringBuilder(); //parts to bundle if (partsToBundle.Any()) { //ensure \bundles directory exists _fileProvider.CreateDirectory(_fileProvider.GetAbsolutePath("bundles")); var bundle = new Bundle(); foreach (var item in partsToBundle) { new PathString(urlHelper.Content(item.Src)) .StartsWithSegments(urlHelper.ActionContext.HttpContext.Request.PathBase, out PathString path); var src = path.Value.TrimStart('/'); //check whether this file exists, if not it should be stored into /wwwroot directory if (!_fileProvider.FileExists(_fileProvider.MapPath(path))) { src = $"wwwroot/{src}"; } bundle.InputFiles.Add(src); } //output file var outputFileName = GetBundleFileName(partsToBundle.Select(x => x.Src).ToArray()); bundle.OutputFileName = "wwwroot/bundles/" + outputFileName + ".js"; //save var configFilePath = _hostingEnvironment.ContentRootPath + "\\" + outputFileName + ".json"; bundle.FileName = configFilePath; lock (s_lock) { //performance optimization. do not bundle and minify for each HTTP request //we periodically re-check already bundles file //so if we have minification enabled, it could take up to several minutes to see changes in updated resource files (or just reset the cache or restart the site) var cacheKey = $"minification.shouldrebuild.scripts-{outputFileName}"; var shouldRebuild = _cacheManager.Get(cacheKey, RecheckBundledFilesPeriod, () => true); if (shouldRebuild) { //process _processor.Process(configFilePath, new List <Bundle> { bundle }); _cacheManager.Set(cacheKey, false, RecheckBundledFilesPeriod); } } //render result.AppendFormat("<script src=\"{0}\"></script>", urlHelper.Content("~/bundles/" + outputFileName + ".min.js")); result.Append(Environment.NewLine); } //parts to not bundle foreach (var item in partsToDontBundle) { result.AppendFormat("<script {1}src=\"{0}\"></script>", urlHelper.Content(item.Src), item.IsAsync ? "async " : ""); result.Append(Environment.NewLine); } return(result.ToString()); } else { //bundling is disabled var result = new StringBuilder(); foreach (var item in _scriptParts[location].Distinct()) { result.AppendFormat("<script {1}src=\"{0}\"></script>", urlHelper.Content(item.Src), item.IsAsync ? "async " : ""); result.Append(Environment.NewLine); } return(result.ToString()); } }