Skip to content

Commit 61a743f

Browse files
committed
Conditionally use the esm package as a fallback, rather than default
1 parent 8401494 commit 61a743f

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

lib/load_brocfile.ts

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import path from 'path';
22
import findup from 'findup-sync';
3-
import esm from 'esm';
4-
5-
const esmRequire = esm(module);
63

74
interface LoadBrocfileOptions {
85
brocfilePath?: string;
@@ -37,8 +34,29 @@ function requireBrocfile(brocfilePath: string) {
3734
// Load brocfile via ts-node
3835
brocfile = require(brocfilePath);
3936
} else {
40-
// Load brocfile via esm shim
41-
brocfile = esmRequire(brocfilePath);
37+
/**
38+
* because 'esm' patches global modules,
39+
* let's only load 'esm' if we absolutely have to.
40+
* See context: https://github.com/broccolijs/broccoli/issues/498
41+
* (and related linkes)
42+
*
43+
* If this function (requireBrocfile) were to be async, we could use
44+
* await import here instead and get rid of the esm package altogether.
45+
*
46+
* However, it may mean that all of broccoli then needs to be converted to ESM (idk)
47+
*
48+
* Definitely, all of broccoli would need to be converted to async.
49+
* the CLI and brocifile loading is currently all sync.
50+
*/
51+
try {
52+
brocfile = require(brocfilePath);
53+
} catch {
54+
// eslint-disable-next-line @typescript-eslint/no-var-requires, node/no-missing-require
55+
const esm = require('esm');
56+
const esmRequire = esm(module);
57+
58+
brocfile = esmRequire(brocfilePath);
59+
}
4260
}
4361

4462
// ESM `export default X` is represented as module.exports = { default: X }

0 commit comments

Comments
 (0)