Declare global imported external library in Rollup Bundle

Viewed 283

I have external dependencies like Lodash that I am including inside my bundle with rollup, lets call this bundle libraryBundle.

libraryBundle has this Lodash dependency built-in, (Not using require, but including all lodash code), which is what I want. Also, it is using some features like _.camelCase inside the bundle successfully, so I can assume the bundle has incorporated the lodash code correctly.

I have included my libraryBundle in a second project, lets call it rollupProject. In my rollupProject I want to make use of Lodash features as well, and since I have all lodash code included in libraryBundle, I see pointless to include the lodash library along with libraryBundle.

So what I would like to do is to be able to use lodash (or any other library included in my bundle) in any other project that makes use of my libraryBundle

I guess there is a way to do this in the file rollup.config.js but I am not able to find the way. This is how i have rollup.config.js right now:

const nrwlConfig = require('@nrwl/react/plugins/bundle-rollup');

module.exports = (config) => {
  const nxConfig = nrwlConfig(config);
  return {
    ...nxConfig,
    external: (ext) => {
      if (['lodash', 'moment'].includes(ext)) return;
      return nxConfig.external(ext);
    },
  };
};

I am using nx so I am extending the configuration of this tool. Lastly what I am doing with the line if (['lodash', 'moment'].includes(ext)) return; is to exclude lodash and moment from externals array so all the code of these libraries is copied inside my bundle.

Thanks in advance and greetings!

0 Answers
Related