insin / nwb

A toolkit for React, Preact, Inferno & vanilla JS apps, React libraries and other npm modules for the web, with no configuration (until you need it)
Other
5.57k stars 331 forks source link

How to prevent peerDependency from being bundled #500

Open coreybruyere opened 5 years ago

coreybruyere commented 5 years ago

This issue is a: Question / support request

I'm not familiar with NWB and came into a project that uses it. How do I exclude peer dependencies from being bundled? I need to ensure that a specific package doesn't make it through to the final build. Basically trying to configure using one of these items: https://www.styled-components.com/docs/faqs#removing-styledcomponents-from-your-library-bundle

lsmoura commented 5 years ago

the peerDependencies are not installed by default. It just warns the requiring component that it needs that dependency to be installed by someone.

Now, if you want to have an external dependency not added on your bundle (this has nothing to do with peerDependencies directly, btw), you can just tell webpack to do so. Add the configuration in the webpack.extra on nwb.config.js file. In the following snipped, i'm adding uniqid to external dependencies:

module.exports = {
  type: 'react-app',
  webpack: {
    extra: {
      externals: 'uniqid',
    },
  },
};

In this case, you'll need to provide uniqid some other way to the app, as a global variable in window.uniqid. Maybe by using a umd import?