Saturday, 24 October 2020

How to build a multi package node module with some optional modules

I am trying since several days to build a node module which will be used by a react application.

Today my module is packaged as a big javascript file packaged with webpack. Some part of this node module is optional and is only needed if we want to use certain features (activated through config). In order to avoid loading unecessary part of the code, I have tried first to use chunking and dynamic import as explained by webpack but this has not worked: chunks are created and packaged in the dist/ folder but i never succeed to run the dynamic import in the node module when called from the react app. The application complains that the chunk cannot be loaded from the app. This make sense for me as this webpack feature was more built to load dynamically part of the application and not as an internal mechanism for a node module dependency to load code on demand (but i could be wrong).

I looked at other projects like babel or react-router where the module is split in several package using lerna and yarn package. So i have tried to build the library with lerna with different packages:

my-library
  core/
    src/
      index.js
  another-package/
    src/
      index.js

I want the index.js file from core to call a method from index.jsfrom another-package only if needed and only if the node-module @my-library/another-package was installed. But i never found the solution to do it. Is it possible to achieve this with ES5/ES6 with webpack/lerna or did i took the wrong approach?



from How to build a multi package node module with some optional modules

No comments:

Post a Comment