From 61a743fe1ac91b28fc4a7eaaf2a3f85874222205 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli Date: Sat, 4 Jun 2022 10:17:05 -0400 Subject: [PATCH 1/2] Conditionally use the esm package as a fallback, rather than default --- lib/load_brocfile.ts | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/load_brocfile.ts b/lib/load_brocfile.ts index 3863188e..c570e510 100644 --- a/lib/load_brocfile.ts +++ b/lib/load_brocfile.ts @@ -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); + } } // ESM `export default X` is represented as module.exports = { default: X } From 8b88372b22020cac5e2fe9d79b7304b81cd72388 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli Date: Sat, 4 Jun 2022 10:21:07 -0400 Subject: [PATCH 2/2] lint:fix --- lib/cli.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cli.ts b/lib/cli.ts index 63b4ea0b..3d1cdac0 100644 --- a/lib/cli.ts +++ b/lib/cli.ts @@ -30,7 +30,7 @@ interface ServeOptions { outputPath?: string; cwd?: string; noWatch?: boolean; - watcher?: WatcherType, + watcher?: WatcherType; environment: EnvironmentType; prod?: boolean; dev?: boolean;