-
Notifications
You must be signed in to change notification settings - Fork 332
Switch to use other field instead of browser #25
Description
It conflicts while you want to expose UMD bundle to unpkg and want to allow users of your package to use browserify to bundle it.
Scenario. You have a package that you want to have UMD bundle, commonjs and es "bundles".
You set pkg.module to point to dist/my-package.es.js. You set pkg.main to point to the dist/my-package.common.js which is CJS. You want users using modern bundlers such as Rollup/Webpack to resolve the ES variant of your module. Browserify users to resolve the commonjs. And browser users to just have a way to include the UMD bundle directly as script tag.
All is great, when you set pkg.browser field to point to the UMD bundle then UNPKG will resolve it, but Browserify users will resolve it too, instead the CJS which is set in pkg.main, because Browserify respects pkg.browser when it is set. Browserify should not resolve the UMD bundle, because it kinda have kinda same wrapper like the UMD, so more unnecessary bytes and duplicate code are added.
One way is to just not set pkg.browser and force users to point to the exact file that they want, instead of using the shortcuts that UNPKG provides for you - such as unpkg.com/my-package and unpkg.com/my-package@version, so you should force your users to use unpkg.com/my-package/dist/my-package.min.js for example.
Conflict comes because UNPKG and Browserify assumes different things for that field.
Unpkg assumes that that file can be used in browser directly without anything and does nothing to that file. While Browserify says "hey give me that file, i'll wrap it, resolve its deps recursively and I as tool, will give you final bundle that you can use in browser".
Edit: Good example is https://github.com/tunnckoCore/randomorg-js, where you can see that I added a notice for the users to not use the shortcut, so they are warned that they should not expect it would work. Notice that it is still not published to npm. When it is published the shortcut will point to dist/randomorg.common.js