-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@angular/ssr): add
isMainModule
function
Adds a new function `isMainModule` that checks if the current module is the main entry point of the application. This is useful to ensure that server listener handlers are only registered when the module is executed directly and not when it's imported as a dependency such as the dev-server. This prevents potential issues with multiple listeners being registered unintentionally.
- Loading branch information
1 parent
09d2eb9
commit f346ee8
Showing
3 changed files
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.dev/license | ||
*/ | ||
|
||
import { argv } from 'node:process'; | ||
import { fileURLToPath } from 'node:url'; | ||
|
||
/** | ||
* Determines whether the provided URL represents the main entry point module. | ||
* | ||
* This function checks if the provided URL corresponds to the main ESM module being executed directly. | ||
* It's useful for conditionally executing code that should only run when a module is the entry point, | ||
* such as starting a server or initializing an application. | ||
* | ||
* It performs two key checks: | ||
* 1. Verifies if the URL starts with 'file:', ensuring it is a local file. | ||
* 2. Compares the URL's resolved file path with the first command-line argument (`process.argv[1]`), | ||
* which points to the file being executed. | ||
* | ||
* @param url The URL of the module to check. This should typically be `import.meta.url`. | ||
* @returns `true` if the provided URL represents the main entry point, otherwise `false`. | ||
* @developerPreview | ||
*/ | ||
export function isMainModule(url: string): boolean { | ||
return url.startsWith('file:') && argv[1] === fileURLToPath(url); | ||
} |