Exemple #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);
        }
        public void Setup()
        {
            bundleProvider = new Mock<IBundleProvider<ScriptBundle>>();

            var collection = new BundleCollection<ScriptBundle>();
            renderer = new Mock<IBundleRenderer<ScriptBundle>>();
            resolver = new Mock<IBundleDependencyResolver<ScriptBundle>>();

            bundler = new ScriptBundler(
                bundleProvider.Object,
                renderer.Object,
                resolver.Object);
        }
        public ActionResult Js(string name)
        {
            var asset = AssetDiscovery.FindByName(name);

            if (asset == null)
            {
                return(this.HttpNotFound());
            }

            string key      = "assets.scripts." + name;
            string contents = this.GetContents(key);

            if (string.IsNullOrWhiteSpace(contents))
            {
                var compressor = new ScriptBundler(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/javascript"));
        }
Exemple #4
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.");
        }