|
| 1 | +import { Plugin } from 'vite'; |
| 2 | +import { findRemoteEntryFile } from '../utils/bundleHelpers'; |
| 3 | +import { warn } from '../utils/logUtils'; |
| 4 | +import { getNormalizeModuleFederationOptions } from '../utils/normalizeModuleFederationOptions'; |
| 5 | + |
| 6 | +const VarRemoteEntry = (): Plugin[] => { |
| 7 | + const mfOptions = getNormalizeModuleFederationOptions(); |
| 8 | + const { name, varFilename, filename } = mfOptions; |
| 9 | + |
| 10 | + let viteConfig: any; |
| 11 | + |
| 12 | + return [ |
| 13 | + { |
| 14 | + name: 'module-federation-var-remote-entry', |
| 15 | + apply: 'serve', |
| 16 | + /** |
| 17 | + * Stores resolved Vite config for later use |
| 18 | + */ |
| 19 | + /** |
| 20 | + * Finalizes configuration after all plugins are resolved |
| 21 | + * @param config - Fully resolved Vite config |
| 22 | + */ |
| 23 | + configResolved(config) { |
| 24 | + viteConfig = config; |
| 25 | + }, |
| 26 | + /** |
| 27 | + * Configures dev server middleware to handle varRemoteEntry requests |
| 28 | + * @param server - Vite dev server instance |
| 29 | + */ |
| 30 | + configureServer(server) { |
| 31 | + server.middlewares.use((req, res, next) => { |
| 32 | + if (!varFilename) { |
| 33 | + next(); |
| 34 | + return; |
| 35 | + } |
| 36 | + if ( |
| 37 | + req.url?.replace(/\?.*/, '') === (viteConfig.base + varFilename).replace(/^\/?/, '/') |
| 38 | + ) { |
| 39 | + res.setHeader('Content-Type', 'text/javascript'); |
| 40 | + res.setHeader('Access-Control-Allow-Origin', '*'); |
| 41 | + console.log({ filename }); |
| 42 | + res.end(generateVarRemoteEntry(filename)); |
| 43 | + } else { |
| 44 | + next(); |
| 45 | + } |
| 46 | + }); |
| 47 | + }, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: 'module-federation-var-remote-entry', |
| 51 | + enforce: 'post', |
| 52 | + /** |
| 53 | + * Initial plugin configuration |
| 54 | + * @param config - Vite config object |
| 55 | + * @param command - Current Vite command (serve/build) |
| 56 | + */ |
| 57 | + config(config, { command }) { |
| 58 | + if (!config.build) config.build = {}; |
| 59 | + }, |
| 60 | + /** |
| 61 | + * Generates the module federation "var" remote entry file |
| 62 | + * @param options - Rollup output options |
| 63 | + * @param bundle - Generated bundle assets |
| 64 | + */ |
| 65 | + async generateBundle(options, bundle) { |
| 66 | + if (!varFilename) return; |
| 67 | + |
| 68 | + const isValidName = isValidVarName(name); |
| 69 | + |
| 70 | + if (!isValidName) { |
| 71 | + warn( |
| 72 | + `Provided remote name "${name}" is not valid for "var" remoteEntry type, thus it's placed in globalThis['${name}'].\nIt may cause problems, so you would better want to use valid var name (see https://www.w3schools.com/js/js_variables.asp).` |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + const remoteEntryFile = findRemoteEntryFile(mfOptions.filename, bundle); |
| 77 | + |
| 78 | + if (!remoteEntryFile) |
| 79 | + throw new Error( |
| 80 | + `Couldn't find a remoteEntry chunk file for ${mfOptions.filename}, can't generate varRemoteEntry file` |
| 81 | + ); |
| 82 | + |
| 83 | + this.emitFile({ |
| 84 | + type: 'asset', |
| 85 | + fileName: varFilename, |
| 86 | + source: generateVarRemoteEntry(remoteEntryFile), |
| 87 | + }); |
| 88 | + }, |
| 89 | + }, |
| 90 | + ]; |
| 91 | + |
| 92 | + function isValidVarName(name: string) { |
| 93 | + return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Generates the final "var" remote entry file |
| 98 | + * @param remoteEntryFile - Path to esm remote entry file |
| 99 | + * @returns Complete "var" remoteEntry.js file source |
| 100 | + */ |
| 101 | + function generateVarRemoteEntry(remoteEntryFile: string) { |
| 102 | + const options = getNormalizeModuleFederationOptions(); |
| 103 | + |
| 104 | + const { name, varFilename } = options; |
| 105 | + |
| 106 | + const isValidName = isValidVarName(name); |
| 107 | + |
| 108 | + // @TODO: implement publicPath/getPublicPath support |
| 109 | + return ` |
| 110 | + ${isValidName ? `var ${name};` : ''} |
| 111 | + ${isValidName ? name : `globalThis['${name}']`} = (function () { |
| 112 | + function getScriptUrl() { |
| 113 | + const currentScript = document.currentScript; |
| 114 | + if (!currentScript) { |
| 115 | + console.error("[VarRemoteEntry] ${varFilename} script should be called from sync <script> tag (document.currentScript is undefined)") |
| 116 | + return '/'; |
| 117 | + } |
| 118 | + return document.currentScript.src.replace(/\\/[^/]*$/, '/'); |
| 119 | + } |
| 120 | +
|
| 121 | + const entry = getScriptUrl() + '${remoteEntryFile}'; |
| 122 | +
|
| 123 | + return { |
| 124 | + get: (...args) => import(entry).then(m => m.get(...args)), |
| 125 | + init: (...args) => import(entry).then(m => m.init(...args)), |
| 126 | + }; |
| 127 | + })(); |
| 128 | + `; |
| 129 | + } |
| 130 | +}; |
| 131 | + |
| 132 | +export default VarRemoteEntry; |
0 commit comments