Skip to content

Commit

Permalink
Add browser field to package.json to fix webpack
Browse files Browse the repository at this point in the history
This should close #305, see #305 (comment):

> Actually, after reading https://webpack.js.org/configuration/resolve/#resolvemainfields more closely, it sounds like adding a `browser` field to `package.json`, that points to `lib/index.js` (same as `main`) should fix things.
>
> > resolve.mainFields
> > [string]
> >
> > When importing from an npm package, e.g. import * as D3 from 'd3', this option will determine which fields in its package.json are checked. The default values will vary based upon the target specified in your webpack configuration.
> >
> > When the target property is set to webworker, web, or left unspecified:
> >
> > webpack.config.js
> >
> > module.exports = {
> >   //...
> >   resolve: {
> >     mainFields: ['browser', 'module', 'main']
> >   }
> > };
> > For any other target (including node):
> >
> > webpack.config.js
> >
> > module.exports = {
> >   //...
> >   resolve: {
> >     mainFields: ['module', 'main']
> >   }
> > };
> > For example, consider an arbitrary library called upstream with a package.json that contains the following fields:
> >
> > {
> >   "browser": "build/upstream.js",
> >   "module": "index"
> > }
> > When we import * as Upstream from 'upstream' this will actually resolve to the file in the browser property. The browser property takes precedence because it's the first item in mainFields. Meanwhile, a Node.js application bundled by webpack will first try to resolve using the file in the module field.
>
> I'll open a PR for that.
  • Loading branch information
josephfrazier authored and slevithan committed Dec 8, 2020
1 parent 760ab95 commit 45c8546
Showing 1 changed file with 1 addition and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -15,6 +15,7 @@
"regular expression",
"unicode"
],
"browser": "./lib/index.js",
"main": "./lib/index.js",
"module": "./src/index.js",
"files": [
Expand Down

0 comments on commit 45c8546

Please sign in to comment.