Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function writeSourceMaps(file, destPath, callback) {
var sourceMapFile;
var commentOpts = getCommentOptions(file.extname);
var comment;
if (destPath == null) {
// Encode source map into comment
comment = convert.fromObject(file.sourceMap).toComment(commentOpts);
} else {
var mapFile = path.join(destPath, file.relative) + '.map';
var sourceMapPath = path.join(file.base, mapFile);
// Create new sourcemap File
sourceMapFile = createSourceMapFile({
cwd: file.cwd,
base: file.base,
path: sourceMapPath,
content: file.sourceMap,
});
var sourcemapLocation = path.relative(file.dirname, sourceMapPath);
sourcemapLocation = normalizePath(sourcemapLocation);
{
enter: empowerAssert.enter
},
espower.createVisitor(jsAst, espowerOptions)
]));
var escodegenOutput = escodegen.generate(modifiedAst, escodegenOptions);
if (inMap) {
file.contents = new Buffer(escodegenOutput.code);
var outMap = convert.fromJSON(escodegenOutput.map.toString());
outMap.setProperty('sources', inMap.sources);
outMap.setProperty('sourcesContent', inMap.sourcesContent);
var reMap;
if (inMap.mappings === '') {
applySourceMap(file, outMap.toJSON());
reMap = convert.fromObject(file.sourceMap);
} else {
reMap = convert.fromObject(mergeSourceMap(inMap, outMap.toJSON()));
}
reMap.setProperty('sources', inMap.sources);
reMap.setProperty('sourcesContent', inMap.sourcesContent);
// do not set sourceMapRoot to keep paths relative until the end of chain
// reMap.setProperty('sourceRoot', file.base);
file.sourceMap = reMap.toObject();
} else {
file.contents = new Buffer(escodegenOutput);
}
}
result.code = _result.code;
result.map = _result.map;
debug(this.opts, "Generation end");
if (this.shebang) {
// add back shebang
result.code = `${this.shebang}\n${result.code}`;
}
if (result.map) {
result.map = this.mergeSourceMap(result.map);
}
if (opts.sourceMaps === "inline" || opts.sourceMaps === "both") {
result.code += "\n" + convertSourceMap.fromObject(result.map).toComment();
}
if (opts.sourceMaps === "inline") {
result.map = null;
}
return this.makeResult(result);
}
}
instrumentSync (code, filename, { sourceMap, registerMap }) {
var instrumented = instrumenter.instrumentSync(code, filename, sourceMap)
if (instrumented !== code) {
registerMap()
}
// the instrumenter can optionally produce source maps,
// this is useful for features like remapping stack-traces.
if (options.produceSourceMap) {
var lastSourceMap = instrumenter.lastSourceMap()
/* istanbul ignore else */
if (lastSourceMap) {
instrumented += '\n' + convertSourceMap.fromObject(lastSourceMap).toComment()
}
}
return instrumented
},
lastFileCoverage () {
function makeFileWithInlineSourceMap() {
var inline = convert.fromObject(makeSourcemap()).toComment();
return new File({
cwd: __dirname,
base: path.join(__dirname, 'assets'),
path: path.join(__dirname, 'assets', 'all.js'),
contents: new Buffer('console.log("line 1.1"),console.log("line 1.2"),console.log("line 2.1"),console.log("line 2.2");\n' + inline),
});
}
it('uses absolute sourceRoot to resolve sources', function(done) {
var file = makeFile();
var map = convert.fromObject(makeSourcemap());
delete map.sourcemap.sourcesContent;
var inline = map.toComment();
file.contents = new Buffer(sourceContent + '\n' + inline);
sourcemaps.add(file, function(err, data) {
expect(data.sourceMap).toExist();
expect(data.sourceMap.sourceRoot).toEqual(path.join(__dirname, 'assets'));
expect(data.sourceMap.sourcesContent).toEqual(['test1\n', 'test2\n']);
done(err);
});
});
function end (cb) {
var src = Buffer.concat(bufs).toString('utf8')
var flags = (opts && opts._flags) || {}
var basedir = flags.basedir || process.cwd()
var filename = path.relative(basedir, file)
var res
try {
res = transformAst(src, { parser: acorn, inputFilename: filename }, walk)
if (flags.debug) {
var sm = convertSourceMap.fromObject(res.map).toComment()
res = res.toString() + '\n' + sm + '\n'
} else {
res = res.toString()
}
} catch (err) {
return cb(err)
}
this.push(res)
this.push(null)
}
function walk (node) {
function sourcemapToComment(map) {
var content = convertSourceMap.fromObject(map).toBase64();
return '/*# sourceMappingURL=data:application/json;base64,' + content + ' */';
}
function reconnectSourceMap (inMap, outMap) {
var mergedRawMap = mergeSourceMap(inMap, outMap.toObject());
var reMap = convert.fromObject(mergedRawMap);
overwritePropertyIfExists('sources', inMap, reMap);
overwritePropertyIfExists('sourceRoot', inMap, reMap);
overwritePropertyIfExists('sourcesContent', inMap, reMap);
return reMap;
}