prepack
Version:
Execute a JS bundle, serialize global state and side effects to a snapshot that can be quickly restored.
153 lines (132 loc) • 5.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _prepackNodeEnvironment = require("./prepack-node-environment");
Object.keys(_prepackNodeEnvironment).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function () {
return _prepackNodeEnvironment[key];
}
});
});
var _prepackStandalone = require("./prepack-standalone");
Object.keys(_prepackStandalone).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function () {
return _prepackStandalone[key];
}
});
});
exports.prepackStdin = prepackStdin;
exports.prepackFile = prepackFile;
exports.prepackFileSync = prepackFileSync;
var _options = require("./options");
var _errors = require("./errors.js");
var _prepackOptions = require("./prepack-options");
var _prepackNodeEnvironment2 = require("./prepack-node-environment.js");
var _prepackStandalone2 = require("./prepack-standalone.js");
require("./types.js");
var _DebugChannel = require("./debugger/server/channel/DebugChannel.js");
var _FileIOWrapper = require("./debugger/common/channel/FileIOWrapper.js");
var _fs = require("fs");
var _fs2 = _interopRequireDefault(_fs);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
/*
Prepack API functions that require Node as the execution environment for Prepack.
*/
function prepackStdin(options = _options.defaultOptions, processSerializedCode, printDiagnostics) {
let sourceMapFilename = options.inputSourceMapFilename || "";
process.stdin.setEncoding("utf8");
process.stdin.resume();
process.stdin.on("data", function (code) {
_fs2.default.readFile(sourceMapFilename, "utf8", function (mapErr, sourceMap) {
if (mapErr) {
//if no sourcemap was provided we silently ignore
if (sourceMapFilename !== "") console.warn(`No sourcemap found at ${sourceMapFilename}.`);
sourceMap = "";
}
let filename = "no-filename-specified";
let serialized;
try {
serialized = (0, _prepackStandalone2.prepackSources)([{ filePath: filename, fileContents: code, sourceMapContents: sourceMap }], options);
processSerializedCode(serialized);
if (printDiagnostics()) process.exit(1);
} catch (err) {
printDiagnostics();
if (!(err instanceof _errors.FatalError)) {
// if it is not a FatalError, it means prepack failed, and we should display the Prepack stack trace.
console.error(err.stack);
}
process.exit(1);
}
});
});
}
function prepackFile(filename, options = _options.defaultOptions, callback, fileErrorHandler) {
if (options.compatibility === "node-cli") {
(0, _prepackNodeEnvironment2.prepackNodeCLI)(filename, options, callback);
return;
}
let sourceMapFilename = options.inputSourceMapFilename || filename + ".map";
_fs2.default.readFile(filename, "utf8", function (fileErr, code) {
if (fileErr) {
if (fileErrorHandler) fileErrorHandler(fileErr);
return;
}
_fs2.default.readFile(sourceMapFilename, "utf8", function (mapErr, sourceMap) {
if (mapErr) {
console.warn(`No sourcemap found at ${sourceMapFilename}.`);
sourceMap = "";
}
let serialized;
try {
serialized = (0, _prepackStandalone2.prepackSources)([{ filePath: filename, fileContents: code, sourceMapContents: sourceMap }], options);
} catch (err) {
callback(err, null);
return;
}
callback(null, serialized);
});
});
}
function prepackFileSync(filenames, options = _options.defaultOptions) {
if (options.compatibility === "node-cli") {
if (filenames.length !== 1) {
console.error(`Does not support multiple file prepack in node-cli mode.`);
process.exit(1);
}
return (0, _prepackNodeEnvironment2.prepackNodeCLISync)(filenames[0], options);
}
const sourceFiles = filenames.map(filename => {
let code = _fs2.default.readFileSync(filename, "utf8");
let sourceMap = "";
let sourceMapFilename = options.inputSourceMapFilename || filename + ".map";
try {
sourceMap = _fs2.default.readFileSync(sourceMapFilename, "utf8");
} catch (_e) {
if (options.inputSourceMapFilename) console.warn(`No sourcemap found at ${sourceMapFilename}.`);
}
return { filePath: filename, fileContents: code, sourceMapContents: sourceMap };
});
let debugChannel;
if (options.debugInFilePath && options.debugOutFilePath) {
let debugOptions = (0, _prepackOptions.getDebuggerOptions)(options);
let ioWrapper = new _FileIOWrapper.FileIOWrapper(false, debugOptions.inFilePath, debugOptions.outFilePath);
debugChannel = new _DebugChannel.DebugChannel(ioWrapper);
}
return (0, _prepackStandalone2.prepackSources)(sourceFiles, options, debugChannel);
}
//# sourceMappingURL=prepack-node.js.map