Esempio n. 1
0
        private bool ProcessBundle(string baseFolder, Bundle bundle)
        {
            OnProcessing(bundle, baseFolder);
            var  inputs  = bundle.GetAbsoluteInputFiles();
            bool changed = false;

            if (bundle.GetAbsoluteInputFiles(true).Count > 1 || bundle.InputFiles.FirstOrDefault() != bundle.OutputFileName)
            {
                BundleHandler.ProcessBundle(baseFolder, bundle);

                if (!bundle.IsMinificationEnabled || !bundle.OutputIsMinFile)
                {
                    string outputFile      = bundle.GetAbsoluteOutputFile();
                    bool   containsChanges = FileHelpers.HasFileContentChanged(outputFile, bundle.Output);

                    if (containsChanges)
                    {
                        OnBeforeBundling(bundle, baseFolder, containsChanges);
                        DirectoryInfo outputFileDirectory = Directory.GetParent(outputFile);
                        outputFileDirectory.Create();

                        File.WriteAllText(outputFile, bundle.Output, new UTF8Encoding(false));
                        OnAfterBundling(bundle, baseFolder, containsChanges);
                        changed = true;
                    }
                }
            }

            string minFile = BundleMinifier.GetMinFileName(bundle.GetAbsoluteOutputFile());

            if (bundle.IsMinificationEnabled || bundle.IsGzipEnabled)
            {
                var result = BundleMinifier.MinifyBundle(bundle);

                changed |= result.Changed;

                if (bundle.IsMinificationEnabled && bundle.SourceMap && !string.IsNullOrEmpty(result.SourceMap))
                {
                    string mapFile   = minFile + ".map";
                    bool   smChanges = FileHelpers.HasFileContentChanged(mapFile, result.SourceMap);

                    if (smChanges)
                    {
                        OnBeforeWritingSourceMap(minFile, mapFile, smChanges);
                        File.WriteAllText(mapFile, result.SourceMap, new UTF8Encoding(false));
                        OnAfterWritingSourceMap(minFile, mapFile, smChanges);
                        changed = true;
                    }
                }
            }

            return(changed);
        }
Esempio n. 2
0
 protected void OnProcessing(Bundle bundle, string baseFolder)
 {
     if (Processing != null)
     {
         Processing(this, new BundleFileEventArgs(bundle.GetAbsoluteOutputFile(), bundle, baseFolder, false));
     }
 }
Esempio n. 3
0
 protected void OnAfterBundling(Bundle bundle, string baseFolder, bool containsChanges)
 {
     if (AfterBundling != null)
     {
         AfterBundling(this, new BundleFileEventArgs(bundle.GetAbsoluteOutputFile(), bundle, baseFolder, containsChanges));
     }
 }
Esempio n. 4
0
 protected void OnBeforeProcess(Bundle bundle, string baseFolder)
 {
     if (BeforeProcess != null)
     {
         BeforeProcess(this, new BundleFileEventArgs(bundle.GetAbsoluteOutputFile(), bundle, baseFolder));
     }
 }
Esempio n. 5
0
        private void ProcessBundle(string baseFolder, Bundle bundle)
        {
            BundleHandler.ProcessBundle(baseFolder, bundle);

            string outputFile = Path.Combine(baseFolder, bundle.OutputFileName);

            OnBeforeProcess(bundle, baseFolder);

            DirectoryInfo outputFileDirectory = Directory.GetParent(outputFile);

            outputFileDirectory.Create();

            File.WriteAllText(outputFile, bundle.Output, new UTF8Encoding(true));

            OnAfterProcess(bundle, baseFolder);

            if (bundle.Minify.ContainsKey("enabled") && bundle.Minify["enabled"].ToString().Equals("true", StringComparison.OrdinalIgnoreCase))
            {
                var result = BundleMinifier.MinifyBundle(bundle);

                if (result != null && bundle.SourceMaps && !string.IsNullOrEmpty(result.SourceMap))
                {
                    string minFile = FileMinifier.GetMinFileName(bundle.GetAbsoluteOutputFile());
                    string mapFile = minFile + ".map";

                    OnBeforeWritingSourceMap(minFile, mapFile);
                    File.WriteAllText(mapFile, result.SourceMap, new UTF8Encoding(true));
                    OnAfterWritingSourceMap(minFile, mapFile);
                }
            }
        }
