-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Differentiate Internal/External Require Calls for Common JS Modules #954
Comments
Try omitting this line:
The externs should be available to all modules without explicitly requiring them. You will have to use the full name of the module instead of just |
So you're telling me there is no way to get closure compiler to do this without altering my code to make it closure-specific? |
Right. closure-compiler will not work with native node modules. |
The problem is the What happens if you alias var externalRequire = require;
/** @suppress {duplicate} */
var crypto = externalRequire('crypto') |
Aliasing So overall, there isn't any way to specify where closure-compiler should look to resolve references like this? E.g. A compiler option to say "If you see a The benefit to doing this would be that it would allow type checking on Node.js projects without any code alteration. If you're open to the idea of supplying a compiler option, I would be interested in making a PR for this. |
We're open to improvements like this, but it may be trickier than you think. During a very early pass in the compiler, The difficult part would be when should a |
Am I right in suggesting that the macro-like expansion of Yes, I agree that it is difficult to know if it supposed to be an external call or if it is just a typo and should be considered an error. The compiler does not have enough information to make a call on that. Would it be possible to add an annotation to explicitly declare the require as an external call? This would mean that the proposed additional functionality would be to not expand out |
In general, we don't like to add annotations. Also, how is adding an annotation any different than aliasing the Also, there are cases where a |
Aliasing the /** @externalRequire */
var crypto = require("crypto"); For cases where a Is there any particular reason for disliking new annotations? On the contrary, annotations are the only way anything non-closure specific can interact sensibly with the compiler. I can understand that they should be used carefully where the compiler doesn't have enough information to make a decision, but I feel that they can be extremely valuable for cases such as this. I can always write a script to alias all of the |
How is this case handled by the Require JS optimizer? It seems like the compiler should aim for parity with already understood methodologies. Also, I would suggest writing up a design document and posting for comments in the Closure-Compiler-Discuss group. Most of the compiler maintainers do not use common js modules, so you'll get more feedback there. Here's an example design document: https://gist.github.com/ChadKillingsworth/b86a4cffaa71571b5d01 |
Node.js preferentially loads up core modules, then knows where to look based on the first few characters of the provided string (lack of As a result, it can tell the difference between native libs like I'll write up a design document this weekend. |
Posted to https://groups.google.com/forum/#!forum/closure-compiler-discuss Design doc draft for reference: |
I'm still keen to get this feature implemented. I originally fixed my problem by using Flowtype instead for type checking (which does resolve It looks like @ChadKillingsworth has been the most enthusiastic one to also get this implemented, and may have started working on this already? Assuming this is the case, is there anything I can do to push the process along? |
@ChadKillingsworth what if the compiler kept a hard-coded list of node libraries, and left the calls to require alone when |
Oh, excuse me, I only just found this issue: #1382 And I agree that having a flag is a better system 👍 Mainly because it makes it easier to use popular npm modules that use core libraries like http without having to modify them (to alias require). |
I want to compile this very simple piece of code:
I know the compiler needs to know where the
crypto
library is, so I'm passing it in via--externs
(I do not want it to be compiled along with my code, I just want it bluntly passed through).However, I cannot get this to compile at all. I'm compiling it with:
where
closure-compiler/contrib/nodejs/crypto.js
is the file in this git repo. I've also tried usingglobals.js
to no avail. What am I doing wrong? Looking at the source, it's producing this error becausemodule$crypto
is not contained inmodulesByProvide
insrc/com/google/javascript/jscomp/Compiler.java:1534
, which seems to just be looping over the files I give it in--js
and not the externs files.Version:
The text was updated successfully, but these errors were encountered: