-
Notifications
You must be signed in to change notification settings - Fork 216
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
Conditionally use esm
package
#499
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,5 @@ | ||
import path from 'path'; | ||
import findup from 'findup-sync'; | ||
import esm from 'esm'; | ||
|
||
const esmRequire = esm(module); | ||
|
||
interface LoadBrocfileOptions { | ||
brocfilePath?: string; | ||
|
@@ -37,8 +34,29 @@ function requireBrocfile(brocfilePath: string) { | |
// Load brocfile via ts-node | ||
brocfile = require(brocfilePath); | ||
} else { | ||
// Load brocfile via esm shim | ||
brocfile = esmRequire(brocfilePath); | ||
/** | ||
* because 'esm' patches global modules, | ||
* let's only load 'esm' if we absolutely have to. | ||
* See context: https://github.com/broccolijs/broccoli/issues/498 | ||
* (and related linkes) | ||
* | ||
* If this function (requireBrocfile) were to be async, we could use | ||
* await import here instead and get rid of the esm package altogether. | ||
* | ||
* However, it may mean that all of broccoli then needs to be converted to ESM (idk) | ||
* | ||
* Definitely, all of broccoli would need to be converted to async. | ||
* the CLI and brocifile loading is currently all sync. | ||
*/ | ||
try { | ||
brocfile = require(brocfilePath); | ||
} catch { | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires, node/no-missing-require | ||
const esm = require('esm'); | ||
const esmRequire = esm(module); | ||
|
||
brocfile = esmRequire(brocfilePath); | ||
} | ||
Comment on lines
+53
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we check the specific error code and only fallback to the esm version if we don't have some other (userland) error? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't get this project to run at all locally, so so I can't see what error is thrown |
||
} | ||
|
||
// ESM `export default X` is represented as module.exports = { default: X } | ||
|
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 the main fix here. importing
esm
cannot happen in module-space because of the hax it employs.