UNPKG

@vue-async/module-loader

Version:
100 lines 4.41 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.execStyles = exports.execScript = exports.createSandbox = void 0; var warning_1 = __importDefault(require("warning")); function createSandbox() { var sandbox = { Buffer: Buffer, require: require, console: console, process: process, setTimeout: setTimeout, setInterval: setInterval, setImmediate: setImmediate, clearTimeout: clearTimeout, clearInterval: clearInterval, clearImmediate: clearImmediate, }; sandbox.global = sandbox; sandbox.exports = {}; sandbox.module = {}; return {}; } exports.createSandbox = createSandbox; function tryGetCwd(path) { var threw = true; var cwd; try { cwd = process.cwd(); threw = false; } finally { if (threw) { // getcwd(3) can fail if the current working directory has been deleted. // Fall back to the directory name of the (absolute) executable path. // It's not really correct but what are the alternatives? return path.dirname(process.execPath); } } return cwd; } /** 加载 entry 脚本 */ function execScript(entry, _proxy) { if (_proxy === void 0) { _proxy = { exports: {} }; } return new Promise(function (resolve, reject) { var http = require('http'); var request = http.get(entry, function (res) { var statusCode = res.statusCode; if (statusCode !== 200) { (0, warning_1.default)(process.env.NODE_ENV === 'production', "[moduleLoader] script had a problem to create, entry\uFF1A".concat(entry)); reject(new Error("script load error, statusCode: ".concat(statusCode))); } res.setEncoding('utf8'); res.setTimeout(10000); var rawData = ''; res.on('data', function (chunk) { rawData += chunk; }); res.on('end', function () { try { // https://github.com/nodejs/node/blob/6add5b31fcc4ae45a8603f886477c544a99e0188/lib/internal/bootstrap_node.js#L415 var exports_1 = (function evalScript(name, body) { var Module = require('module'); var path = require('path'); var cwd = tryGetCwd(path); // @ts-ignore var _module = new Module(name); _module.filename = path.join(cwd, name); _module.paths = Module._nodeModulePaths(cwd); var script = "global.__filename = ".concat(JSON.stringify(name), ";\n global.exports = exports;\n global.module = module;\n global.__dirname = __dirname;\n global.require = require;\n return require(\"vm\").runInThisContext(").concat(JSON.stringify(body), ", {\n filename: ").concat(JSON.stringify(name), ",\n displayErrors: true\n });"); _module._compile(script, "".concat(name, "-wrapper")); return _module.exports; })(entry.substr(entry.lastIndexOf('/') + 1), rawData.toString()); resolve(exports_1); } catch (err) { (0, warning_1.default)(process.env.NODE_ENV === 'production', "[moduleLoader] script had a problem to create, entry\uFF1A".concat(entry)); reject(new Error("script load error, error: ".concat(err.message))); } }); }); request.on('error', function (err) { (0, warning_1.default)(process.env.NODE_ENV === 'production', "[moduleLoader] http.request error, entry\uFF1A".concat(entry)); reject(new Error("script load error, error: ".concat(err.message))); }); }); } exports.execScript = execScript; /** 加载 styles */ function execStyles(styles, _styleFor) { // load css if (styles.length) { // do something } return Promise.resolve(); } exports.execStyles = execStyles; //# sourceMappingURL=ssr.js.map