|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const mfConfig = require('./modulefederation.config'); |
| 4 | +const fetch = require('node-fetch'); |
| 5 | +const {init, loadRemote} = require('@module-federation/runtime') |
| 6 | + |
| 7 | + |
| 8 | +const sharedConfig = {}; |
| 9 | +for (const [packageName, packageConfig] of Object.entries(mfConfig.shared)) { |
| 10 | + let version = false; |
| 11 | + try { |
| 12 | + version = require(path.join(packageName, 'package.json')).version; |
| 13 | + } catch (e) { |
| 14 | + // Handle error if needed |
| 15 | + } |
| 16 | + if (typeof packageConfig === 'string') { |
| 17 | + sharedConfig[packageName] = { |
| 18 | + version, |
| 19 | + lib: () => require(packageName), |
| 20 | + }; |
| 21 | + } else { |
| 22 | + sharedConfig[packageName] = { |
| 23 | + version, |
| 24 | + ...packageConfig, |
| 25 | + lib: () => require(packageName), |
| 26 | + }; |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +module.exports = async () => { |
| 34 | + let remotes = []; |
| 35 | + |
| 36 | + const harnessPath = path.resolve(__dirname, 'node_modules', 'federation-test'); |
| 37 | + |
| 38 | + let harnessData = []; |
| 39 | + for (const [remote, entry] of Object.entries(mfConfig.remotes)) { |
| 40 | + const [name, url] = entry.split('@'); |
| 41 | + const manifest = url.replace('remoteEntry.js', 'mf-manifest.json'); |
| 42 | + const response = await fetch(manifest); |
| 43 | + const data = await response.json(); |
| 44 | + |
| 45 | + const parsedPath = new URL(url).origin; |
| 46 | + const subPath = data.metaData.remoteEntry.path; |
| 47 | + |
| 48 | + const buildUrl = (parsedPath, subPath, file) => { |
| 49 | + return subPath ? `${parsedPath}/${subPath}/${file}` : `${parsedPath}/${file}`; |
| 50 | + }; |
| 51 | + |
| 52 | + remotes.push(buildUrl(parsedPath, subPath, data.metaData.remoteEntry.name)); |
| 53 | + |
| 54 | + const jsFiles = [ |
| 55 | + ...data.shared.flatMap(shared => [...shared.assets.js.sync, ...shared.assets.js.async].map(file => buildUrl(parsedPath, subPath, file))), |
| 56 | + ...data.exposes.flatMap(expose => [...expose.assets.js.sync, ...expose.assets.js.async].map(file => buildUrl(parsedPath, subPath, file))) |
| 57 | + ]; |
| 58 | + |
| 59 | + const cssFiles = [ |
| 60 | + ...data.shared.flatMap(shared => [...shared.assets.css.sync, ...shared.assets.css.async].map(file => buildUrl(parsedPath, subPath, file))), |
| 61 | + ...data.exposes.flatMap(expose => [...expose.assets.css.sync, ...expose.assets.css.async].map(file => buildUrl(parsedPath, subPath, file))) |
| 62 | + ]; |
| 63 | + |
| 64 | + remotes.push(...jsFiles, ...cssFiles); |
| 65 | + |
| 66 | + const fakePackagePath = path.resolve(__dirname, 'node_modules', data.id); |
| 67 | + const fakePackageJsonPath = path.join(fakePackagePath, 'package.json'); |
| 68 | + const fakePackageIndexPath = path.join(fakePackagePath, 'index.js'); |
| 69 | + |
| 70 | + if (!fs.existsSync(fakePackagePath)) { |
| 71 | + fs.mkdirSync(fakePackagePath, { recursive: true }); |
| 72 | + } |
| 73 | + |
| 74 | + const exportsContent = data.exposes.reduce((exportsObj, expose) => { |
| 75 | + let exposeName = expose.name; |
| 76 | + if (!exposeName.endsWith('.js')) { |
| 77 | + exposeName += '.js'; |
| 78 | + } |
| 79 | + exportsObj[expose.path] = './virtual' + exposeName; |
| 80 | + const resolvePath = path.join(fakePackagePath, './virtual' + exposeName); |
| 81 | + |
| 82 | + harnessData.push(resolvePath); |
| 83 | + |
| 84 | + fs.writeFileSync(resolvePath, ` |
| 85 | + const container = require('./remoteEntry.js')[${JSON.stringify(data.id)}]; |
| 86 | + const target = {}; |
| 87 | +
|
| 88 | + let e; |
| 89 | + const cx = container.get(${JSON.stringify(expose.path)}).then((m) => { |
| 90 | + e = m(); |
| 91 | + Object.assign(target, e); |
| 92 | + }); |
| 93 | +
|
| 94 | + |
| 95 | + module.exports = new Proxy(target, { |
| 96 | + get(target, prop) { |
| 97 | + if(prop === 'setupTest') return cx; |
| 98 | + if (!e) { |
| 99 | + return cx; |
| 100 | + } else if (prop in e) { |
| 101 | + return e[prop]; |
| 102 | + } else { |
| 103 | + return e; |
| 104 | + } |
| 105 | + } |
| 106 | + }); |
| 107 | + `, 'utf-8'); |
| 108 | + return exportsObj; |
| 109 | + }, {}); |
| 110 | + |
| 111 | + const packageJsonContent = { |
| 112 | + name: data.id, |
| 113 | + version: '1.0.0', |
| 114 | + // main: 'index.js', |
| 115 | + exports: exportsContent |
| 116 | + }; |
| 117 | + const indexJsContent = ` |
| 118 | + module.exports = () => 'Hello from fake package!'; |
| 119 | + `; |
| 120 | + |
| 121 | + fs.writeFileSync(fakePackageJsonPath, JSON.stringify(packageJsonContent, null, 2)); |
| 122 | + fs.writeFileSync(fakePackageIndexPath, indexJsContent); |
| 123 | + |
| 124 | + for (const fileUrl of remotes) { |
| 125 | + const fileName = path.basename(fileUrl); |
| 126 | + const filePath = path.join(fakePackagePath, fileName); |
| 127 | + const fileResponse = await fetch(fileUrl); |
| 128 | + const fileData = await fileResponse.buffer(); |
| 129 | + fs.writeFileSync(filePath, fileData); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + if (!fs.existsSync(harnessPath)) { |
| 134 | + fs.mkdirSync(harnessPath, { recursive: true }); |
| 135 | + } |
| 136 | + |
| 137 | + fs.writeFileSync('node_modules/federation-test/index.js', `module.exports = Promise.all(${JSON.stringify(harnessData)}.map((p) => require(p).setupTest))`, 'utf-8'); |
| 138 | + fs.writeFileSync('node_modules/federation-test/package.json', '{"name": "federation-test", "main": "./index.js"}', 'utf-8'); |
| 139 | + |
| 140 | + // init({ |
| 141 | + // name: 'host', |
| 142 | + // remotes: [{ |
| 143 | + // name: "remote", |
| 144 | + // entry: "remote/remoteEntry.js", |
| 145 | + // alias: "remote", |
| 146 | + // type: 'commonjs' |
| 147 | + // }], |
| 148 | + // shared: sharedConfig, |
| 149 | + // }); |
| 150 | + |
| 151 | + // await loadRemote('remote/Button').then(console.log) |
| 152 | +}; |
| 153 | + |
0 commit comments