10000 `browser` vs `module` fields in `package.json` · Issue #4674 · webpack/webpack · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

browser vs module fields in package.json #4674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
alex35mil opened this issue Apr 8, 2017 · 24 comments
Closed

browser vs module fields in package.json #4674

alex35mil opened this issue Apr 8, 2017 · 24 comments

Comments

@alex35mil
Copy link

Do you want to request a feature or report a bug?
I don't think it's a bug, but confusing and inefficient behavior.

What is the current behavior?
When module's package.json contains browser, module & main fields, webpack is bundling browser build by default.

What is the expected behavior?
The key point of the module build is to optimize compiled bundle. And even if a package has hq ES build, it's not being used by default in favor of legacy UMD/IIFE builds. So expected behavior is to use module build by default.

@sokra
Copy link
Member
sokra commented Apr 9, 2017

We may need a field module-browser...

@stereobooster
Copy link
stereobooster commented Dec 26, 2017

Another way to solve it nodejs/node-eps#60. Not advocating this approach, just want to let you know.

UPD: there is also esnext prposal. Started to collect all those undocumented package.json fields in small repository

@stereobooster
Copy link

@alexfedoseev found solution, package should be in this form to work properly with wepback

{
  "name": "main-module-browser",
  "main": "dist/index.js",
  "module": "dist/index.esm.js",
  "browser": {
    "./dist/index.js": "./dist/index.browser.js",
    "./dist/index.esm.js": "./dist/index.browser.esm.js"
  }
}

Full example https://github.com/stereobooster/main-module-browser-test

@DrSensor
Copy link

Just have similiar problem and happen to found this issue.
@stereobooster What is the difference between index.js and index.browser.js? Is it umd and cjs or something else?

@stereobooster
Copy link

@DrSensor here is full spec https://github.com/defunctzombie/package-browser-field-spec

it doesn't specify types of the packages. One thing that matters is if code uses browser-specific (example: Dom) or server-specific (example: filesystem) features.

dmbch added a commit to untool/untool that referenced this issue Jul 6, 2018
dmbch added a commit to untool/untool that referenced this issue Jul 6, 2018
tkfm-yamaguchi added a commit to tkfm-yamaguchi/Leaflet.TileGeoJSON that referenced this issue Feb 19, 2019
To get rid of the problems with using some bundler packages which
adopts 'browser' field rather than 'module'.
See: webpack/webpack#4674
@mesqueeb
Copy link

Does this mean that currently webpack's order of importing is browser > module > main?

I would also prefer module > browser > main.

jayfreestone added a commit to jayfreestone/priority-plus that referenced this issue Mar 31, 2021
The module landscape is a mess. Currently the UMD build is preferred by
Webpack and esbuild, since the `browser` field takes precedence over the
`module` field.

However, conditional exports (yet another field!) now allow for a
separate approach. Setting the first type to `import` results in both
bundlers favouring it over the UMD build. I'm leaving Browser around as
a legacy field for now.

See:

- webpack/webpack#4674
- evanw/esbuild#187
- https://webpack.js.org/guides/package-exports/#providing-different-versions-depending-on-target-environment
- https://nodejs.org/api/packages.html#packages_exports_sugar
@kopax-polyconseil
Copy link

I think we can close it in favor exports, also good answer #4674 (comment) for legacy projects, feel free to feedback

And docs https://webpack.js.org/guides/package-exports/#providing-different-versions-depending-on-target-environment, anyway feel free to improve it

Hi ! I don't get your snippet, how does it work ? It choose randomly ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0