Esempio n. 6
0
        public static void ProcessBundle(string baseFolder, Bundle bundle)
        {
            StringBuilder sb         = new StringBuilder();
            List <string> inputFiles = bundle.GetAbsoluteInputFiles();

            foreach (string input in inputFiles)
            {
                string file = Path.Combine(baseFolder, input);

                if (File.Exists(file))
                {
                    string content;

                    if (input.EndsWith(".css", StringComparison.OrdinalIgnoreCase) && AdjustRelativePaths(bundle))
                    {
                        content = CssRelativePath.Adjust(file, bundle.GetAbsoluteOutputFile());
                    }
                    else
                    {
                        content = BundleMinifier.ReadAllText(file);
                    }

                    sb.AppendLine(content);
                }
            }

            bundle.Output = sb.ToString().Trim();
        }
 protected void OnAfterProcess(Bundle bundle, string baseFolder)
 {
     if (AfterProcess != null)
     {
         AfterProcess(this, new BundleFileEventArgs(bundle.GetAbsoluteOutputFile(), bundle, baseFolder));
     }
 }
Esempio n. 8
0
        public static MinificationResult MinifyBundle(Bundle bundle)
        {
            string file      = bundle.GetAbsoluteOutputFile();
            string extension = Path.GetExtension(file).ToUpperInvariant();
            var    minResult = new MinificationResult(file, null, null);

            if (!string.IsNullOrEmpty(bundle.Output) && bundle.IsMinificationEnabled)
            {
                try
                {
                    switch (extension)
                    {
                    case ".JS":
                        MinifyJavaScript(bundle, minResult);
                        break;

                    case ".CSS":
                        MinifyCss(bundle, minResult);
                        break;

                    case ".HTML":
                    case ".HTM":
                        MinifyHtml(bundle, minResult);
                        break;
                    }
                }
                catch (Exception ex)
                {
                    AddGenericException(minResult, ex);
                }
            }

            if (minResult.HasErrors)
            {
                OnErrorMinifyingFile(minResult);
            }
            else if (bundle.IsGzipEnabled)
            {
                string minFile = bundle.IsMinificationEnabled
                    ? GetMinFileName(bundle.GetAbsoluteOutputFile(), bundle.IsDebugMinificationEnabled)
                    : bundle.GetAbsoluteOutputFile();
                GzipFile(minFile, bundle, minResult);
            }

            return(minResult);
        }
Esempio n. 9
0
        private void ProcessBundle(string baseFolder, Bundle bundle)
        {
            var inputLastModified = bundle.GetAbsoluteInputFiles().Max(inputFile => File.GetLastWriteTimeUtc(inputFile));

            if ((bundle.GetAbsoluteInputFiles().Count > 1 || bundle.InputFiles.FirstOrDefault() != bundle.OutputFileName) &&
                inputLastModified > File.GetLastWriteTimeUtc(bundle.GetAbsoluteOutputFile()))
            {
                BundleHandler.ProcessBundle(baseFolder, bundle);

                string outputFile      = Path.Combine(baseFolder, bundle.OutputFileName);
                bool   containsChanges = FileHelpers.HasFileContentChanged(outputFile, bundle.Output);

                if (containsChanges)
                {
                    OnProcessing(bundle, baseFolder);
                    DirectoryInfo outputFileDirectory = Directory.GetParent(outputFile);
                    outputFileDirectory.Create();

                    File.WriteAllText(outputFile, bundle.Output, new UTF8Encoding(false));
                    OnAfterBundling(bundle, baseFolder, containsChanges);
                }
            }

            string minFile = GetMinFileName(bundle.GetAbsoluteOutputFile());

            if (bundle.Minify.ContainsKey("enabled") && bundle.Minify["enabled"].ToString().Equals("true", StringComparison.OrdinalIgnoreCase) &&
                inputLastModified > File.GetLastWriteTimeUtc(minFile))
            {
                var result = BundleMinifier.MinifyBundle(bundle);

                if (result != null && bundle.SourceMap && !string.IsNullOrEmpty(result.SourceMap))
                {
                    string mapFile   = minFile + ".map";
                    bool   smChanges = FileHelpers.HasFileContentChanged(mapFile, result.SourceMap);

                    if (smChanges)
                    {
                        OnBeforeWritingSourceMap(minFile, mapFile, smChanges);
                        File.WriteAllText(mapFile, result.SourceMap, new UTF8Encoding(false));
                        OnAfterWritingSourceMap(minFile, mapFile, smChanges);
                    }
                }
            }

            Telemetry.TrackCompile(bundle);
        }
