Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function convertCode2Script(code) {
// clean source code comment
return code
.replace(/\/\/\/
consumer = await new SourceMapConsumer(converter.toJSON());
}
if (!consumer) {
throw new AppError({ code: 'NoSourceMap' });
}
return {
consumer,
codeFileContent,
};
}
const COMMENT_REGEX = convert.commentRegex;
const MAP_FILE_COMMENT_REGEX = convert.mapFileCommentRegex;
/** Extract either source map comment from file content */
function getSourceMapComment(fileContent: string): string {
const sourceMapComment =
getFirstRegexMatch(COMMENT_REGEX, fileContent) ||
getFirstRegexMatch(MAP_FILE_COMMENT_REGEX, fileContent) ||
'';
// Remove trailing EOLs
return sourceMapComment.trim();
}
const LF = '\n';
const CR_LF = '\r\n';
function detectEOL(content: string): string {
function loadSourceMap(file, state, callback) {
// Try to read inline source map
state.map = convert.fromSource(state.content);
if (state.map) {
state.map = state.map.toObject();
// Sources in map are relative to the source file
state.path = file.dirname;
state.content = convert.removeComments(state.content);
// Remove source map comment from source
file.contents = new Buffer(state.content, 'utf8');
return callback();
}
// Look for source map comment referencing a source map file
var mapComment = convert.mapFileCommentRegex.exec(state.content);
var mapFile;
if (mapComment) {
mapFile = path.resolve(file.dirname, mapComment[1] || mapComment[2]);
state.content = convert.removeMapFileComments(state.content);
// Remove source map comment from source
file.contents = new Buffer(state.content, 'utf8');
} else {
// If no comment try map file with same name as source file
mapFile = file.path + '.map';
}
// Sources in external map are relative to map file
state.path = path.dirname(mapFile);
fs.readFile(mapFile, onRead);
let output = this._services.getEmitOutput(this._files[filename].tsname);
if (output.emitSkipped)
throw new Error("Typescript emit error");
let jsname = tsToJs(this._files[filename].tsname);
let jstext = output.outputFiles
.filter((file) => (file.name == jsname))[0].text;
let mapname = tsToJsMap(this._files[filename].tsname);
let mapfile = output.outputFiles
.filter((file) => (file.name == mapname))[0]
if (mapfile) {
// replace the source map url with the actual source map
let sourcemap = convert.fromJSON(mapfile.text);
jstext = jstext.replace(convert.mapFileCommentRegex, sourcemap.toComment());
}
return {
failure: false,
errors: [],
js: jstext
}
} else {
return {
failure: true,
errors: diagnostics,
js: undefined
}
}
}
var fileContent = file.contents.toString();
var sourceMap;
if (options && options.loadMaps) {
var sourcePath = ''; //root path for the sources in the map
// Try to read inline source map
sourceMap = convert.fromSource(fileContent);
if (sourceMap) {
sourceMap = sourceMap.toObject();
// sources in map are relative to the source file
sourcePath = path.dirname(file.path);
fileContent = convert.removeComments(fileContent);
} else {
// look for source map comment referencing a source map file
var mapComment = convert.mapFileCommentRegex.exec(fileContent);
var mapFile;
if (mapComment) {
mapFile = path.resolve(path.dirname(file.path), mapComment[1] || mapComment[2]);
fileContent = convert.removeMapFileComments(fileContent);
// if no comment try map file with same name as source file
} else {
mapFile = file.path + '.map';
}
// sources in external map are relative to map file
sourcePath = path.dirname(mapFile);
try {
sourceMap = JSON.parse(stripBom(fs.readFileSync(mapFile, 'utf8')));
} catch(e) {}
exports.removeComments = function (src) {
if (!src.replace) return src;
return src.replace(convert.commentRegex, '').replace(convert.mapFileCommentRegex, '');
};
public loadFileFromComment(bundleItem: BundleItem) {
let commentMatch = convertSourceMap.mapFileCommentRegex.exec(bundleItem.source);
if (commentMatch && commentMatch[1]) {
let map: convertSourceMap.SourceMapConverter;
let dirname = path.dirname(bundleItem.filename);
if (!commentMatch[1].startsWith("data:")) {
let mapFilename = path.join(dirname, commentMatch[1]);
try {
let mapJson = fs.readFileSync(mapFilename, "utf-8");
map = convertSourceMap.fromJSON(mapJson);
}
catch (error) {
this.log.debug("Source map %s doesn't exist", mapFilename);
}
}