Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var sourceMapPath;
var sourceContent;
// Browserify, as an example, stores a datauri at sourceMappingURL.
if (/data:application\/json;base64,([^\s]+)/.test(sourceMapFile)) {
// Set sourceMapPath to the file that the map is inlined.
sourceMapPath = filename;
sourceContent = new Buffer(RegExp.$1, 'base64').toString();
} else {
// If sourceMapPath is relative, expand relative to the file
// refering to it.
sourceMapPath = path.resolve(path.dirname(filename), sourceMapFile);
sourceContent = grunt.file.read(sourceMapPath);
}
var sourceMap = JSON.parse(sourceContent);
var sourceMapConsumer = new SourceMapConsumer(sourceMap);
// Consider the relative path from source files to new sourcemap.
var sourcePathToSourceMapPath =
path.relative(path.dirname(this.dest), path.dirname(sourceMapPath));
// sourceMap path references are URLs, so ensure forward slashes are used for paths passed to sourcemap library
sourcePathToSourceMapPath = sourcePathToSourceMapPath.replace(/\\/g, '/');
// Store the sourceMap so that it may later be consumed.
this.maps.push([
sourceMapConsumer, relativeFilename, sourcePathToSourceMapPath
]);
// Remove the old sourceMappingURL.
src = src.replace(/[@#]\s+sourceMappingURL=[^\s]+/, '');
// Create a node from the source map for the file.
node = SourceNode.fromStringWithSourceMap(
src, sourceMapConsumer, sourcePathToSourceMapPath
);
} else {
value: function instrumentSync(code, fileName) {
var result = this._r = _babelTransform.transform(code, _extends({}, this.babelOptions, { filename: fileName }));
this._babelMap = new _SourceMapConsumer$SourceMapGenerator.SourceMapConsumer(result.map);
// PARSE
var program = esprima.parse(result.code, {
loc: true,
range: true,
tokens: this.opts.preserveComments,
comment: true
});
if (this.opts.preserveComments) {
program = escodegen.attachComments(program, program.comments, program.tokens);
}
return this.instrumentASTSync(program, fileName, code);
}
}, {
mergeSourceMap(map: Object) {
const inputMap = this.opts.inputSourceMap;
if (inputMap) {
const inputMapConsumer = new sourceMap.SourceMapConsumer(inputMap);
const outputMapConsumer = new sourceMap.SourceMapConsumer(map);
const mergedGenerator = new sourceMap.SourceMapGenerator({
file: inputMapConsumer.file,
sourceRoot: inputMapConsumer.sourceRoot,
});
// This assumes the output map always has a single source, since Babel always compiles a
// single source file to a single output file.
const source = outputMapConsumer.sources[0];
inputMapConsumer.eachMapping(function(mapping) {
const generatedPosition = outputMapConsumer.generatedPositionFor({
line: mapping.generatedLine,
column: mapping.generatedColumn,
source: source,
knownHelpers: known,
knownHelpersOnly: opts.o
};
if (opts.map) {
options.srcName = template.path;
}
if (opts.data) {
options.data = true;
}
var precompiled = Handlebars.precompile(template.source, options);
// If we are generating a source map, we have to reconstruct the SourceNode object
if (opts.map) {
var consumer = new _sourceMap.SourceMapConsumer(precompiled.map);
precompiled = _sourceMap.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']);
}
});
async function _resolveAndFetch(generatedSource: Source): SourceMapConsumer {
// Fetch the sourcemap over the network and create it.
const { sourceMapURL, baseURL } = _resolveSourceMapURL(generatedSource);
const fetched = await networkRequest(sourceMapURL, { loadFromCache: false });
// Create the source map and fix it up.
let map = new SourceMapConsumer(fetched.content, baseURL);
if (generatedSource.isWasm) {
map = new WasmRemap(map);
}
return map;
}
if (options.sourceMap && intermediateSourceMap) {
// Webpack doesn't need sourceMappingURL since we pass them on explicitely.
newContent = newContent.replace(/^\/\/# sourceMappingURL=[^\r\n]*/gm, '');
if (previousSourceMap) {
// If there's a previous sourcemap, we have to chain them.
// See https://github.com/mozilla/source-map/issues/216#issuecomment-150839869 for a simple
// source map chaining example.
// Use http://sokra.github.io/source-map-visualization/ to validate sourcemaps make sense.
// Force the previous sourcemap to use the filename we made up for it.
// In order for source maps to be chained, the consumed source map `file` needs to be in the
// consumers source map `sources` array.
previousSourceMap.file = inputFilePath;
// Chain the sourcemaps.
const consumer = new source_map_1.SourceMapConsumer(intermediateSourceMap);
const generator = source_map_1.SourceMapGenerator.fromSourceMap(consumer);
generator.applySourceMap(new source_map_1.SourceMapConsumer(previousSourceMap));
newSourceMap = generator.toJSON();
}
else {
// Otherwise just return our generated sourcemap.
newSourceMap = intermediateSourceMap;
}
}
// Webpack typings for previousSourceMap are wrong, they are JSON objects and not strings.
// tslint:disable-next-line:no-any
this.callback(null, newContent, newSourceMap);
}
exports.default = buildOptimizerLoader;
function inlineHostConfigDeclarations(bundle, srcReconcilerJS, sourceMapJSON) {
const ast = parse(bundle);
const consumerPromise = new SourceMapConsumer(sourceMapJSON);
const MAIN_FILE_CONTENTS = bundle;
const REACT_RECONCILER_CALL_CODE =
'Reconciler(__DEV__ ? HostConfigDev : HostConfigProd)';
// get position in source
const RECONCILER_JS_SOURCE = srcReconcilerJS;
const { line, code, pos } = searchCode(
RECONCILER_JS_SOURCE,
REACT_RECONCILER_CALL_CODE,
);
// const hostConfigObjectExpression = getHostConfigObjectExpression()
return (consumer => {
const {
line: targetLine,
async function prettifyAndMapFrameObject(code, frameObject) {
var pretty = cache[code];
if (!pretty) {
let unprettifiedCode = code;
var { code, map } = prettyFast(code, {
indent: " ",
url: "meaningless.js",
columnLevelMapAccuracy: true
});
pretty = {
code,
consumer: await new sourceMap.SourceMapConsumer(map.toString())
};
cache[unprettifiedCode] = pretty;
}
var generatedPosition = pretty.consumer.generatedPositionFor({
source: "meaningless.js",
line: frameObject.lineNumber,
column: frameObject.columnNumber
});
return {
formatted: pretty.code,
mappedFrameObject: {
fileName: frameObject.fileName + " (prettified)",
lineNumber: generatedPosition.line,
columnNumber: generatedPosition.column
_replacementToSourceNode(oldNode, newString) {
var map = oldNode.toStringWithSourceMap({
file: "?"
}).map;
var original = new SourceMapConsumer(map.toJSON()).originalPositionFor({
line: 1,
column: 0
});
if(original) {
return new SourceNode(original.line, original.column, original.source, newString);
} else {
return newString;
}
}