-
Notifications
You must be signed in to change notification settings - Fork 29
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
External package using styled-components and tree shaking (preserveModules/input): "Cannot read properties of undefined (reading 'withConfig')" #170
Comments
Thanks @danielchoragwicki for the exhaustive description. I'll look into it to identify the problem. ---- UPDATE ---- The problem is button => undefined |
That's a good question, can you try to debug and check the styled object in both cases please? |
I’m not sure how to approach debugging this issue. I can’t log the When styled-components is not present in shared (when the app is running), the code in the bundle looks like this and imports esm: // example-styled-components-package.js?v=5adf8547
var ExampleButtonElement = styled_components_browser_esm_default.button.withConfig({
displayName: "ExampleButtonstyled__ExampleButtonElement",
componentId: "sc-1bjgygh-0"
})(["", ""], () => Ce(["background:red;border:0;margin:0;padding:20px;"])); On the other hand, when the styled-component is placed in shared and doesn’t work: // example-styled-components-package.js?v=b5947703
var import_styled_components = __toESM(require_viteViteHost_loadShare_styled_mf_2_components_loadShare(), 1);
var ExampleButtonElement = import_styled_components.default.button.withConfig({
displayName: "ExampleButtonstyled__ExampleButtonElement",
componentId: "sc-1bjgygh-0"
})(["", ""], () => (0, import_styled_components.css)(["background:red;border:0;margin:0;padding:20px;"])); // node_modules/__mf__virtual/viteViteHost__loadShare__styled_mf_2_components__loadShare__.js
var require_viteViteHost_loadShare_styled_mf_2_components_loadShare = __commonJS({
"node_modules/__mf__virtual/viteViteHost__loadShare__styled_mf_2_components__loadShare__.js"(exports, module) {
var {loadShare} = require_index_cjs();
var {initPromise} = require_viteViteHost_mf_v_runtimeInit_mf_v();
var res = initPromise.then( (_) => loadShare("styled-components", {
customShareInfo: {
shareConfig: {
singleton: true,
strictVersion: false,
requiredVersion: "^5.3.3"
}
}
}));
var exportModule = (/*mf top-level-await placeholder replacement mf*/
res.then( (factory) => factory()));
module.exports = exportModule;
}
});
const __mfproxy__awaitrequire_viteViteHost_loadShare_styled_mf_2_components_loadShare = await require_viteViteHost_loadShare_styled_mf_2_components_loadShare();
const __mfproxy__require_viteViteHost_loadShare_styled_mf_2_components_loadShare = () => __mfproxy__awaitrequire_viteViteHost_loadShare_styled_mf_2_components_loadShare;
export {__mfproxy__require_viteViteHost_loadShare_styled_mf_2_components_loadShare as require_viteViteHost_loadShare_styled_mf_2_components_loadShare}; |
To unblock you, as a workaround, you can use this check eg.
|
We have found a clue about what causes this issue. It occurs when our external packages, which use styled-components, have the output.preserveModules option enabled in their vite config (https://rollupjs.org/configuration-options/#output-preservemodules) or when the input option is specified instead (https://rollupjs.org/configuration-options/#input). However, we need one of these options to enable tree-shaking and create separate chunks for all modules. |
Thanks for your commitment 👏 |
When I add styled-components to
shared
and import a component with styled-components from an external package, I get an error:Uncaught TypeError: Cannot read properties of undefined (reading 'withConfig')
. It doesn't matter whether I import the component into the host or remote, the situation is the same.I have prepared a simple repo with this issue: https://github.com/danielchoragwicki/module-federation-with-config-bug/
You have to add the external package (here:
example-styled-components-package
) tovite-host
using yalc, and then run the projects.Yalc is used just for demo, but this problem occurs with any own package that uses styled-components in my project installing via npm. My packages have a similar configuration to
example-styled-components-package
.I also noticed that the package added by yalc with the
--link
flag (yalc add example-styled-components-package --link
) doesn't cause the problem.The text was updated successfully, but these errors were encountered: