Esempio n. 1
0
        public async Task MinifyFileAsync(MinifiedFile minifiedFile)
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            var root = Path.GetDirectoryName(Solution.GetFullName());

            var destinationFile = minifiedFile.GetFullOutputPath(root);

            if (!File.Exists(destinationFile))
            {
                InfoBar.NewMessage()
                .WithErrorImage()
                .WithText("The path ")
                .WithText(minifiedFile.OutputFile, underline: true)
                .WithText(" is not valid.")
                .Publish();

                return;
            }

            MinifyResult result = null;

            switch (minifiedFile.MinifyType)
            {
            case MinifyType.CSHtml:
                result = await CSHtmlMinifier.MinifyFileAsync(minifiedFile.GetFullSourcePath(root), destinationFile, ((CsHtmlMinifiedFile)minifiedFile).UsePreMailer);

                break;

            case MinifyType.Js:
                if (!await JsMinifier.TryMinifyFileAsync("esbuild.exe", minifiedFile.GetFullSourcePath(root), destinationFile, ((JsMinifiedFile)minifiedFile).Options))
                {
                    InfoBar.NewMessage()
                    .WithErrorImage()
                    .WithText("The file ")
                    .WithText(minifiedFile.SourceFile, underline: true)
                    .WithText(" contains invalid js.")
                    .Publish();
                }
                break;

            default:
                throw new InvalidOperationException("Please create an issue on GitHub, if this throws.");
            }

            if (result is object && !result.Success)
            {
                InfoBar.NewMessage()
                .WithErrorImage()
                .WithText("The file ")
                .WithText(minifiedFile.SourceFile, underline: true)
                .WithText(" produced warnings. ")
                .WithText(result.Message)
                .Publish();
            }
        }
Esempio n. 2
0
        protected internal void SetScriptCache(
            string path, string script)
        {
            if (!_cachedScripts.Contains(path))
            {
                lock (_cachedScriptLock)
                {
                    if (!_cachedScripts.Contains(path))
                    {
                        _cachedScripts.Add(path);
                    }
                    else
                    {
                        return;
                    }
                }

                Logger.AddEntry("Minification of ({0}) STARTED", LogEventType.Information, path);
                script.MinifyAsync().ContinueWith(t =>
                {
                    MinifyResult compression = t.Result;
                    if (compression.Success)
                    {
                        Logger.AddEntry("Minification of ({0}) COMPLETED", LogEventType.Information, path);
                        byte[] minBytes = Encoding.UTF8.GetBytes(compression.MinScript);
                        SetMinCacheBytes(path, minBytes);

                        string fileName = Path.GetFileName(path);
                        string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path);
                        string pathWithoutFileName      = path.TruncateFront(fileName.Length);
                        string minPath = Path.Combine(pathWithoutFileName, "{0}.min.js"._Format(fileNameWithoutExtension));
                        SetMinCacheBytes(minPath, minBytes);

                        Logger.AddEntry("GZipping the minified bytes of ({0}) STARTED", LogEventType.Information, path);
                        minBytes.GZipAsync().ContinueWith(g =>
                        {
                            Logger.AddEntry("GZipping the minified bytes of ({0}) COMPLETED", LogEventType.Information, path);
                            byte[] zippedMinBytes = g.Result;
                            SetZippedMinCacheBytes(minPath, zippedMinBytes);
                        });
                    }
                    else
                    {
                        string message = compression.Exception != null ? compression.Exception.Message : string.Empty;
                        string stack   = string.Empty;
                        if (!string.IsNullOrEmpty(compression.Exception.StackTrace))
                        {
                            stack = compression.Exception.StackTrace;
                        }
                        Logger.AddEntry("Compression of ({0}) failed: {1}\r\n{2}", LogEventType.Warning, path, message, stack);
                    }
                });

                Task.Run(() =>
                {
                    byte[] scriptBytes = Encoding.UTF8.GetBytes(script);
                    SetCacheBytes(path, scriptBytes);
                    Logger.AddEntry("GZipping the bytes of ({0}) STARTED", LogEventType.Information, path);
                    scriptBytes.GZipAsync().ContinueWith(g =>
                    {
                        Logger.AddEntry("GZipping the minified bytes of ({0}) COMPLETED", LogEventType.Information, path);
                        SetZippedCacheBytes(path, g.Result);
                    });
                });
            }
        }