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

Angular MF with assets (images) in embedded app #697

Closed
sevaru opened this issue Feb 9, 2021 · 9 comments
Closed

Angular MF with assets (images) in embedded app #697

sevaru opened this issue Feb 9, 2021 · 9 comments
Labels
good first issue Good for newcomers help wanted Extra attention is needed Implementation Detail Outside the scope of Module Federation

Comments

@sevaru
Copy link

sevaru commented Feb 9, 2021

It's more question then issue.

Is there any practice on use images in html and css
cause by defaul if I use pathes like ../../assets/pic.png

when I open embedded app in shell app urls looking in shell app host e.g. localhost:4200 instead of localhost:4201 in angular example.

I would really appreciate any suggestions on that

@sevaru sevaru changed the title Angular MF with assets images host practice Angular MF with assets (images) in embedded app Feb 9, 2021
@cstefan
Copy link

cstefan commented Feb 22, 2021

Good point! For small SVGs I would just inline them in the compilation. When working with Less you have data-uri('startup.svg')
For bigger pictures I would look at setting the whole path with the help of a variable with the entry-point.
background-image: url($entry-point + '/startup.png');

But, ... I see many issues.
Have you found anything else?

@sevaru
Copy link
Author

sevaru commented Feb 26, 2021

yes @cstefan thank, we inline small assets too.

but we have problems with fonts, and big assets for now.

No i have to postpone my research for now.
But if I came back to it I'll let you now.

Copy link
Member

anything webpack can process can be federated, images, css, side effects, the works. Im not sure if this is an angular specific issue

@vaindil
Copy link

vaindil commented Jun 23, 2021

I'm running into this issue as well. Angular typically loads assets against the base URL of its site, which is set with the <base> tag in index.html. (I have no idea if this is typical of other frameworks too, I only have experience with Angular.) The sub-app's base path is apparently ignored with the standard Webpack setup for federation--only the shell app's base is used, which leads to this issue.

Still trying to work on this, but I'm new to Webpack config stuff since Angular has handled it all for me in the past.

@ScriptedAlchemy
Copy link
Member

Don't know how to handle angular specific. But in webpack the file-loader accepts a post transform option which you could use to infer the url internally

Webpack usually figures it out with some parsing of document.currentScript

@ScriptedAlchemy
Copy link
Member

Looks like there's a way to set this in webpack instead of depending on markup. Not sure how but someone told me it's possible. If so then we can get an at runtime base assignment

@ScriptedAlchemy ScriptedAlchemy added good first issue Good for newcomers help wanted Extra attention is needed Implementation Detail Outside the scope of Module Federation labels Jul 3, 2021
@axell9641
Copy link

Did anybody find a way to share static resources (images) between the remotes and the shell?

@neubs-bsi
Copy link

You can declare shared assets from a library in the webpack config in the shared option of your ModuleFederationPlugin:

          '.shared/assets': {
            singleton: true,
            eager: true,
            import: './shared/assets',
            requiredVersion: require("../../package.json").version,
          },

Just add this to both webpack configurations. It should then use the assets from your remote as well as your shell. This works if you have a monorepo and a shared assets library.

@Markiewic
Copy link

Markiewic commented Sep 20, 2023

I think I solved this problem relatively well, but I still want to explore a more convenient solution.

You need to use the variable __webpack_public_path__ from webpack to access the base address from which any remote module was loaded.

For example, to access an asset from a remote module, you can specify its path like this:
some.component.ts (component from remote)

declare const __webpack_public_path__: string;

@Component(...)
export class SomeComponent {
  public publicPath = __webpack_public_path__; // here will be smth like `localhost:4201/`
}

some.component.html

<img [src]="publicPath + 'assets/some-asset.png'" />

some-asset.png is served by remote, but will be available from both the host and remote, since we are building the correct link to it.

To simplify your development, you can provide a variable in the exposed modules and make pipe that will build the correct link itself.

<img [src]="'assets/some-asset.png' | localAsset" />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed Implementation Detail Outside the scope of Module Federation
Projects
None yet
Development

No branches or pull requests

7 participants