prepack
Version:
Execute a JS bundle, serialize global state and side effects to a snapshot that can be quickly restored.
69 lines (57 loc) • 2.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = function (realm, code, filename, sourceType = "script", startLine = 1) {
try {
let plugins = [];
if (realm.react.enabled) {
plugins.push("jsx");
}
if (realm.stripFlow) {
plugins.push("flow");
}
let ast = (0, _babylon.parse)(code, { filename, sourceType, startLine, plugins });
(0, _traverseFast2.default)(ast, node => {
(0, _invariant2.default)(node.loc);
node.loc.source = filename;
return false;
});
return ast;
} catch (e) {
if (e instanceof SyntaxError) {
// Babel reports all errors as syntax errors, even if a ReferenceError should be thrown.
// What we do here is a totally robust way to address that issue.
let referenceErrors = ["Invalid left-hand side in postfix operation", "Invalid left-hand side in prefix operation", "Invalid left-hand side in assignment expression"];
let error;
if (referenceErrors.some(msg => e.message.indexOf(msg) >= 0)) {
error = (0, _construct.Construct)(realm, realm.intrinsics.ReferenceError, [new _index.StringValue(realm, e.message)]);
} else {
error = (0, _construct.Construct)(realm, realm.intrinsics.SyntaxError, [new _index.StringValue(realm, e.message)]);
}
// These constructors are currently guaranteed to produce an object with
// built-in error data. Append location information about the syntax error
// and the source code to it so that we can use it to print nicer errors.
(0, _invariant2.default)(error.$ErrorData);
error.$ErrorData.locationData = {
filename: filename,
sourceCode: code,
loc: e.loc,
stackDecorated: false
};
throw new _completions.ThrowCompletion(error, e.loc);
} else {
throw e;
}
}
};
var _invariant = require("../invariant.js");
var _invariant2 = _interopRequireDefault(_invariant);
var _completions = require("../completions.js");
var _index = require("../values/index.js");
var _construct = require("../methods/construct.js");
var _traverseFast = require("../utils/traverse-fast.js");
var _traverseFast2 = _interopRequireDefault(_traverseFast);
var _babylon = require("babylon");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
//# sourceMappingURL=parse.js.map