Skip to content
This repository has been archived by the owner on Aug 20, 2018. It is now read-only.

Please add a link for "Don't forget to polyfill require" #7

Closed
Pomax opened this issue Jun 14, 2015 · 14 comments
Closed

Please add a link for "Don't forget to polyfill require" #7

Pomax opened this issue Jun 14, 2015 · 14 comments

Comments

@Pomax
Copy link

Pomax commented Jun 14, 2015

This statement is absolutely not what someone who's looking for information on why loading json isn't working hopes to find. It just makes them more angry because no one's explaining how the hell to use this loader =)

@dmitry
Copy link

dmitry commented Jun 20, 2015

I agree. Related issue: webpack/webpack#140

@donaldpipowitch
Copy link

+1. I have no idea how to use var pkg = require('../package.json'); in a Node project. I set target: 'node' and externals: [ /\.json$/ ],, but it doesn't work. I don't know if json-loader helps me here?

Update

Okay, it looks like I need to do this:

  externals: [
    function(context, request, callback) {
      if(/\.json$/.test(request)) {
        callback(null, 'commonjs ' + request);
      } else {
        callback();
      }
    }
  ],

@donaldpipowitch
Copy link

This doesn't seem to work when I use require(path.join(process.cwd(), 'package.json'));. I created a separate issue for that: webpack/webpack#2413

@Pomax
Copy link
Author

Pomax commented Apr 29, 2016

@donaldpipowitch requir calls with magic names (ie only having some meaning at runtime) will always be a problem because the bundler has no idea what you're asking for (it would have to actually run the code to figure out which thing you're actually requiring) so you're going to have to use relative, explicit strings like require('./package.json').

@donaldpipowitch
Copy link

I don't want to bundle my JSON file in that case. I want a vanilla Node require. Webpack should do nothing. (I have a hack in webpack/webpack#2413 which works.)

@Pomax
Copy link
Author

Pomax commented Apr 29, 2016

if it's code that will end up in your bundle, there is no vanilla node, because the bundle will run in the browser. Unless your webpack config is set to target node, specifically (in which case bundling is arguably a bad idea, just rely on npm). However, if it's code that never ends up even being bundled in, then you don't need a webpack loader; node already knows how to require .json files

@donaldpipowitch
Copy link

As mentioned above it is target: 'node' and will not be executed in the browser. The JSON should not be bundled, but required at runtime.

node already knows how to require .json files

That is exactly what I meant with Nodes "vanilla require". I use webpack just for other things then JSON.

@Pomax
Copy link
Author

Pomax commented Apr 30, 2016

in that case I don't quite understand why you're commenting on a thread that is specifically about webpack's json-loader, which is necessary only when you need to bundle. If your require is in code that doesn't need to be bundled, then this library is not relevant to your needs, but more importantly even if it is, this particular issue --about a README improvement-- has nothing to do with the question you really want to ask. Better to open a new issue, possibly not even on this repo.

@donaldpipowitch
Copy link

Sorry, I don't know if you read my comments. In my first commend I said "I don't know if json-loader helps me here?". So yes, maybe this was the wrong issue in the first place but nobody corrected me, so I thought I was correct. That was nearly a month ago. Now I know json-loader was the wrong tool for my problem. I DID created a new issue in a different repository. I just commented here for people who look at this loader for the same problem in the first place - just like me. I don't even use json-loader. But if you ask someone how to load JSON in a webpack based Node project, everyone seems to redirect you to this repository as the only valid solution...

You even said "this statement is absolutely not what someone who's looking for information on why loading json isn't working hopes to find." This is what drove me here, too. Loading JSON in a Node project wasn't working for me. However this README doesn't tell what this loader really does (or not does which was more important for me as it turns out). It just says "don't forget the require polyfill". Browsers don't have a require which could be polyfilled. Only Node has require. So yeah, that's why I thought this missing polyfill was the reason why I couldn't load JSON in Node when using webpack, because I have no clue if webpack alters Nodes require. If not, why wouldn't it work (especially with target: 'node'), right? Sorry for that! But seriously... I still don't know what this require polyfill even does? Why do I need a polyfill for bundling (as in inlining) JSON?

@Pomax
Copy link
Author

Pomax commented Apr 30, 2016

Fair enough, but this is the webpack/json-loader repository. It's for webpack, the README says json loader for webpack in the title, so that part should be relatively obvious. If you're using webpack to bundle your code and you need to bundle in a json resource, then you want this loader. If you're not forming a webpacked bundle, then you this library is not what you were looking for.

Webpack, like all bundlers, absolutely rewrites require to do what it needs to do based on how it packs static assets into bundle form (just like browserify, requirejs, etc)

@rob3c
Copy link

rob3c commented May 2, 2016

As an alternative to using a require polyfill for runtime loading of json resources that you aren't using json-loader to inline with webpack for whatever reason (they're too big and you don't want to slow down app loading, they change at runtime, etc.), you might be interested in the solution I posted in response to the issue @donaldpipowitch referenced: webpack/webpack#2413 (comment)

@donaldpipowitch
Copy link

If you're using webpack to bundle your code and you need to bundle in a json resource, then you want this loader. If you're not forming a webpacked bundle, then you this library is not what you were looking for.

I do use webpack to bundle code, but this doesn't mean that I want to bundle my JSON, because it isn't static. I just wanted clarify that. It is not either bundling and using webpack or not bundling and don't use webpack. You can use webpack for bundling most things (like your code), but not everything (like some JSON). And it is quite hard to figure out how to opt-out from bundling in some cases.

@rob3c Thanks, I'll look into that.

@kadamwhite
Copy link

I would like to vote to remove the statement entirely, instead of adding a link, because this sentence makes it seem that if I do want to bundle the JSON with my generated library, then I need to do some additional work in order to make it function in Node environments—which does not seem to be the case.

In webpack/webpack#140 @sokra goes so far as to state "The sentence is pretty old, I should remove it from all the loaders.", indicating this is a legacy of an earlier time.

@SpaceK33z
Copy link
Contributor

@kadamwhite thanks for your insights. I've removed the statement.

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

Successfully merging a pull request may close this issue.

6 participants