-
Notifications
You must be signed in to change notification settings - Fork 77
feat: add support for disabling imports for shared modules #330
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
base: main
Are you sure you want to change the base?
Conversation
Yep, I was looking at this. |
e0cf5c3
to
bce3588
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks great to me 👏👏
Can we recreate the import scenario in the multi example e2e test please?
I can try though that may take a bit, will be lower priority for me. |
bce3588
to
ca3a8c2
Compare
@gioboa can you explain how exactly the And given this effectively requires a config change, does that mean needing to copy and duplicate an app to simulate any of this? Do you have suggestions on the best way to test this given its an internal detail and not a visual one? Thanks. |
ca3a8c2
to
9591159
Compare
We have the e2e script that is using playwright here.
We can modify the config on one remote to use the new import:false setting. If the code is working properly the test will pass. WDYT? |
That still leaves me confused as to what is running where. Line 24 in f76706b
pnpm filter start , but I can't actually figure out all of what packages are getting a dev server started, and what are what?
|
9591159
to
0bd73db
Compare
The e2e test is simple but useful test to verify the library, we are testing if the milti example app is still working or not. |
Sorry your still not getting it. Im asking what apps in the examples folder are being started, and what their roles are respectfully for the tests. |
It's |
I got it working though will need a better way to verify things since the promo/banner seem to be random. The one key is that you have to put in a static reference in the host So
is required.
will trigger in the bundle a call to Is there any specific real test you would like for this? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any specific real test you would like for this?
No one in particular, just one valid to activate the new code.
5663dbb
to
2899188
Compare
fce43e0
to
706b165
Compare
@gioboa this is ready now. I also in the processed have reproduced #132
If you have a shared dependency but its not included in package json, it breaks ( How do you think that should be handled since we know the likely cause. Would be a separate PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you think that should be handled since we know the likely cause. Would be a separate PR.
with that check the PR is self contained, so let's add it please. Thanks for your commitment 💪
Sorry, what. your being cryptic 🤣 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry, let's add the check to prevent this error
Cannot read properties of null (reading 'id') [plugin vite:dep-pre-bundle]
That should be in its own separate PR b/c its not actually connected, I just found the error by chance, so I want to know what you want the behavior to be. Silently ignore, throw an error? I can submit a PR for that once you clarify and thats a very simple change. |
Options:
So based on that, throw an error should be fine. What do you think? |
I can throw an error. as for Anyways ill make that PR quickly. Please review and merge this. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gioboa Is there any further you need me to do on this PR? Thanks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned, the new code is currently unused, so I think we should find a different way to support the import false
configuration.
That simply then means just not rendering the dead logic, but that goes against what scriptedachamy advised in the previous PR, though it also means the import: false is never sent to MF anyways and it becomes closer to my original approach. Thoughts? |
Can we have your opinion @ScriptedAlchemy on this please? |
no, you deleted the entire template object. Im saying have a get method return a throw else return the original. C'mon export function generateLocalSharedImportMap() {
const options = getNormalizeModuleFederationOptions();
return `
import {loadShare} from "@module-federation/runtime";
const importMap = {
${Array.from(getUsedShares())
.sort()
.map((pkg) => {
const shareItem = getNormalizeShareItem(pkg);
return `
${JSON.stringify(pkg)}: async () => {
${
shareItem?.shareConfig.import === false
? `throw new Error(\`Shared module '\${${JSON.stringify(pkg)}}' must be provided by host\`);`
: `let pkg = await import("${getPreBuildLibImportId(pkg)}");
return pkg;`
}
}
`;
})
.join(',')}
}
const usedShared = {
${Array.from(getUsedShares())
.sort()
.map((key) => {
const shareItem = getNormalizeShareItem(key);
if (!shareItem) return null;
const getBody =
shareItem.shareConfig.import === false
? `
const shared = await loadShare(${JSON.stringify(key)});
if (shared) return () => shared;
throw new Error(\`Shared module '\${${JSON.stringify(key)}}' must be provided by host\`);
`
: `
usedShared[${JSON.stringify(key)}].loaded = true
const {${JSON.stringify(key)}: pkgDynamicImport} = importMap
const res = await pkgDynamicImport()
const exportModule = {...res}
// All npm packages pre-built by vite will be converted to esm
Object.defineProperty(exportModule, "__esModule", {
value: true,
enumerable: false
})
return function () {
return exportModule
}
`;
return `
${JSON.stringify(key)}: {
name: ${JSON.stringify(key)},
version: ${JSON.stringify(shareItem.version)},
scope: [${JSON.stringify(shareItem.scope)}],
loaded: false,
from: ${JSON.stringify(options.name)},
async get () {${getBody}
},
shareConfig: {
singleton: ${shareItem.shareConfig.singleton},
requiredVersion: ${JSON.stringify(shareItem.shareConfig.requiredVersion)},
${shareItem.shareConfig.import === false ? 'import: false,' : ''}
}
}
`;
})
.filter((x) => x !== null)
.join(',')}
}
const usedRemotes = [${Object.keys(getUsedRemotesMap())
.map((key) => {
const remote = options.remotes[key];
if (!remote) return null;
return `
{
entryGlobalName: ${JSON.stringify(remote.entryGlobalName)},
name: ${JSON.stringify(remote.name)},
type: ${JSON.stringify(remote.type)},
entry: ${JSON.stringify(remote.entry)},
shareScope: ${JSON.stringify(remote.shareScope) ?? 'default'},
}
`;
})
.filter((x) => x !== null)
.join(',')}
]
export {
usedShared,
usedRemotes
}
`;
} |
Other than that - this is pretty much exactly what I was expecting. Well done. |
- Add import flag to shareConfig interface and normalization - Handle import: false modules in virtual remote entry generation - Add e2e test
706b165
to
6ceda80
Compare
@gioboa @ScriptedAlchemy done. |
@gioboa following up. |
I'm waiting the feedback from ScriptedAlchemy |
@ScriptedAlchemy ping 🙃 |
@ScriptedAlchemy can you please review. |
Looking to get feedback on this as a draft before merging.
Close #304