-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
require is unavailable when scope hoisting enabled in Parcel 2 #5883
Comments
The documentation for Parcel 2 suggests that scope hoisting is intended to work in the case that a require is called from inside a function or inside an if statement as is happening in jspreadsheet-ce.
Ref: https://v2.parceljs.org/features/scope-hoisting/#wrapped-assets |
The problem seems to be that const b = require("./b");
console.log(b())
// b.js
if (typeof require === "function") {
var jSuites = require("./c.js");
}
console.log(jSuites);
let x = () => {
return eval("1");
};
module.exports = x;
// c.js
module.exports = "c"; |
Ah interesting. Does it have something to do with these lines of code? I'm struggling to see how the attribute that get set here would prevent the replacement from happening. Is it the parcel/packages/shared/scope-hoisting/src/hoist.js Lines 125 to 138 in b70830d
|
Looks like it's because of this condition: parcel/packages/shared/scope-hoisting/src/hoist.js Lines 539 to 548 in 72ce767
|
@mischnic So there needs to be an extra condition in the expression in the |
Yeah, something like this should work: !path.scope.hasBinding(path.node.argument.name) &&
- !path.scope.getData('shouldWrap')
+ !(path.scope.getData('shouldWrap') && path.node.argument.name === 'module') |
🐛 bug report
Building for production with Parcel 2 results in some dependencies failing to require their subdependencies, specifically I am seeing this with
jspreadsheet-ce
failing to require its dependencyjsuites
.🎛 Configuration (.babelrc, package.json, cli command)
🤔 Expected Behavior
I expected
jspreadsheet-ce
to requirejsuites
and to see no errors.😯 Current Behavior
I see this error in the browser:
The line that error fails on indicates that
jSuites
isundefined
:I believe the error is being caused by these lines of code in the
jspreadsheet-ce
jexcel.js
file.Ref: https://github.com/jspreadsheet/ce/blob/v4.6.0/dist/jexcel.js#L10-L13
Parcel 2 compiles those lines of code to:
When I debug those ☝🏻 lines of code I find that
require
isundefined
.If I disable scope hoisting with
--no-scope-hoist
, thenrequire
has a value and is no longer undefined.Something to do with how the scope hoisting is being performed is causing
require
to no longer be available.💁 Possible Solution
The fact that disabling scope hoisting with
--no-scope-hoist
causesrequire
to be defined suggests thatrequire
is not being defined or passed into the hoisted scope appropriately. I can't tell if this is an idiosyncracy of the jspreadsheet-ce package, or if this is a bug in Parcel 2.🔦 Context
I'm using jspreadsheet-ce in a project that was not using Parcel, and was importing its dependencies directly from unpkg.com. I'm migrating to using Parcel 2 and this is the only issue I have.
💻 Code Sample
https://github.com/leighmcculloch/parcel-bundler--parcel--issue-5883
🌍 Your Environment
2.0.0-beta.1
v15.9.0
npm 7.5.3
Ubuntu 20.04.1 LTS (Code Name: Focal)
The text was updated successfully, but these errors were encountered: