-
Notifications
You must be signed in to change notification settings - Fork 160
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
Support ESM #233
Comments
@grant it's possible to build a dual-mode module, I just finished doing this for yargs-parser. This might be worth thinking about for the functions framework. |
Alright. So I have a prototype of using dynamic imports in ESM with what I think would work for the FF implementation:
const USER_FUNCTION = 'myFunc'
const USER_FILE = './foo.js'
async function run() {
const myFile = await import(USER_FILE);
const res = myFile[USER_FUNCTION]();
console.log(res);
}
run();
export const myFunc = () => 3; {
"name": "dynamic-import",
"main": "index.js",
"type": "module",
"scripts": {
"start": "node ."
}
} You got to be pretty careful with how you load your file. |
@grant isn't |
I don't think the FF user needs to provide module exports like that. Maybe provide an example if you think so? This case is very different than most modules that support ESM. The FF loads the user's code which can be a ESM, we don't use the FF itself as a module/library (it's a binary). |
Hey folks, just wanted to provide a nudge and a backlink to firebase/firebase-tools#2994 -- on the Firebase side we have 50+ developers 👍 requesting ESM support. We can't add it for Firebase until it gets added for FF, so if it's possible to make progress that would be great for our users! |
@mbleigh @grant the approach of using dyamic imports seems pretty smart, perhaps we could start with this being something that users opt into, with a slightly modified Test with the folks who are excited for this feature in |
Just adding my +1 to this as it's currently preventing me using ES modules in Cloud Functions (not firebase). |
This feature stopped working today (or, yesterday):
|
This still isnt working for me any working solution to fix this? |
@arcinston this feature should be working. Please open a new bug describing your project configuration if you are having issues. |
Right now this module doesn't support ESM. If you try to use modules, your function will not load.
I believe there are a few things we need to do to allow functions using modules to be supported:
Repro
Allow modules in
package.json
Use ESM import/export
Observe error:
Solution
Here's the rough solution I think, after prototyping a bit:
loader.js#getUserFunction
:const functionModulePath = await getFunctionModulePath(codeLocation);
loader.js#getFunctionModulePath
await import(codeLocation)
; wherecodeLocation is full file (including index.js part)
.require.resolve(codeLocation)
, normal requireindex.js
: We need to async/awaitgetUserFunction
:CC: @bcoe
The text was updated successfully, but these errors were encountered: