Search code examples
javascriptjspmsystemjs

jspm multiple apps, common local module


I am new to jspm and I am trying to evaluate JSPM as a replacement for my current apps that use requirejs.

How does jspm handle multiple modules/packages? For example:

mycustomlib
   |
   |-src
   |-jspm_packages
   |    |-npm
   |    |-github
   |-config.js

The above config.js file contains all the dependencies that are needed for mycustomlib. Note, mycustomlib is not published to npm and is NOT going to live on github. It will be my private custom library in my repo.

Now say I have few apps that use mycustomlib but have their own config.js, since they have other dependencies too.

mycustomlib
  |
myapp1
  |
  |-src
  |-config.js

myapp2
  |
  |-src
  |-config.js

how should I make sure myapp1 and myapp2 can also use the config.js from mycustomlib, so that they can use mycustomlib and its dependencies?

In requirejs approach, I was including the mycustomlib/config.js in index.html and then myapp1/myapp2 had to their own main.js which included requirejs.config({....}); requirejs will merge the configurations.

I guess this boils down to, what is the recommended way to use local packages/modules?


Solution

  • To my knowledge there isn't a mechanism to do this exactly what you are hoping right now. Perhaps someone will eventually implement a local JSPM Custom Registry. Here are two alternative approaches you might consider:

    1. (ab)use the JSPM linking mechanism

    One workflow that I have used to install local packages via JSPM is to assign the package a github/npm/jspm alias which otherwise doesn't exist and use the JSPM linking mechanism to install the local package rather than retrieving a remote copy. In your case you might (replace version specs as appropriate):

    From within mycustomlib directory (this command may take some time):

    jspm link npm:[email protected]
    

    In each of myapp1 and myapp2:

    jspm install --link npm:[email protected]
    

    This is a bit of a kludge and you will find you need to re-link the package when the anything in mycustomlib changes and often when changing dependencies of myapp1/myapp2. The jspm-linker package can alleviate some of this pain and automatically re-link for you.

    2. Configure your own registry

    You have several options for setting up "your own" registry. If you don't already have access to any of these types of services, it's probably easiest for the average person to use the 3rd-party git custom JSPM registry jspm-git which would allow you to install your own libraries directly from an ordinary git repository where you are hosting your library. As far as I know you cannot use local paths with jspm-git.

    Regarding sharing your config.js:

    As long as you have properly configured each package as a JSPM package sharing a config.js is unnecessary.