forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload-esm-from-memory.ts
50 lines (44 loc) · 1.82 KB
/
load-esm-from-memory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* @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 type { ApplicationRef, Type } from '@angular/core';
import type { ɵextractRoutesAndCreateRouteTree, ɵgetOrCreateAngularServerApp } from '@angular/ssr';
import { assertIsError } from '../error';
import { loadEsmModule } from '../load-esm';
/**
* Represents the exports available from the main server bundle.
*/
interface MainServerBundleExports {
default: (() => Promise<ApplicationRef>) | Type<unknown>;
ɵextractRoutesAndCreateRouteTree: typeof ɵextractRoutesAndCreateRouteTree;
ɵgetOrCreateAngularServerApp: typeof ɵgetOrCreateAngularServerApp;
}
/**
* Represents the exports available from the server bundle.
*/
interface ServerBundleExports {
reqHandler?: unknown;
}
export function loadEsmModuleFromMemory(
path: './main.server.mjs',
): Promise<MainServerBundleExports>;
export function loadEsmModuleFromMemory(path: './server.mjs'): Promise<ServerBundleExports>;
export function loadEsmModuleFromMemory(
path: './main.server.mjs' | './server.mjs',
): Promise<MainServerBundleExports | ServerBundleExports> {
return loadEsmModule(new URL(path, 'memory://')).catch((e) => {
assertIsError(e);
// While the error is an 'instanceof Error', it is extended with non transferable properties
// and cannot be transferred from a worker when using `--import`. This results in the error object
// displaying as '[Object object]' when read outside of the worker. Therefore, we reconstruct the error message here.
const error: Error & { code?: string } = new Error(e.message);
error.stack = e.stack;
error.name = e.name;
error.code = e.code;
throw error;
}) as Promise<MainServerBundleExports>;
}