-
Notifications
You must be signed in to change notification settings - Fork 49
Global refactoring. Added support for serveral angular2 applications. #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alexpods
wants to merge
1
commit into
master
Choose a base branch
from
multiapp2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { Router } from 'angular2/router'; | ||
|
|
||
| document.addEventListener('DOMContentLoaded', function onDOMContentLoaded() { | ||
| const providers = []; | ||
|
|
||
| window[__APPS_BROWSER_BUNDLE_NAME__].main(providers).then(function(compRef) { | ||
| const injector = compRef.injector; | ||
| const router: Router = injector.getOptional(Router); | ||
|
|
||
| return Promise.resolve() | ||
| .then(() => router && (<any> router)._currentNavigation) | ||
| .then(() => new Promise(function(resolve) { setTimeout(resolve) })) | ||
| .then(() => document.dispatchEvent(new Event('BootstrapComplete'))) | ||
| }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { Router } from 'angular2/router'; | ||
|
|
||
| const global = new Function('return this')(); | ||
|
|
||
| setTimeout(function() { | ||
| const providers = []; | ||
|
|
||
| global[__APPS_WORKER_APP_BUNDLE_NAME__].main(providers).then(function(compRef) { | ||
| const injector = compRef.injector; | ||
| const router: Router = injector.getOptional(Router); | ||
|
|
||
| return Promise.resolve() | ||
| .then(() => router && (<any> router)._currentNavigation) | ||
| .then(() => new Promise(function(resolve) { setTimeout(resolve) })) | ||
| .then(() => postMessage('APP_READY', undefined)) | ||
| }); | ||
| }, 100); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import { Provider } from 'angular2/core'; | ||
| import { WebWorkerInstance, WORKER_SCRIPT } from 'angular2/platform/worker_render'; | ||
|
|
||
| document.addEventListener('DOMContentLoaded', function onDOMContentLoaded() { | ||
| const providers = [ | ||
| new Provider(WORKER_SCRIPT, { useValue: window['__WORKER_SCRIPT_URL'] }) | ||
| ]; | ||
|
|
||
| const appRef = window[__APPS_WORKER_UI_BUNDLE_NAME__].main(providers); | ||
| const worker = appRef.injector.get(WebWorkerInstance).worker; | ||
|
|
||
| worker.addEventListener('message', function onAppReady(event) { | ||
| if (event.data === 'APP_READY') { | ||
| worker.removeEventListener('message', onAppReady, false); | ||
| URL.revokeObjectURL(window['__WORKER_SCRIPT_URL']); | ||
| delete window['__WORKER_SCRIPT_URL']; | ||
| setTimeout(function() { document.dispatchEvent(new Event('BootstrapComplete')) }); | ||
| } | ||
| }, false); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| import { Request, Response } from 'express'; | ||
| import { resolve as resolvePath, relative as relativePath } from 'path'; | ||
| import { platform, Provider, ComponentRef, ExceptionHandler } from 'angular2/core'; | ||
| import { DOM } from 'angular2/src/platform/dom/dom_adapter'; | ||
| import { DOCUMENT } from 'angular2/platform/common_dom'; | ||
| import { ROUTER_PROVIDERS, APP_BASE_HREF, Router } from 'angular2/router'; | ||
| import { REQUEST_URL } from 'angular2-universal-preview'; | ||
|
|
||
| const { | ||
| createPrebootHTML, | ||
| getBrowserCode, | ||
| prebootConfigDefault | ||
| } = require('angular2-universal-preview'); | ||
|
|
||
| const { | ||
| parseDocument, | ||
| serializeDocument | ||
| } = require('angular2-universal-preview/dist/server/src/platform/document'); | ||
|
|
||
| const req = __non_webpack_require__; | ||
|
|
||
| const VENDOR_PATH = req.resolve(__PUBLIC_DIR__ + '/' + __VENDOR_BUNDLE_NAME__); | ||
| const VENDOR_URL = '/' + relativePath(__PUBLIC_DIR__, VENDOR_PATH); | ||
|
|
||
| const WORKER_APP_LOADER_ORIGIN = 'window.location.origin'; | ||
| const WORKER_APP_READY_MESSAGE = 'APP_READY'; | ||
|
|
||
| function createBrowserScripts(browserUrl) { | ||
| return ` | ||
| <script type="text/javascript" src="${VENDOR_URL}"></script> | ||
| <script type="text/javascript" src="${browserUrl}"></script> | ||
| `; | ||
| } | ||
|
|
||
| function createWorkerScripts(workerUrl, workerAppUrl) { | ||
| return ` | ||
| <script type="text/javascript" src="${VENDOR_URL}"></script> | ||
| <script type="text/javascript" src="${workerUrl}"></script> | ||
| <script> | ||
| window.__WORKER_SCRIPT_URL = URL.createObjectURL(new Blob([ | ||
| "${createWorkerAppScripts(workerAppUrl).replace(/\n/g, '\\n\\\n')}" | ||
| ], { | ||
| type: 'text/javascript' | ||
| })); | ||
| </script> | ||
| ` | ||
| } | ||
|
|
||
| function createWorkerAppScripts(workerAppUrl) { | ||
| return ` | ||
| var importScripts_ = this.importScripts; | ||
|
|
||
| this.importScripts = function importScripts() { | ||
| for (var i = 0, scripts = new Array(arguments.length); i < scripts.length; ++i) { | ||
| var script = arguments[i]; | ||
| var origin = '" + ${WORKER_APP_LOADER_ORIGIN} + "'; | ||
|
|
||
| if (script.indexOf('http:') !== 0 || script.indexOf('https:') !== 0) { | ||
| script = origin + (script[0] === '/' ? script : '/' + script); | ||
| } | ||
|
|
||
| scripts[i] = script; | ||
| } | ||
|
|
||
| return importScripts_.apply(this, scripts); | ||
| }; | ||
|
|
||
| importScripts('${VENDOR_URL}', '${workerAppUrl}'); | ||
| `; | ||
| } | ||
|
|
||
| export function serveUniversal(name, indexHtml, options: any = {}) { | ||
| let server, browserScripts = '', workerScripts = '', prebootHtml = '', prebootPromise = Promise.resolve(); | ||
|
|
||
| if (options.server) { | ||
| const serverPath = typeof options.server === 'string' | ||
| ? options.server | ||
| : resolvePath(__PRIVATE_DIR__, name + '-' + __APPS_SERVER_BUNDLE_NAME__ + '.js'); | ||
|
|
||
| server = req(serverPath); | ||
| } | ||
|
|
||
| if (options.browser) { | ||
| const browserPath = typeof options.browser === 'string' | ||
| ? options.browser | ||
| : resolvePath(__PUBLIC_DIR__, name + '-' + __APPS_BROWSER_BUNDLE_NAME__ + '.js'); | ||
|
|
||
| const browserUrl = '/' + relativePath(__PUBLIC_DIR__, browserPath); | ||
|
|
||
| browserScripts = createBrowserScripts(browserUrl); | ||
| } | ||
|
|
||
| if (options.worker) { | ||
| const workerPaths = Array.isArray(options.worker) | ||
| ? options.worker | ||
| : [ | ||
| resolvePath(__PUBLIC_DIR__, name + '-' + __APPS_WORKER_UI_BUNDLE_NAME__ + '.js'), | ||
| resolvePath(__PUBLIC_DIR__, name + '-' + __APPS_WORKER_APP_BUNDLE_NAME__+ '.js') | ||
| ]; | ||
|
|
||
| const workerUrl = '/' + relativePath(__PUBLIC_DIR__, workerPaths[0]); | ||
| const workerAppUrl = '/' + relativePath(__PUBLIC_DIR__, workerPaths[1]); | ||
|
|
||
| workerScripts = createWorkerScripts(workerUrl, workerAppUrl); | ||
| } | ||
|
|
||
| const prebootOptions = options.preboot !== false | ||
| ? prebootConfigDefault(Object.assign({}, __PREBOOT__, options.preboot)) | ||
| : false; | ||
|
|
||
| return (req: Request, res: Response, next: Function) => prebootPromise.then(() => Promise.resolve(indexHtml)) | ||
| .then((html) => { | ||
| if (__SS__ && server) { | ||
| const REQUEST_PROVIDERS = [ | ||
| new Provider(ExceptionHandler, { useFactory: () => new ExceptionHandler(DOM, true) }), | ||
| new Provider(DOCUMENT, { useValue: parseDocument(html) }), | ||
| new Provider(REQUEST_URL, { useValue: req.originalUrl }), | ||
| ]; | ||
|
|
||
| return server.main(REQUEST_PROVIDERS, req).then(function(compRef) { | ||
| const injector = compRef.injector; | ||
| const router: Router = injector.getOptional(Router); | ||
|
|
||
| return Promise.resolve() | ||
| .then(() => router && (<any> router)._currentNavigation) | ||
| .then(() => new Promise(function(resolve) { setTimeout(resolve) })) | ||
| .then(() => serializeDocument(compRef.injector.get(DOCUMENT))) | ||
| }); | ||
| } | ||
| return html; | ||
| }) | ||
| .then((html) => { | ||
| if (prebootOptions && (workerScripts || browserScripts)) { | ||
| return getBrowserCode(prebootOptions).then((code) => { | ||
| const prebootHtml = createPrebootHTML(code, prebootOptions); | ||
|
|
||
| return html.replace('</body>', prebootHtml + '</body>') | ||
| }); | ||
| } | ||
| return html; | ||
| }) | ||
| .then((html) => { | ||
| let scripts = ''; | ||
|
|
||
| if (__WW__ && workerScripts) { | ||
| scripts = workerScripts; | ||
| } else if (browserScripts) { | ||
| scripts = browserScripts; | ||
| } | ||
|
|
||
| return res.status(200).send(scripts ? html.replace('</body>', scripts + '</body>') : html); | ||
| }) | ||
| .catch(err => next(err)) | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { Component } from 'angular2/core'; | ||
|
|
||
| @Component({ | ||
| selector: 'app', | ||
| template: '<h1>Admin application</h1>' | ||
| }) | ||
| export class App {} |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { Provider } from 'angular2/core'; | ||
| import { bootstrap } from 'angular2/platform/browser'; | ||
| import { ROUTER_PROVIDERS, APP_BASE_HREF } from 'angular2/router'; | ||
| import { App } from './app'; | ||
|
|
||
| export function main(BOOT_PROVIDERS) { | ||
| return bootstrap(App, [ | ||
| BOOT_PROVIDERS, | ||
| ROUTER_PROVIDERS, | ||
| new Provider(APP_BASE_HREF, { useValue: '/' }) | ||
| ]); | ||
| }; |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
| const express = require('express'); | ||
| const basicAuth = require('basic-auth-connect'); | ||
| const Router = express.Router; | ||
|
|
||
| exports.createRouter = function($) { | ||
| const router = Router(); | ||
| const indexHtml = fs.readFileSync(path.resolve(__dirname, 'index.html'), { encoding: 'utf8' }); | ||
|
|
||
| router.use(basicAuth('user', 'pass')); | ||
|
|
||
| router.get('/*', $.serveUniversal(indexHtml, { | ||
| browser: true, | ||
| })); | ||
|
|
||
| return router; | ||
| } | ||
|
|
||
| exports.createBuilds = function($) { | ||
| return [ | ||
| $.createBrowserConfig('./boot_browser.ts'), | ||
| ]; | ||
| } |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is probably a typo