Ejemplo n.º 1
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
                };

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

            await UpdateDependenciesInHtmlFileAsync(directory, styleDefinitions, scriptDefinitions);
        }