Esempio n. 10
0
        private static MinificationResult MinifyHtml(Bundle bundle)
        {
            string file     = bundle.GetAbsoluteOutputFile();
            string content  = ReadAllText(file);
            var    settings = HtmlOptions.GetSettings(bundle);
            string minFile  = GetMinFileName(file);

            var minifier  = new HtmlMinifier(settings);
            var minResult = new MinificationResult(file, null, null);

            try
            {
                MarkupMinificationResult result = minifier.Minify(content, generateStatistics: true);
                minResult.MinifiedContent = result.MinifiedContent.Trim();

                if (!result.Errors.Any())
                {
                    bool containsChanges = FileHelpers.HasFileContentChanged(minFile, result.MinifiedContent);

                    OnBeforeWritingMinFile(file, minFile, bundle, containsChanges);

                    if (containsChanges)
                    {
                        File.WriteAllText(minFile, result.MinifiedContent, new UTF8Encoding(false));
                    }

                    OnAfterWritingMinFile(file, minFile, bundle, containsChanges);

                    GzipFile(minFile, bundle, containsChanges);
                }
                else
                {
                    foreach (var error in result.Errors)
                    {
                        minResult.Errors.Add(new MinificationError
                        {
                            FileName     = file,
                            Message      = error.Message,
                            LineNumber   = error.LineNumber,
                            ColumnNumber = error.ColumnNumber
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                minResult.Errors.Add(new MinificationError
                {
                    FileName     = file,
                    Message      = ex.Message,
                    LineNumber   = 0,
                    ColumnNumber = 0
                });
            }

            return(minResult);
        }
Esempio n. 11
0
        private void CleanBundle(string baseFolder, Bundle bundle)
        {
            string outputFile = bundle.GetAbsoluteOutputFile();

            baseFolder = baseFolder.DemandTrailingPathSeparatorChar();
            if (!bundle.GetAbsoluteInputFiles().Contains(outputFile, StringComparer.OrdinalIgnoreCase))
            {
                if (File.Exists(outputFile))
                {
                    FileHelpers.RemoveReadonlyFlagFromFile(outputFile);
                    File.Delete(outputFile);
                    Console.WriteLine($"Deleted {FileHelpers.MakeRelative(baseFolder, outputFile).Cyan().Bright()}");
                }
            }

            string minFile = BundleMinifier.GetMinFileName(bundle.GetAbsoluteOutputFile());
            string mapFile = minFile + ".map";
            string gzFile  = minFile + ".gz";

            if (File.Exists(minFile))
            {
                FileHelpers.RemoveReadonlyFlagFromFile(minFile);
                File.Delete(minFile);
                Console.WriteLine($"Deleted {FileHelpers.MakeRelative(baseFolder, minFile).Cyan().Bright()}");
            }

            if (File.Exists(mapFile))
            {
                FileHelpers.RemoveReadonlyFlagFromFile(mapFile);
                File.Delete(mapFile);
                Console.WriteLine($"Deleted {mapFile.Cyan().Bright()}");
            }

            if (File.Exists(gzFile))
            {
                FileHelpers.RemoveReadonlyFlagFromFile(gzFile);
                File.Delete(gzFile);
                Console.WriteLine($"Deleted {gzFile.Cyan().Bright()}");
            }
        }
Esempio n. 12
0
        private static MinificationResult MinifyCss(Bundle bundle)
        {
            string file     = bundle.GetAbsoluteOutputFile();
            string content  = ReadAllText(file);
            var    settings = CssOptions.GetSettings(bundle);
            string minFile  = GetMinFileName(file);

            var minifier = new Minifier();
            var result   = new MinificationResult(file, null, null);

            try
            {
                result.MinifiedContent = minifier.MinifyStyleSheet(content, settings).Trim();

                if (!minifier.Errors.Any())
                {
                    bool containsChanges = FileHelpers.HasFileContentChanged(minFile, result.MinifiedContent);

                    OnBeforeWritingMinFile(file, minFile, bundle, containsChanges);

                    if (containsChanges)
                    {
                        File.WriteAllText(minFile, result.MinifiedContent, new UTF8Encoding(false));
                    }

                    OnAfterWritingMinFile(file, minFile, bundle, containsChanges);

                    GzipFile(minFile, bundle, containsChanges);
                }
                else
                {
                    AddAjaxminErrors(minifier, result);
                }
            }
            catch (Exception ex)
            {
                result.Errors.Add(new MinificationError
                {
                    FileName     = file,
                    Message      = ex.Message,
                    LineNumber   = 0,
                    ColumnNumber = 0
                });
            }

            return(result);
        }
Esempio n. 13
0
        public static void ProcessBundle(string baseFolder, Bundle bundle)
        {
            DateTime      mostRecentWrite = default(DateTime);
            StringBuilder sb         = new StringBuilder();
            List <string> inputFiles = bundle.GetAbsoluteInputFiles();

            for (int i = 0; i < inputFiles.Count; i++)
            {
                var input = inputFiles[i];

                string file = Path.Combine(baseFolder, input);

                if (File.Exists(file))
                {
                    string content;

                    if (input.EndsWith(".css", StringComparison.OrdinalIgnoreCase) && AdjustRelativePaths(bundle))
                    {
                        content = CssRelativePath.Adjust(file, bundle.GetAbsoluteOutputFile());
                    }
                    else
                    {
                        content = FileHelpers.ReadAllText(file);
                    }
                    var lastWriteFile = System.IO.File.GetLastWriteTimeUtc(file);
                    if (mostRecentWrite < lastWriteFile)
                    {
                        mostRecentWrite = lastWriteFile;
                    }

                    // adding new line only if there are more than 1 files
                    // otherwise we are preserving file integrity
                    if (sb.Length > 0)
                    {
                        sb.AppendLine();
                    }

                    sb.Append(content);
                }
            }

            bundle.MostRecentWrite = mostRecentWrite;
            bundle.Output          = sb.ToString();
        }
Esempio n. 14
0
        public static void ProcessBundle(string baseFolder, Bundle bundle)
        {
            StringBuilder sb         = new StringBuilder();
            List <string> inputFiles = bundle.GetAbsoluteInputFiles();

            //string ext = Path.GetExtension(bundle.OutputFileName);

            // Support both directories and specific files
            //foreach (string input in bundle.InputFiles)
            //{
            //    string fullPath = Path.Combine(baseFolder, input);

            //    if (Directory.Exists(fullPath))
            //    {
            //        DirectoryInfo dir = new DirectoryInfo(fullPath);
            //        SearchOption search = bundle.Recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
            //        var files = dir.GetFiles("*" + ext, search);
            //        inputFiles.AddRange(files.Select(f => f.FullName));
            //    }
            //}

            foreach (string input in inputFiles)
            {
                string file = Path.Combine(baseFolder, input);
                string content;

                if (input.EndsWith(".css", System.StringComparison.OrdinalIgnoreCase) && AdjustRelativePaths(bundle))
                {
                    content = CssRelativePath.Adjust(file, bundle.GetAbsoluteOutputFile());
                }
                else
                {
                    content = BundleMinifier.ReadAllText(file);
                }

                sb.AppendLine(content);
            }

            bundle.Output = sb.ToString().Trim();
        }
Esempio n. 15
0
        public static void ProcessBundle(string baseFolder, Bundle bundle)
        {
            StringBuilder sb = new StringBuilder();
            List<string> inputFiles = bundle.GetAbsoluteInputFiles();
            //string ext = Path.GetExtension(bundle.OutputFileName);

            // Support both directories and specific files
            //foreach (string input in bundle.InputFiles)
            //{
            //    string fullPath = Path.Combine(baseFolder, input);

            //    if (Directory.Exists(fullPath))
            //    {
            //        DirectoryInfo dir = new DirectoryInfo(fullPath);
            //        SearchOption search = bundle.Recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
            //        var files = dir.GetFiles("*" + ext, search);
            //        inputFiles.AddRange(files.Select(f => f.FullName));
            //    }
            //}

            foreach (string input in inputFiles)
            {
                string file = Path.Combine(baseFolder, input);
                string content;

                if (input.EndsWith(".css", System.StringComparison.OrdinalIgnoreCase) && AdjustRelativePaths(bundle))
                {
                    content = CssRelativePath.Adjust(file, bundle.GetAbsoluteOutputFile());
                }
                else
                {
                    content = BundleMinifier.ReadAllText(file);
                }

                sb.AppendLine(content);
            }

            bundle.Output = sb.ToString().Trim();
        }
Esempio n. 16
0
        public bool FilesChanged(FileSystemEventArgs e)
        {
            if (!IsFileValid(e.FullPath))
            {
                return(false);
            }

            if (!BundleFileProcessor.IsFileConfigured(_configFile, e.FullPath).Any())
            {
                return(false);
            }

            var inputs            = _bundle.GetAbsoluteInputFiles();
            var inputLastModified = inputs.Count > 0 ? inputs.Max(inputFile => File.GetLastWriteTimeUtc(inputFile)) : DateTime.MaxValue;

            if ((_bundle.GetAbsoluteInputFiles().Count > 1 || _bundle.InputFiles.FirstOrDefault() != _bundle.OutputFileName) &&
                inputLastModified > File.GetLastWriteTimeUtc(_bundle.GetAbsoluteOutputFile()))
            {
                return(_processor.Process(_configFile, new Bundle[] { _bundle }));
            }

            return(false);
        }
Esempio n. 17
0
        private static MinificationResult MinifyCss(Bundle bundle)
        {
            string file = bundle.GetAbsoluteOutputFile();
            string content = File.ReadAllText(file);
            var settings = CssOptions.GetSettings(bundle);
            string minFile = GetMinFileName(file);

            var minifier = new Minifier();
            var result = new MinificationResult(file, null, null);

            try
            {
                result.MinifiedContent = minifier.MinifyStyleSheet(content, settings);

                if (!minifier.Errors.Any())
                {
                    OnBeforeWritingMinFile(file, minFile, bundle);
                    File.WriteAllText(minFile, result.MinifiedContent, new UTF8Encoding(true));
                    OnAfterWritingMinFile(file, minFile, bundle);

                    GzipFile(minFile, bundle);
                }
                else
                {
                    FileMinifier.AddAjaxminErrors(minifier, result);
                }
            }
            catch (Exception ex)
            {
                result.Errors.Add(new MinificationError
                {
                    FileName = file,
                    Message = ex.Message,
                    LineNumber = 0,
                    ColumnNumber = 0
                });
            }

            return result;
        }
Esempio n. 18
0
 protected void OnMinificationSkipped(Bundle bundle, string baseFolder, bool containsChanges)
 {
     MinificationSkipped?.Invoke(this, new BundleFileEventArgs(bundle.GetAbsoluteOutputFile(), bundle, baseFolder, containsChanges));
 }
Esempio n. 19
0
 protected void OnBeforeBundling(Bundle bundle, string baseFolder, bool containsChanges)
 {
     BeforeBundling?.Invoke(this, new BundleFileEventArgs(bundle.GetAbsoluteOutputFile(), bundle, baseFolder, containsChanges));
 }
 protected void OnProcessing(Bundle bundle, string baseFolder)
 {
     if (Processing != null)
     {
         Processing(this, new BundleFileEventArgs(bundle.GetAbsoluteOutputFile(), bundle, baseFolder, false));
     }
 }
Esempio n. 21
0
        private bool ProcessBundle(string baseFolder, Bundle bundle)
        {
            OnProcessing(bundle, baseFolder);
            var  inputs  = bundle.GetAbsoluteInputFiles();
            bool changed = false;

            if (bundle.GetAbsoluteInputFiles(true).Count > 1 || bundle.InputFiles.FirstOrDefault() != bundle.OutputFileName)
            {
                BundleHandler.ProcessBundle(baseFolder, bundle);

                if (!bundle.IsMinificationEnabled || !bundle.OutputIsMinFile)
                {
                    string outputFile      = bundle.GetAbsoluteOutputFile();
                    bool   containsChanges = FileHelpers.HasFileContentChanged(outputFile, bundle.Output);

                    if (containsChanges)
                    {
                        OnBeforeBundling(bundle, baseFolder, containsChanges);
                        DirectoryInfo outputFileDirectory = Directory.GetParent(outputFile);
                        outputFileDirectory.Create();

                        File.WriteAllText(outputFile, bundle.Output, new UTF8Encoding(false));
                        OnAfterBundling(bundle, baseFolder, containsChanges);
                        changed = true;
                    }
                }
            }

            MinificationResult minResult = null;
            var minFile = BundleMinifier.GetMinFileName(bundle.GetAbsoluteOutputFile());

            if (bundle.IsMinificationEnabled)
            {
                var outputWriteTime = File.GetLastWriteTimeUtc(minFile);
                var minifyChanged   = bundle.MostRecentWrite >= outputWriteTime;

                if (minifyChanged)
                {
                    minResult = BundleMinifier.MinifyBundle(bundle);

                    // If no change is detected, then the minFile is not modified, so we need to update the write time manually
                    if (!minResult.Changed && File.Exists(minFile))
                    {
                        File.SetLastWriteTimeUtc(minFile, DateTime.UtcNow);
                    }
                    changed |= minResult.Changed;

                    if (bundle.SourceMap && !string.IsNullOrEmpty(minResult.SourceMap))
                    {
                        string mapFile   = minFile + ".map";
                        bool   smChanges = FileHelpers.HasFileContentChanged(mapFile, minResult.SourceMap);

                        if (smChanges)
                        {
                            OnBeforeWritingSourceMap(minFile, mapFile, smChanges);
                            File.WriteAllText(mapFile, minResult.SourceMap, new UTF8Encoding(false));
                            OnAfterWritingSourceMap(minFile, mapFile, smChanges);
                            changed = true;
                        }
                    }
                }
                else
                {
                    OnMinificationSkipped(bundle, baseFolder, false);
                }
            }

            if (bundle.IsGzipEnabled)
            {
                var fileToGzip = bundle.IsMinificationEnabled ?
                                 minFile : bundle.GetAbsoluteOutputFile();

                if (minResult == null)
                {
                    BundleMinifier.GzipFile(fileToGzip, bundle, false, File.ReadAllText(fileToGzip));
                }
                else
                {
                    BundleMinifier.GzipFile(fileToGzip, bundle, minResult.Changed, minResult.MinifiedContent);
                }
            }

            return(changed);
        }
Esempio n. 22
0
        private static MinificationResult MinifyJavaScript(Bundle bundle)
        {
            string file = bundle.GetAbsoluteOutputFile();
            var settings = JavaScriptOptions.GetSettings(bundle);
            var minifier = new Minifier();
            var result = new MinificationResult(file, null, null);

            string minFile = GetMinFileName(file);
            string mapFile = minFile + ".map";

            try
            {
                if (!bundle.SourceMaps)
                {
                    result.MinifiedContent = minifier.MinifyJavaScript(File.ReadAllText(file), settings);

                    if (!minifier.Errors.Any())
                    {
                        OnBeforeWritingMinFile(file, minFile, bundle);
                        File.WriteAllText(minFile, result.MinifiedContent, new UTF8Encoding(true));
                        OnAfterWritingMinFile(file, minFile, bundle);
                    }
                    else
                    {
                        FileMinifier.AddAjaxminErrors(minifier, result);
                    }
                }
                else
                {
                    using (StringWriter writer = new StringWriter())
                    {
                        using (V3SourceMap sourceMap = new V3SourceMap(writer))
                        {
                            settings.SymbolsMap = sourceMap;
                            sourceMap.StartPackage(minFile, mapFile);

                            minifier.FileName = file;
                            result.MinifiedContent = minifier.MinifyJavaScript(File.ReadAllText(file), settings);

                            if (!minifier.Errors.Any())
                            {
                                OnBeforeWritingMinFile(file, minFile, bundle);
                                File.WriteAllText(minFile, result.MinifiedContent, new UTF8Encoding(true));
                                OnAfterWritingMinFile(file, minFile, bundle);
                            }
                            else
                            {
                                FileMinifier.AddAjaxminErrors(minifier, result);
                            }
                        }

                        result.SourceMap = writer.ToString();
                    }
                }

                GzipFile(minFile, bundle);
            }
            catch (Exception ex)
            {
                result.Errors.Add(new MinificationError
                {
                    FileName = file,
                    Message = ex.Message,
                    LineNumber = 0,
                    ColumnNumber = 0
                });
            }

            return result;
        }
        private void ProcessBundle(string baseFolder, Bundle bundle)
        {
            OnProcessing(bundle, baseFolder);

            if (bundle.GetAbsoluteInputFiles().Count > 1 || bundle.InputFiles.FirstOrDefault() != bundle.OutputFileName)
            {
                BundleHandler.ProcessBundle(baseFolder, bundle);

                string outputFile = Path.Combine(baseFolder, bundle.OutputFileName);
                bool containsChanges = FileHelpers.HasFileContentChanged(outputFile, bundle.Output);

                if (containsChanges)
                {
                    DirectoryInfo outputFileDirectory = Directory.GetParent(outputFile);
                    outputFileDirectory.Create();

                    File.WriteAllText(outputFile, bundle.Output, new UTF8Encoding(false));
                }

                OnAfterBundling(bundle, baseFolder, containsChanges);
            }

            if (bundle.Minify.ContainsKey("enabled") && bundle.Minify["enabled"].ToString().Equals("true", StringComparison.OrdinalIgnoreCase))
            {
                var result = BundleMinifier.MinifyBundle(bundle);

                if (result != null && bundle.SourceMap && !string.IsNullOrEmpty(result.SourceMap))
                {
                    string minFile = GetMinFileName(bundle.GetAbsoluteOutputFile());
                    string mapFile = minFile + ".map";
                    bool smChanges = FileHelpers.HasFileContentChanged(mapFile, result.SourceMap);

                    OnBeforeWritingSourceMap(minFile, mapFile, smChanges);

                    if (smChanges)
                    {
                        File.WriteAllText(mapFile, result.SourceMap, new UTF8Encoding(false));
                    }

                    OnAfterWritingSourceMap(minFile, mapFile, smChanges);
                }
            }
        }
        private void ProcessBundle(string baseFolder, Bundle bundle)
        {
            BundleHandler.ProcessBundle(baseFolder, bundle);

            string outputFile = Path.Combine(baseFolder, bundle.OutputFileName);

            OnBeforeProcess(bundle, baseFolder);

            DirectoryInfo outputFileDirectory = Directory.GetParent(outputFile);
            outputFileDirectory.Create();

            File.WriteAllText(outputFile, bundle.Output, new UTF8Encoding(true));

            OnAfterProcess(bundle, baseFolder);

            if (bundle.Minify.ContainsKey("enabled") && bundle.Minify["enabled"].ToString().Equals("true", StringComparison.OrdinalIgnoreCase))
            {
                var result = BundleMinifier.MinifyBundle(bundle);

                if (result != null && bundle.SourceMaps && !string.IsNullOrEmpty(result.SourceMap))
                {
                    string minFile = FileMinifier.GetMinFileName(bundle.GetAbsoluteOutputFile());
                    string mapFile = minFile + ".map";

                    OnBeforeWritingSourceMap(minFile, mapFile);
                    File.WriteAllText(mapFile, result.SourceMap, new UTF8Encoding(true));
                    OnAfterWritingSourceMap(minFile, mapFile);
                }
            }
        }
Esempio n. 25
0
        private static MinificationResult MinifyJavaScript(Bundle bundle)
        {
            string file     = bundle.GetAbsoluteOutputFile();
            var    settings = JavaScriptOptions.GetSettings(bundle);
            var    result   = new MinificationResult(file, null, null);

            string minFile = GetMinFileName(file);
            string mapFile = minFile + ".map";

            try
            {
                if (!bundle.SourceMap)
                {
                    UgliflyResult uglifyResult;
                    try
                    {
                        uglifyResult = Uglify.Js(ReadAllText(file), settings);
                    }
                    catch
                    {
                        uglifyResult = new UgliflyResult(null,
                                                         new List <UglifyError> {
                            new UglifyError
                            {
                                IsError = true,
                                File    = file,
                                Message = "Error processing file"
                            }
                        });
                    }

                    result.MinifiedContent = uglifyResult.Code?.Trim();

                    if (!uglifyResult.HasErrors && !string.IsNullOrEmpty(result.MinifiedContent))
                    {
                        bool containsChanges = FileHelpers.HasFileContentChanged(minFile, result.MinifiedContent);

                        OnBeforeWritingMinFile(file, minFile, bundle, containsChanges);
                        result.Changed |= containsChanges;

                        if (containsChanges)
                        {
                            File.WriteAllText(minFile, result.MinifiedContent, new UTF8Encoding(false));
                            OnAfterWritingMinFile(file, minFile, bundle, containsChanges);
                        }

                        GzipFile(minFile, bundle, containsChanges);
                    }
                    else
                    {
                        AddAjaxminErrors(uglifyResult, result);
                    }
                }
                else
                {
                    using (StringWriter writer = new StringWriter())
                    {
                        using (V3SourceMap sourceMap = new V3SourceMap(writer))
                        {
                            settings.SymbolsMap = sourceMap;
                            sourceMap.StartPackage(minFile, mapFile);
                            sourceMap.SourceRoot = bundle.SourceMapRootPath;

                            if (file.EndsWith(".min.js"))
                            {
                                var inputs = bundle.GetAbsoluteInputFiles();

                                if (inputs.Count == 1)
                                {
                                    file = inputs[0];
                                }
                            }

                            UgliflyResult uglifyResult;
                            try
                            {
                                uglifyResult = Uglify.Js(ReadAllText(file), file, settings);
                            }
                            catch
                            {
                                uglifyResult = new UgliflyResult(null,
                                                                 new List <UglifyError> {
                                    new UglifyError
                                    {
                                        IsError = true,
                                        File    = file,
                                        Message = "Error processing file"
                                    }
                                });
                            }

                            result.MinifiedContent = uglifyResult.Code?.Trim();

                            if (!uglifyResult.HasErrors && !string.IsNullOrEmpty(result.MinifiedContent))
                            {
                                bool containsChanges = FileHelpers.HasFileContentChanged(minFile, result.MinifiedContent);
                                result.Changed |= containsChanges;
                                OnBeforeWritingMinFile(file, minFile, bundle, containsChanges);

                                if (containsChanges)
                                {
                                    File.WriteAllText(minFile, result.MinifiedContent, new UTF8Encoding(false));
                                    OnAfterWritingMinFile(file, minFile, bundle, containsChanges);
                                }


                                GzipFile(minFile, bundle, containsChanges);
                            }
                            else
                            {
                                AddAjaxminErrors(uglifyResult, result);
                            }
                        }

                        result.SourceMap = writer.ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                result.Errors.Add(new MinificationError
                {
                    FileName     = file,
                    Message      = ex.Message,
                    LineNumber   = 0,
                    ColumnNumber = 0
                });
            }

            return(result);
        }
Esempio n. 26
0
        private static MinificationResult MinifyHtml(Bundle bundle)
        {
            string file     = bundle.GetAbsoluteOutputFile();
            string content  = ReadAllText(file);
            var    settings = HtmlOptions.GetSettings(bundle);
            string minFile  = GetMinFileName(file);

            var minResult = new MinificationResult(file, null, null);

            try
            {
                UgliflyResult uglifyResult;

                try
                {
                    uglifyResult = Uglify.Html(content, settings, file);
                }
                catch
                {
                    uglifyResult = new UgliflyResult(null,
                                                     new List <UglifyError> {
                        new UglifyError
                        {
                            IsError = true,
                            File    = file,
                            Message = "Error processing file"
                        }
                    });
                }

                minResult.MinifiedContent = uglifyResult.Code?.Trim();

                if (!uglifyResult.HasErrors && !string.IsNullOrEmpty(minResult.MinifiedContent))
                {
                    bool containsChanges = FileHelpers.HasFileContentChanged(minFile, minResult.MinifiedContent);
                    minResult.Changed |= containsChanges;
                    OnBeforeWritingMinFile(file, minFile, bundle, containsChanges);

                    if (containsChanges)
                    {
                        File.WriteAllText(minFile, minResult.MinifiedContent, new UTF8Encoding(false));
                        OnAfterWritingMinFile(file, minFile, bundle, containsChanges);
                    }

                    GzipFile(minFile, bundle, containsChanges);
                }
                else
                {
                    foreach (var error in uglifyResult.Errors)
                    {
                        minResult.Errors.Add(new MinificationError
                        {
                            FileName     = file,
                            Message      = error.Message,
                            LineNumber   = error.StartLine,
                            ColumnNumber = error.StartColumn
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                minResult.Errors.Add(new MinificationError
                {
                    FileName     = file,
                    Message      = ex.Message,
                    LineNumber   = 0,
                    ColumnNumber = 0
                });
            }

            return(minResult);
        }
Esempio n. 27
0
        private static MinificationResult MinifyJavaScript(Bundle bundle)
        {
            string file     = bundle.GetAbsoluteOutputFile();
            var    settings = JavaScriptOptions.GetSettings(bundle);
            var    minifier = new Minifier();
            var    result   = new MinificationResult(file, null, null);

            string minFile = GetMinFileName(file);
            string mapFile = minFile + ".map";

            try
            {
                if (!bundle.SourceMap)
                {
                    result.MinifiedContent = minifier.MinifyJavaScript(ReadAllText(file), settings).Trim();

                    if (!minifier.Errors.Any())
                    {
                        bool containsChanges = FileHelpers.HasFileContentChanged(minFile, result.MinifiedContent);

                        OnBeforeWritingMinFile(file, minFile, bundle, containsChanges);

                        if (containsChanges)
                        {
                            File.WriteAllText(minFile, result.MinifiedContent, new UTF8Encoding(false));
                        }

                        GzipFile(minFile, bundle, containsChanges);

                        OnAfterWritingMinFile(file, minFile, bundle, containsChanges);
                    }
                    else
                    {
                        AddAjaxminErrors(minifier, result);
                    }
                }
                else
                {
                    using (StringWriter writer = new StringWriter())
                    {
                        using (V3SourceMap sourceMap = new V3SourceMap(writer))
                        {
                            settings.SymbolsMap = sourceMap;
                            sourceMap.StartPackage(minFile, mapFile);

                            minifier.FileName      = file;
                            result.MinifiedContent = minifier.MinifyJavaScript(ReadAllText(file), settings).Trim();

                            if (!minifier.Errors.Any())
                            {
                                bool containsChanges = FileHelpers.HasFileContentChanged(minFile, result.MinifiedContent);

                                OnBeforeWritingMinFile(file, minFile, bundle, containsChanges);

                                if (containsChanges)
                                {
                                    File.WriteAllText(minFile, result.MinifiedContent, new UTF8Encoding(false));
                                }

                                OnAfterWritingMinFile(file, minFile, bundle, containsChanges);

                                GzipFile(minFile, bundle, containsChanges);
                            }
                            else
                            {
                                AddAjaxminErrors(minifier, result);
                            }
                        }

                        result.SourceMap = writer.ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                result.Errors.Add(new MinificationError
                {
                    FileName     = file,
                    Message      = ex.Message,
                    LineNumber   = 0,
                    ColumnNumber = 0
                });
            }

            return(result);
        }
Esempio n. 28
0
        private static MinificationResult MinifyHtml(Bundle bundle)
        {
            string file = bundle.GetAbsoluteOutputFile();
            string content = File.ReadAllText(file);
            var settings = HtmlOptions.GetSettings(bundle);
            string minFile = GetMinFileName(file);

            var minifier = new HtmlMinifier(settings);
            var minResult = new MinificationResult(file, null, null);

            try
            {
                MarkupMinificationResult result = minifier.Minify(content, generateStatistics: true);
                minResult.MinifiedContent = result.MinifiedContent;

                if (!result.Errors.Any())
                {
                    OnBeforeWritingMinFile(file, minFile, bundle);
                    File.WriteAllText(minFile, result.MinifiedContent, new UTF8Encoding(true));
                    OnAfterWritingMinFile(file, minFile, bundle);

                    GzipFile(minFile, bundle);
                }
                else
                {
                    foreach (var error in result.Errors)
                    {
                        minResult.Errors.Add(new MinificationError
                        {
                            FileName = file,
                            Message = error.Message,
                            LineNumber = error.LineNumber,
                            ColumnNumber = error.ColumnNumber
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                minResult.Errors.Add(new MinificationError
                {
                    FileName = file,
                    Message = ex.Message,
                    LineNumber = 0,
                    ColumnNumber = 0
                });
            }

            return minResult;
        }
 protected void OnAfterBundling(Bundle bundle, string baseFolder, bool containsChanges)
 {
     if (AfterBundling != null)
     {
         AfterBundling(this, new BundleFileEventArgs(bundle.GetAbsoluteOutputFile(), bundle, baseFolder, containsChanges));
     }
 }