Ejemplo n.º 1
0
        private void MinifyJsDirectory(string path)
        {
            foreach (var file in Directory.GetFiles(path, "*.js", SearchOption.AllDirectories))
            {
                try
                {
                    var text = File.ReadAllText(file, Encoding.UTF8);

                    var result = new CrockfordJsMinifier().Minify(text, false, Encoding.UTF8);

                    if (result.Errors.Count > 0)
                    {
                        Logger.Error("Error minifying javascript: " + result.Errors[0].Message);
                    }
                    else
                    {
                        text = result.MinifiedContent;
                        File.WriteAllText(file, text, Encoding.UTF8);
                    }
                }
                catch (Exception ex)
                {
                    Logger.ErrorException("Error minifying css", ex);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Safely Attempts to minify the provided string.
        /// </summary>
        /// <param name="recording">The string to minify</param>
        /// <returns>Minified version of the string, or, if errors were encountered, returns the original string.</returns>
        private string Minify(string recording)
        {
            var settings    = new HtmlMinificationSettings();
            var cssMinifier = new KristensenCssMinifier();
            var jsMinifier  = new CrockfordJsMinifier();

            var minifier = new HtmlMinifier(settings, cssMinifier, jsMinifier);

            MarkupMinificationResult result = minifier.Minify(recording);

            if (result.Errors.Count != 0)
            {
                var builder = new StringBuilder("Attempt to minify rendering failed");

                foreach (var error in result.Errors)
                {
                    builder.AppendLine(error.Category + " - " + error.Message);
                }

                Log.Warn(builder.ToString(), this);
                return(recording);
            }

            return(result.MinifiedContent);
        }
Ejemplo n.º 3
0
        private void MinifyJsDirectory(string path)
        {
            foreach (var file in Directory.GetFiles(path, "*.js", SearchOption.AllDirectories))
            {
                if (file.IndexOf(".min.", StringComparison.OrdinalIgnoreCase) != -1)
                {
                    continue;
                }
                if (file.IndexOf("bower_", StringComparison.OrdinalIgnoreCase) != -1)
                {
                    continue;
                }

                try
                {
                    var text = _fileSystem.ReadAllText(file, Encoding.UTF8);

                    var result = new CrockfordJsMinifier().Minify(text, false, Encoding.UTF8);

                    if (result.Errors.Count > 0)
                    {
                        Logger.Error("Error minifying javascript: " + result.Errors[0].Message);
                    }
                    else
                    {
                        text = result.MinifiedContent;
                        _fileSystem.WriteAllText(file, text, Encoding.UTF8);
                    }
                }
                catch (Exception ex)
                {
                    Logger.ErrorException("Error minifying css", ex);
                }
            }
        }
        public IJsMinifier CreateMinifier(string minifierName)
        {
            IJsMinifier minifier;

            switch (minifierName)
            {
            case "CrockfordJsMinifier":
                minifier = new CrockfordJsMinifier();
                break;

#if !NETSTANDARD1_6
            case "MsAjaxJsMinifier":
                minifier = new MsAjaxJsMinifier();
                break;

            case "YuiJsMinifier":
                minifier = new YuiJsMinifier();
                break;
#endif
            case "NUglifyJsMinifier":
                minifier = new NUglifyJsMinifier();
                break;

            default:
                throw new NotSupportedException();
            }

            return(minifier);
        }
Ejemplo n.º 5
0
        public IJsMinifier CreateMinifier(string minifierName)
        {
#if DNXCORE50
            IJsMinifier minifier = new CrockfordJsMinifier();
#else
            IJsMinifier minifier;

            switch (minifierName)
            {
            case "CrockfordJsMinifier":
                minifier = new CrockfordJsMinifier();
                break;

            case "MsAjaxJsMinifier":
                minifier = new MsAjaxJsMinifier();
                break;

            case "YuiJsMinifier":
                minifier = new YuiJsMinifier();
                break;

            default:
                throw new NotSupportedException();
            }
#endif

            return(minifier);
        }
		/// <summary>
		/// Safely Attempts to minify the provided string.
		/// </summary>
		/// <param name="recording">The string to minify</param>
		/// <returns>Minified version of the string, or, if errors were encountered, returns the original string.</returns>
		private string Minify(string recording)
		{
			var settings = new HtmlMinificationSettings();
			var cssMinifier = new KristensenCssMinifier();
			var jsMinifier = new CrockfordJsMinifier();

			var minifier = new HtmlMinifier(settings, cssMinifier, jsMinifier);

			MarkupMinificationResult result = minifier.Minify(recording);

			if (result.Errors.Count != 0)
			{
				var builder = new StringBuilder("Attempt to minify rendering failed");

				foreach (var error in result.Errors)
				{
					builder.AppendLine(error.Category + " - " + error.Message);
				}

				Log.Warn(builder.ToString(), this);
				return recording;
			}

			return result.MinifiedContent;
		}
		protected string Minify(string content)
		{
			var settings = new HtmlMinificationSettings();
			var cssMinifier = new KristensenCssMinifier();
			var jsMinifier = new CrockfordJsMinifier();
			var minifier = new HtmlMinifier(settings, cssMinifier, jsMinifier);

			MarkupMinificationResult result = minifier.Minify(content);

			if (result.Errors.Count != 0)
			{
				Log.Warn("Attempt to minify content failed", this);
				return content;
			}

			return result.MinifiedContent;
		}
Ejemplo n.º 8
0
        /// <summary>
        /// HTML minification.
        /// </summary>
        /// <param name="html">Code to minify.</param>
        /// <returns>Minified html.</returns>
        public static string MinifyHtml(string html)
        {
            var settings     = new HtmlMinificationSettings();
            var cssMinifier  = new KristensenCssMinifier();
            var jsMinifier   = new CrockfordJsMinifier();
            var htmlMinifier = new HtmlMinifier(settings, cssMinifier, jsMinifier);

            MarkupMinificationResult result = htmlMinifier.Minify(
                html,
                generateStatistics: false);

            if (result.Errors.Count == 0)
            {
                return(result.MinifiedContent);
            }
            else
            {
                return(html);
            }
        }
        public async Task <Stream> ModifyJs(Stream sourceStream, bool enableMinification)
        {
            using (sourceStream)
            {
                string content;

                using (var memoryStream = new MemoryStream())
                {
                    await sourceStream.CopyToAsync(memoryStream).ConfigureAwait(false);

                    content = Encoding.UTF8.GetString(memoryStream.ToArray());

                    if (enableMinification)
                    {
                        try
                        {
                            var result = new CrockfordJsMinifier().Minify(content, false, Encoding.UTF8);

                            if (result.Errors.Count > 0)
                            {
                                _logger.Error("Error minifying javascript: " + result.Errors[0].Message);
                            }
                            else
                            {
                                content = result.MinifiedContent;
                            }
                        }
                        catch (Exception ex)
                        {
                            _logger.ErrorException("Error minifying javascript", ex);
                        }
                    }
                }

                var bytes = Encoding.UTF8.GetBytes(content);

                return(new MemoryStream(bytes));
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Gets a stream containing all concatenated javascript
        /// </summary>
        /// <returns>Task{Stream}.</returns>
        private async Task <Stream> GetAllJavascript(string mode, string culture, string version, bool enableMinification)
        {
            var memoryStream = new MemoryStream();
            var newLineBytes = Encoding.UTF8.GetBytes(Environment.NewLine);

            await AppendResource(memoryStream, "bower_components/jquery/dist/jquery.min.js", newLineBytes).ConfigureAwait(false);

            //await AppendLocalization(memoryStream, culture, excludePhrases).ConfigureAwait(false);
            await memoryStream.WriteAsync(newLineBytes, 0, newLineBytes.Length).ConfigureAwait(false);

            if (!string.IsNullOrWhiteSpace(mode))
            {
                var appModeBytes = Encoding.UTF8.GetBytes(string.Format("window.appMode='{0}';", mode));
                await memoryStream.WriteAsync(appModeBytes, 0, appModeBytes.Length).ConfigureAwait(false);
            }

            // Write the version string for the dashboard comparison function
            var versionString = string.Format("window.dashboardVersion='{0}';", version);
            var versionBytes  = Encoding.UTF8.GetBytes(versionString);

            await memoryStream.WriteAsync(versionBytes, 0, versionBytes.Length).ConfigureAwait(false);

            await memoryStream.WriteAsync(newLineBytes, 0, newLineBytes.Length).ConfigureAwait(false);

            var builder = new StringBuilder();

            var commonFiles = new[]
            {
                "bower_components/requirejs/require.js",
                "thirdparty/jquerymobile-1.4.5/jquery.mobile.custom.js",
                "thirdparty/browser.js",
                "thirdparty/jquery.unveil-custom.js",
                "apiclient/logger.js",
                "apiclient/md5.js",
                "apiclient/store.js",
                "apiclient/device.js",
                "apiclient/credentials.js",
                "apiclient/ajax.js",
                "apiclient/events.js",
                "apiclient/deferred.js",
                "apiclient/apiclient.js"
            }.ToList();

            commonFiles.Add("apiclient/connectionmanager.js");

            foreach (var file in commonFiles)
            {
                using (var fs = _fileSystem.GetFileStream(GetDashboardResourcePath(file), FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
                {
                    using (var streamReader = new StreamReader(fs))
                    {
                        var text = await streamReader.ReadToEndAsync().ConfigureAwait(false);

                        builder.Append(text);
                        builder.Append(Environment.NewLine);
                    }
                }
            }

            foreach (var file in GetScriptFiles())
            {
                var path = GetDashboardResourcePath("scripts/" + file);

                using (var fs = _fileSystem.GetFileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
                {
                    using (var streamReader = new StreamReader(fs))
                    {
                        var text = await streamReader.ReadToEndAsync().ConfigureAwait(false);

                        builder.Append(text);
                        builder.Append(Environment.NewLine);
                    }
                }
            }

            var js = builder.ToString();

            if (enableMinification)
            {
                try
                {
                    var result = new CrockfordJsMinifier().Minify(js, false, Encoding.UTF8);

                    if (result.Errors.Count > 0)
                    {
                        _logger.Error("Error minifying javascript: " + result.Errors[0].Message);
                    }
                    else
                    {
                        js = result.MinifiedContent;
                    }
                }
                catch (Exception ex)
                {
                    _logger.ErrorException("Error minifying javascript", ex);
                }
            }

            var bytes = Encoding.UTF8.GetBytes(js);
            await memoryStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);

            memoryStream.Position = 0;
            return(memoryStream);
        }
        /// <summary>
        /// Gets a stream containing all concatenated javascript
        /// </summary>
        /// <returns>Task{Stream}.</returns>
        private async Task <Stream> GetAllJavascript()
        {
            var memoryStream = new MemoryStream();
            var newLineBytes = Encoding.UTF8.GetBytes(Environment.NewLine);

            // jQuery + jQuery mobile
            await AppendResource(memoryStream, "thirdparty/jquery-2.1.1.min.js", newLineBytes).ConfigureAwait(false);
            await AppendResource(memoryStream, "thirdparty/jquerymobile-1.4.3/jquery.mobile-1.4.3.min.js", newLineBytes).ConfigureAwait(false);

            await AppendResource(memoryStream, "thirdparty/jquery.unveil-custom.js", newLineBytes).ConfigureAwait(false);

            // This script produces errors in older versions of safari
            if ((Request.UserAgent ?? string.Empty).IndexOf("chrome/", StringComparison.OrdinalIgnoreCase) != -1)
            {
                await AppendResource(memoryStream, "thirdparty/cast_sender.js", newLineBytes).ConfigureAwait(false);
            }

            await AppendLocalization(memoryStream).ConfigureAwait(false);

            await memoryStream.WriteAsync(newLineBytes, 0, newLineBytes.Length).ConfigureAwait(false);

            // Write the version string for the dashboard comparison function
            var versionString = string.Format("window.dashboardVersion='{0}';", _appHost.ApplicationVersion);
            var versionBytes  = Encoding.UTF8.GetBytes(versionString);

            await memoryStream.WriteAsync(versionBytes, 0, versionBytes.Length).ConfigureAwait(false);

            await memoryStream.WriteAsync(newLineBytes, 0, newLineBytes.Length).ConfigureAwait(false);

            var builder = new StringBuilder();

            using (var fs = _fileSystem.GetFileStream(GetDashboardResourcePath("thirdparty/mediabrowser.apiclient.js"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
            {
                using (var streamReader = new StreamReader(fs))
                {
                    var text = await streamReader.ReadToEndAsync().ConfigureAwait(false);

                    builder.Append(text);
                    builder.Append(Environment.NewLine);
                }
            }

            foreach (var file in GetScriptFiles())
            {
                var path = GetDashboardResourcePath("scripts/" + file);

                using (var fs = _fileSystem.GetFileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
                {
                    using (var streamReader = new StreamReader(fs))
                    {
                        var text = await streamReader.ReadToEndAsync().ConfigureAwait(false);

                        builder.Append(text);
                        builder.Append(Environment.NewLine);
                    }
                }
            }

            var js = builder.ToString();

            try
            {
                var result = new CrockfordJsMinifier().Minify(js, false, Encoding.UTF8);

                js = result.MinifiedContent;
            }
            catch (Exception ex)
            {
                Logger.ErrorException("Error minifying javascript", ex);
            }

            var bytes = Encoding.UTF8.GetBytes(js);
            await memoryStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);

            memoryStream.Position = 0;
            return(memoryStream);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Gets a stream containing all concatenated javascript
        /// </summary>
        /// <returns>Task{Stream}.</returns>
        private async Task <Stream> GetAllJavascript(string culture, string version)
        {
            var memoryStream = new MemoryStream();
            var newLineBytes = Encoding.UTF8.GetBytes(Environment.NewLine);

            // jQuery + jQuery mobile
            await AppendResource(memoryStream, "thirdparty/jquery-2.1.1.min.js", newLineBytes).ConfigureAwait(false);
            await AppendResource(memoryStream, "thirdparty/jquerymobile-1.4.5/jquery.mobile-1.4.5.min.js", newLineBytes).ConfigureAwait(false);

            await AppendResource(memoryStream, "thirdparty/jquery.unveil-custom.js", newLineBytes).ConfigureAwait(false);

            await AppendResource(memoryStream, "thirdparty/cast_sender.js", newLineBytes).ConfigureAwait(false);
            await AppendResource(memoryStream, "thirdparty/browser.js", newLineBytes).ConfigureAwait(false);

            await AppendLocalization(memoryStream, culture).ConfigureAwait(false);

            await memoryStream.WriteAsync(newLineBytes, 0, newLineBytes.Length).ConfigureAwait(false);

            // Write the version string for the dashboard comparison function
            var versionString = string.Format("window.dashboardVersion='{0}';", version);
            var versionBytes  = Encoding.UTF8.GetBytes(versionString);

            await memoryStream.WriteAsync(versionBytes, 0, versionBytes.Length).ConfigureAwait(false);

            await memoryStream.WriteAsync(newLineBytes, 0, newLineBytes.Length).ConfigureAwait(false);

            var builder = new StringBuilder();

            foreach (var file in new[]
            {
                "thirdparty/apiclient/md5.js",
                "thirdparty/apiclient/sha1.js",
                "thirdparty/apiclient/store.js",
                "thirdparty/apiclient/network.js",
                "thirdparty/apiclient/device.js",
                "thirdparty/apiclient/credentials.js",
                "thirdparty/apiclient/mediabrowser.apiclient.js",
                "thirdparty/apiclient/connectservice.js",
                "thirdparty/apiclient/connectionmanager.js"
            })
            {
                using (var fs = _fileSystem.GetFileStream(GetDashboardResourcePath(file), FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
                {
                    using (var streamReader = new StreamReader(fs))
                    {
                        var text = await streamReader.ReadToEndAsync().ConfigureAwait(false);

                        builder.Append(text);
                        builder.Append(Environment.NewLine);
                    }
                }
            }

            foreach (var file in GetScriptFiles())
            {
                var path = GetDashboardResourcePath("scripts/" + file);

                using (var fs = _fileSystem.GetFileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
                {
                    using (var streamReader = new StreamReader(fs))
                    {
                        var text = await streamReader.ReadToEndAsync().ConfigureAwait(false);

                        builder.Append(text);
                        builder.Append(Environment.NewLine);
                    }
                }
            }

            var js = builder.ToString();

            try
            {
                var result = new CrockfordJsMinifier().Minify(js, false, Encoding.UTF8);

                js = result.MinifiedContent;
            }
            catch (Exception ex)
            {
                _logger.ErrorException("Error minifying javascript", ex);
            }

            var bytes = Encoding.UTF8.GetBytes(js);
            await memoryStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);

            memoryStream.Position = 0;
            return(memoryStream);
        }
Ejemplo n.º 13
0
        /// <inheritdoc />
        public IEnumerable <IDocument> Execute(IReadOnlyList <IDocument> inputs, IExecutionContext context)
        {
            CrockfordJsMinifier minifier = new CrockfordJsMinifier();

            return(Minify(inputs, context, (x) => minifier.Minify(x, _isInlineCode), "JS"));
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Gets a stream containing all concatenated javascript
        /// </summary>
        /// <returns>Task{Stream}.</returns>
        private async Task<Stream> GetAllJavascript(string culture, string version)
        {
            var memoryStream = new MemoryStream();
            var newLineBytes = Encoding.UTF8.GetBytes(Environment.NewLine);

            // jQuery + jQuery mobile
            await AppendResource(memoryStream, "thirdparty/jquery-2.1.1.min.js", newLineBytes).ConfigureAwait(false);
            await AppendResource(memoryStream, "thirdparty/jquerymobile-1.4.5/jquery.mobile-1.4.5.min.js", newLineBytes).ConfigureAwait(false);

            await AppendResource(memoryStream, "thirdparty/jquery.unveil-custom.js", newLineBytes).ConfigureAwait(false);

            await AppendResource(memoryStream, "thirdparty/cast_sender.js", newLineBytes).ConfigureAwait(false);
            await AppendResource(memoryStream, "thirdparty/browser.js", newLineBytes).ConfigureAwait(false);

            await AppendLocalization(memoryStream, culture).ConfigureAwait(false);
            await memoryStream.WriteAsync(newLineBytes, 0, newLineBytes.Length).ConfigureAwait(false);

            // Write the version string for the dashboard comparison function
            var versionString = string.Format("window.dashboardVersion='{0}';", version);
            var versionBytes = Encoding.UTF8.GetBytes(versionString);

            await memoryStream.WriteAsync(versionBytes, 0, versionBytes.Length).ConfigureAwait(false);
            await memoryStream.WriteAsync(newLineBytes, 0, newLineBytes.Length).ConfigureAwait(false);

            var builder = new StringBuilder();

            foreach (var file in new[]
            {
                "thirdparty/apiclient/md5.js",
                "thirdparty/apiclient/sha1.js",
                "thirdparty/apiclient/store.js",
                "thirdparty/apiclient/network.js",
                "thirdparty/apiclient/device.js",
                "thirdparty/apiclient/credentials.js",
                "thirdparty/apiclient/mediabrowser.apiclient.js",
                "thirdparty/apiclient/connectservice.js",
                "thirdparty/apiclient/connectionmanager.js"
            })
            {
                using (var fs = _fileSystem.GetFileStream(GetDashboardResourcePath(file), FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
                {
                    using (var streamReader = new StreamReader(fs))
                    {
                        var text = await streamReader.ReadToEndAsync().ConfigureAwait(false);
                        builder.Append(text);
                        builder.Append(Environment.NewLine);
                    }
                }
            }

            foreach (var file in GetScriptFiles())
            {
                var path = GetDashboardResourcePath("scripts/" + file);

                using (var fs = _fileSystem.GetFileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
                {
                    using (var streamReader = new StreamReader(fs))
                    {
                        var text = await streamReader.ReadToEndAsync().ConfigureAwait(false);
                        builder.Append(text);
                        builder.Append(Environment.NewLine);
                    }
                }
            }

            var js = builder.ToString();

            try
            {
                var result = new CrockfordJsMinifier().Minify(js, false, Encoding.UTF8);

                js = result.MinifiedContent;
            }
            catch (Exception ex)
            {
                _logger.ErrorException("Error minifying javascript", ex);
            }

            var bytes = Encoding.UTF8.GetBytes(js);
            await memoryStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);

            memoryStream.Position = 0;
            return memoryStream;
        }
Ejemplo n.º 15
0
        /// <inheritdoc />
        public async Task <IEnumerable <IDocument> > ExecuteAsync(IExecutionContext context)
        {
            CrockfordJsMinifier minifier = new CrockfordJsMinifier();

            return(await MinifyAsync(context, (x) => minifier.Minify(x, _isInlineCode), "JS"));
        }