Esempio n. 1
0
        public async Task BundleAsync(string directory, bool forceBuild, bool bundle, bool minify, string bundleName)
        {
            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];

            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);
            var    scriptContext = GetScriptContext(bundleDefinitions);
            string styleDefinitions;
            string scriptDefinitions;

            if (bundle || minify)
            {
                var options = new BundleOptions
                {
                    Directory        = directory,
                    FrameworkVersion = frameworkVersion,
                    ProjectFileName  = projectName,
                    BundleName       = bundleName,
                    Minify           = minify
                };

                styleDefinitions  = StyleBundler.Bundle(options, styleContext);
                scriptDefinitions = ScriptBundler.Bundle(options, scriptContext);
            }
            else
            {
                styleDefinitions  = GenerateStyleDefinitions(styleContext);
                scriptDefinitions = GenerateScriptDefinitions(scriptContext);
            }

            await UpdateDependenciesInHtmlFileAsync(directory, styleDefinitions, scriptDefinitions);
        }
Esempio n. 2
0
        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
                };

                Logger.LogInformation("Generating style bundle...");
                styleDefinitions = StyleBundler.Bundle(options, styleContext);
                Logger.LogInformation($"Style bundle has been generated successfully.");

                Logger.LogInformation("Generating script bundle...");
                scriptDefinitions = ScriptBundler.Bundle(options, scriptContext);
                Logger.LogInformation($"Script bundle has been generated successfully.");
            }
            else
            {
                Logger.LogInformation("Generating style references...");
                styleDefinitions = GenerateStyleDefinitions(styleContext);
                Logger.LogInformation("Generating script references...");
                scriptDefinitions = GenerateScriptDefinitions(scriptContext);
            }

            await UpdateDependenciesInHtmlFileAsync(directory, styleDefinitions, scriptDefinitions);

            Logger.LogInformation("Script and style references in the index.html file have been updated.");
        }