Exemplo n.º 1
0
        public Task ProcessAsync(FileProcessContext fileProcessContext, PreProcessorDelegate next)
        {
            var jsMin  = new JsMin();
            var result = jsMin.ProcessAsync(fileProcessContext);

            fileProcessContext.Update(result);
            return(next(fileProcessContext));
        }
Exemplo n.º 2
0
        public Task ProcessAsync(FileProcessContext fileProcessContext, PreProcessorDelegate next)
        {
            //ensure the Urls in the css are changed to absolute
            var parsedUrls = ReplaceUrlsWithAbsolutePaths(fileProcessContext.FileContent, fileProcessContext.WebFile.FilePath);

            fileProcessContext.Update(parsedUrls);
            return(next(fileProcessContext));
        }
        public async Task ProcessAsync(FileProcessContext fileProcessContext, PreProcessorDelegate next)
        {
            var result = await _nodeServices.InvokeAsync <string>(
                _nodeScript.Value.FileName, fileProcessContext.FileContent);

            fileProcessContext.Update(result);

            await next(fileProcessContext);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Minifies Css
        /// </summary>
        /// <param name="fileProcessContext"></param>
        /// <param name="next"></param>
        /// <returns></returns>

        public async Task ProcessAsync(FileProcessContext fileProcessContext, PreProcessorDelegate next)
        {
            using (var reader = new StringReader(fileProcessContext.FileContent))
            {
                var cssMin    = new CssMin();
                var minResult = cssMin.Minify(reader);
                fileProcessContext.Update(minResult);
                await next(fileProcessContext);
            }
        }
Exemplo n.º 5
0
        public async Task ProcessAsync(FileProcessContext fileProcessContext, PreProcessorDelegate next)
        {
            var sb = new StringBuilder();

            IEnumerable <string> importedPaths;
            var removedImports = ParseImportStatements(fileProcessContext.FileContent, out importedPaths);

            //need to write the imported sheets first since these theoretically should *always* be at the top for browser to support them
            foreach (var importPath in importedPaths)
            {
                var uri      = new Uri(fileProcessContext.WebFile.FilePath, UriKind.RelativeOrAbsolute).MakeAbsoluteUri(_siteInfo.GetBaseUrl());
                var absolute = uri.ToAbsolutePath(importPath);

                var path = _requestHelper.Content(absolute);
                //is it external?
                if (path.Contains(Constants.SchemeDelimiter))
                {
                    //Pretty sure we just leave the external refs in there
                    //TODO: Look in CDF, we have tests for this, pretty sure the ParseImportStatements removes that
                }
                else
                {
                    //it's internal (in theory)
                    var filePath = _fileSystemHelper.GetFileInfo(path);
                    var content  = await _fileSystemHelper.ReadContentsAsync(filePath);

                    //This needs to be put back through the whole pre-processor pipeline before being added,
                    // so we'll clone the original webfile with it's new path, this will inherit the whole pipeline,
                    // and then we'll execute the pipeline for that file
                    var clone     = fileProcessContext.WebFile.Duplicate(path);
                    var processed = await clone.Pipeline.ProcessAsync(new FileProcessContext(content, clone, fileProcessContext.BundleContext));

                    sb.Append(processed);

                    ////  _fileSystemHelper.MapWebPath(path.StartsWith("/") ? path : string.Format("~/{0}", path));
                    //if (System.IO.File.Exists(filePath))
                    //{

                    //}
                    //else
                    //{
                    //    //TODO: Need to log this
                    //}
                }
            }

            sb.Append(removedImports);

            fileProcessContext.Update(sb.ToString());

            await next(fileProcessContext);
        }
Exemplo n.º 6
0
        public Task ProcessAsync(FileProcessContext fileProcessContext, PreProcessorDelegate next)
        {
            if (fileProcessContext.WebFile.DependencyType == WebFileType.Js)
            {
                throw new InvalidOperationException("Cannot use " + nameof(NuglifyCss) + " with a js file source");
            }

            var result = Uglify.Css(fileProcessContext.FileContent, fileProcessContext.WebFile.FilePath, _settings.CssCodeSettings);

            if (result.HasErrors)
            {
                //TODO: need to format this exception message nicely
                throw new InvalidOperationException(
                          string.Join(",", result.Errors.Select(x => x.Message)));
            }

            fileProcessContext.Update(result.Code);

            return(next(fileProcessContext));
        }
Exemplo n.º 7
0
        public Task ProcessAsync(FileProcessContext fileProcessContext, PreProcessorDelegate next)
        {
            //Info for source mapping, see http://ajaxmin.codeplex.com/wikipage?title=SourceMaps
            // as an example, see http://ajaxmin.codeplex.com/SourceControl/latest#AjaxMinTask/AjaxMinManifestTask.cs under ProcessJavaScript
            // When a source map provider is specified, the process is:
            // * Create a string builder/writer to capture the output of the source map
            // * Create a sourcemap from the SourceMapFactory based on the provider name
            // * Set some V3 source map provider (the default) properties
            // * Call StartPackage, passing in the file path of the original file and the file path of the map (which will be appended to the end of the minified output)
            // * Do the minification
            // * Call EndPackage, and close/dispose of writers
            // * Get the source map result from the string builder

            if (fileProcessContext.WebFile.DependencyType == WebFileType.Css)
            {
                throw new InvalidOperationException("Cannot use " + nameof(NuglifyJs) + " with a css file source");
            }

            var nuglifyJsCodeSettings = _settings.JsCodeSettings;

            //Its very important that we clone here because the code settings is a singleton and we are changing it (i.e. the CodeSettings class is mutable)
            var codeSettings = nuglifyJsCodeSettings.CodeSettings.Clone();

            if (nuglifyJsCodeSettings.SourceMapType != SourceMapType.None)
            {
                var sourceMap = fileProcessContext.BundleContext.GetSourceMapFromContext(nuglifyJsCodeSettings.SourceMapType);

                codeSettings.SymbolsMap = sourceMap;

                //These are a couple of options that could be needed for V3 source maps

                //sourceRoot is explained here:
                //  http://blog.teamtreehouse.com/introduction-source-maps
                //  https://www.html5rocks.com/en/tutorials/developertools/sourcemaps/
                //sourceMap.SourceRoot =

                //SafeHeader is used to avoid XSS and adds some custom json to the top of the file , here's what the source code says:
                // "if we want to add the cross-site script injection protection string" it adds this to the top ")]}'"
                // explained here: https://www.html5rocks.com/en/tutorials/developertools/sourcemaps/ under "Potential XSSI issues"
                // ** not needed for inline
                //sourceMap.SafeHeader =

                //generate a minified file path - this is really not used but is used in our inline source map like test.js --> test.min.js
                //var extension = Path.GetExtension(fileProcessContext.WebFile.FilePath);
                //var minPath = fileProcessContext.WebFile.FilePath.Substring(
                //                  0,
                //                  fileProcessContext.WebFile.FilePath.LastIndexOf('.')) + ".min" + extension;

                //we then need to 'StartPackage', this will be called once per file for the same source map instance but that is ok it doesn't cause any harm
                var fileName = fileProcessContext.BundleContext.BundleName + fileProcessContext.BundleContext.FileExtension;
                sourceMap.StartPackage(fileName, fileName + ".map");
            }

            //now do the processing
            var result = Uglify.Js(fileProcessContext.FileContent, fileProcessContext.WebFile.FilePath, codeSettings);

            if (result.HasErrors)
            {
                throw new InvalidOperationException(
                          string.Join(",", result.Errors.Select(x => x.ToString())));
            }

            fileProcessContext.Update(result.Code);

            if (nuglifyJsCodeSettings.SourceMapType != SourceMapType.None)
            {
                AddSourceMapAppenderToContext(fileProcessContext.BundleContext, nuglifyJsCodeSettings.SourceMapType);
            }

            return(next(fileProcessContext));
        }
        public Task ProcessAsync(FileProcessContext fileProcessContext, PreProcessorDelegate next)
        {
            var sb = new StringBuilder();

            using (var reader = new StringReader(fileProcessContext.FileContent))
            {
                var line = reader.ReadLine();
                while (line != null)
                {
                    var isTrim     = true;
                    var foundIndex = 0;
                    for (int i = 0; i < line.Length; i++)
                    {
                        char c = line[i];
                        if (isTrim && char.IsWhiteSpace(c))
                        {
                            continue;
                        }

                        isTrim = false;
                        if (c == _sourceMappingUrl[foundIndex])
                        {
                            foundIndex++;
                            if (foundIndex == _sourceMappingUrl.Length)
                            {
                                // found! parse it
                                var match = RegexStatements.SourceMap.Match(line);
                                if (match.Success)
                                {
                                    var url = match.Groups[1].Value;
                                    // convert to it's absolute path
                                    var contentPath = _requestHelper.Content(fileProcessContext.WebFile.FilePath);
                                    var uri         = new Uri(contentPath, UriKind.RelativeOrAbsolute).MakeAbsoluteUri(_siteInfo.GetBaseUrl());
                                    var absolute    = uri.ToAbsolutePath(url);
                                    var path        = _requestHelper.Content(absolute);
                                    // replace the source map with the correct url
                                    WriteLine(sb, $"{_sourceMappingUrl}={path};");
                                }
                                else
                                {
                                    // should have matched, perhaps the source map is formatted in a weird way, we're going to ignore
                                    // it since if it's rendered without the correct path then other errors will occur.
                                }
                                break; // exit for loop
                            }
                        }
                        else
                        {
                            // not found on this line
                            WriteLine(sb, line);
                            break; // exit for loop
                        }
                    }

                    // next
                    line = reader.ReadLine();
                }
            }

            fileProcessContext.Update(sb.ToString());
            return(next(fileProcessContext));
        }
Exemplo n.º 9
0
            public async Task ProcessAsync(FileProcessContext fileProcessContext, PreProcessorDelegate next)
            {
                await next(fileProcessContext);

                fileProcessContext.Update(fileProcessContext.FileContent + "\nFooter");
            }
Exemplo n.º 10
0
            public async Task ProcessAsync(FileProcessContext fileProcessContext, PreProcessorDelegate next)
            {
                await next(fileProcessContext);

                fileProcessContext.Update("Header\n" + fileProcessContext.FileContent);
            }
Exemplo n.º 11
0
            public async Task ProcessAsync(FileProcessContext fileProcessContext, PreProcessorDelegate next)
            {
                await next(fileProcessContext);

                fileProcessContext.Update("WrappedHeader\n" + fileProcessContext.FileContent + "\nWrappedFooter");
            }