Exemple #1
0
        //private void MinifySelection()
        //{
        //    var view = ProjectHelpers.GetCurentTextView();

        //    if (view != null)
        //    {
        //        _dte.UndoContext.Open("Minify");

        //        string content = view.Selection.SelectedSpans[0].GetText();
        //        string extension = Path.GetExtension(_dte.ActiveDocument.FullName).ToLowerInvariant();
        //        string result = MinifyString(extension, content);

        //        view.TextBuffer.Replace(view.Selection.SelectedSpans[0].Span, result);

        //        _dte.UndoContext.Close();
        //    }
        //}

        private void MinifyFile(string extension)
        {
            var selectedPaths = GetSelectedFilePaths(_dte);

            foreach (string path in selectedPaths.Where(p => p.EndsWith(extension, StringComparison.OrdinalIgnoreCase)))
            {
                string minPath = GetMinFileName(path, extension);

                if (!path.EndsWith(".min" + extension) && !File.Exists(minPath) && _dte.Solution.FindProjectItem(path) != null)
                {
                    if (extension.Equals(".js", StringComparison.OrdinalIgnoreCase))
                    {
                        JavaScriptSaveListener.Minify(path, minPath, false);
                    }
                    else
                    {
                        CssSaveListener.Minify(path, minPath);
                    }

                    MarginBase.AddFileToProject(path, minPath);
                }
            }

            EnableSync(extension);
        }
        private static void WriteMinFile(string filePath, string bundlePath, string content, string extension)
        {
            string minPath = bundlePath.Replace(Path.GetExtension(bundlePath), ".min" + Path.GetExtension(bundlePath));

            if (extension.Equals(".js", StringComparison.OrdinalIgnoreCase))
            {
                JavaScriptSaveListener.Minify(bundlePath, minPath, true);
                MarginBase.AddFileToProject(filePath, minPath);

                if (WESettings.GetBoolean(WESettings.Keys.GenerateJavaScriptSourceMaps))
                {
                    MarginBase.AddFileToProject(filePath, minPath + ".map");
                }

                MarginBase.AddFileToProject(filePath, minPath + ".gzip");
            }
            else if (extension.Equals(".css", StringComparison.OrdinalIgnoreCase))
            {
                string minContent = MinifyFileMenu.MinifyString(extension, content);

                ProjectHelpers.CheckOutFileFromSourceControl(minPath);

                using (StreamWriter writer = new StreamWriter(minPath, false, new UTF8Encoding(true)))
                {
                    writer.Write(minContent);
                }

                MarginBase.AddFileToProject(filePath, minPath);

                if (WESettings.GetBoolean(WESettings.Keys.CssEnableGzipping))
                {
                    CssSaveListener.GzipFile(filePath, minPath, minContent);
                }
            }
        }
Exemple #3
0
        private static void MinifyFile(string file, string minFile, CodeSettings settings, bool isBundle)
        {
            Minifier minifier = new Minifier();

            if (!isBundle)
            {
                minifier.FileName = Path.GetFileName(file);
            }

            string content = minifier.MinifyJavaScript(File.ReadAllText(file), settings);

            if (WESettings.GetBoolean(WESettings.Keys.GenerateJavaScriptSourceMaps))
            {
                content += Environment.NewLine + "//@ sourceMappingURL=" + Path.GetFileName(minFile) + ".map";
            }

            ProjectHelpers.CheckOutFileFromSourceControl(minFile);
            using (StreamWriter writer = new StreamWriter(minFile, false, new UTF8Encoding(true)))
            {
                writer.Write(content);
            }

            if (WESettings.GetBoolean(WESettings.Keys.JavaScriptEnableGzipping))
            {
                CssSaveListener.GzipFile(file, minFile, content);
            }
        }
Exemple #4
0
        //private void BuildTypeScript()
        //{
        //    foreach (Project project in _dte.Solution.Projects)
        //    {
        //        new TypeScriptMargin().CompileProjectFiles(project);
        //    }
        //}

        private void Minify()
        {
            _dte.StatusBar.Text = "Web Essentials: Minifying files...";
            var files = GetFiles();

            foreach (string path in files)
            {
                string extension = Path.GetExtension(path);
                string minPath   = MinifyFileMenu.GetMinFileName(path, extension);

                if (!path.EndsWith(".min" + extension, StringComparison.Ordinal) && File.Exists(minPath) && _dte.Solution.FindProjectItem(path) != null)
                {
                    if (extension.Equals(".js", StringComparison.OrdinalIgnoreCase))
                    {
                        JavaScriptSaveListener.Minify(path, minPath, false);
                    }
                    else
                    {
                        CssSaveListener.Minify(path, minPath);
                    }
                }
            }

            _dte.StatusBar.Text = "Web Essentials: Files minified";
        }
Exemple #5
0
 private static void GzipFile(string file, string minFile, string content)
 {
     if (WESettings.GetBoolean(WESettings.Keys.CssEnableGzipping))
     {
         string gzipFile = minFile + ".gzip";
         ProjectHelpers.CheckOutFileFromSourceControl(gzipFile);
         byte[] gzipContent = CssSaveListener.Compress(content);
         File.WriteAllBytes(gzipFile, gzipContent);
         ProjectHelpers.AddFileToActiveProject(gzipFile);
         MarginBase.AddFileToProject(file, gzipFile);
     }
 }