Skip to content

Commit

Permalink
fix: base #159
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangHongEn committed Oct 29, 2024
1 parent 19daa1f commit ebcfbb1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/vite-vite/vite-host/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default defineConfig({
remote2: 'mfapp02@https://unpkg.com/mf-app-02/dist/remoteEntry.js',
remote3:
'remote1@https://unpkg.com/[email protected]/dist/mf-manifest.json',
'@namespace/viteViteRemote': 'http://localhost:5176/mf-manifest.json',
'@namespace/viteViteRemote': 'http://localhost:5176/testbase/mf-manifest.json',
},
filename: 'remoteEntry-[hash].js',
manifest: true,
Expand Down
2 changes: 1 addition & 1 deletion examples/vite-vite/vite-remote/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default defineConfig({
preview: {
port: 5176,
},
base: 'http://localhost:5176',
base: 'http://localhost:5176/testbase',
plugins: [
react({ jsxImportSource: '@emotion/react' }),
federation({
Expand Down
10 changes: 7 additions & 3 deletions src/plugins/pluginAddEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const addEntry = ({
fileName,
inject = 'entry',
}: AddEntryOptions): Plugin[] => {
const devEntryPath = entryPath.startsWith('virtual:mf') ? '/@id/' + entryPath : entryPath;
let devEntryPath = entryPath.startsWith('virtual:mf') ? '@id/' + entryPath : entryPath;
let entryFiles: string[] = [];
let htmlFilePath: string;
let _command: string;
Expand All @@ -29,17 +29,21 @@ const addEntry = ({
config(config, { command }) {
_command = command;
},
configResolved(config) {
viteConfig = config;
devEntryPath = config.base + devEntryPath.replace(/^\//, '');
},
configureServer(server) {
server.httpServer?.once?.('listening', () => {
const { port } = server.config.server;
fetch(path.join(`http://localhost:${port}`, `${devEntryPath}`)).catch((e) => {});
fetch(`http://localhost:${port}${devEntryPath}`).catch((e) => { });
});
server.middlewares.use((req, res, next) => {
if (!fileName) {
next();
return;
}
if (req.url && req.url.startsWith(fileName.replace(/^\/?/, '/'))) {
if (req.url && req.url.startsWith((viteConfig.base + fileName).replace(/^\/?/, '/'))) {
req.url = devEntryPath;
}
next();
Expand Down
12 changes: 8 additions & 4 deletions src/plugins/pluginMFManifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,21 @@ const Manifest = (): Plugin[] => {
let remoteEntryFile: string;
let publicPath: string;
let _command: string;
let viteConfig: any;
return [
{
name: 'module-federation-manifest',
apply: 'serve',
configResolved(config) {
viteConfig = config;
},
configureServer(server) {
server.middlewares.use((req, res, next) => {
if (!mfManifestName) {
next();
return;
}
if (req.url === mfManifestName.replace(/^\/?/, '/')) {
if (req.url === (viteConfig.base + mfManifestName).replace(/^\/?/, '/')) {
res.setHeader('Content-Type', 'application/json');
res.setHeader('Access-Control-Allow-Origin', '*');
res.end(
Expand Down Expand Up @@ -93,11 +97,11 @@ const Manifest = (): Plugin[] => {
'.tsx',
'.json',
];
publicPath = config.base ? config.base.replace(/\/?$/, '/') : 'auto';
let base = config.base;
if (_command === 'serve') {
const origin = config.server.origin;
publicPath = origin ? origin.replace(/\/?$/, '/') : 'auto';
base = (config.server.origin || '') + config.base;
}
publicPath = base ? base.replace(/\/?$/, '/') : 'auto';
},
async generateBundle(options, bundle) {
if (!mfManifestName) return;
Expand Down

0 comments on commit ebcfbb1

Please sign in to comment.