示例#1
0
文件: Translator.cs 项目: nrnoble/h5
        public string GenerateSourceMap(string fileName, string content, Action<SourceMapBuilder> before = null)
        {
            if (AssemblyInfo.SourceMap.Enabled)
            {
                var projectPath = Path.GetDirectoryName(Location);

                SourceMapGenerator.Generate(fileName, projectPath, ref content,
                    before,
                    (sourceRelativePath) =>
                    {
                        string path = null;
                        ParsedSourceFile sourceFile = null;

                        try
                        {
                            path = Path.Combine(projectPath, sourceRelativePath);
                            sourceFile = ParsedSourceFiles.First(pf => pf.ParsedFile.FileName == path);

                            return sourceFile.SyntaxTree.TextSource ?? sourceFile.SyntaxTree.ToString(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], SourceFiles, AssemblyInfo.SourceMap.Eol
                );
            }

            return content;
        }
示例#2
0
        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)
        {
            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       = 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)];
                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());

            Logger.ZLogTrace("Generated source map for {0}", scriptFileName);

            var encoded = "//# sourceMappingURL=data:application/json;base64," + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(map));

            content = content + Emitter.NEW_LINE + encoded + Emitter.NEW_LINE;
        }