-
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.
refactor(@angular-devkit/build-angular): update ESM in memory file lo…
…ader to work with Node.js 20 This commit refactors the ESM Node.js in memory loader to work with Node.js 20+
- Loading branch information
1 parent
91019bd
commit 69d157d
Showing
6 changed files
with
76 additions
and
23 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
40 changes: 40 additions & 0 deletions
40
...lar_devkit/build_angular/src/utils/server-rendering/esm-in-memory-loader/node-18-utils.ts
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,40 @@ | ||
/** | ||
* @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.io/license | ||
*/ | ||
|
||
import { join } from 'node:path'; | ||
import { pathToFileURL } from 'node:url'; | ||
import { workerData } from 'node:worker_threads'; | ||
|
||
let IS_NODE_18: boolean | undefined; | ||
function isNode18(): boolean { | ||
return (IS_NODE_18 ??= process.versions.node.startsWith('18.')); | ||
} | ||
|
||
/** Call the initialize hook when running on Node.js 18 */ | ||
export function callInitializeIfNeeded( | ||
initialize: (typeof import('./loader-hooks'))['initialize'], | ||
): void { | ||
if (isNode18()) { | ||
initialize(workerData); | ||
} | ||
} | ||
|
||
export function getESMLoaderArgs(): string[] { | ||
if (isNode18()) { | ||
return [ | ||
'--no-warnings', // Suppress `ExperimentalWarning: Custom ESM Loaders is an experimental feature...`. | ||
'--loader', | ||
pathToFileURL(join(__dirname, 'loader-hooks.js')).href, // Loader cannot be an absolute path on Windows. | ||
]; | ||
} | ||
|
||
return [ | ||
'--import', | ||
pathToFileURL(join(__dirname, 'register-hooks.js')).href, // Loader cannot be an absolute path on Windows. | ||
]; | ||
} |
15 changes: 15 additions & 0 deletions
15
...ar_devkit/build_angular/src/utils/server-rendering/esm-in-memory-loader/register-hooks.ts
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,15 @@ | ||
/** | ||
* @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.io/license | ||
*/ | ||
|
||
// TODO: remove the below once @types/node are version 20.x.x | ||
// @ts-expect-error "node:module"' has no exported member 'register'.ts(2305) | ||
import { register } from 'node:module'; | ||
import { pathToFileURL } from 'node:url'; | ||
import { workerData } from 'node:worker_threads'; | ||
|
||
register('./loader-hooks.js', pathToFileURL(__filename), { data: workerData }); |
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