Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async createSourceNode({ source, code, map }) {
if (map instanceof SourceMapGenerator) {
map = map.toJSON()
}
if (map) {
const consumer = await new SourceMapConsumer(map)
return SourceNode.fromStringWithSourceMap(code, consumer)
} else {
// Source code need to be mapped line by line for debugging in devtols to work.
const lines = code.split('\n')
const node = new SourceNode()
for (let i = 0; i < lines.length; i++) {
node.add(new SourceNode(i + 1, 0, source, lines[i]))
}
return node.join('\n')
// return new SourceNode(1, 0, source, code)
}
}
LOG.entry(method, context, script);
// Convert the script into a source map.
let sourceFileName = script.getIdentifier();
let sourceCode = script.getContents();
let sourceMap = this.convertScriptToSourceMap(context, script);
// Allow someone else to post-process the converted script.
const transformedScript = this.transformScript(sourceFileName, sourceCode, sourceMap);
sourceFileName = transformedScript.sourceFileName;
sourceCode = transformedScript.sourceCode;
sourceMap = transformedScript.sourceMap;
// Create a new source node from the script contents and source map
const sourceMapConsumer = new SourceMapConsumer(sourceMap);
const result = SourceNode.fromStringWithSourceMap(sourceCode, sourceMapConsumer);
LOG.exit(method, result);
return result;
}
knownHelpersOnly: opts.o
};
if (opts.map) {
options.srcName = template.path;
}
if (opts.data) {
options.data = true;
}
let precompiled = Handlebars.precompile(template.source, options);
// If we are generating a source map, we have to reconstruct the SourceNode object
if (opts.map) {
let consumer = new SourceMapConsumer(precompiled.map);
precompiled = SourceNode.fromStringWithSourceMap(precompiled.code, consumer);
}
if (opts.simple) {
output.add([precompiled, '\n']);
} else {
if (!template.name) {
throw new Handlebars.Exception('Name missing for template');
}
if (opts.amd && !multiple) {
output.add('return ');
}
output.add([objectName, '[\'', template.name, '\'] = template(', precompiled, ');\n']);
}
});
knownHelpersOnly: opts.o
};
if (opts.map) {
options.srcName = template.path;
}
if (opts.data) {
options.data = true;
}
let precompiled = Handlebars.precompile(template.source, options);
// If we are generating a source map, we have to reconstruct the SourceNode object
if (opts.map) {
let consumer = new SourceMapConsumer(precompiled.map);
precompiled = SourceNode.fromStringWithSourceMap(
precompiled.code,
consumer
);
}
if (opts.simple) {
output.add([precompiled, '\n']);
} else {
if (!template.name) {
throw new Handlebars.Exception('Name missing for template');
}
if (opts.amd && !multiple) {
output.add('return ');
}
output.add([
function getNodeFromFiles(scriptFile, mapFile) {
if (fs.existsSync(scriptFile) && fs.existsSync(mapFile)) {
try {
return SourceNode.fromStringWithSourceMap(
getScriptFromFile(scriptFile),
new SourceMapConsumer(getMapFromMapFile(mapFile))
);
} catch (error) {
console.error('getNodeFromFiles() error: ' + error);
return new SourceNode();
}
} else {
return new SourceNode();
}
}
if (this.sourceMap === false) {
return this.callback(null, [
prependText,
processedSource,
appendText
].join(separator));
}
if (!map) {
map = makeIdentitySourceMap(source, this.resourcePath);
}
node = new SourceNode(null, null, null, [
new SourceNode(null, null, this.resourcePath, prependText),
SourceNode.fromStringWithSourceMap(processedSource, new SourceMapConsumer(map))
]).join(separator);
result = node.toStringWithSourceMap();
this.callback(null, result.code, result.map.toString());
};
this._children.map(function(item) {
if (typeof item.node === "function") return item.node(options);
const sourceAndMap = item.sourceAndMap(options);
if (sourceAndMap.map) {
return SourceNode.fromStringWithSourceMap(
sourceAndMap.source,
new SourceMapConsumer(sourceAndMap.map)
);
} else {
return sourceAndMap.source;
}
})
);
node(options) {
this._ensureValueString();
this._ensureSourceMapObject();
this._ensureOriginalSourceString();
let node = SourceNode.fromStringWithSourceMap(
this._valueAsString,
new SourceMapConsumer(this._sourceMapAsObject)
);
node.setSourceContent(this._name, this._originalSourceAsString);
if (this._hasInnerSourceMap) {
this._ensureInnerSourceMapObject();
node = applySourceMap(
node,
new SourceMapConsumer(this._innerSourceMapAsObject),
this._name,
this._removeOriginalSource
);
}
return node;
}
node(options) {
var innerSourceMap = this._innerSourceMap;
var sourceMap = this._sourceMap;
if(innerSourceMap) {
sourceMap = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(sourceMap));
if(this._originalSource)
sourceMap.setSourceContent(this._name, this._originalSource);
innerSourceMap = new SourceMapConsumer(innerSourceMap);
sourceMap.applySourceMap(innerSourceMap, this._name);
sourceMap = sourceMap.toJSON();
}
return SourceNode.fromStringWithSourceMap(this._value, new SourceMapConsumer(sourceMap));
}
SourceMapSource.prototype.node = function(options) {
var innerSourceMap = this._innerSourceMap;
var sourceMap = this._sourceMap;
if(innerSourceMap) {
innerSourceMap = new SourceMapConsumer(innerSourceMap);
sourceMap = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(sourceMap));
sourceMap.setSourceContent(this._name, this._originalSource);
sourceMap.applySourceMap(innerSourceMap, this._name);
sourceMap = sourceMap.toJSON();
}
return SourceNode.fromStringWithSourceMap(this._value, new SourceMapConsumer(sourceMap));
};