Skip to content

Commit

Permalink
fix: fixed null value display when using angular universal (#852)
Browse files Browse the repository at this point in the history
fixes #764
  • Loading branch information
kamilchlebek authored and anjmao committed Oct 9, 2018
1 parent c115f59 commit 77482c0
Show file tree
Hide file tree
Showing 17 changed files with 1,614 additions and 64 deletions.
18 changes: 17 additions & 1 deletion integration/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@
"scripts": []
},
"configurations": {
"server-e2e": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.server-e2e.ts"
}
]
},
"production": {
"fileReplacements": [
{
Expand Down Expand Up @@ -115,6 +123,14 @@
"**/node_modules/**"
]
}
},
"server": {
"builder": "@angular-devkit/build-angular:server",
"options": {
"outputPath": "dist/integration-server",
"main": "src/main.server.ts",
"tsConfig": "src/tsconfig.server.json"
}
}
}
},
Expand Down Expand Up @@ -142,4 +158,4 @@
}
},
"defaultProject": "integration"
}
}
25 changes: 25 additions & 0 deletions integration/e2e/protractor-server.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { SpecReporter } = require('jasmine-spec-reporter');

exports.config = {
allScriptsTimeout: 11000,
specs: [
'./src/**/*.server-e2e-spec.ts'
],
capabilities: {
browserName: 'chrome',
},
directConnect: true,
baseUrl: 'http://localhost:4000/',
framework: 'jasmine',
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 30000,
print: function() {}
},
onPrepare() {
require('ts-node').register({
project: require('path').join(__dirname, './tsconfig.e2e.json')
});
jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
}
}
2 changes: 1 addition & 1 deletion integration/e2e/protractor.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ exports.config = {
});
jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
}
};
};
13 changes: 13 additions & 0 deletions integration/e2e/src/app.server-e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { browser, by } from 'protractor';

describe('ng-select:universal', () => {
beforeEach(() => {
browser.driver.get(browser.baseUrl + '/');
});

it('should not have null value', async() => {
const input = await browser.driver.findElement(by.css('.ng-input input'));
const value = await input.getAttribute('value');
expect(value).not.toEqual('null');
});
});
43 changes: 27 additions & 16 deletions integration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,48 @@
"name": "integration",
"version": "0.0.0",
"scripts": {
"postinstall": "webdriver-manager update --gecko false --standalone false",
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"start:server": "node ./dist/integration-server/server",
"build": "yarn build:client && yarn build:server",
"build:client": "ng build --configuration=server-e2e",
"build:server": "ng run integration:server && webpack --config ./webpack.server.config.js --progress --colors",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
"e2e": "ng e2e",
"e2e:server": "concurrently \"yarn start:server\" \"protractor ./e2e/protractor-server.conf.js\" --kill-others --success first"
},
"private": true,
"dependencies": {
"@angular/animations": "^6.0.0",
"@angular/common": "^6.0.0",
"@angular/compiler": "^6.0.0",
"@angular/core": "^6.0.0",
"@angular/forms": "^6.0.0",
"@angular/http": "^6.0.0",
"@angular/platform-browser": "^6.0.0",
"@angular/platform-browser-dynamic": "^6.0.0",
"@angular/router": "^6.0.0",
"@angular/animations": "^6.1.0",
"@angular/common": "^6.1.0",
"@angular/compiler": "^6.1.0",
"@angular/core": "^6.1.0",
"@angular/forms": "^6.1.0",
"@angular/http": "^6.1.0",
"@angular/platform-browser": "^6.1.0",
"@angular/platform-browser-dynamic": "^6.1.0",
"@angular/platform-server": "^6.1.0",
"@angular/router": "^6.1.0",
"@nguniversal/express-engine": "^6.1.0",
"@nguniversal/module-map-ngfactory-loader": "^6.1.0",
"core-js": "^2.5.4",
"rxjs": "^6.0.0",
"ts-loader": "^5.2.1",
"zone.js": "^0.8.26",
"@ng-select/ng-select": "file:../dist/"
},
"devDependencies": {
"@angular/compiler-cli": "^6.0.0",
"@angular-devkit/build-angular": "~0.6.1",
"typescript": "~2.7.2",
"@angular/cli": "~6.0.1",
"@angular/language-service": "^6.0.0",
"@angular/compiler-cli": "^6.1.0",
"@angular/language-service": "^6.1.0",
"@types/jasmine": "~2.8.6",
"@types/jasminewd2": "~2.0.3",
"@types/node": "~8.9.4",
"codelyzer": "~4.2.1",
"concurrently": "^4.0.1",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~1.7.1",
Expand All @@ -44,6 +53,8 @@
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.3.0",
"ts-node": "~5.0.1",
"tslint": "~5.9.1"
"tslint": "~5.9.1",
"typescript": "~2.7.2",
"webpack-cli": "^3.1.0"
}
}
}
31 changes: 31 additions & 0 deletions integration/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'zone.js/dist/zone-node';
import 'reflect-metadata';
import { enableProdMode } from '@angular/core';
import * as express from 'express';
import { join } from 'path';
import { ngExpressEngine } from '@nguniversal/express-engine';
import { provideModuleMap } from '@nguniversal/module-map-ngfactory-loader';
import { AppServerModuleNgFactory, LAZY_MODULE_MAP } from './dist/integration-server/main';

