Skip to content

Commit 750b58f

Browse files
committed
Allow emulator to load ESM (doesn't work).
1 parent 9664b02 commit 750b58f

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/emulator/functionsEmulatorRuntime.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ import * as _ from "lodash";
2727
let triggers: EmulatedTriggerMap | undefined;
2828
let developerPkgJSON: PackageJSON | undefined;
2929

30+
/**
31+
* Dynamically load import function to prevent TypeScript from
32+
* transpiling into a require.
33+
*
34+
* See https://github.com/microsoft/TypeScript/issues/43329.
35+
*/
36+
// eslint-disable-next-line @typescript-eslint/no-implied-eval
37+
const dynamicImport = new Function(
38+
"modulePath",
39+
"console.log(modulePath); return import(modulePath)"
40+
);
41+
3042
function isFeatureEnabled(
3143
frb: FunctionsRuntimeBundle,
3244
feature: keyof FunctionsRuntimeFeatures
@@ -1065,8 +1077,13 @@ async function initializeRuntime(
10651077
try {
10661078
triggerModule = require(frb.cwd);
10671079
} catch (err) {
1068-
await moduleResolutionDetective(frb, err);
1069-
return;
1080+
if (err.code === "ERR_REQUIRE_ESM") {
1081+
// tslint:disable:no-unsafe-assignment
1082+
triggerModule = await dynamicImport(require.resolve(frb.cwd));
1083+
} else {
1084+
await moduleResolutionDetective(frb, err);
1085+
return;
1086+
}
10701087
}
10711088
}
10721089
if (extensionTriggers) {

0 commit comments

Comments
 (0)