From ac84468f1cea7ec33ecf68c9ddfc3ac29245fb2f Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Tue, 14 Jun 2016 12:34:31 -0700 Subject: [PATCH] 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 --- .../@angular/platform-server/core_private.ts | 7 +- modules/@angular/platform-server/index.dart | 2 +- modules/@angular/platform-server/index.ts | 3 +- .../@angular/platform-server/src/server.ts | 46 +++++++ .../platform-server/test/integration_spec.ts | 34 +++++ .../platform-server/testing/server.ts | 2 +- tools/public_api_guard/public_api_spec.ts | 126 +----------------- 7 files changed, 89 insertions(+), 131 deletions(-) create mode 100644 modules/@angular/platform-server/src/server.ts create mode 100644 modules/@angular/platform-server/test/integration_spec.ts diff --git a/modules/@angular/platform-server/core_private.ts b/modules/@angular/platform-server/core_private.ts index 1c7f41a379..c5b8be3d1f 100644 --- a/modules/@angular/platform-server/core_private.ts +++ b/modules/@angular/platform-server/core_private.ts @@ -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; diff --git a/modules/@angular/platform-server/index.dart b/modules/@angular/platform-server/index.dart index 61ae1b6809..ff9d666822 100644 --- a/modules/@angular/platform-server/index.dart +++ b/modules/@angular/platform-server/index.dart @@ -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'; diff --git a/modules/@angular/platform-server/index.ts b/modules/@angular/platform-server/index.ts index 2676ccb72c..b144175511 100644 --- a/modules/@angular/platform-server/index.ts +++ b/modules/@angular/platform-server/index.ts @@ -1,2 +1 @@ -// TODO: vsavkin add SERVER_PROVIDERS and SERVER_APP_PROVIDERS -export {Parse5DomAdapter} from './src/parse5_adapter'; +export {SERVER_APPLICATION_PROVIDERS, SERVER_PLATFORM_PROVIDERS, serverBootstrap} from './src/server'; diff --git a/modules/@angular/platform-server/src/server.ts b/modules/@angular/platform-server/src/server.ts new file mode 100644 index 0000000000..7507951314 --- /dev/null +++ b/modules/@angular/platform-server/src/server.ts @@ -0,0 +1,46 @@ +import {PlatformLocation} from '@angular/common'; +import {ComponentRef, OpaqueToken, PLATFORM_COMMON_PROVIDERS, PLATFORM_INITIALIZER, PlatformRef, ReflectiveInjector, Type, assertPlatform, coreLoadAndBootstrap, createPlatform, getPlatform} from '@angular/core'; +import {BROWSER_APP_COMPILER_PROVIDERS, BROWSER_APP_PROVIDERS, BrowserPlatformLocation} from '@angular/platform-browser'; + +import {ReflectionCapabilities, reflector, wtfInit} from '../core_private'; + +import {Parse5DomAdapter} from './parse5_adapter'; + +const SERVER_PLATFORM_MARKER = new OpaqueToken('ServerPlatformMarker'); + +/** + * A set of providers to initialize the Angular platform in a server. + * + * Used automatically by `serverBootstrap`, or can be passed to {@link platform}. + */ +export const SERVER_PLATFORM_PROVIDERS: Array = [ + {provide: SERVER_PLATFORM_MARKER, useValue: true}, PLATFORM_COMMON_PROVIDERS, + {provide: PLATFORM_INITIALIZER, useValue: initParse5Adapter, multi: true}, + {provide: PlatformLocation, useClass: BrowserPlatformLocation} +]; + +export const SERVER_APPLICATION_PROVIDERS: Array = + [BROWSER_APP_PROVIDERS, BROWSER_APP_COMPILER_PROVIDERS]; + +function initParse5Adapter() { + Parse5DomAdapter.makeCurrent(); + wtfInit(); +} + + +export function serverPlatform(): PlatformRef { + if (!getPlatform()) { + createPlatform(ReflectiveInjector.resolveAndCreate(SERVER_PLATFORM_PROVIDERS)); + } + return assertPlatform(SERVER_PLATFORM_MARKER); +} + + +export function serverBootstrap( + appComponentType: Type, + customProviders?: Array): Promise> { + reflector.reflectionCapabilities = new ReflectionCapabilities(); + let providers = [SERVER_APPLICATION_PROVIDERS, customProviders || []]; + var appInjector = ReflectiveInjector.resolveAndCreate(providers, serverPlatform().injector); + return coreLoadAndBootstrap(appComponentType, appInjector); +} diff --git a/modules/@angular/platform-server/test/integration_spec.ts b/modules/@angular/platform-server/test/integration_spec.ts new file mode 100644 index 0000000000..57995d27fe --- /dev/null +++ b/modules/@angular/platform-server/test/integration_spec.ts @@ -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 {getDOM} from '@angular/platform-browser/src/dom/dom_adapter'; +import {serverBootstrap} from '@angular/platform-server'; + +import {Parse5DomAdapter} from '../src/parse5_adapter'; + +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(''); + serverBootstrap(MyServerApp).then(() => { + expect(getDOM().getText(body)).toEqual('Works!'); + }); + })); + }); +} + +@Component({selector: 'app', template: `Works!`}) +class MyServerApp { +} diff --git a/modules/@angular/platform-server/testing/server.ts b/modules/@angular/platform-server/testing/server.ts index 9e6713a14c..9c6ffa6739 100644 --- a/modules/@angular/platform-server/testing/server.ts +++ b/modules/@angular/platform-server/testing/server.ts @@ -5,7 +5,7 @@ import {APPLICATION_COMMON_PROVIDERS, APP_ID, NgZone, PLATFORM_COMMON_PROVIDERS, import {BrowserDetection, DOMTestComponentRenderer} from '@angular/platform-browser/testing'; import {AnimationDriver, NoOpAnimationDriver} from '../core_private'; -import {Parse5DomAdapter} from '../index'; +import {Parse5DomAdapter} from '../src/parse5_adapter'; import {DOCUMENT, BROWSER_SANITIZATION_PROVIDERS, EventManager, EVENT_MANAGER_PLUGINS, ELEMENT_PROBE_PROVIDERS, DomEventsPlugin,} from '@angular/platform-browser'; import {getDOM, DomRootRenderer, DomRootRenderer_, DomSharedStylesHost, SharedStylesHost} from '../platform_browser_private'; diff --git a/tools/public_api_guard/public_api_spec.ts b/tools/public_api_guard/public_api_spec.ts index 4d6dc0289c..d2f726a48e 100644 --- a/tools/public_api_guard/public_api_spec.ts +++ b/tools/public_api_guard/public_api_spec.ts @@ -1655,129 +1655,9 @@ const PLATFORM_BROWSER_TESTING_E2E = [ ]; const PLATFORM_SERVER = [ - 'Parse5DomAdapter', - 'Parse5DomAdapter.addClass(element:any, className:string):any', - 'Parse5DomAdapter.adoptNode(node:any):any', - 'Parse5DomAdapter.animate(element:any, keyframes:any[], options:any):any', - 'Parse5DomAdapter.appendChild(el:any, node:any):any', - 'Parse5DomAdapter.attributeMap(element:any):Map', - 'Parse5DomAdapter.attrToPropMap:any', - 'Parse5DomAdapter.cancelAnimationFrame(id:number):any', - 'Parse5DomAdapter.childNodes(el:any):Node[]', - 'Parse5DomAdapter.childNodesAsList(el:any):any[]', - 'Parse5DomAdapter.classList(element:any):string[]', - 'Parse5DomAdapter.clearNodes(el:any):any', - 'Parse5DomAdapter.clone(node:Node):Node', - 'Parse5DomAdapter.content(node:any):string', - 'Parse5DomAdapter.createComment(text:string):Comment', - 'Parse5DomAdapter.createElement(tagName:any):HTMLElement', - 'Parse5DomAdapter.createElementNS(ns:any, tagName:any):HTMLElement', - 'Parse5DomAdapter.createEvent(eventType:string):Event', - 'Parse5DomAdapter.createHtmlDocument():Document', - 'Parse5DomAdapter.createMouseEvent(eventType:any):Event', - 'Parse5DomAdapter.createScriptTag(attrName:string, attrValue:string):HTMLElement', - 'Parse5DomAdapter.createShadowRoot(el:any):HTMLElement', - 'Parse5DomAdapter.createStyleElement(css:string):HTMLStyleElement', - 'Parse5DomAdapter.createTemplate(html:any):HTMLElement', - 'Parse5DomAdapter.createTextNode(text:string):Text', - 'Parse5DomAdapter.defaultDoc():Document', - 'Parse5DomAdapter.dispatchEvent(el:any, evt:any):any', - 'Parse5DomAdapter.elementMatches(node:any, selector:string, matcher:any=null):boolean', - 'Parse5DomAdapter.firstChild(el:any):Node', - 'Parse5DomAdapter.getAnimationPrefix():string', - 'Parse5DomAdapter.getAttribute(element:any, attribute:string):string', - 'Parse5DomAdapter.getAttributeNS(element:any, ns:string, attribute:string):string', - 'Parse5DomAdapter.getBaseHref():string', - 'Parse5DomAdapter.getBoundingClientRect(el:any):any', - 'Parse5DomAdapter.getChecked(el:any):boolean', - 'Parse5DomAdapter.getComputedStyle(el:any):any', - 'Parse5DomAdapter.getCookie(name:string):string', - 'Parse5DomAdapter.getData(el:any, name:string):string', - 'Parse5DomAdapter.getDistributedNodes(el:any):Node[]', - 'Parse5DomAdapter.getElementsByClassName(element:any, name:string):HTMLElement[]', - 'Parse5DomAdapter.getElementsByTagName(element:any, name:string):HTMLElement[]', - 'Parse5DomAdapter.getEventKey(event:any):string', - 'Parse5DomAdapter.getGlobalEventTarget(target:string):any', - 'Parse5DomAdapter.getHistory():History', - 'Parse5DomAdapter.getHost(el:any):string', - 'Parse5DomAdapter.getHref(el:any):string', - 'Parse5DomAdapter.getInnerHTML(el:any):string', - 'Parse5DomAdapter.getLocation():Location', - 'Parse5DomAdapter.getOuterHTML(el:any):string', - 'Parse5DomAdapter.getProperty(el:any, name:string):any', - 'Parse5DomAdapter.getShadowRoot(el:any):Element', - 'Parse5DomAdapter.getStyle(element:any, styleName:string):string', - 'Parse5DomAdapter.getTemplateContent(el:any):Node', - 'Parse5DomAdapter.getText(el:any, isRecursive?:boolean):string', - 'Parse5DomAdapter.getTitle():string', - 'Parse5DomAdapter.getTransitionEnd():string', - 'Parse5DomAdapter.getUserAgent():string', - 'Parse5DomAdapter.getValue(el:any):string', - 'Parse5DomAdapter.getXHR():Type', - 'Parse5DomAdapter.hasAttribute(element:any, attribute:string):boolean', - 'Parse5DomAdapter.hasAttributeNS(element:any, ns:string, attribute:string):boolean', - 'Parse5DomAdapter.hasClass(element:any, className:string):boolean', - 'Parse5DomAdapter.hasProperty(element:any, name:string):boolean', - 'Parse5DomAdapter.hasShadowRoot(node:any):boolean', - 'Parse5DomAdapter.hasStyle(element:any, styleName:string, styleValue:string=null):boolean', - 'Parse5DomAdapter.importIntoDoc(node:any):any', - 'Parse5DomAdapter.insertAfter(el:any, node:any):any', - 'Parse5DomAdapter.insertAllBefore(el:any, nodes:any):any', - 'Parse5DomAdapter.insertBefore(el:any, node:any):any', - 'Parse5DomAdapter.invoke(el:Element, methodName:string, args:any[]):any', - 'Parse5DomAdapter.isCommentNode(node:any):boolean', - 'Parse5DomAdapter.isElementNode(node:any):boolean', - 'Parse5DomAdapter.isPrevented(evt:any):boolean', - 'Parse5DomAdapter.isShadowRoot(node:any):boolean', - 'Parse5DomAdapter.isTemplateElement(el:any):boolean', - 'Parse5DomAdapter.isTextNode(node:any):boolean', - 'Parse5DomAdapter.log(error:any):any', - 'Parse5DomAdapter.logError(error:any):any', - 'Parse5DomAdapter.logGroup(error:any):any', - 'Parse5DomAdapter.logGroupEnd():any', - 'Parse5DomAdapter.makeCurrent():any', - 'Parse5DomAdapter.nextSibling(el:any):Node', - 'Parse5DomAdapter.nodeName(node:any):string', - 'Parse5DomAdapter.nodeValue(node:any):string', - 'Parse5DomAdapter.on(el:any, evt:any, listener:any):any', - 'Parse5DomAdapter.onAndCancel(el:any, evt:any, listener:any):Function', - 'Parse5DomAdapter.parentElement(el:any):Node', - 'Parse5DomAdapter.parse(templateHtml:string):any', - 'Parse5DomAdapter.performanceNow():number', - 'Parse5DomAdapter.preventDefault(evt:any):any', - 'Parse5DomAdapter.query(selector:any):any', - 'Parse5DomAdapter.querySelector(el:any, selector:string):any', - 'Parse5DomAdapter.querySelectorAll(el:any, selector:string):any[]', - 'Parse5DomAdapter.remove(el:any):HTMLElement', - 'Parse5DomAdapter.removeAttribute(element:any, attribute:string):any', - 'Parse5DomAdapter.removeAttributeNS(element:any, ns:string, name:string):any', - 'Parse5DomAdapter.removeChild(el:any, node:any):any', - 'Parse5DomAdapter.removeClass(element:any, className:string):any', - 'Parse5DomAdapter.removeStyle(element:any, styleName:string):any', - 'Parse5DomAdapter.replaceChild(el:any, newNode:any, oldNode:any):any', - 'Parse5DomAdapter.requestAnimationFrame(callback:any):number', - 'Parse5DomAdapter.resetBaseElement():void', - 'Parse5DomAdapter.resolveAndSetHref(el:any, baseUrl:string, href:string):any', - 'Parse5DomAdapter.setAttribute(element:any, attribute:string, value:string):any', - 'Parse5DomAdapter.setAttributeNS(element:any, ns:string, attribute:string, value:string):any', - 'Parse5DomAdapter.setChecked(el:any, value:boolean):any', - 'Parse5DomAdapter.setCookie(name:string, value:string):any', - 'Parse5DomAdapter.setData(el:any, name:string, value:string):any', - 'Parse5DomAdapter.setGlobalVar(path:string, value:any):any', - 'Parse5DomAdapter.setInnerHTML(el:any, value:any):any', - 'Parse5DomAdapter.setProperty(el:any, name:string, value:any):any', - 'Parse5DomAdapter.setStyle(element:any, styleName:string, styleValue:string):any', - 'Parse5DomAdapter.setText(el:any, value:string):any', - 'Parse5DomAdapter.setTitle(newTitle:string):any', - 'Parse5DomAdapter.setValue(el:any, value:string):any', - 'Parse5DomAdapter.supportsAnimation():boolean', - 'Parse5DomAdapter.supportsCookies():boolean', - 'Parse5DomAdapter.supportsDOMEvents():boolean', - 'Parse5DomAdapter.supportsNativeShadowDOM():boolean', - 'Parse5DomAdapter.supportsWebAnimation():boolean', - 'Parse5DomAdapter.tagName(element:any):string', - 'Parse5DomAdapter.templateAwareRoot(el:any):any', - 'Parse5DomAdapter.type(node:any):string', + 'const SERVER_APPLICATION_PROVIDERS:Array', + 'const SERVER_PLATFORM_PROVIDERS:Array', + 'serverBootstrap(appComponentType:Type, customProviders?:Array):Promise>', ]; const PLATFORM_SERVER_TESTING = [