UNPKG

5.59 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6var _exportNames = {
7 prepackStdin: true,
8 prepackFile: true,
9 prepackFileSync: true
10};
11exports.prepackStdin = prepackStdin;
12exports.prepackFile = prepackFile;
13exports.prepackFileSync = prepackFileSync;
14
15var _options = require("./options");
16
17var _errors = require("./errors.js");
18
19var _prepackStandalone = require("./prepack-standalone.js");
20
21var _DebugChannel = require("./debugger/server/channel/DebugChannel.js");
22
23var _FileIOWrapper = require("./debugger/common/channel/FileIOWrapper.js");
24
25var _statistics = require("./serializer/statistics.js");
26
27var _fs = _interopRequireDefault(require("fs"));
28
29var _prepackStandalone2 = require("./prepack-standalone");
30
31Object.keys(_prepackStandalone2).forEach(function (key) {
32 if (key === "default" || key === "__esModule") return;
33 if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
34 Object.defineProperty(exports, key, {
35 enumerable: true,
36 get: function () {
37 return _prepackStandalone2[key];
38 }
39 });
40});
41
42function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
43
44/**
45 * Copyright (c) 2017-present, Facebook, Inc.
46 * All rights reserved.
47 *
48 * This source code is licensed under the BSD-style license found in the
49 * LICENSE file in the root directory of this source tree. An additional grant
50 * of patent rights can be found in the PATENTS file in the same directory.
51 */
52
53/*
54 Prepack API functions that require Node as the execution environment for Prepack.
55 */
56function createStatistics(options) {
57 let gc = global.gc; // eslint-disable-line no-undef
58
59 return options.profile !== undefined ? new _statistics.SerializerStatistics(() => Date.now(), () => {
60 if (gc) gc();
61 return process.memoryUsage().heapUsed;
62 }, !!gc) : new _statistics.SerializerStatistics();
63}
64
65function prepackStdin(options = _options.defaultOptions, processSerializedCode, printDiagnostics) {
66 let sourceMapFilename = options.inputSourceMapFilename || "";
67 process.stdin.setEncoding("utf8");
68 process.stdin.resume();
69 process.stdin.on("data", function (code) {
70 _fs.default.readFile(sourceMapFilename, "utf8", function (mapErr, sourceMap = "") {
71 if (mapErr) {
72 //if no sourcemap was provided we silently ignore
73 if (sourceMapFilename !== "") console.warn(`No sourcemap found at ${sourceMapFilename}.`);
74 }
75
76 let filename = "no-filename-specified";
77 let serialized;
78 let success;
79
80 try {
81 serialized = (0, _prepackStandalone.prepackSources)([{
82 filePath: filename,
83 fileContents: code,
84 sourceMapContents: sourceMap
85 }], options, undefined, createStatistics(options));
86 processSerializedCode(serialized);
87 success = printDiagnostics(false);
88 } catch (err) {
89 printDiagnostics(err instanceof _errors.FatalError);
90
91 if (!(err instanceof _errors.FatalError)) {
92 // if it is not a FatalError, it means prepack failed, and we should display the Prepack stack trace.
93 console.error(err.stack);
94 }
95
96 success = false;
97 }
98
99 if (!success) process.exit(1);
100 });
101 });
102}
103
104function prepackFile(filename, options = _options.defaultOptions, callback, fileErrorHandler) {
105 let sourceMapFilename = options.inputSourceMapFilename !== undefined ? options.inputSourceMapFilename : filename + ".map";
106
107 _fs.default.readFile(filename, "utf8", function (fileErr, code) {
108 if (fileErr) {
109 if (fileErrorHandler) fileErrorHandler(fileErr);
110 return;
111 }
112
113 _fs.default.readFile(sourceMapFilename, "utf8", function (mapErr, _sourceMap) {
114 let sourceMap = _sourceMap;
115
116 if (mapErr) {
117 console.warn(`No sourcemap found at ${sourceMapFilename}.`);
118 sourceMap = "";
119 }
120
121 let serialized;
122
123 try {
124 serialized = (0, _prepackStandalone.prepackSources)([{
125 filePath: filename,
126 fileContents: code,
127 sourceMapContents: sourceMap
128 }], options, undefined, createStatistics(options));
129 } catch (err) {
130 callback(err, null);
131 return;
132 }
133
134 callback(null, serialized);
135 });
136 });
137}
138
139function prepackFileSync(filenames, options = _options.defaultOptions) {
140 const sourceFiles = filenames.map(filename => {
141 let code = _fs.default.readFileSync(filename, "utf8");
142
143 let sourceMap = "";
144 let sourceMapFilename = options.inputSourceMapFilename !== undefined ? options.inputSourceMapFilename : filename + ".map";
145
146 try {
147 sourceMap = _fs.default.readFileSync(sourceMapFilename, "utf8");
148 } catch (_e) {
149 if (options.inputSourceMapFilename !== undefined) console.warn(`No sourcemap found at ${sourceMapFilename}.`);
150 }
151
152 return {
153 filePath: filename,
154 fileContents: code,
155 sourceMapContents: sourceMap
156 };
157 }); // The existence of debug[In/Out]FilePath represents the desire to use the debugger.
158
159 if (options.debugInFilePath !== undefined && options.debugOutFilePath !== undefined) {
160 if (options.debuggerConfigArgs === undefined) options.debuggerConfigArgs = {};
161 let ioWrapper = new _FileIOWrapper.FileIOWrapper(false, options.debugInFilePath, options.debugOutFilePath);
162 options.debuggerConfigArgs.debugChannel = new _DebugChannel.DebugChannel(ioWrapper);
163 options.debuggerConfigArgs.sourcemaps = sourceFiles;
164 }
165
166 return (0, _prepackStandalone.prepackSources)(sourceFiles, options, options.debuggerConfigArgs, createStatistics(options));
167}
168//# sourceMappingURL=prepack-node.js.map
\No newline at end of file