Skip to content

[Bug]: Relative imports for shared items are missed #329

@awesome-q

Description

@awesome-q

Describe the bug

If I specify a share configuration as follows:

const sharedDeps = {
   '@my-org/my-ui-library': { singleton: true },
   '@my-org/my-ui-library/foo': { singleton: true },
   '@my-org/my-ui-library/bar': { singleton: true },
}

If i was importing from a host or remote, my import statement would look like this, which would work fine:

// app/host-app/index.ts
import foo from '@my-org/my-ui-library/foo'; // works fine

// app/remote/index.ts
import bar from '@my-org/my-ui-library/bar'; // works fine

However lets assume that in my ui library, foo actually has a dependency on bar:

// my-ui-library/src/foo.ts
import bar from '../bar'; // does not work

This relative import is not shared. Instead of importing the loadShare proxy module, the import will just go to the generated chunk. This causes it to get loaded, but not to update the shareScope. As a consequence, when a remote or some other code path tries to load my-ui-library/bar and checks to see if a shared copy is available, it appears as though it is not and proceeds to load it again.

It looks like the problem is due to the underlying logic itself. In pluginProxySharedModule_preBuild, the shared keys are transformed to regex expressions that are added as aliases. This will match bare specifiers, but not relative imports.

Version

1.7.1

Reproduction

https://github.com/awesome-q/module-federation-example

Relevant log output

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions