-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.ts
105 lines (85 loc) · 2.4 KB
/
main.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { enableProdMode } from '@angular/core';
import { NestFactory } from '@nestjs/core';
import { ApplicationModule } from './app.module';
import { join } from 'path';
export interface IMock {
[key: string]: any;
}
/*
* Window | Document | Global mocking
*/
export function createWindowMocks(
template: string,
additionalWindowMocks: IMock = {},
globalNodeMocks: IMock = {}
) {
if (!template && typeof template !== 'string') {
console.error(`
A template of your index.html file must be provided.
Example:
import { createWindowMocks } from '@trilon/ng-universal';
const template = readFileSync(join(DIST_FOLDER, 'Your_CLI_Project_Name', 'index.html')).toString();
createWindowMocks(template);
`);
return;
}
const domino = require('domino');
const win = domino.createWindow(template);
const noop = () => {};
win.scrollTo = noop;
win.screen = {};
win.alert = noop;
win.requestAnimationFrame = noop;
Object.keys(additionalWindowMocks).forEach(key => {
win[key] = additionalWindowMocks[key];
});
global['window'] = win;
global['document'] = win.document;
global['navigator'] = {};
global['CSS'] = null;
global['Prism'] = null;
global['HTMLElement'] = null;
global['HTMLElement'] = win.HTMLElement;
global['DOMTokenList'] = win.DOMTokenList;
global['Node'] = win.Node;
global['Text'] = win.Text;
global['HTMLCanvasElement'] = win.HTMLCanvasElement;
global['navigator'] = win.navigator;
global['MutationObserver'] = getMockMutationObserver();
global['requestAnimationFrame'] = noop;
console.log('INSIDE WINDOW MOCKS');
console.log(document);
Object.keys(globalNodeMocks).forEach(key => {
global[key] = globalNodeMocks[key];
});
Object.defineProperty(win.document.body.style, 'transform', {
value: () => {
return {
enumerable: true,
configurable: true
};
}
});
}
export function getMockMutationObserver() {
return class {
observe(node, options) {
}
disconnect() {
}
takeRecords() {
return [];
}
};
}
enableProdMode();
async function bootstrap() {
const html = join(process.cwd(), 'dist/browser', 'index.html');
createWindowMocks(html);
console.log('NEST BOOTSTRAP');
console.log('~~~~~~~~~~~~~~~~~~~~');
const app = await NestFactory.create(ApplicationModule);
app.setGlobalPrefix('api');
await app.listen(4000);
}
bootstrap();