/// <summary> /// Creates a new bundle and returns a bundle context to add files to it /// </summary> /// <param name="bundleName"></param> /// <returns></returns> /// <remarks> /// The bundle is write once - so if it already exists, a noop context is returned that does nothing /// </remarks> public ISmidgeRequire CreateJsBundle(string bundleName) { if (string.IsNullOrWhiteSpace(bundleName)) { throw new ArgumentNullException(nameof(bundleName)); } if (_bundleManager.Exists(bundleName)) { return(new NoopSmidgeRequire()); } return(new SmidgeRequire(bundleName, _bundleManager, WebFileType.Js, _requestHelper)); }
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) { // Pass through attribute that is also a well-known HTML attribute. // this is required to make sure that other tag helpers executing against this element have // the value copied across if (Source != null) { output.CopyHtmlAttribute("src", context); } if (_bundleManager.Exists(Source)) { var result = (await _smidgeHelper.GenerateJsUrlsAsync(Source, Debug)).ToArray(); var currAttr = output.Attributes.ToDictionary(x => x.Name, x => x.Value); using (var writer = new StringWriter()) { foreach (var s in result) { var builder = new TagBuilder(output.TagName) { TagRenderMode = TagRenderMode.Normal }; builder.MergeAttributes(currAttr); builder.Attributes["src"] = s; builder.WriteTo(writer, _encoder); } writer.Flush(); output.PostElement.SetHtmlContent(new HtmlString(writer.ToString())); } //This ensures the original tag is not written. output.TagName = null; } }
// only issue with creating bundles like this is that we don't have full control over the bundle options, though that could public void CreateCssBundle(string bundleName, BundlingOptions bundleOptions, params string[]?filePaths) { if (filePaths?.Any(f => !f.StartsWith("/") && !f.StartsWith("~/")) ?? false) { throw new InvalidOperationException("All file paths must be absolute"); } if (_bundles.Exists(bundleName)) { throw new InvalidOperationException($"The bundle name {bundleName} already exists"); } PreProcessPipeline pipeline = bundleOptions.OptimizeOutput ? _cssOptimizedPipeline.Value : _cssNonOptimizedPipeline.Value; Bundle bundle = _bundles.Create(bundleName, pipeline, WebFileType.Css, filePaths); bundle.WithEnvironmentOptions(ConfigureBundleEnvironmentOptions(bundleOptions)); }