示例#1
0
        private static void MinifyJavaScript(Bundle bundle, MinificationResult minResult)
        {
            var settings = JavaScriptOptions.GetSettings(bundle);

            if (!bundle.SourceMap)
            {
                var uglifyResult = Uglify.Js(bundle.Output, settings);
                WriteMinFile(bundle, minResult, uglifyResult);
            }
            else
            {
                string minFile = GetMinFileName(minResult.FileName);
                string mapFile = minFile + ".map";

                using (StringWriter writer = new StringWriter())
                {
                    using (V3SourceMap sourceMap = new V3SourceMap(writer))
                    {
                        settings.SymbolsMap = sourceMap;
                        sourceMap.StartPackage(minFile, mapFile);
                        sourceMap.SourceRoot = bundle.SourceMapRootPath;

                        string file = minResult.FileName;

                        if (bundle.OutputIsMinFile)
                        {
                            var inputs = bundle.GetAbsoluteInputFiles();

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

                        var uglifyResult = Uglify.Js(bundle.Output, file, settings);
                        WriteMinFile(bundle, minResult, uglifyResult);
                    }

                    minResult.SourceMap = writer.ToString();
                }
            }
        }
示例#2
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);
        }
示例#3
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);
        }