Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ interface ServeOptions {
outputPath?: string;
cwd?: string;
noWatch?: boolean;
watcher?: WatcherType,
watcher?: WatcherType;
environment: EnvironmentType;
prod?: boolean;
dev?: boolean;
Expand Down
28 changes: 23 additions & 5 deletions lib/load_brocfile.ts
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';
Copy link
Author

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.


const esmRequire = esm(module);

interface LoadBrocfileOptions {
brocfilePath?: string;
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The 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 }
Expand Down