-
-
Save jdrew1303/dd1a04e4f715b0d2813a44197d11678e to your computer and use it in GitHub Desktop.
Run Angular 4 app in NodeJS using jsdom instead of a real DOM
This file contains hidden or 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
import 'zone.js'; | |
import 'reflect-metadata'; | |
import * as path from 'path'; | |
import * as fs from 'fs'; | |
// import { XMLHttpRequest } from 'xmlhttprequest'; | |
import { JSDOM } from 'jsdom'; | |
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; | |
import { ResourceLoader } from '@angular/compiler'; | |
import { AppModule } from './app/app.module'; | |
const dom = new JSDOM('<!DOCTYPE html><html><app-root></app-root></html>'); | |
// global['XMLHttpRequest'] = XMLHttpRequest; | |
global['document'] = dom.window.document; | |
const p = platformBrowserDynamic(); | |
class FileSystemResourceLoader extends ResourceLoader { | |
get(url: string): Promise<string> { | |
return new Promise((resolve, reject) => { | |
fs.readFile(url, 'utf8', (err, data) => { | |
err ? reject(err) : resolve(data); | |
}); | |
}); | |
} | |
} | |
p.bootstrapModule(AppModule, { | |
providers: [ | |
{provide: ResourceLoader, useClass: FileSystemResourceLoader} | |
] | |
}) | |
.then(ngModule => console.log(dom.serialize())) | |
.catch(err => console.log(err)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment