How do I structure a library for NPM (With modules) - eviltoast

Lets say I have a simple JS library. Most of the JS code is inside an object named after the library with the properties being the method functions. Some of these method functions require other method functions (Example, methodC() requires methodA()).

const myLibrary = {
   methodA: function() {...},
   methodB: functiom() {...},
   methodC: function() {...},
   methodD: function() {...}
}

How would I split these methods up into modules and split up this code into multiple files to make it a standard file structure for a NPM package. Currently all of the code is in one large JS file, and it could be broken down into modules. I would like to keep it all in the one object myLibrary like most NPM packages which when used, have the following syntax…

myLibrary.methodB(true, true);

Were all the methods are contained inside a object.

Any help will be most appreciated.

  • walter_wiggles@lemmy.nz
    link
    fedilink
    arrow-up
    2
    ·
    7 months ago

    I would recommend checking out Rollup (https://rollupjs.org/introduction/).

    Rollup is one option that will fit a lot of situations, but there are other ways to bundle a library/module that focus more on how you want to use it. Is it used by other libs, by a UI application, etc.