Multi-File Modules with Classes

Welcome Forums General PowerShell Q&A Multi-File Modules with Classes

This topic contains 3 replies, has 2 voices, and was last updated by

 
Participant
1 week, 4 days ago.

  • Author
    Posts
  • #175519

    Participant
    Topics: 3
    Replies: 3
    Points: 14
    Rank: Member

    Hi!

    If I want to make a module where each component function and class has its own file in the module folder (to make editing neater), how do I get this to work so that functions can access each other and all can use the classes?

    I've read stuff where a PSM1 dot-sources separate files and exports their functions explicitly, and also references to specifying the files as Nested Modules in the PSD1.  The latter seems to have issues with functions accessing each other unless you have multiple PSD1s – which seems a bit excessive if each nested "module" is going to contain only one function that you'd never be importing on its own.

    I've not seen any reference to this topic where classes are specifically brought into it too.  I need the classes to be usable both by the functions and also their being "exportable" by the module (i.e. visible to "using module").

    Thanks for any thoughts from anyone who's battled with this already!

  • #175522

    Participant
    Topics: 2
    Replies: 483
    Points: 1,151
    Helping Hand
    Rank: Community Hero

    Classes cannot be exported from a module unless they are located directly in the psm1 file. There really isn't a workaround there, you need them in the module file itself, before any functions are dot sourced.

    • #175531

      Participant
      Topics: 3
      Replies: 3
      Points: 14
      Rank: Member

      Thanks Joel!  That's unfortunate but I guess it's understandable  🙁

  • #175549

    Participant
    Topics: 2
    Replies: 483
    Points: 1,151
    Helping Hand
    Rank: Community Hero

    If you want to develop the module having classes in separate files as well, you might implement a build step as part of a release pipeline where all class files' contents are collated along with necessary parts of the PSM1 and you put together a new PSM1 before you release the module and use it in production.

You must be logged in to reply to this topic.