Share components between packages? #1405
-
|
I've got a few different front end apps, and I'm trying to build a core set of shared components between them, so I can stop repeating the same components in every app. My "peerDependencies": {
"react": "^16.14.0",
"theme-ui": "^0.3.4"
},
"devDependencies": {
"react": "^16.14.0",
"theme-ui": "^0.3.4"
},
"dependencies": {
"react-helmet": "^6.1.0",
"react-lottie-player": "^1.1.0"
}The package is then installed (via As soon as I try to import and use a simple component from the library, the app crashes:
Per #283, I've double-checked with I've tried adjusting the component and the dependencies too; removing the JSX pragma, making Theme UI a standard dependency... I can't seem to find a combination that works. Is this a Theme UI limitation? A React problem? A Gatsby problem? A Lerna problem? Just a really bad idea? What's the best way to share components between multiple Gatsby apps in a monorepo? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
|
Naturally, after struggling for an hour or two and writing this up, I now have a good idea of where the problem lies:
I thought I could work around this by moving What did seem to work is to add this to my const path = require("path");
exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
resolve: {
alias: {
react: path.resolve(__dirname, "node_modules/react"),
},
},
});
};That's not a Gatsby thing, it's a Webpack thing. But really at the end of the day, it's all a Lerna thing, I think. So for now, I think I answered my own question! But I would welcome any other suggestions, further reading, or example repositories when it comes to building a shared component library with Theme UI. Thanks! |
Beta Was this translation helpful? Give feedback.
-
|
For anyone here in the future, our docs link to a number of open source component kits for perusal: https://dev.theme-ui.com/resources/#component-kits |
Beta Was this translation helpful? Give feedback.
-
|
If anyone's struggling with why imported components don't use the local theme, again this might be a Lerna monorepo artifact. If Theme UI resolves to two different installations, you'll run into problems. I'm now using this in const path = require('path')
exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
resolve: {
alias: {
react: path.resolve(__dirname, 'node_modules/react'),
'theme-ui': path.resolve(__dirname, 'node_modules/theme-ui')
}
}
})
}Now that both React and Theme UI always resolve to my local project, everything works great. (If I understand correctly, this is what |
Beta Was this translation helpful? Give feedback.
Naturally, after struggling for an hour or two and writing this up, I now have a good idea of where the problem lies:
reactindevDependenciesmeansreact-components/node_modules/react/existsreact-components/into each Gatsby app'snode_modules/node_modules/react/ornode_modules/react-components/node_modules/react/, causing the errorI thought I could work around this by moving
reactfrom the component librarydevDependenciesto the root packagedevDependencies, but this didn't actually seem to work!What did seem to work is to add this to my
gatsby-node.js…