UNPKG

@vue-async/module-loader

Version:
91 lines 3.97 kB
import warning from 'warning'; export 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 {}; } 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 脚本 */ export 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) { warning(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) { warning(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) { warning(process.env.NODE_ENV === 'production', "[moduleLoader] http.request error, entry\uFF1A".concat(entry)); reject(new Error("script load error, error: ".concat(err.message))); }); }); } /** 加载 styles */ export function execStyles(styles, _styleFor) { // load css if (styles.length) { // do something } return Promise.resolve(); } //# sourceMappingURL=ssr.js.map