Esempio n. 1
0
        public ActionResult Css(string name)
        {
            var    asset    = AssetDiscovery.FindByName(name);
            string key      = "assets.styles." + name;
            string contents = this.GetContents(key);

            if (string.IsNullOrWhiteSpace(contents))
            {
                var compressor = new StyleBundler(Log.Logger, asset);
                contents = compressor.Compress();

                this.SetContents(key, contents, DateTimeOffset.UtcNow.AddMinutes(asset.CacheDurationInMinutes));
            }

            this.Response.Cache.SetMaxAge(TimeSpan.FromMinutes(asset.CacheDurationInMinutes));
            return(this.Content(contents, "text/css"));
        }
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
                };

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

            await UpdateDependenciesInHtmlFileAsync(directory, styleDefinitions, scriptDefinitions);
        }