예제 #1
0
        private string GetFileContentConsideringMinification(IBundlerContext context, string fileName)
        {
            var isMinFile = IsMinFile(fileName);

            if (!context.IsMinificationEnabled || isMinFile)
            {
                var fileContent = GetFileInfo(context, fileName).ReadAsString();
                Logger.LogDebug($"- {fileName} ({fileContent.Length} bytes)");
                if (context.IsMinificationEnabled && isMinFile)
                {
                    Logger.LogDebug("  > Already minified");
                }

                return(fileContent);
            }

            var minFileInfo = GetMinFileInfoOrNull(fileName);

            if (minFileInfo != null)
            {
                var fileContent = minFileInfo.ReadAsString();
                Logger.LogDebug($"- {fileName}");
                Logger.LogDebug($"  > Using the pre-minified file: {minFileInfo.Name} ({fileContent.Length} bytes)");
                return(fileContent);
            }

            return(GetAndMinifyFileContent(context, fileName));
        }
예제 #2
0
    private string GetAndMinifyFileContent(IBundlerContext context, string fileName)
    {
        var fileContent     = GetFileInfo(context, fileName).ReadAsString();
        var nonMinifiedSize = fileContent.Length;

        Logger.LogDebug($"- {fileName} ({nonMinifiedSize} bytes) - non minified, minifying...");

        try
        {
            fileContent = Minifier.Minify(
                fileContent,
                context.BundleRelativePath,
                fileName
                );

            Logger.LogInformation($"  > Minified {fileName} ({nonMinifiedSize} bytes -> {fileContent.Length} bytes)");

            return(fileContent);
        }
        catch (Exception ex)
        {
            Logger.LogWarning($"Unable to minify the file: {fileName}. Return file content without minification.", ex);
        }

        return(fileContent);
    }
예제 #3
0
        private void AddFileToBundle(IBundlerContext context, StringBuilder bundleContentBuilder, string fileName)
        {
            var fileContent = GetFileContentConsideringMinification(context, fileName);

            fileContent = ProcessBeforeAddingToTheBundle(context, fileName, fileContent);
            bundleContentBuilder.Append(fileContent);
        }
예제 #4
0
        public BundleResult Bundle(IBundlerContext context)
        {
            Logger.LogInformation($"Bundling {context.BundleRelativePath} ({context.ContentFiles.Count} files)");

            var sb = new StringBuilder();

            Logger.LogDebug("Bundle files:");
            foreach (var file in context.ContentFiles)
            {
                var fileContent = GetFileContent(context, file);
                Logger.LogDebug($"- {file} ({fileContent.Length} bytes)");
                sb.AppendLine(fileContent);
            }

            var bundleContent = sb.ToString();

            if (context.IsMinificationEnabled)
            {
                Logger.LogInformation($"Minifying {context.BundleRelativePath} ({bundleContent.Length} bytes)");
                bundleContent = Minifier.Minify(bundleContent, context.BundleRelativePath);
            }

            Logger.LogInformation($"Bundled {context.BundleRelativePath} ({bundleContent.Length} bytes)");

            return(new BundleResult(bundleContent));
        }
예제 #5
0
        private void AddFileToBundle(IBundlerContext context, StringBuilder bundleContentBuilder, string fileName)
        {
            string fileContent = null;

            if (context.IsMinificationEnabled && !IsMinFile(fileName))
            {
                var minFileInfo = GetMinFileInfoOrNull(fileName);
                if (minFileInfo != null)
                {
                    Logger.LogDebug($"- {fileName} ({minFileInfo.Length} bytes) - already minified");
                    fileContent = minFileInfo.ReadAsString();
                }
            }

            if (fileContent == null)
            {
                fileContent = GetFileContent(context, fileName);
                Logger.LogDebug($"- {fileName} ({fileContent.Length} bytes) - non minified");

                if (context.IsMinificationEnabled)
                {
                    var nonMinifiedSize = fileContent.Length;
                    fileContent = Minifier.Minify(fileContent, context.BundleRelativePath);
                    Logger.LogInformation($"  > Minified {fileName} ({nonMinifiedSize} bytes -> {fileContent.Length} bytes)");
                }
            }

            fileContent = ProcessBeforeAddingToTheBundle(context, fileName, fileContent);
            bundleContentBuilder.Append(fileContent);
        }
예제 #6
0
        private void AddFileToBundle(IBundlerContext context, StringBuilder bundleContentBuilder, string fileName)
        {
            if (context.IsMinificationEnabled && !IsMinFile(fileName))
            {
                var minFileInfo = GetMinFileInfoOrNull(fileName);
                if (minFileInfo != null)
                {
                    Logger.LogDebug($"- {fileName} ({minFileInfo.Length} bytes)");
                    bundleContentBuilder.Append(NormalizedCode(minFileInfo.ReadAsString()));
                    return;
                }
            }

            var fileContent = GetFileContent(context, fileName);

            Logger.LogDebug($"- {fileName} ({fileContent.Length} bytes)");

            if (context.IsMinificationEnabled)
            {
                var nonMinifiedSize = fileContent.Length;
                fileContent = Minifier.Minify(fileContent, context.BundleRelativePath);
                Logger.LogInformation($"  -- Minified {context.BundleRelativePath} ({nonMinifiedSize} bytes -> {fileContent.Length} bytes)");
            }

            bundleContentBuilder.Append(NormalizedCode(fileContent));
        }
