Skip to content

Commit

Permalink
Generate ESM-compatible web framework SSR function (#5540)
Browse files Browse the repository at this point in the history
---

Co-authored-by: Simon Wagner <[email protected]>
  • Loading branch information
austincrim and simonnepomuk committed Feb 22, 2023
1 parent 74f82cf commit c05b9cd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
- Allow configuration of the Cloud Function generated for full-stack web frameworks (#5504)
- Improve error message during deploy when given invalid hosting rewrite rule (#5533)
- Generate ESM-compatible SSR function for web frameworks (#5540)
31 changes: 22 additions & 9 deletions src/frameworks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,15 +572,28 @@ ${firebaseDefaults ? `__FIREBASE_DEFAULTS__=${JSON.stringify(firebaseDefaults)}\
if (bootstrapScript) await writeFile(join(functionsDist, "bootstrap.js"), bootstrapScript);

// TODO move to templates
await writeFile(
join(functionsDist, "server.js"),
`const { onRequest } = require('firebase-functions/v2/https');
const server = import('firebase-frameworks');
exports.${functionId} = onRequest(${JSON.stringify(
frameworksBackend || {}
)}, (req, res) => server.then(it => it.handle(req, res)));
`
);

if (packageJson.type === "module") {
await writeFile(
join(functionsDist, "server.js"),
`import { onRequest } from 'firebase-functions/v2/https';
const server = import('firebase-frameworks');
export const ${functionId} = onRequest(${JSON.stringify(
frameworksBackend || {}
)}, (req, res) => server.then(it => it.handle(req, res)));
`
);
} else {
await writeFile(
join(functionsDist, "server.js"),
`const { onRequest } = require('firebase-functions/v2/https');
const server = import('firebase-frameworks');
exports.${functionId} = onRequest(${JSON.stringify(
frameworksBackend || {}
)}, (req, res) => server.then(it => it.handle(req, res)));
`
);
}
} else {
// No function, treat as an SPA
// TODO(jamesdaniels) be smarter about this, leave it to the framework?
Expand Down

0 comments on commit c05b9cd

Please sign in to comment.