Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
transform("constructRuntime", runtimeBody => {
// Attach `hot` property to module in `require`.
traverse.cheap(runtimeBody, node => {
if (
node.type === "ObjectProperty" &&
node.key.name === "require"
) {
const fnBody = node.value.body.body;
const moduleObjIdx = fnBody.findIndex(fnBody, bodyNode =>
bodyNode.type === "VariableDeclaration" &&
bodyNode.declarations[0] &&
bodyNode.declarations[0].id.name === "moduleObj"
);
fnBody.splice(moduleObjIdx + 1, 0, moduleHot);
}
});
// Attach the hot-patch run-time to the end of the run-time body.
return runtimeBody.concat(hotPatchRuntime);
let getAst = function () {
let ast;
try {
ast = babylon.parse(code, {
allowReturnOutsideFunction: true,
allowSuperOutsideMethod: true
});
traverse.cheap(ast, function (node) {
clearNode(node);
if (node.leadingComments) { node.leadingComments.forEach(clearNode); }
if (node.trailingComments) { node.trailingComments.forEach(clearNode); }
node[FROM_TEMPLATE] = true;
});
} catch (err) {
err.stack = `${err.stack}from\n${stack}`;
throw err;
}
getAst = function () {
return ast;
};
return ast;
};
(consumer) => {
traverse.cheap(babelAst, node => {
if (node.loc) {
const originalStart = consumer.originalPositionFor(node.loc.start);
if (originalStart.line) {
node.loc.start.line = originalStart.line;
node.loc.start.column = originalStart.column;
}
const originalEnd = consumer.originalPositionFor(node.loc.end);
if (originalEnd.line) {
node.loc.end.line = originalEnd.line;
node.loc.end.column = originalEnd.column;
}
}
});
}
);
export default pluggable(function updateReferences (module) {
traverse.cheap(module.ast, node => {
if (
node.type === "CallExpression" &&
node.callee.name === "require"
) {
const originalVal = node.arguments[0].value;
const correspondingModule = module.dependenciesByInternalRef[originalVal];
node.arguments[0].value = correspondingModule.hash;
node.arguments[0].raw = `"${correspondingModule.hash}"`;
}
});
return assign({}, module, { ast: module.ast });
});