예제 #7
0
 protected override string ProcessBeforeAddingToTheBundle(IBundlerContext context, string filePath, string fileContent)
 {
     return(CssRelativePath.Adjust(
                fileContent,
                GetAbsolutePath(filePath),
                GetAbsolutePath(context.BundleRelativePath)
                ));
 }
예제 #8
0
 protected override string GetFileContent(IBundlerContext context, string file)
 {
     return(CssRelativePath.Adjust(
                base.GetFileContent(context, file),
                GetAbsolutePath(file),
                GetAbsolutePath(context.BundleRelativePath)
                ));
 }
예제 #9
0
        protected virtual IFileInfo GetFileInfo(IBundlerContext context, string file)
        {
            var fileInfo = WebContentFileProvider.GetFileInfo(file);

            if (!fileInfo.Exists)
            {
                throw new AbpException($"Could not find file '{file}' using {nameof(IWebContentFileProvider)}");
            }

            return(fileInfo);
        }
예제 #10
0
    protected virtual IFileInfo GetFileInfo(IBundlerContext context, string file)
    {
        var fileInfo = HostEnvironment.WebRootFileProvider.GetFileInfo(file);

        if (!fileInfo.Exists)
        {
            throw new AbpException($"Could not find file '{file}'");
        }

        return(fileInfo);
    }
예제 #11
0
        private string GetAndMinifyFileContent(IBundlerContext context, string fileName)
        {
            var fileContent     = GetFileInfo(context, fileName).ReadAsString();
            var nonMinifiedSize = fileContent.Length;

            Logger.LogDebug($"- {fileName} ({nonMinifiedSize} bytes) - non minified, minifying...");

            fileContent = Minifier.Minify(
                fileContent,
                context.BundleRelativePath,
                fileName
                );

            Logger.LogInformation($"  > Minified {fileName} ({nonMinifiedSize} bytes -> {fileContent.Length} bytes)");

            return(fileContent);
        }
예제 #12
0
        public BundleResult Bundle(IBundlerContext context)
        {
            Logger.LogInformation($"Bundling {context.BundleRelativePath} ({context.ContentFiles.Count} files)");

            var bundleContentBuilder = new StringBuilder();

            Logger.LogDebug("Bundle files:");
            foreach (var file in context.ContentFiles)
            {
                AddFileToBundle(context, bundleContentBuilder, file);
            }

            var bundleContent = bundleContentBuilder.ToString();

            Logger.LogInformation($"Bundled {context.BundleRelativePath} ({bundleContent.Length} bytes)");

            return(new BundleResult(bundleContent));
        }
예제 #13
0
    private string GetFileContentConsideringMinification(IBundlerContext context, string fileName)
    {
        var isIgnoredForMinification = BundlingOptions.MinificationIgnoredFiles.Contains(fileName);
        var isMinFile = IsMinFile(fileName);

        if (!context.IsMinificationEnabled || isIgnoredForMinification || isMinFile)
        {
            var fileContent = GetFileInfo(context, fileName).ReadAsString();
            Logger.LogDebug($"- {fileName} ({fileContent.Length} bytes)");
            if (context.IsMinificationEnabled)
            {
                if (isMinFile)
                {
                    Logger.LogDebug("  > Already minified.");
                }
                else if (isIgnoredForMinification)
                {
                    Logger.LogDebug("  > Ignored for minification.");
                }
            }

            return(fileContent);
        }

        var minFileInfo = GetMinFileInfoOrNull(fileName);

        if (minFileInfo != null)
        {
            var fileContent = minFileInfo.ReadAsString();
            Logger.LogDebug($"- {fileName}");
            Logger.LogDebug($"  > Using the pre-minified file: {minFileInfo.Name} ({fileContent.Length} bytes)");
            return(fileContent);
        }

        return(GetAndMinifyFileContent(context, fileName));
    }
예제 #14
0
 protected virtual string ProcessBeforeAddingToTheBundle(IBundlerContext context, string filePath, string fileContent)
 {
     return(fileContent);
 }
예제 #15
0
 protected override string ProcessBeforeAddingToTheBundle(IBundlerContext context, string filePath, string fileContent)
 {
     return(fileContent.EnsureEndsWith(';') + Environment.NewLine);
 }
예제 #16
0
 protected virtual string GetFileContent(IBundlerContext context, string file)
 {
     return(GetFileInfo(context, file).ReadAsString());
 }