Skip to content

Instantly share code, notes, and snippets.

@ScriptedAlchemy
Created February 16, 2023 21:01
Show Gist options
  • Save ScriptedAlchemy/4399049a30dd06da4166d67eec416906 to your computer and use it in GitHub Desktop.
Save ScriptedAlchemy/4399049a30dd06da4166d67eec416906 to your computer and use it in GitHub Desktop.
Async-node + async fetch chunk loading
/******/ // load script equivalent for server side
/******/ __webpack_require__.l = function(url,callback,chunkId) {
/******/ if(!global.__remote_scope__) {
/******/ // create a global scope for container, similar to how remotes are set on window in the browser
/******/ global.__remote_scope__ = {
/******/ _config: {},
/******/ }
/******/ }
/******/
/******/ function executeLoad(url, callback, name) {
/******/ if(!name) {
/******/ throw new Error('__webpack_require__.l name is required for ' + url);
/******/ }
/******/
/******/ if (typeof global.__remote_scope__[name] !== 'undefined') return callback(global.__remote_scope__[name]);
/******/
/******/ const vm = require('vm');
/******/ (global.webpackChunkLoad || global.fetch || require("node-fetch"))(url).then(function (res) {
/******/ return res.text();
/******/ }).then(function (scriptContent) {
/******/ console.log('in script loading promise for', name);
/******/ try {
/******/ // TODO: remove conditional in v7, this is to prevent breaking change between v6.0.x and v6.1.x
/******/ const vmContext = typeof URLSearchParams === 'undefined' ?{exports, require, module, global, __filename, __dirname, URL, URLSearchParams, console, process,Buffer, ...global, remoteEntryName: name}:
/******/ {exports, require, module, global, __filename, __dirname, URL, URLSearchParams, console, process,Buffer, ...global, remoteEntryName: name};
/******/ const remote = vm.runInNewContext(scriptContent + '\nmodule.exports', vmContext, {filename: 'node-federation-loader-' + name + '.vm'});
/******/ const foundContainer = remote[name] || remote
/******/
/******/ if(!global.__remote_scope__[name]) {
/******/ global.__remote_scope__[name] = {
/******/ get: foundContainer.get,
/******/ init: function(initScope, initToken) {
/******/ try {
/******/ foundContainer.init(initScope, initToken)
/******/ } catch (e) {
/******/ return 1
/******/ }
/******/ }
/******/ };
/******/ global.__remote_scope__._config[name] = url;
/******/ }
/******/ callback(global.__remote_scope__[name]);
/******/ } catch (e) {
/******/ console.error('executeLoad hit catch block');
/******/ e.target = {src: url};
/******/ callback(e);
/******/ }
/******/ }).catch((e) => {
/******/ e.target = {src: url};
/******/ callback(e);
/******/ });
/******/ }
/******/ executeLoad(url,callback,chunkId)
/******/ }
/******/ // ReadFile + VM.run chunk loading for javascript
/******/ __webpack_require__.f.readFileVm = function(chunkId, promises) {
/******/
/******/ var installedChunkData = installedChunks[chunkId];
/******/ if(installedChunkData !== 0) { // 0 means "already installed".
/******/ // array of [resolve, reject, promise] means "currently loading"
/******/ if(installedChunkData) {
/******/ promises.push(installedChunkData[2]);
/******/ } else {
/******/ if(!/^webpack(_container_remote_(home_(SharedNav|menu)|checkout_menu)|\-runtime)$/.test(chunkId)) {
/******/ // load the chunk and return promise to it
/******/ var promise = new Promise(async function(resolve, reject) {
/******/ installedChunkData = installedChunks[chunkId] = [resolve, reject];
/******/ var filename = require('path').join(__dirname, "" + __webpack_require__.u(chunkId));
/******/ var fs = require('fs');
// if its on disk, do what we normally do with async-node target
/******/ if(fs.existsSync(filename)) {
/******/ fs.readFile(filename, 'utf-8', function(err, content) {
/******/ if(err) return reject(err);
/******/ var chunk = {};
/******/ require('vm').runInThisContext('(function(exports, require, __dirname, __filename) {' + content + '\n})', filename)(chunk, require, require('path').dirname(filename), filename);
/******/ installChunk(chunk);
/******/ });
/******/ } else {
/******/ // otherwise, load it from the network and pass back to VM
/******/ function loadScript(url, cb, chunkID) {
/******/ if (global.webpackChunkLoad) {
/******/ global.webpackChunkLoad(url).then(function (resp) {
/******/ return resp.text();
/******/ }).then(function (rawData) {
/******/ cb(null, rawData);
/******/ }).catch(function (err) {
/******/ console.error('Federated Chunk load failed', error);
/******/ return cb(error)
/******/ });
/******/ } else {
/******/ //TODO https support
/******/ let request = (url.startsWith('https') ? require('https') : require('http')).get(url, function (resp) {
/******/ if (resp.statusCode === 200) {
/******/ let rawData = '';
/******/ resp.setEncoding('utf8');
/******/ resp.on('data', chunk => {
/******/ rawData += chunk;
/******/ });
/******/ resp.on('end', () => {
/******/ cb(null, rawData);
/******/ });
/******/ } else {
/******/ cb(resp);
/******/ }
/******/ });
/******/ request.on('error', error => {
/******/ console.error('Federated Chunk load failed', error);
/******/ return cb(error)
/******/ });
/******/ }
/******/ }
/******/
/******/
/******/ var remotes = {};
/******/ Object.assign(global.__remote_scope__._config, remotes)
/******/ const remoteRegistry = global.__remote_scope__._config
/******/ var requestedRemote = remoteRegistry["shop"]
/******/
/******/ if(typeof requestedRemote === 'function'){
/******/ requestedRemote = await requestedRemote()
/******/ }
/******/
/******/ var scriptUrl = new URL(requestedRemote);
/******/
/******/ var chunkName = __webpack_require__.u(chunkId);
/******/
/******/ var fileToReplace = require('path').basename(scriptUrl.pathname);
/******/ scriptUrl.pathname = scriptUrl.pathname.replace(fileToReplace, chunkName);
/******/
/******/ loadScript(scriptUrl.toString(), function(err, content) {
/******/
/******/ if(err) {console.error('error loading remote chunk', scriptUrl.toString(),'got',content,'with error', err); return reject(err);}
/******/ var chunk = {};
/******/ try {
/******/ require('vm').runInThisContext('(function(exports, require, __dirname, __filename) {' + content + '\n})', filename)(chunk, require, require('path').dirname(filename), filename);
/******/ } catch (e) {
/******/ console.error('runInThisContext threw', e)
/******/ }
/******/ installChunk(chunk);
/******/ }, chunkId);
/******/ }
/******/ });
/******/ promises.push(installedChunkData[2] = promise);
/******/ } else installedChunks[chunkId] = 0;
/******/ }
/******/ }
/******/ };
/******/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment