How to share functionality between plugins?

Hi kind strangers,

I maintain around six plugins which all need a component like a Checkboxgroup.
How would I share the code between the plugins instead of duplicating it six times and then maintaining it six times?

I tried a “my-core” plugin and then import like
import CheckboxGroup from '../../../../my-core/sharedComponents/CheckboxGroup'

I tried putting it under src/sharedComponents and import it like
import CheckboxGroup from '../../../../../sharedComponents/CheckboxGroup'

Both times something like

ERROR in ./src/plugins/seedcreator/admin/src/pages/App/CreateSeeder.js 5:0-78
Module not found: Error: Can't resolve '../../../../../sharedPluginComponents/CheckboxGroup' in 'C:\Users\<username>\Desktop\git\<projectname>\src\plugins\seedcreator\admin\src\pages\App'

Thank you for your time!

I did it via hardlinking every single shared file.
Not the most robust scalable solution though.

I now did it via local npm packages.
I added all shared components to a local npm package and import it locally in every plugin like this:

"dependencies": {
    "sharedplugincomponents": "file:../sharedPluginComponents",
    "sharedpluginutils": "file:../sharedPluginUtils"
},

I now went back to manually copy / paste and multiplied maintain them for budget / time reasons.
Really bad :frowning:

@bliss Your answer to your own question helped me do it the right way :slight_smile: I hope your budget and time increase so you can do it the right way too!

1 Like