Example #1
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);
                }
            }
        }
        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);
        }
        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);
        }