private async Task UpdateDependenciesInHtmlFileAsync(string directory, string styleDefinitions, string scriptDefinitions) { var htmlFilePath = Path.Combine(PathHelper.GetWwwRootPath(directory), "index.html"); if (!File.Exists(htmlFilePath)) { throw new BundlingException($"index.html file could not be found in the following path:{htmlFilePath}"); } Encoding fileEncoding; string content; using (var reader = new StreamReader(htmlFilePath, true)) { fileEncoding = reader.CurrentEncoding; content = await reader.ReadToEndAsync(); } content = UpdatePlaceholders(content, BundlingConsts.StylePlaceholderStart, BundlingConsts.StylePlaceholderEnd, styleDefinitions); content = UpdatePlaceholders(content, BundlingConsts.ScriptPlaceholderStart, BundlingConsts.ScriptPlaceholderEnd, scriptDefinitions); using (var writer = new StreamWriter(htmlFilePath, false, fileEncoding)) { await writer.WriteAsync(content); await writer.FlushAsync(); } }
private string BundleFiles(BundleOptions options, List <BundleDefinition> bundleDefinitions) { var staticAssetsFilePath = Path.Combine(options.Directory, "bin", "Debug", options.FrameworkVersion, $"{options.ProjectFileName}.StaticWebAssets.xml"); if (!File.Exists(staticAssetsFilePath)) { throw new BundlingException( "Unable to find static web assets file. You need to build the project to generate static web assets file."); } var staticAssetsDefinitions = new XmlDocument(); staticAssetsDefinitions.Load(staticAssetsFilePath); var builder = new StringBuilder(); foreach (var definition in bundleDefinitions) { string content; if (definition.Source.StartsWith("_content")) { var pathFragments = definition.Source.Split('/').ToList(); var basePath = $"{pathFragments[0]}/{pathFragments[1]}"; var node = staticAssetsDefinitions.SelectSingleNode($"//ContentRoot[@BasePath='{basePath}']"); if (node?.Attributes == null) { throw new AbpException("Not found: " + definition.Source); } var path = node.Attributes["Path"].Value; var absolutePath = definition.Source.Replace(basePath, path); content = GetFileContent(absolutePath, options.Minify); } else if (definition.Source.StartsWith("_framework")) { var slashIndex = definition.Source.IndexOf('/'); var fileName = definition.Source.Substring(slashIndex + 1, definition.Source.Length - slashIndex - 1); var filePath = Path.Combine(PathHelper.GetFrameworkFolderPath(options.Directory, options.FrameworkVersion), fileName); content = GetFileContent(filePath, false); } else { var filePath = Path.Combine(PathHelper.GetWwwRootPath(options.Directory), definition.Source); content = GetFileContent(filePath, options.Minify); } content = ProcessBeforeAddingToTheBundle(definition.Source, Path.Combine(options.Directory, "wwwroot"), content); builder.AppendLine(content); } return(builder.ToString()); }
public string Bundle(BundleOptions options, BundleContext context) { var bundleFilePath = Path.Combine(PathHelper.GetWwwRootPath(options.Directory), $"{options.BundleName}{FileExtension}"); var bundledContent = BundleFiles(options, context); File.WriteAllText(bundleFilePath, bundledContent); return(GenerateDefinition(bundleFilePath)); }
public string Bundle(BundleOptions options, BundleContext context) { var bundleFilePath = Path.Combine(PathHelper.GetWwwRootPath(options.Directory), $"{options.BundleName}{FileExtension}"); var bundleFileDefinitions = context.BundleDefinitions.Where(t => t.ExcludeFromBundle == false).ToList(); var fileDefinitionsExcludingFromBundle = context.BundleDefinitions.Where(t => t.ExcludeFromBundle).ToList(); var bundledContent = BundleFiles(options, bundleFileDefinitions); File.WriteAllText(bundleFilePath, bundledContent); return(GenerateDefinition(bundleFilePath, fileDefinitionsExcludingFromBundle)); }
public async Task BundleAsync(string directory, bool forceBuild) { var projectFiles = Directory.GetFiles(directory, "*.csproj"); if (!projectFiles.Any()) { throw new BundlingException( "No project file found in the directory. The working directory must have a Blazor project file."); } var projectFilePath = projectFiles[0]; var config = ConfigReader.Read(PathHelper.GetWwwRootPath(directory)); var bundleConfig = config.Bundle; if (forceBuild) { var projects = new List <DotNetProjectInfo>() { new DotNetProjectInfo(string.Empty, projectFilePath, true) }; DotNetProjectBuilder.BuildProjects(projects, string.Empty); } var frameworkVersion = GetTargetFrameworkVersion(projectFilePath); var projectName = Path.GetFileNameWithoutExtension(projectFilePath); var assemblyFilePath = PathHelper.GetAssemblyFilePath(directory, frameworkVersion, projectName); var startupModule = GetStartupModule(assemblyFilePath); var bundleDefinitions = new List <BundleTypeDefinition>(); FindBundleContributorsRecursively(startupModule, 0, bundleDefinitions); bundleDefinitions = bundleDefinitions.OrderByDescending(t => t.Level).ToList(); var styleContext = GetStyleContext(bundleDefinitions, bundleConfig.Parameters); var scriptContext = GetScriptContext(bundleDefinitions, bundleConfig.Parameters); string styleDefinitions; string scriptDefinitions; if (bundleConfig.Mode is BundlingMode.Bundle || bundleConfig.Mode is BundlingMode.BundleAndMinify) { var options = new BundleOptions { Directory = directory, FrameworkVersion = frameworkVersion, ProjectFileName = projectName, BundleName = bundleConfig.Name.IsNullOrEmpty() ? "global" : bundleConfig.Name, Minify = bundleConfig.Mode == BundlingMode.BundleAndMinify }; styleDefinitions = StyleBundler.Bundle(options, styleContext); scriptDefinitions = ScriptBundler.Bundle(options, scriptContext); } else { styleDefinitions = GenerateStyleDefinitions(styleContext); scriptDefinitions = GenerateScriptDefinitions(scriptContext); } await UpdateDependenciesInHtmlFileAsync(directory, styleDefinitions, scriptDefinitions); }