UNPKG

2.6 kBPlain TextView Raw
1#! /usr/bin/env node
2
3var script = process.argv[2].replace(/\\n/g, '\n')
4
5// node -e "console.log('var foo = ', "
6var Handlebars = require('./lib'),
7 SourceMap = require('source-map'),
8 SourceMapConsumer = SourceMap.SourceMapConsumer;
9
10var template = Handlebars.precompile(script, {
11 srcName: 'input.hbs',
12 destName: 'output.js',
13
14 assumeObjects: true,
15 compat: false,
16 strict: true,
17 trackIds: true,
18 knownHelpersOnly: false
19 });
20
21// return console.log(template);
22
23var consumer = new SourceMapConsumer(template.map),
24 lines = template.code.split('\n'),
25 srcLines = script.split('\n');
26
27console.log();
28console.log('Source:');
29srcLines.forEach(function(source, index) {
30 console.log(index+1, source);
31});
32console.log();
33console.log('Generated:');
34console.log(template.code);
35lines.forEach(function(source, index) {
36 // console.log(index+1, source);
37});
38console.log();
39console.log('Map:');
40console.log(template.map);
41console.log();
42
43function collectSource(lines, lineName, colName, order) {
44 var ret = {},
45 ordered = [],
46 last;
47
48 // TODO : Find the content that does not have a source mapping
49 function collect(current) {
50 if (last) {
51 var mapLines = lines.slice(last[lineName] - 1, current && current[lineName]);
52 if (mapLines.length) {
53 if (current) {
54 mapLines[mapLines.length-1] = mapLines[mapLines.length-1].slice(0, current[colName]);
55 }
56 mapLines[0] = mapLines[0].slice(last[colName]);
57 }
58 ret[last[lineName] + ':' + last[colName]] = mapLines.join('\n');
59 ordered.push({
60 startLine: last[lineName],
61 startCol: last[colName],
62 endLine: current && current[lineName]
63 });
64 }
65 last = current;
66 }
67
68 consumer.eachMapping(collect, undefined, order);
69 collect();
70
71 return ret;
72}
73
74srcLines = collectSource(srcLines, 'originalLine', 'originalColumn', SourceMapConsumer.ORIGINAL_ORDER);
75lines = collectSource(lines, 'generatedLine', 'generatedColumn');
76
77false && consumer.eachMapping(function(mapping) {
78 var originalSrc = srcLines[mapping.originalLine + ':' + mapping.originalColumn],
79 generatedSrc = lines[mapping.generatedLine + ':' + mapping.generatedColumn];
80
81 if (!mapping.originalLine) {
82 console.log('generated', mapping.generatedLine + ':' + mapping.generatedColumn, generatedSrc);
83 } else {
84 console.log('map',
85 mapping.source,
86 mapping.originalLine + ':' + mapping.originalColumn,
87 originalSrc,
88 '->',
89 mapping.generatedLine + ':' + mapping.generatedColumn,
90 generatedSrc);
91 }
92});