Skip to content

Commit

Permalink
fix: throw an error when server.ts calls run function
Browse files Browse the repository at this point in the history
  • Loading branch information
9kubczas4 committed Apr 9, 2024
1 parent de3f129 commit b6fdea3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
@@ -0,0 +1 @@
- (Angular 17+) throw an error when server.ts calls `run` function (#6651)
49 changes: 49 additions & 0 deletions src/frameworks/angular/utils.ts
Expand Up @@ -11,6 +11,45 @@ import { AssertionError } from "assert";
import { assertIsString } from "../../utils";
import { coerce } from "semver";

/**
* @param serverFilePath path to server.ts
* @return true when `run` function is called in server.ts, it breaks SSR (Angular 17+) on Firebase.
*/
const isRunImplementedAndCalledInServer = async (serverFilePath: string): Promise<boolean> => {
// Dynamically import the TypeScript compiler module
const ts = await import("typescript");
const sourceCode = ts.sys.readFile(serverFilePath);
const functionName = "run";

if (!sourceCode) {
return false;
}

const sourceFile = ts.createSourceFile("server.ts", sourceCode, ts.ScriptTarget.Latest, true);

let isRunDefined = false;
let isRunCalled = false;

// Recursive function to traverse the AST
const traverse = (node: any): void => {

Check warning on line 34 in src/frameworks/angular/utils.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
if (ts.isFunctionDeclaration(node) && node.name && node.name.getText() === functionName) {

Check warning on line 35 in src/frameworks/angular/utils.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe argument of type `any` assigned to a parameter of type `Node`
isRunDefined = true;
}
if (
ts.isCallExpression(node) &&

Check warning on line 39 in src/frameworks/angular/utils.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe argument of type `any` assigned to a parameter of type `Node`
ts.isIdentifier(node.expression) &&
node.expression.getText() === functionName
) {
isRunCalled = true;
}
ts.forEachChild(node, traverse);

Check warning on line 45 in src/frameworks/angular/utils.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe argument of type `any` assigned to a parameter of type `Node`
};

traverse(sourceFile);

return isRunDefined && isRunCalled;
};

async function localesForTarget(

Check warning on line 53 in src/frameworks/angular/utils.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing return type on function
dir: string,
architectHost: WorkspaceNodeModulesArchitectHost,
Expand Down Expand Up @@ -482,6 +521,16 @@ export async function getServerConfig(sourceDir: string, configuration: string)
);
}
const serverEntry = buildTarget ? "server.mjs" : serverTarget && "main.js";

if (
serverEntry === "server.mjs" &&
(await isRunImplementedAndCalledInServer(join(sourceDir, "server.ts")))
) {
throw new FirebaseError(
"For SSR to work properly, please remove the `run` function call from the `server.ts` file.",
);
}

const externalDependencies: string[] = (serverTargetOptions.externalDependencies as any) || [];
const bundleDependencies = serverTargetOptions.bundleDependencies ?? true;
const { locales: browserLocales } = await localesForTarget(
Expand Down

0 comments on commit b6fdea3

Please sign in to comment.