fix: cleanup public api of platform-server
BREAKING CHANGE: Parse5Adapter is no longer exported as public API, use serverBootstrap() Parse5Adapter is an implementation detail not a public API Closes #9237 Closes #9205
This commit is contained in:
@ -1,9 +1,8 @@
|
||||
import {__core_private__ as r, __core_private_types__ as t} from '@angular/core';
|
||||
|
||||
export type NoOpAnimationPlayer = t.NoOpAnimationPlayer;
|
||||
export var NoOpAnimationPlayer: typeof t.NoOpAnimationPlayer = r.NoOpAnimationPlayer;
|
||||
export type AnimationPlayer = t.AnimationPlayer;
|
||||
export var AnimationPlayer: typeof t.AnimationPlayer = r.AnimationPlayer;
|
||||
export var reflector: typeof t.reflector = r.reflector;
|
||||
export var ReflectionCapabilities: typeof t.ReflectionCapabilities = r.ReflectionCapabilities;
|
||||
export var wtfInit: typeof t.wtfInit = r.wtfInit;
|
||||
export type NoOpAnimationDriver = t.NoOpAnimationDriver;
|
||||
export var NoOpAnimationDriver: typeof t.NoOpAnimationDriver = r.NoOpAnimationDriver;
|
||||
export type AnimationDriver = t.AnimationDriver;
|
||||
|
@ -1,2 +1,2 @@
|
||||
// TODO: vsavkin add SERVER_PROVIDERS and SERVER_APP_PROVIDERS
|
||||
// TODO: vsavkin add SERVER_PROVIDERS and SERVER_APPLICATION_PROVIDERS
|
||||
export 'package:angular2/src/platform/server/html_adapter.dart';
|
||||
|
@ -1,2 +1 @@
|
||||
// TODO: vsavkin add SERVER_PROVIDERS and SERVER_APP_PROVIDERS
|
||||
export {Parse5DomAdapter} from './src/parse5_adapter';
|
||||
export {SERVER_PLATFORM_PROVIDERS, serverBootstrap, serverPlatform} from './src/server';
|
||||
|
77
modules/@angular/platform-server/src/server.ts
Normal file
77
modules/@angular/platform-server/src/server.ts
Normal file
@ -0,0 +1,77 @@
|
||||
import {PlatformLocation} from '@angular/common';
|
||||
import {ComponentRef, OpaqueToken, PLATFORM_COMMON_PROVIDERS, PLATFORM_INITIALIZER, PlatformRef, ReflectiveInjector, Type, assertPlatform, coreLoadAndBootstrap, createPlatform, getPlatform} from '@angular/core';
|
||||
|
||||
import {ReflectionCapabilities, reflector, wtfInit} from '../core_private';
|
||||
|
||||
import {Parse5DomAdapter} from './parse5_adapter';
|
||||
|
||||
const SERVER_PLATFORM_MARKER = new OpaqueToken('ServerPlatformMarker');
|
||||
|
||||
function notSupported(feature: string): Error {
|
||||
throw new Error(`platform-server does not support '${feature}'.`);
|
||||
}
|
||||
|
||||
class ServerPlatformLocation extends PlatformLocation {
|
||||
getBaseHrefFromDOM(): string { throw notSupported('getBaseHrefFromDOM'); };
|
||||
onPopState(fn: any): void { notSupported('onPopState'); };
|
||||
onHashChange(fn: any): void { notSupported('onHashChange'); };
|
||||
get pathname(): string { throw notSupported('pathname'); }
|
||||
get search(): string { throw notSupported('search'); }
|
||||
get hash(): string { throw notSupported('hash'); }
|
||||
replaceState(state: any, title: string, url: string): void { notSupported('replaceState'); };
|
||||
pushState(state: any, title: string, url: string): void { notSupported('pushState'); };
|
||||
forward(): void { notSupported('forward'); };
|
||||
back(): void { notSupported('back'); };
|
||||
}
|
||||
|
||||
/**
|
||||
* A set of providers to initialize the Angular platform in a server.
|
||||
*
|
||||
* Used automatically by `serverBootstrap`, or can be passed to {@link platform}.
|
||||
* @experimental
|
||||
*/
|
||||
export const SERVER_PLATFORM_PROVIDERS: Array<any /*Type | Provider | any[]*/> = [
|
||||
{provide: SERVER_PLATFORM_MARKER, useValue: true}, PLATFORM_COMMON_PROVIDERS,
|
||||
{provide: PLATFORM_INITIALIZER, useValue: initParse5Adapter, multi: true},
|
||||
{provide: PlatformLocation, useClass: ServerPlatformLocation}
|
||||
];
|
||||
|
||||
|
||||
function initParse5Adapter() {
|
||||
Parse5DomAdapter.makeCurrent();
|
||||
wtfInit();
|
||||
}
|
||||
|
||||
/**
|
||||
* @experimental
|
||||
*/
|
||||
export function serverPlatform(): PlatformRef {
|
||||
if (!getPlatform()) {
|
||||
createPlatform(ReflectiveInjector.resolveAndCreate(SERVER_PLATFORM_PROVIDERS));
|
||||
}
|
||||
return assertPlatform(SERVER_PLATFORM_MARKER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to bootstrap Angular in server environment (such as node).
|
||||
*
|
||||
* This version of bootstrap only creates platform injector and does not define anything for
|
||||
* application injector. It is expected that application providers are imported from other
|
||||
* packages such as `@angular/platform-browser` or `@angular/platform-browser-dynamic`.
|
||||
*
|
||||
* ```
|
||||
* import {BROWSER_APP_PROVIDERS} from '@angular/platform-browser';
|
||||
* import {BROWSER_APP_COMPILER_PROVIDERS} from '@angular/platform-browser-dynamic';
|
||||
*
|
||||
* serverBootstrap(..., [BROWSER_APP_PROVIDERS, BROWSER_APP_COMPILER_PROVIDERS])
|
||||
* ```
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
export function serverBootstrap(
|
||||
appComponentType: Type,
|
||||
providers: Array<any /*Type | Provider | any[]*/>): Promise<ComponentRef<any>> {
|
||||
reflector.reflectionCapabilities = new ReflectionCapabilities();
|
||||
var appInjector = ReflectiveInjector.resolveAndCreate(providers, serverPlatform().injector);
|
||||
return coreLoadAndBootstrap(appComponentType, appInjector);
|
||||
}
|
34
modules/@angular/platform-server/test/integration_spec.ts
Normal file
34
modules/@angular/platform-server/test/integration_spec.ts
Normal file
@ -0,0 +1,34 @@
|
||||
import {Component, disposePlatform} from '@angular/core';
|
||||
import {afterEach, async, beforeEach, ddescribe, describe, expect, iit, inject, it, xdescribe, xit} from '@angular/core/testing';
|
||||
import {BROWSER_APP_PROVIDERS} from '@angular/platform-browser';
|
||||
import {BROWSER_APP_COMPILER_PROVIDERS} from '@angular/platform-browser-dynamic';
|
||||
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
|
||||
import {serverBootstrap} from '@angular/platform-server';
|
||||
|
||||
function writeBody(html: string): any {
|
||||
var dom = getDOM();
|
||||
var doc = dom.defaultDoc();
|
||||
var body = dom.querySelector(doc, 'body');
|
||||
dom.setInnerHTML(body, html);
|
||||
return body;
|
||||
}
|
||||
|
||||
export function main() {
|
||||
if (getDOM().supportsDOMEvents()) return; // NODE only
|
||||
|
||||
describe('platform-server integration', () => {
|
||||
|
||||
afterEach(() => disposePlatform());
|
||||
|
||||
it('should bootstrap', async(() => {
|
||||
var body = writeBody('<app></app>');
|
||||
serverBootstrap(MyServerApp, [
|
||||
BROWSER_APP_PROVIDERS, BROWSER_APP_COMPILER_PROVIDERS
|
||||
]).then(() => { expect(getDOM().getText(body)).toEqual('Works!'); });
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
@Component({selector: 'app', template: `Works!`})
|
||||
class MyServerApp {
|
||||
}
|
Reference in New Issue
Block a user