private CompilerResult ProcessConfig(string baseFolder, Config config)
        {
            ICompiler compiler = CompilerService.GetCompiler(config);

            var result = compiler.Compile(config);

            if (result.Errors.Any(e => !e.IsWarning))
            {
                return(result);
            }

            if (Path.GetExtension(config.OutputFile).Equals(".css", StringComparison.OrdinalIgnoreCase) && AdjustRelativePaths(config))
            {
                result.CompiledContent = CssRelativePath.Adjust(result.CompiledContent, config);
            }

            config.Output = result.CompiledContent;

            FileInfo outputFile      = config.GetAbsoluteOutputFile();
            bool     containsChanges = FileHelpers.HasFileContentChanged(outputFile.FullName, config.Output);

            OnBeforeProcess(config, baseFolder, containsChanges);

            if (containsChanges)
            {
                string dir = outputFile.DirectoryName;

                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }

                File.WriteAllText(outputFile.FullName, config.Output, new UTF8Encoding(true));
            }

            OnAfterProcess(config, baseFolder, containsChanges);

            //if (!config.Minify.ContainsKey("enabled") || config.Minify["enabled"].ToString().Equals("true", StringComparison.OrdinalIgnoreCase))
            //{
            FileMinifier.MinifyFile(config);
            //}

            if (!string.IsNullOrEmpty(result.SourceMap))
            {
                string absolute  = config.GetAbsoluteOutputFile().FullName;
                string mapFile   = absolute + ".map";
                bool   smChanges = FileHelpers.HasFileContentChanged(mapFile, result.SourceMap);

                OnBeforeWritingSourceMap(absolute, mapFile, smChanges);

                if (smChanges)
                {
                    File.WriteAllText(mapFile, result.SourceMap, new UTF8Encoding(true));
                }

                OnAfterWritingSourceMap(absolute, mapFile, smChanges);
            }

            return(result);
        }
示例#2
0
        private CompilerResult ProcessConfig(string baseFolder, Config config)
        {
            ICompiler compiler = CompilerService.GetCompiler(config);

            var result = compiler.Compile(config);

            if (result.HasErrors)
            {
                return(result);
            }

            config.Output = result.CompiledContent;
            string outputFile = Path.Combine(baseFolder, config.OutputFile);

            OnBeforeProcess(config, baseFolder);
            File.WriteAllText(outputFile, config.Output, new UTF8Encoding(true));
            OnAfterProcess(config, baseFolder);

            if (config.Minify.ContainsKey("enabled") && config.Minify["enabled"].ToString().Equals("true", StringComparison.OrdinalIgnoreCase))
            {
                FileMinifier.MinifyFile(config);
            }

            if (config.SourceMap)
            {
                if (!string.IsNullOrEmpty(result.SourceMap))
                {
                    string aboslute = config.GetAbsoluteOutputFile();
                    string mapFile  = aboslute + ".map";

                    OnBeforeWritingSourceMap(aboslute, mapFile);
                    File.WriteAllText(mapFile, result.SourceMap, new UTF8Encoding(true));
                    OnAfterWritingSourceMap(aboslute, mapFile);
                }
            }

            return(result);
        }