Example #1
0
        static void Test2()
        {
            SourceMaps.List <SourceFile> sources = new SourceMaps.List <SourceFile>();

            sources.Add(new SourceFile("http://www.mysite.com/source1.txt", LoadFromFile(@"..\..\..\Website\source1.txt")));
            sources.Add(new SourceFile("http://www.mysite.com/source2.txt", LoadFromFile(@"..\..\..\Website\source2.txt")));

            string destfile = "";

            foreach (var sf in sources)
            {
                destfile += sf.content.ToLower();
            }
            destfile += "\r\n//# sourceMappingURL=myapp.js.map";

            SaveToFile(@"..\..\..\Website\myapp.js", destfile);

            SourceFile target = new SourceFile("http://www.mysite.com/myapp.js", destfile);

            // build tokens/source map entries
            SourceMaps.List <SourceMapEntry> sme = new SourceMaps.List <SourceMapEntry>();

            // this will map every single character, no name specified
            int cumulative_l = 0;

            foreach (var sf in sources)
            {
                for (int cx = 0; cx < sf.content.Length; cx++)
                {
                    string c   = ""; //sf.content[cx].ToString();
                    var    tok = new TokenSourceFileLocation(sf, c, cx);
                    sme.add(new SourceMapEntry(tok, cx + cumulative_l));
                }
                cumulative_l += sf.length;
            }

            // this maps only identifiers

            /*
             * Regex R = new Regex(@"[_a-zA-Z][_a-zA-Z0-9]*");
             * int cumulative_l = 0;
             * foreach(var sf in sources)
             * {
             * MatchCollection mc = R.Matches(sf.content);
             * foreach(Match match in mc)
             * {
             *    var keyword = match.Value.ToLower();
             *
             *    var tok = new TokenSourceFileLocation(sf, match.Value.ToLower(), match.Index);
             *    sme.add( new SourceMapEntry(tok,match.Index+cumulative_l));
             * }
             * cumulative_l += sf.length;
             * }
             */

            Uri sourceMapUri = Uri.parse("http://www.mysite.com/myapp.map");
            Uri fileUri      = Uri.parse("http://www.mysite.com/myapp.js");

            SourceMapBuilder sourceMapBuilder = new SourceMapBuilder(sourceMapUri, fileUri, target);

            foreach (var e in sme)
            {
                sourceMapBuilder.addMapping(e.targetOffset, e.sourceLocation);
            }
            String sourceMap = sourceMapBuilder.build();

            SaveToFile(@"..\..\..\Website\myapp.js.map", sourceMap);
        }