Skip to content

Commit

Permalink
Merge pull request #31 from IKatsuba/feature-platform
Browse files Browse the repository at this point in the history
feat(ngx-ssr-platform): create the platform package
  • Loading branch information
IKatsuba authored Jan 15, 2021
2 parents 9ecd34b + 70aeb52 commit 6ffd7ab
Show file tree
Hide file tree
Showing 23 changed files with 510 additions and 10 deletions.
45 changes: 45 additions & 0 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,51 @@
}
}
}
},
"ngx-ssr-platform": {
"projectType": "library",
"root": "libs/ngx-ssr/platform",
"sourceRoot": "libs/ngx-ssr/platform/src",
"prefix": "ngx-ssr",
"architect": {
"build": {
"builder": "@nrwl/angular:package",
"options": {
"tsConfig": "libs/ngx-ssr/platform/tsconfig.lib.json",
"project": "libs/ngx-ssr/platform/ng-package.json"
},
"configurations": {
"production": {
"tsConfig": "libs/ngx-ssr/platform/tsconfig.lib.prod.json"
}
}
},
"lint": {
"builder": "@nrwl/linter:eslint",
"options": {
"lintFilePatterns": [
"libs/ngx-ssr/platform/src/**/*.ts",
"libs/ngx-ssr/platform/src/**/*.html"
]
}
},
"test": {
"builder": "@nrwl/jest:jest",
"outputs": ["coverage/libs/ngx-ssr/platform"],
"options": {
"jestConfig": "libs/ngx-ssr/platform/jest.config.js",
"passWithNoTests": true
}
},
"deploy": {
"builder": "@ng-builders/semrel:release",
"options": {
"npm": {
"pkgRoot": "dist/libs/ngx-ssr/platform"
}
}
}
}
}
},
"cli": {
Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ module.exports = {
'<rootDir>/libs/rickandmorty/api',
'<rootDir>/libs/rickandmorty/utils',
'<rootDir>/libs/ngx-ssr/timeout',
'<rootDir>/libs/ngx-ssr/platform',
],
};
31 changes: 31 additions & 0 deletions libs/ngx-ssr/platform/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"extends": ["../../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts"],
"extends": [
"plugin:@nrwl/nx/angular",
"plugin:@angular-eslint/template/process-inline-templates"
],
"parserOptions": {
"project": ["libs/ngx-ssr/platform/tsconfig.*?.json"]
},
"rules": {
"@angular-eslint/directive-selector": [
"error",
{ "type": "attribute", "prefix": "ngxSsr", "style": "camelCase" }
],
"@angular-eslint/component-selector": [
"error",
{ "type": "element", "prefix": "ngx-ssr", "style": "kebab-case" }
]
}
},
{
"files": ["*.html"],
"extends": ["plugin:@nrwl/nx/angular-template"],
"rules": {}
}
]
}
1 change: 1 addition & 0 deletions libs/ngx-ssr/platform/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @ngx-ssr/platform
23 changes: 23 additions & 0 deletions libs/ngx-ssr/platform/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module.exports = {
displayName: 'ngx-ssr-platform',
preset: '../../../jest.preset.js',
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
globals: {
'ts-jest': {
tsConfig: '<rootDir>/tsconfig.spec.json',
stringifyContentPathRegex: '\\.(html|svg)$',
astTransformers: {
before: [
'jest-preset-angular/build/InlineFilesTransformer',
'jest-preset-angular/build/StripStylesTransformer',
],
},
},
},
coverageDirectory: '../../../coverage/libs/ngx-ssr/platform',
snapshotSerializers: [
'jest-preset-angular/build/AngularNoNgAttributesSnapshotSerializer.js',
'jest-preset-angular/build/AngularSnapshotSerializer.js',
'jest-preset-angular/build/HTMLCommentSerializer.js',
],
};
7 changes: 7 additions & 0 deletions libs/ngx-ssr/platform/ng-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$schema": "../../../node_modules/ng-packagr/ng-package.schema.json",
"dest": "../../../dist/libs/ngx-ssr/platform",
"lib": {
"entryFile": "src/index.ts"
}
}
24 changes: 24 additions & 0 deletions libs/ngx-ssr/platform/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "@ngx-ssr/platform",
"version": "0.0.0-development",
"keywords": [
"angular",
"angulae ssr",
"universal",
"angular/universal",
"ssr",
"platform",
"is-server",
"is-browser"
],
"peerDependencies": {
"@angular/common": "^11.0.6",
"@angular/core": "^11.0.6"
},
"dependencies": {
"tslib": "^2.0.0"
},
"publishConfig": {
"access": "public"
}
}
4 changes: 4 additions & 0 deletions libs/ngx-ssr/platform/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './lib/is-server.directive';
export * from './lib/is-browser.directive';
export * from './lib/ngx-ssr-platform.module';
export * from './lib/tokens';
35 changes: 35 additions & 0 deletions libs/ngx-ssr/platform/src/lib/is-browser.directive.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { IsBrowserDirective } from './is-browser.directive';
import { render, screen } from '@testing-library/angular';
import { IS_BROWSER_PLATFORM } from './tokens';

