Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When I use vite as host to realize "shared-directory" case of "https://github.com/module-federation/module-federation-examples" can not run after build #212

Open
ghost opened this issue Dec 4, 2024 · 3 comments
Labels
bug Something isn't working COMMUNITY: PR is welcomed We think it's a good feature to have but would love for the community to help with the PR for it help wanted Extra attention is needed

Comments

@ghost
Copy link

ghost commented Dec 4, 2024

Repreduction

https://github.com/kehuay/shared-directory

  1. pnpm --filter=shared-directory-* build
  2. pnpm --filter=shared-directory-* preview

Results

  • rust host

image

  • vite host

image

@ghost ghost changed the title When I use vite as host to realize "https://github.com/module-federation/module-federation-examples" "Shared - directory" case can not run after build When I use vite as host to realize "shared-directory" case of "https://github.com/module-federation/module-federation-examples" can not run after build Dec 4, 2024
@gioboa
Copy link
Collaborator

gioboa commented Dec 9, 2024

Hi @kehuay thanks for you feedback. We need to investigate.

@gioboa gioboa added bug Something isn't working help wanted Extra attention is needed COMMUNITY: PR is welcomed We think it's a good feature to have but would love for the community to help with the PR for it labels Dec 9, 2024
@frolovsky
Copy link

@kehuay
Thank you for reporting this issue! After reviewing your case, I’ve been able to identify the cause of the problem. Here’s an explanation:

1. This is not a bug with @module-federation/vite.

The issue arises from how Vite handles module resolution in different modes (development vs. build/preview). In development mode (vite), Vite uses an internal module resolution mechanism that automatically resolves paths like shared/file2 relative to the source directory (src). This is why your imports work as expected during development.

However, during the build or preview phase (vite build or vite preview), Vite relies on Rollup for module bundling. Rollup does not support implicit path resolution like Vite. As a result, imports such as shared/file2 will not be resolved unless explicitly configured.

In short, the issue is related to unresolved paths in the build process, not a bug in the @module-federation/vite plugin. But, ideally, the build should also fail, just like when trying to build without the plugin(?) @gioboa


2. How to fix this

You can resolve this issue in two ways:

  1. Update your imports to use relative paths. For example:
// Instead of this:
import file1Default from "shared/dir1/file1";
import { A } from "shared/file2";

// Use this:
import file1Default from "./shared/dir1/file1";
import { A } from "./shared/file2";
  1. Configure an alias in vite.config.js
    If you want to keep the existing absolute-like paths (e.g., shared/file2), you can define an alias for shared in your vite.config.js file:
export default defineConfig({
  resolve: {
    alias: {
      shared: '/src/shared', // Adjust this path based on your project structure
    },
  },
});

@frolovsky
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working COMMUNITY: PR is welcomed We think it's a good feature to have but would love for the community to help with the PR for it help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants