public string GenerateSourceMap(string fileName, string content, Action <SourceMapBuilder> before = null) { if (this.AssemblyInfo.SourceMap.Enabled) { var projectPath = Path.GetDirectoryName(this.Location); SourceMapGenerator.Generate(fileName, projectPath, ref content, before, (sourceRelativePath) => { string path = null; ParsedSourceFile sourceFile = null; try { path = Path.Combine(projectPath, sourceRelativePath); sourceFile = this.ParsedSourceFiles.First(pf => pf.ParsedFile.FileName == path); return(sourceFile.SyntaxTree.TextSource ?? sourceFile.SyntaxTree.ToString(Translator.GetFormatter())); } catch (Exception ex) { throw (TranslatorException)TranslatorException.Create( "Could not get ParsedSourceFile for SourceMap. Exception: {0}; projectPath: {1}; sourceRelativePath: {2}; path: {3}.", ex.ToString(), projectPath, sourceRelativePath, path); } }, new string[0], this.SourceFiles, this.AssemblyInfo.SourceMap.Eol, this.Log ); } return(content); }
public static void Generate(string scriptFileName, string basePath, ref string content, Action <SourceMapBuilder> beforeGenerate, Func <string, string> sourceContent, string[] names, IList <string> sourceFiles, UnicodeNewline?forceEols, ILogger logger) { var fileName = Path.GetFileName(scriptFileName); var generator = new SourceMapGenerator(fileName, "", basePath, forceEols); StringLocation location = null; string script = content; int offset = 0; content = tokenRegex.Replace(content, match => { location = SourceMapGenerator.LocationFromPos(script, match.Index, location, ref offset); offset += match.Length; var sourceLine = int.Parse(match.Groups[2].Value); var sourceCol = int.Parse(match.Groups[3].Value); var sourcePath = sourceFiles[int.Parse(match.Groups[1].Value)]; //sourcePath = sourcePath.Substring(basePath.Length + 1); generator.RecordLocation(location.Line, location.Column, sourcePath, sourceLine, sourceCol); return(""); }); var sources = generator.SourceMapBuilder.SourceUrlList; List <string> contents = new List <string>(); foreach (var source in sources) { contents.Add(sourceContent(source)); } // Chrome handles it very strange, need more investigate //generator._sourceMapBuilder.SourceNameList.AddRange(names); beforeGenerate?.Invoke(generator.SourceMapBuilder); var map = generator.GetSourceMap(contents.ToArray()); if (logger != null) { logger.Trace("SourceMap for " + scriptFileName); logger.Trace(map); } var encoded = "//# sourceMappingURL=data:application/json;base64," + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(map)); if (logger != null) { logger.Trace("Base64 SourceMap for " + scriptFileName); logger.Trace(encoded); } content = content + Emitter.NEW_LINE + encoded + Emitter.NEW_LINE; }