enableProdMode();

const app = express();
const PORT = process.env.PORT || 4000;
const DIST_FOLDER = join(process.cwd(), 'dist');

app.engine('html', ngExpressEngine({
bootstrap: AppServerModuleNgFactory,
providers: [
provideModuleMap(LAZY_MODULE_MAP),
],
}));

app.set('view engine', 'html');
app.set('views', join(DIST_FOLDER, 'integration'));
app.get('*.*', express.static(join(DIST_FOLDER, 'integration')));
app.get('*', (req, res) => {
res.render('index', {req});
});
app.listen(PORT, () => {
console.log(`Node server listening on http://localhost:${PORT}`);
});
2 changes: 1 addition & 1 deletion integration/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { NgSelectModule, NG_SELECT_DEFAULT_CONFIG } from '@ng-select/ng-select';
imports: [
CommonModule,
FormsModule,
BrowserModule,
BrowserModule.withServerTransition({ appId: 'ng-select' }),
NgSelectModule
],
providers: [
Expand Down
15 changes: 15 additions & 0 deletions integration/src/app/app.server.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { NgModule } from '@angular/core';
import { ServerModule } from '@angular/platform-server';

import { AppModule } from './app.module';
import { AppComponent } from './app.component';

@NgModule({
imports: [
AppModule,
ServerModule,
],
bootstrap: [AppComponent],
})
export class AppServerModule {
}
4 changes: 4 additions & 0 deletions integration/src/environments/environment.server-e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const environment = {
production: false,
manualBootstrap: true
};
3 changes: 2 additions & 1 deletion integration/src/environments/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
// The list of which env maps to which file can be found in `.angular-cli.json`.

export const environment = {
production: false
production: false,
manualBootstrap: false
};
1 change: 1 addition & 0 deletions integration/src/main.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { AppServerModule } from './app/app.server.module';
8 changes: 7 additions & 1 deletion integration/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ if (environment.production) {
enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AppModule);
if (environment.manualBootstrap) {
window['doBootstrap'] = () => {
platformBrowserDynamic().bootstrapModule(AppModule);
};
} else {
platformBrowserDynamic().bootstrapModule(AppModule);
}
10 changes: 10 additions & 0 deletions integration/src/tsconfig.server.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.app.json",
"compilerOptions": {
"outDir": "../out-tsc/app-server",
"baseUrl": "."
},
"angularCompilerOptions": {
"entryModule": "app/app.server.module#AppServerModule"
}
}
31 changes: 31 additions & 0 deletions integration/webpack.server.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const path = require('path');
const webpack = require('webpack');

module.exports = {
entry: { server: './server.ts' },
resolve: { extensions: ['.js', '.ts'] },
target: 'node',
mode: 'none',
externals: [/node_modules/],
output: {
path: path.join(__dirname, 'dist/integration-server'),
filename: '[name].js'
},
module: {
rules: [{ test: /\.ts$/, loader: 'ts-loader' }]
},
plugins: [
// Temporary Fix for issue: https://github.com/angular/angular/issues/11580
// for 'WARNING Critical dependency: the request of a dependency is an expression'
new webpack.ContextReplacementPlugin(
/(.+)?angular(\\|\/)core(.+)?/,
path.join(__dirname, 'src'), // location of your src
{} // a map of your routes
),
new webpack.ContextReplacementPlugin(
/(.+)?express(\\|\/)(.+)?/,
path.join(__dirname, 'src'),
{}
)
]
};

0 comments on commit 77482c0

Please sign in to comment.