Example #1
0
        /// <summary>
        /// The crunch css.
        /// </summary>
        /// <param name="configuration">
        /// The configuration.
        /// </param>
        private static void Crunch(ConsoleConfiguration configuration)
        {
            // Get the currently operating directory.
            // http://stackoverflow.com/questions/52797/how-do-i-get-the-path-of-the-assembly-the-code-is-in
            string directoryName = Path.GetDirectoryName(configuration.InputPath);
            string rootFolder    = Path.IsPathRooted(directoryName) ? directoryName : AppDomain.CurrentDomain.BaseDirectory;
            string fileName      = configuration.InputPath;

            if (!string.IsNullOrWhiteSpace(fileName) && !string.IsNullOrWhiteSpace(rootFolder))
            {
                string actualInputPath = Path.GetFullPath(Path.Combine(rootFolder, fileName));
                string outputPath      = GetOutPutPath(
                    rootFolder,
                    actualInputPath,
                    configuration.OutputPath,
                    configuration.TargetType,
                    configuration.Minify);

                CruncherOptions options = new CruncherOptions
                {
                    Minify           = configuration.Minify,
                    AllowRemoteFiles = true,
                    RootFolder       = Path.GetDirectoryName(actualInputPath)
                };

                string crunched;

                if (configuration.TargetType == CrunchTargetType.CSS)
                {
                    CssCruncher cssCruncher = new CssCruncher(options);
                    crunched = cssCruncher.Crunch(actualInputPath);
                    crunched = cssCruncher.Minify(crunched);
                }
                else
                {
                    JavaScriptCruncher javaScriptCruncher = new JavaScriptCruncher(options);
                    crunched = javaScriptCruncher.Crunch(actualInputPath);
                    crunched = javaScriptCruncher.Minify(crunched);
                }

                FileHelper.WriteFile(outputPath, crunched);
            }
        }
Example #2
0
        /// <summary>
        /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler" /> interface.
        /// </summary>
        /// <param name="context">An <see cref="T:System.Web.HttpContext" /> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param>
        public override void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;
            string      path    = request.QueryString["path"];
            string      key     = path.ToMd5Fingerprint();
            bool        fallback;
            bool        minify = bool.TryParse(request.QueryString["minify"], out fallback);

            if (!string.IsNullOrWhiteSpace(path))
            {
                minify = minify || CruncherConfiguration.Instance.MinifyJavaScript;
                string combinedJavaScript = (string)CacheManager.GetItem(key);

                if (string.IsNullOrWhiteSpace(combinedJavaScript))
                {
                    string[]      javaScriptFiles = path.Split('|');
                    StringBuilder stringBuilder   = new StringBuilder();

                    CruncherOptions cruncherOptions = new CruncherOptions
                    {
                        MinifyCacheKey     = path,
                        Minify             = minify,
                        AllowRemoteFiles   = CruncherConfiguration.Instance.AllowRemoteDownloads,
                        RemoteFileMaxBytes = CruncherConfiguration.Instance.MaxBytes,
                        RemoteFileTimeout  = CruncherConfiguration.Instance.Timeout
                    };

                    cruncherOptions.CacheFiles  = cruncherOptions.Minify;
                    cruncherOptions.CacheLength = cruncherOptions.Minify ? CruncherConfiguration.Instance.MaxCacheDays : 0;
                    minify = cruncherOptions.Minify;

                    this.javaScriptCruncher = new JavaScriptCruncher(cruncherOptions);

                    // Loop through and process each file.
                    foreach (string javaScriptFile in javaScriptFiles)
                    {
                        // Local files.
                        if (PreprocessorManager.Instance.AllowedExtensionsRegex.IsMatch(javaScriptFile))
                        {
                            // Get the path from the server.
                            // Loop through each possible directory.
                            List <string> files = new List <string>();

                            foreach (string javaScriptFolder in CruncherConfiguration.Instance.JavaScriptPaths)
                            {
                                if (!string.IsNullOrWhiteSpace(javaScriptFolder) && javaScriptFolder.StartsWith("~/"))
                                {
                                    string actual = HttpContext.Current.Server.MapPath(javaScriptFolder);

                                    if (actual != null)
                                    {
                                        files.AddRange(Directory.GetFiles(actual, javaScriptFile, SearchOption.AllDirectories));
                                    }
                                }
                            }

                            // We only want the first file.
                            string first = files.FirstOrDefault();
                            cruncherOptions.RootFolder = Path.GetDirectoryName(first);
                            stringBuilder.Append(this.javaScriptCruncher.Crunch(first));
                        }
                        else
                        {
                            // Remote files.
                            string remoteFile = this.GetUrlFromToken(javaScriptFile).ToString();
                            stringBuilder.Append(this.javaScriptCruncher.Crunch(remoteFile));
                        }
                    }

                    // Minify and fix any missing semicolons between IIFE's
                    combinedJavaScript = this.javaScriptCruncher.Minify(stringBuilder.ToString())
                                         .Replace(")(function(", ");(function(");
                }

                if (!string.IsNullOrWhiteSpace(combinedJavaScript))
                {
                    IList <string> fileMonitors;

                    // Configure response headers
                    if (this.javaScriptCruncher == null)
                    {
                        // There should always be a valid list of monitors in the cache.
                        fileMonitors = (List <string>)CacheManager.GetItem(key + "_FILE_MONITORS");
                    }
                    else
                    {
                        fileMonitors = this.javaScriptCruncher.FileMonitors;
                    }

                    this.SetHeaders(path, context, ResponseType.JavaScript, minify, fileMonitors);
                    context.Response.Write(combinedJavaScript);

                    // Compress the response if applicable.
                    if (CruncherConfiguration.Instance.CompressResources)
                    {
                        CompressionModule.CompressResponse(context);
                    }
                }
                else
                {
                    context.Response.StatusCode        = (int)HttpStatusCode.NotFound;
                    context.Response.StatusDescription = HttpStatusCode.NotFound.ToString();
                }
            }
        }