-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Implement the node module resolution algorithm for CommonJS modules #2094
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
Conversation
d9dbccc to
7099138
Compare
|
cc @samthor This will require changes in the JS version package as well: |
7099138 to
89e0f23
Compare
|
@ChadKillingsworth, can you use |
|
Wouldn't it be rewritten twice that way? |
|
@MatrixFrog tells me it doesn't really matter whether |
|
This change is causing failures for some internal tests. |
|
Looks like a red herring. The tests were already broken. |
| try { | ||
| // JSON objects need wrapped in parens to parse properly | ||
| input.getSourceFile().setCode("(" + input.getSourceFile().getCode() + ")"); | ||
| } catch (Exception e) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What exceptions do we expect here exactly?
Why is it OK to just ignore them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the same Error as handling as other parse points use.
|
|
||
| Node root = input.getAstRoot(this); | ||
| if (root == null) { | ||
| continue; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there valid cases where root == null? What are they?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My thought was for invalid JSON. Since we wrap the JSON in parens, I think it will always have at least a root node.
|
|
||
| n.addChildToFront( | ||
| IR.exprResult( | ||
| IR.call(IR.getprop(IR.name("goog"), IR.string("provide")), IR.string(moduleName))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is for use with Node.js, right?
Shouldn't we be rewriting this to look like a non-JSON Node.js module then, rather than a goog.provide()?
Perhaps Node.js modules get rewritten to look like goog.provide() anyway?
Even if that's so, isn't it safer to rewrite this as a Node.js module & allow the later pass to rewrite it again as a goog.provide(). Otherwise, we're duplicating the logic here & risking inconsistent behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussed this yesterday & agreed to leave it as-is.
| } | ||
|
|
||
| // Load as a file | ||
| String fullModulePath = nodeModulesFolder.getKey() + "node_modules/" + requireName; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't MODULE_SLASH be used here?
| // /foo/ -> bar/node_modules/baz/foo_bar_baz.js | ||
| // /foo/node_modules/bar/ -> baz/foo_bar_baz.js | ||
| for (String modulePath : modulePaths) { | ||
| String[] nodeModulesDirs = modulePath.split("/node_modules/"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use MODULE_SLASH here & several other places below?
…ules. Adds support to rewrite JSON files passed in as source to module exports.
89e0f23 to
5097def
Compare
|
@brad4d Review items should now be addressed. |
|
@ChadKillingsworth What's the resolution for the literal slashes in strings? I don't see a response to those questions. |
|
@ChadKillingsworth nevermind about the '/' question. I see now. |
|
If we used the wrong slash somewhere I'm sure one of our valiant Windows users will let us know quite soon, so I'm not too worried about it :) (Does Travis run tests on Windows machines? Or can we make it do so if it doesn't already?) |
|
Travis doesn't run tests on windows.:( I think for now we're safe with just the "/" character. If someone complains - I'll be happy to fix it. |
|
FYI, internal review appears to be complete. |
|
@MatrixFrog should I be concerned about the Travis failure on this PR? |
|
I can fix travis magically. |
|
One of the suggested code improvements from our internal tools actually broke the code. :( |
|
Global presubmit looks good, so I'm submitting the change. |
…les. Adds support to rewrite JSON files passed in as source to module exports. Closes google#2094 ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=138143265
Feature referenced by multiple issues and PRs, but especially by #1773.
Adds support for CommonJS module rewriting to handle named (non-path based) module lookups. This implements Node Module Resolution in the compiler natively.
One note: the compiler does not discover source files but depends on all necessary files to be passed in as source. I have work started for the Java npm version which takes an entry point then discovers and passes all required files.
As it's required by the algorithm, this also adds support for JSON files to be passed to the compiler as source. These files are rewritten to module exports. This rewriting is dependent on the file name ending with ".json". That's the same restrictions node has.
I only enabled this support for CommonJS modules, but it will definitely be wanted for ES6 modules as well.