Is it possible to alias server-only modules in Plugins?

default discord avatar
brachypelma
4 weeks ago
8

I am working on a plugin that includes a file that requires server-only modules. For now, I am just building it as a mock plugin in a plugins directory inside by Payload src directory. If I alias the file requiring server-only modules following the documented instructions, (

https://payloadcms.com/docs/admin/webpack#aliasing-server-only-modules

) I still get compile errors. Interestingly, I can get everything to compile if I rework my plugin as a collection and load it in the collections aray (the plugin I am writing just adds a collection to the incoming config).



The fact that I can get this to work when I add my collection directly to the collections array but not when I add a plugin that does the same thing makes me wonder whether it is possible to use the documented aliasing technique in plugins. I'm afraid I don't understand what's going on under the hood clearly enough to understand why aliasing would work for collections but not plugins.

  • default discord avatar
    noheadphones
    4 weeks ago

    Aliasing works for plugins too, are you able to share the plugin code?

  • default discord avatar
    brachypelma
    4 weeks ago

    Let me see if I can quickly put together a small reproducible example

  • default discord avatar
    noheadphones
    4 weeks ago

    Actually have you looked into other examples (mine)

    https://github.com/NouanceLabs/payload-dashboard-analytics

    payload team has this one too:


    https://github.com/payloadcms/plugin-stripe


    You can see how both modules handle server aliasing

  • default discord avatar
    brachypelma
    4 weeks ago

    Thank you. Oddly enough, I was unable to reproduce the error in a simplified example. So I probably messed something up in my actual Payload instance. I will have to dig a bit deeper.



    Ok, this is a bit embarrassing, but apparently the problem was the plugin name. I was calling it ImportExport. I know import and export are reserved keywords, but I thought writing them the way I did would be ok. When I changed the name to something else (DatabaseTransfer), I stopped getting compile errors.

  • default discord avatar
    noheadphones
    4 weeks ago

    Glad it's resolved!

  • default discord avatar
    brachypelma
    4 weeks ago

    Me too. Thank you for your help.

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.