twing
Version:
First-class Twig engine for Node.js
36 lines (35 loc) • 1.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createSourceMapRuntime = void 0;
const path_1 = require("path");
const source_map_1 = require("source-map");
const createSourceMapRuntime = () => {
let stack = [
new source_map_1.SourceNode()
];
return {
get sourceMap() {
const { map } = stack[0].toStringWithSourceMap();
return JSON.parse(map.toString());
},
enterSourceMapBlock: (line, column, nodeType, source, outputBuffer) => {
outputBuffer.start();
let sourceName = source.name;
if ((0, path_1.isAbsolute)(sourceName)) {
sourceName = (0, path_1.relative)('.', sourceName);
}
const node = new source_map_1.SourceNode(line, column - 1, sourceName, '', nodeType);
stack[0].setSourceContent(sourceName, source.code);
stack.push(node);
},
leaveSourceMapBlock: (outputBuffer) => {
const sourceNode = stack.pop();
const content = outputBuffer.getAndFlush();
if (sourceNode.children.length === 0) {
sourceNode.add(content);
}
stack[stack.length - 1].add(sourceNode);
}
};
};
exports.createSourceMapRuntime = createSourceMapRuntime;