describe('IsBrowserDirective', () => {
it('should create an element', async () => {
await render(IsBrowserDirective, {
template: '<div *isBrowser>Some text</div>',
providers: [
{
provide: IS_BROWSER_PLATFORM,
useValue: true,
},
],
});

expect(screen.getByText('Some text').outerHTML).toStrictEqual(
'<div>Some text</div>'
);
});

it('should not create an element', async () => {
await render(IsBrowserDirective, {
template: '<div *isBrowser>Some text</div>',
providers: [
{
provide: IS_BROWSER_PLATFORM,
useValue: false,
},
],
});

expect(() => screen.getByText('Some text')).toThrowError();
});
});
23 changes: 23 additions & 0 deletions libs/ngx-ssr/platform/src/lib/is-browser.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
Directive,
Inject,
TemplateRef,
ViewContainerRef,
} from '@angular/core';
import { IS_BROWSER_PLATFORM } from './tokens';

@Directive({
// eslint-disable-next-line @angular-eslint/directive-selector
selector: '[isBrowser]',
})
export class IsBrowserDirective {
constructor(
@Inject(IS_BROWSER_PLATFORM) isBrowser: boolean,
templateRef: TemplateRef<any>,
viewContainer: ViewContainerRef
) {
if (isBrowser) {
viewContainer.createEmbeddedView(templateRef);
}
}
}
35 changes: 35 additions & 0 deletions libs/ngx-ssr/platform/src/lib/is-server.directive.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { IsServerDirective } from './is-server.directive';
import { render, screen } from '@testing-library/angular';
import { IS_SERVER_PLATFORM } from './tokens';

describe('IsServerDirective', () => {
it('should create an element', async () => {
await render(IsServerDirective, {
template: '<div *isServer>Some text</div>',
providers: [
{
provide: IS_SERVER_PLATFORM,
useValue: true,
},
],
});

expect(screen.getByText('Some text').outerHTML).toStrictEqual(
'<div>Some text</div>'
);
});

it('should not create an element', async () => {
await render(IsServerDirective, {
template: '<div *isServer>Some text</div>',
providers: [
{
provide: IS_SERVER_PLATFORM,
useValue: false,
},
],
});

expect(() => screen.getByText('Some text')).toThrowError();
});
});
23 changes: 23 additions & 0 deletions libs/ngx-ssr/platform/src/lib/is-server.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
Directive,
Inject,
TemplateRef,
ViewContainerRef,
} from '@angular/core';
import { IS_SERVER_PLATFORM } from './tokens';

@Directive({
// eslint-disable-next-line @angular-eslint/directive-selector
selector: '[isServer]',
})
export class IsServerDirective {
constructor(
@Inject(IS_SERVER_PLATFORM) isServer: boolean,
templateRef: TemplateRef<any>,
viewContainer: ViewContainerRef
) {
if (isServer) {
viewContainer.createEmbeddedView(templateRef);
}
}
}
11 changes: 11 additions & 0 deletions libs/ngx-ssr/platform/src/lib/ngx-ssr-platform.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { IsBrowserDirective } from './is-browser.directive';
import { IsServerDirective } from './is-server.directive';

@NgModule({
imports: [CommonModule],
declarations: [IsBrowserDirective, IsServerDirective],
exports: [IsBrowserDirective, IsServerDirective],
})
export class NgxSsrPlatformModule {}
18 changes: 18 additions & 0 deletions libs/ngx-ssr/platform/src/lib/tokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { inject, InjectionToken, PLATFORM_ID } from '@angular/core';
import { isPlatformBrowser, isPlatformServer } from '@angular/common';

export const IS_SERVER_PLATFORM = new InjectionToken<boolean>('Is server?', {
factory() {
const platform = inject(PLATFORM_ID);

return isPlatformServer(platform);
},
});

export const IS_BROWSER_PLATFORM = new InjectionToken<boolean>('Is browser?', {
factory() {
const platform = inject(PLATFORM_ID);

return isPlatformBrowser(platform);
},
});
1 change: 1 addition & 0 deletions libs/ngx-ssr/platform/src/test-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import 'jest-preset-angular';
16 changes: 16 additions & 0 deletions libs/ngx-ssr/platform/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "../../../tsconfig.base.json",
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.lib.prod.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
19 changes: 19 additions & 0 deletions libs/ngx-ssr/platform/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/out-tsc",
"target": "es2015",
"declaration": true,
"declarationMap": true,
"inlineSources": true,
"types": [],
"lib": ["dom", "es2018"]
},
"angularCompilerOptions": {
"skipTemplateCodegen": true,
"strictMetadataEmit": true,
"enableResourceInlining": true
},
"exclude": ["src/test-setup.ts", "**/*.spec.ts"],
"include": ["**/*.ts"]
}
10 changes: 10 additions & 0 deletions libs/ngx-ssr/platform/tsconfig.lib.prod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
"extends": "./tsconfig.lib.json",
"compilerOptions": {
"declarationMap": false
},
"angularCompilerOptions": {
"enableIvy": false
}
}
10 changes: 10 additions & 0 deletions libs/ngx-ssr/platform/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"files": ["src/test-setup.ts"],
"include": ["**/*.spec.ts", "**/*.d.ts"]
}
Loading

0 comments on commit 6ffd7ab

Please sign in to comment.