Skip to content

Conversation

@simonnepomuk
Copy link
Contributor

Description

This should fix two issues:

  • When trying to integrate custom web frameworks through express the generated handle function tries to import the root path (where the package.json lies). Actually, it should import the file specified in the main property of the package.json
  • When trying to integrate custom web frameworks through express with "type": "module" in package.json the generated function fails to execute since the generated code contains a require(...) statement.

Should close:

Scenarios Tested

I linked the firebase-tools to my branch using npm link and ran the tests in the firebase-framework-tools repo. Is this how it should be done? I needed to replace the site ID in a couple of files to a site ID which I have access to.

This was also tested in the branch of the web framework support PR of my SvelteKit adapter with emulators:start and deploy.

@google-cla
Copy link

google-cla bot commented Nov 16, 2022

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@simonnepomuk simonnepomuk changed the title fix: use ES6 module syntax and use main file in express integration Web Frameworks support: Use ES6 module syntax and use main file in express integration Nov 16, 2022
@jamesdaniels jamesdaniels self-requested a review November 23, 2022 20:55
@jamesdaniels jamesdaniels self-assigned this Nov 23, 2022
@simonnepomuk
Copy link
Contributor Author

@jamesdaniels
I think this might need some functionality to determine if "type": "module" exists in the package.json and either load the adapted code from this PR or the preexisting code that is currently being replaced by this PR. Otherwise it probably breaks CJS code.

@jamesdaniels
Copy link
Member

@simonnepomuk thanks for this, I'll pull down on to my machine and experiment some more

Copy link
Member

@jamesdaniels jamesdaniels left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I'm testing on my machine, mind adding a changelog entry?

@simonnepomuk
Copy link
Contributor Author

While I'm testing on my machine, mind adding a changelog entry?

Will do.

Comment on lines 524 to 529
await writeFile(
join(functionsDist, "server.js"),
`const { onRequest } = require('firebase-functions/v2/https');
const server = import('firebase-frameworks');
exports.ssr = onRequest((req, res) => server.then(it => it.handle(req, res)));
`
`import { onRequest } from 'firebase-functions/v2/https';
const server = import('firebase-frameworks');
export const ssr = onRequest((req, res) => server.then(it => it.handle(req, res)));`
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with your comment on the PR that we will need some logic to retain the old CJS behavior as well.

Should be straightforward with something like if (packageJson.type === 'module') ....

We could extract the snippets to consts, keep them as inline literals, or even bring them out into the templates folder like the TODO comment suggests, but I'll defer to @jamesdaniels on that one.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could see

const importStatements = packageJson.type === 'module' ?
  "import { onRequest } from 'firebase-functions/v2/https';" : 
  "const { onRequest } = require('firebase-functions/v2/https');"`

if we wanted to DRY it up

const importsFrom = [["onRequest"], "firebase-functions/v2/https"];
const importStatements = importsFrom.map(([imports, from]) => packageJson.type === "module" ? ... : ...);
...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need the same for the export, right? Might be easier/clearer to just repeat the whole block.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SG, let's just repeat the block

return (
bootstrapScript +
";\nexports.handle = async (req, res) => (await bootstrap).handle(req, res);"
";\nexport const handle = async (req, res) => (await bootstrap).handle(req, res);"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment regarding CJS support

return (
bootstrapScript +
";\nexports.handle = async (req, res) => (await bootstrap).app(req, res);"
";\nexport const handle = async (req, res) => (await bootstrap).app(req, res);"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment regarding CJS support

@austincrim
Copy link
Contributor

@simonnepomuk Let me know if you're still up for getting this merged. If not, we can adapt your PR and move forward ourselves, with attribution back to your work.

@simonnepomuk
Copy link
Contributor Author

@simonnepomuk Let me know if you're still up for getting this merged. If not, we can adapt your PR and move forward ourselves, with attribution back to your work.

Hey @austincrim, thx for the review. My interest in using firebase dwindled over the last months because of some regulatory issues. Feel free to move forward.

@austincrim
Copy link
Contributor

#5540

@austincrim austincrim closed this Feb 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants