|
| 1 | +import { |
| 2 | + Compilation, |
| 3 | + Compiler, |
| 4 | + DefinePlugin, |
| 5 | + RspackPluginInstance, |
| 6 | +} from '@rspack/core'; |
| 7 | +import * as pc from 'picocolors'; |
| 8 | +import { |
| 9 | + logger, |
| 10 | + readCachedProjectGraph, |
| 11 | + readProjectsConfigurationFromProjectGraph, |
| 12 | + workspaceRoot, |
| 13 | +} from '@nx/devkit'; |
| 14 | +import { ModuleFederationConfig } from '../../../utils/models'; |
| 15 | +import { extname, join } from 'path'; |
| 16 | +import { existsSync } from 'fs'; |
| 17 | +import { |
| 18 | + buildStaticRemotes, |
| 19 | + getDynamicMfManifestFile, |
| 20 | + getRemotes, |
| 21 | + getStaticRemotes, |
| 22 | + parseRemotesConfig, |
| 23 | + startRemoteProxies, |
| 24 | + startStaticRemotesFileServer, |
| 25 | +} from '../../utils'; |
| 26 | +import { NxModuleFederationDevServerConfig } from '../../models'; |
| 27 | + |
| 28 | +const PLUGIN_NAME = 'NxModuleFederationDevServerPlugin'; |
| 29 | + |
| 30 | +export class NxModuleFederationDevServerPlugin implements RspackPluginInstance { |
| 31 | + private nxBin = require.resolve('nx/bin/nx'); |
| 32 | + |
| 33 | + constructor( |
| 34 | + private _options: { |
| 35 | + config: ModuleFederationConfig; |
| 36 | + devServerConfig?: NxModuleFederationDevServerConfig; |
| 37 | + } |
| 38 | + ) { |
| 39 | + this._options.devServerConfig ??= { |
| 40 | + host: 'localhost', |
| 41 | + }; |
| 42 | + } |
| 43 | + |
| 44 | + apply(compiler: Compiler) { |
| 45 | + const isDevServer = process.env['WEBPACK_SERVE']; |
| 46 | + if (!isDevServer) { |
| 47 | + return; |
| 48 | + } |
| 49 | + compiler.hooks.watchRun.tapAsync( |
| 50 | + PLUGIN_NAME, |
| 51 | + async (compiler, callback) => { |
| 52 | + compiler.hooks.beforeCompile.tapAsync( |
| 53 | + PLUGIN_NAME, |
| 54 | + async (params, callback) => { |
| 55 | + const staticRemotesConfig = await this.setup(); |
| 56 | + |
| 57 | + logger.info( |
| 58 | + `NX Starting module federation dev-server for ${pc.bold( |
| 59 | + this._options.config.name |
| 60 | + )} with ${Object.keys(staticRemotesConfig).length} remotes` |
| 61 | + ); |
| 62 | + |
| 63 | + const mappedLocationOfRemotes = await buildStaticRemotes( |
| 64 | + staticRemotesConfig, |
| 65 | + this._options.devServerConfig, |
| 66 | + this.nxBin |
| 67 | + ); |
| 68 | + startStaticRemotesFileServer( |
| 69 | + staticRemotesConfig, |
| 70 | + workspaceRoot, |
| 71 | + this._options.devServerConfig.staticRemotesPort |
| 72 | + ); |
| 73 | + startRemoteProxies(staticRemotesConfig, mappedLocationOfRemotes, { |
| 74 | + pathToCert: this._options.devServerConfig.sslCert, |
| 75 | + pathToKey: this._options.devServerConfig.sslCert, |
| 76 | + }); |
| 77 | + |
| 78 | + new DefinePlugin({ |
| 79 | + 'process.env.NX_MF_DEV_REMOTES': process.env.NX_MF_DEV_REMOTES, |
| 80 | + }).apply(compiler); |
| 81 | + |
| 82 | + callback(); |
| 83 | + } |
| 84 | + ); |
| 85 | + callback(); |
| 86 | + } |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + private async setup() { |
| 91 | + const projectGraph = readCachedProjectGraph(); |
| 92 | + const { projects: workspaceProjects } = |
| 93 | + readProjectsConfigurationFromProjectGraph(projectGraph); |
| 94 | + const project = workspaceProjects[this._options.config.name]; |
| 95 | + if (!this._options.devServerConfig.pathToManifestFile) { |
| 96 | + this._options.devServerConfig.pathToManifestFile = |
| 97 | + getDynamicMfManifestFile(project, workspaceRoot); |
| 98 | + } else { |
| 99 | + const userPathToManifestFile = join( |
| 100 | + workspaceRoot, |
| 101 | + this._options.devServerConfig.pathToManifestFile |
| 102 | + ); |
| 103 | + if (!existsSync(userPathToManifestFile)) { |
| 104 | + throw new Error( |
| 105 | + `The provided Module Federation manifest file path does not exist. Please check the file exists at "${userPathToManifestFile}".` |
| 106 | + ); |
| 107 | + } else if ( |
| 108 | + extname(this._options.devServerConfig.pathToManifestFile) !== '.json' |
| 109 | + ) { |
| 110 | + throw new Error( |
| 111 | + `The Module Federation manifest file must be a JSON. Please ensure the file at ${userPathToManifestFile} is a JSON.` |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + this._options.devServerConfig.pathToManifestFile = userPathToManifestFile; |
| 116 | + } |
| 117 | + |
| 118 | + const { remotes, staticRemotePort } = getRemotes( |
| 119 | + this._options.config, |
| 120 | + projectGraph, |
| 121 | + this._options.devServerConfig.pathToManifestFile |
| 122 | + ); |
| 123 | + this._options.devServerConfig.staticRemotesPort ??= staticRemotePort; |
| 124 | + |
| 125 | + const remotesConfig = parseRemotesConfig( |
| 126 | + remotes, |
| 127 | + workspaceRoot, |
| 128 | + projectGraph |
| 129 | + ); |
| 130 | + const staticRemotesConfig = await getStaticRemotes( |
| 131 | + remotesConfig.config ?? {}, |
| 132 | + this._options.devServerConfig?.devRemoteFindOptions, |
| 133 | + this._options.devServerConfig?.host |
| 134 | + ); |
| 135 | + const devRemotes = remotes.filter((r) => !staticRemotesConfig[r]); |
| 136 | + process.env.NX_MF_DEV_REMOTES = JSON.stringify([ |
| 137 | + ...(devRemotes.length > 0 ? devRemotes : []), |
| 138 | + project.name, |
| 139 | + ]); |
| 140 | + return staticRemotesConfig ?? {}; |
| 141 | + } |
| 142 | +} |
0 commit comments