-
Notifications
You must be signed in to change notification settings - Fork 1
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
Handling irregular require
statements
#50
Comments
Hi. I will take a look into this later. |
No rush!! :) I'm not blocked because of the patch I made. Please take your time. Life is busy. Thanks for letting me know. |
So first, thanks for reporting this issue. As always your issue is very informative and shows a deep understanding of the plugin. I think the patch you made is great for a quick fix. If you don't mind you can make a pull request with your changes. I think it is a good idea to add this return conditions to dynamic and es6 as well.
I agree. I will work on a real solution. As I barely use common js myself but I really need some research on the possible cases to cover. |
Made PR #51 :) I didn't do any work on the others beside cjs, though, as I didn't see an obvious equivalent after a quick glance and don't have any test cases for it. Thanks again! |
Great. Thanks. No, I neither see an equivalent case for the others, although dynamic imports allow more creative constructions as well. I will just add it for the sake of completeness . If there is a situation, which isn't anticipated, this will prevent crashes. |
Hmm I wonder if it would be wise to log the code when this happens so people at least know what's going on and can figure things out from there in the future. |
Yes, it should say something like 'this is a bug' 😉 |
Resolved in #52 |
require
statementsrequire
statements
Thanks again for the quick response! I really appreciate all your hard work on this package. It's a life saver! |
Hi again! 👋
As I mentioned before, thanks for your work on this project! 🙂
Today I used patch-package to patch [email protected] for the project I'm working on (source code for the plugin configuration here).
It seems
require
statements that don't match the expected pattern of declaration and instantiation in one expression (e.g.typeof window<"u"&&typeof require<"u"&&(window.Buffer=require("buffer/").Buffer);
) cause an error that closes the program and fails to build:Essentially, it seems that
core.js
's#cjsNodeToUnit
assumes the acorn node will be a straight-forward expression with declaration and instantiation without any complexity (e.g.var name = require('mod');
). However, I just received the error when#cjsNodeToUnit
was trying to parse the node when thecode
variable was set totypeof window<"u"&&typeof require<"u"&&(window.Buffer=require("buffer/").Buffer);
(I think some dependency of mine was trying to polyfillBuffer
). I stringified thenode
, whose contents follow in the dropdown:JSON.stringify(node, null, 2)
It appears that there are no declarations in the statement, so
node.declarations
isundefined
. When#cjsNodeToUnit
accesses that property, it throws the error.acorn
parses the expression withoperator: '&&'
and the actual assignment of the import (where it should haveoperator: '='
) is nested inleft
s andright
s.Fortunately for my current use case, I don't need to do any modification of this particular import statement, so I made a quick patch that essentially ignores the problematic line. However, I suggest a better long-term solution would be to traverse the
node
to find theoperator: '='
and operate on that (accounting in any case fornode.declarations
beingundefined
).Some other examples of different kinds of
require
s that likely don't match the expected pattern follow:Here is the diff that solved my problem:
Thank you again for all your hard work on this awesome library!
This issue body was partially generated by patch-package.
The text was updated successfully, but these errors were encountered: