-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Use browser field in package.json instead of main field #556
Conversation
it will be used instead of the real polyfill which is useless in node and also it breaks node as well: `ReferenceError: window is not defined`
Just curious @oroce, are you using picturefill on the backend for server-side rendering, or something else? |
Not directly. We are using a react component (what we are rendering on server side) which does basically the following thing:
|
Ok, this makes sense to me, but I'm trying to think of situations where moving it to browser would be detrimental, if that situation exists. Thoughts from @aFarkas and @baloneysandwiches? |
I don't have any idea how this is actually treated, but if you add empty.js than there is no function (not even a noop) returned. So doesn't this produce an error?
How do other libs treat this problem? Pretty sure picturefill isn't the only script. |
I know npm will error out if it can't find the file in the main field, but I'm not sure it errors if it can't find a |
@mike-engel Yes, require will throw an error if a file is not found. @aFarkas yes it wont be a function. There are two options:
I can live with any of them |
Yes, but to go with the 1. option, don't we have to export a noop function to avoid getting uncaught errors as @aFarkas mentioned? |
haha just realized, if you want to use both in node and in browser you can do:
|
Sorry for the long delay @oroce. Are you comfortable with your try/catch method, or would you rather see this PR go through? If the PR does go through, I'd like to see PF still exported via |
@mike-engel I would be happy with wrapping
Shall I update the pull request? |
Just looking at this. So from what I understand, if there is a simultaneous What's the best way to approach this, then? |
@jegtnes the first part is right. Actually browser field is a kinda old thing. In browserify it was introduced back in 2013. (browserify/browserify#174) Webpack uses |
Ok @oroce, I still think this is a good idea. We want to wait, however, until we're ready to release a Picturefill 4.0 since this would definitely be a breaking change. I'm not too concerned yet since |
@mike-engel yup, I agree that we should wait until 4.0. In 3.X the recommended way should be using |
+1 |
This doesn't seem right, you can't expect all libraries to use the |
Thank you for all of the thoughts on this. As I'm archiving and deprecating this project per #723 I'm going to close this without implementing a big change. Thanks! |
We've been using picturefill for like 4months, we are absolutely satisfied with it but as we are moving our React rendering to the backend we realized picturefill breaks in nodejs. Since it uses
main
field inpackage.json
node tries to load thedist/picturefill.js
which containswindow.matchMedia
andwindow
in node is undefined.Solutions:
Wrap polyfill in if where we are checking for the existance of
window
Imho it looks ugly:(
Use
matchmedia
npm module which works on backend and client side as well (we should add a build step to picturefill)Use
browser
field instead of mainfield
I would go with the 3rd one. Here why:
polyfill
shouldn't work on backend since it usesmatchMedia
and picturefill shouldn't support node, but it does not mean it should break.Browser field is a de facto way of separating client side module from backend, here's the spec: https://gist.github.com/defunctzombie/4339901
Also the 3rd way wont break any use cases:
<script>
tag, you would still download the dist packagebrowser
field: https://github.com/substack/node-browserify#browser-fieldbrowser
field: http://webpack.github.io/docs/configuration.html#resolve-packagemainsI moved
dist/picturefill.js
to browser field and changed the main fieldempty.js
, which just an empty js file.