Search code examples
javascriptlernaparceljs

Is there a way to include only one module in the bundle?


I'm building a microservice backend with lerna, parcel and docker. I have multiple microservices, which each rely on some common code in a “commons” package in the monorepo.

This commons package is never published to NPM. Therefore, it works fine in development, but as soon as I try to build the docker containers, the commons module is not available inside the containers.

Is there some way to tell parcel to externalize all dependencies (like it does by default on nodejs) except for the commons package? Is this possible?


Solution

  • You can mark a particular package for inclusion in the bundle by using the includeNodeModules option. For example, In the package.json of the project that you want output a bundle that includes commons package rather than simply referencing it, you would have:

    {
       "main": "dist/index.js" // Or wherever you want to put the bundle.
       "targets": {
        "main": {
          "includeNodeModules": ["commons"]
        }
      }
    }
    

    For bundles that target nodejs (e.g. "library" packages), this option is false by default for every dependency (e.g. all dependencies will be "externalized" except what you put in this list - exactly what you asked for above).

    (This assumes you are using Parcel 2+ - make sure you are referencing "parcel": "^2.0.1" not parcel-bundler (which is the deprecated v1 version)).