Skip to content

Commit 41fda2f

Browse files
authored
Merge pull request #727 from izaera/IFI-2783
feat: update liferay-cli to account for changes in Angular architecture
2 parents fa28f65 + c315eab commit 41fda2f

File tree

9 files changed

+127
-57
lines changed

9 files changed

+127
-57
lines changed

projects/js-toolkit/packages/liferay-cli/src/dependencies.json

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,32 @@
11
{
22
"facet-angular": {
33
"dependencies": {
4-
"@angular/animations": "10.2.2",
5-
"@angular/common": "10.2.2",
6-
"@angular/compiler": "10.2.2",
7-
"@angular/core": "10.2.2",
8-
"@angular/forms": "10.2.2",
9-
"@angular/platform-browser": "10.2.2",
10-
"@angular/platform-browser-dynamic": "10.2.2",
11-
"@angular/router": "10.2.2",
12-
"rxjs": "6.6.3",
13-
"tslib": "2.0.3",
14-
"zone.js": "0.10.3"
4+
"@angular/animations": "~12.2.0",
5+
"@angular/common": "~12.2.0",
6+
"@angular/compiler": "~12.2.0",
7+
"@angular/core": "~12.2.0",
8+
"@angular/forms": "~12.2.0",
9+
"@angular/platform-browser": "~12.2.0",
10+
"@angular/platform-browser-dynamic": "~12.2.0",
11+
"@angular/router": "~12.2.0",
12+
"components-lib": "../components-app/dist/components-lib",
13+
"rxjs": "~6.6.0",
14+
"tslib": "^2.3.0",
15+
"zone.js": "~0.11.4"
1516
},
1617
"devDependencies": {
17-
"@angular/cli": "10.2.0",
18-
"@angular/compiler-cli": "10.2.2",
19-
"@angular-devkit/build-angular": "0.1002.0",
20-
"@types/jasmine": "3.5.14",
21-
"@types/jasminewd2": "2.0.8",
22-
"@types/node": "12.11.1",
23-
"codelyzer": "6.0.1",
24-
"jasmine-core": "3.6.0",
25-
"jasmine-spec-reporter": "5.0.2",
26-
"karma": "5.0.9",
27-
"karma-chrome-launcher": "3.1.0",
28-
"karma-coverage-istanbul-reporter": "3.0.3",
29-
"karma-jasmine": "4.0.1",
30-
"karma-jasmine-html-reporter": "1.5.4",
31-
"protractor": "7.0.0",
32-
"ts-node": "8.3.0",
33-
"tslint": "6.1.3",
34-
"typescript": "4.0.5"
18+
"@angular/cli": "~12.2.12",
19+
"@angular/compiler-cli": "~12.2.0",
20+
"@angular-devkit/build-angular": "~12.2.12",
21+
"@types/jasmine": "~3.8.0",
22+
"@types/node": "^12.11.1",
23+
"jasmine-core": "~3.8.0",
24+
"karma": "~6.3.0",
25+
"karma-chrome-launcher": "~3.1.0",
26+
"karma-coverage": "~2.0.3",
27+
"karma-jasmine": "~4.0.0",
28+
"karma-jasmine-html-reporter": "~1.7.0",
29+
"typescript": "~4.3.5"
3530
}
3631
},
3732
"facet-react": {

projects/js-toolkit/packages/liferay-cli/src/new/facet-angular/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@ const facet: Facet = {
5353

5454
await renderer.render('src/app/app.component.html', context);
5555
await renderer.render('src/app/app.component.ts', context);
56-
await renderer.render('src/app/dynamic.loader.ts', context);
56+
await renderer.render('src/app/app.dynamic.loader.ts', context);
5757
await renderer.render('src/app/app.module.ts', context);
58-
await renderer.render('src/types/LiferayParams.ts', context);
58+
await renderer.render('src/app/app.url.resolver.ts', context);
59+
await renderer.render('src/types/base.url.map.ts', context);
60+
await renderer.render('src/types/liferay.params.ts', context);
5961
await renderer.render('src/polyfills.ts', context);
6062
await renderer.render('src/index.ts', context);
6163

projects/js-toolkit/packages/liferay-cli/src/new/facet-angular/templates/src/app/app.component.ts.ejs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import { Component } from '@angular/core';
22

3-
import LiferayParams from '../types/LiferayParams'
3+
import { LiferayParams } from '../types/liferay.params';
44

55
declare const Liferay: any;
66

77
@Component({
8-
templateUrl:
9-
Liferay.ThemeDisplay.getPathContext() +
10-
'/o/<%= pkgJson.name %>-<%= pkgJson.version %>/app/app.component.html'
8+
templateUrl: './app.component.html'
119
})
1210
export class AppComponent {
13-
params: LiferayParams;
11+
params?: LiferayParams;
1412
labels: any;
1513

1614
constructor() {
@@ -25,6 +23,6 @@ export class AppComponent {
2523
}
2624

2725
get configurationJSON() {
28-
return JSON.stringify(this.params.configuration, null, 2);
26+
return JSON.stringify(this.params?.configuration, null, 2);
2927
}
3028
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ import {
66
Type,
77
} from '@angular/core';
88

9-
import LiferayParams from '../types/LiferayParams'
9+
import { LiferayParams } from '../types/liferay.params';
1010
import { AppComponent } from './app.component';
1111

1212
/**
1313
* This class loads an Angular component dinamically so that we can attach it to
1414
* the portlet's DOM, which is different for each portlet instance and thus,
1515
* cannot be determined until the page is rendered (during runtime).
1616
*/
17-
export class DynamicLoader {
17+
export class AppDynamicLoader {
1818
constructor(private injector: Injector) {}
1919

2020
loadComponent<T>(
2121
component: Type<AppComponent>, params: LiferayParams) {
22-
22+
2323
const node = document.getElementById(params.portletElementId);
24-
24+
2525
(<NgZone>this.injector.get(NgZone)).run(() => {
2626
const componentFactory = this.injector
2727
.get(ComponentFactoryResolver)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { UrlResolver } from '@angular/compiler';
2+
3+
import { BaseURLMap } from '../types/base.url.map';
4+
import { LiferayParams } from '../types/liferay.params';
5+
6+
declare const Liferay: any;
7+
8+
/**
9+
* This is a map from `baseUrl`s to server URLs so that the Angular Compiler
10+
* knows the place from where templates must be downloaded
11+
*/
12+
const BASE_URL_MAP: BaseURLMap = {
13+
'./AppComponent': '/app'
14+
};
15+
16+
export class AppUrlResolver implements UrlResolver {
17+
/* Initial LiferayParams object (injected from index.ts) */
18+
static params: LiferayParams;
19+
20+
resolve(baseUrl: string, url: string): string {
21+
if (url.startsWith('.')) {
22+
url = url.substring(1);
23+
}
24+
25+
if (!url.startsWith('/')) {
26+
url = '/' + url;
27+
}
28+
29+
const mappedBaseUrl = BASE_URL_MAP[baseUrl];
30+
31+
if (!mappedBaseUrl) {
32+
throw new Error(`Unknown baseUrl: ${baseUrl}`);
33+
}
34+
35+
return Liferay.ThemeDisplay.getPathContext() +
36+
AppUrlResolver.params.contextPath +
37+
mappedBaseUrl +
38+
url;
39+
}
40+
}
Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import './polyfills';
22

3+
import { UrlResolver } from '@angular/compiler';
34
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
45

56
import { AppComponent } from './app/app.component';
7+
import { AppDynamicLoader } from './app/app.dynamic.loader';
68
import { AppModule } from './app/app.module';
7-
import { DynamicLoader } from './app/dynamic.loader';
8-
import LiferayParams from './types/LiferayParams'
9+
import { AppUrlResolver } from './app/app.url.resolver';
10+
import { LiferayParams } from './types/liferay.params';
911

1012
/**
1113
* This is the actual method that initializes the portlet. It is invoked by the
@@ -14,15 +16,29 @@ import LiferayParams from './types/LiferayParams'
1416
* @param params an object with values of interest to the portlet
1517
*/
1618
export default function(params: LiferayParams) {
19+
AppUrlResolver.params = params;
20+
1721
platformBrowserDynamic()
18-
.bootstrapModule(AppModule)
22+
.bootstrapModule(
23+
AppModule,
24+
{
25+
providers:[
26+
// Inject custom AppUrlResolver to resolve static resources
27+
{
28+
deps: [],
29+
provide: UrlResolver,
30+
useClass: AppUrlResolver,
31+
},
32+
],
33+
})
1934
.then((injector: any) => {
2035
// Load the bootstrap component dinamically so that we can attach it
2136
// to the portlet's DOM, which is different for each portlet
2237
// instance and, thus, cannot be determined until the page is
2338
// rendered (during runtime).
24-
const dynamicLoader = new DynamicLoader(injector);
39+
const dynamicLoader = new AppDynamicLoader(injector);
2540

2641
dynamicLoader.loadComponent(AppComponent, params);
27-
});
42+
})
43+
.catch(err => console.error(err));
2844
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Map from `baseUrl`s to server URLs
3+
*/
4+
export interface BaseURLMap {
5+
[baseUrl: string]: string;
6+
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
* See https://tinyurl.com/js-ext-portlet-entry-point for the most recent
55
* information on the structure of this type.
66
*/
7-
export default interface LiferayParams {
8-
portletElementId: string;
9-
contextPath: string;
10-
portletNamespace: string;
7+
export interface LiferayParams {
118
configuration: {
129
system: any,
1310
portletInstance: any
1411
}
12+
contextPath: string;
13+
portletElementId: string;
14+
portletNamespace: string;
1515
}
1616

projects/js-toolkit/packages/liferay-cli/src/new/facet-angular/templates/tsconfig.json.ejs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
11
{
2+
"angularCompilerOptions": {
3+
"enableI18nLegacyMessageIdFormat": false,
4+
"strictInjectionParameters": true,
5+
"strictInputAccessModifiers": true,
6+
"strictTemplates": true
7+
},
8+
"compileOnSave": false,
29
"compilerOptions": {
3-
"emitDecoratorMetadata": true,
10+
"baseUrl": "./",
11+
"declaration": false,
12+
"downlevelIteration": true,
413
"experimentalDecorators": true,
5-
"inlineSources": true,
6-
"lib": ["es2015", "dom"],
14+
"forceConsistentCasingInFileNames": true,
15+
"importHelpers": true,
16+
"lib": [
17+
"es2015",
18+
"dom"
19+
],
720
"module": "commonjs",
821
"moduleResolution": "node",
9-
"noImplicitAny": true,
10-
"noStrictGenericChecks": true,
11-
"outDir": "build",
22+
"noFallthroughCasesInSwitch": true,
23+
"noImplicitReturns": true,
24+
"outDir": "./build",
1225
"sourceMap": true,
13-
"suppressImplicitAnyIndexErrors": true,
26+
"strict": true,
1427
"target": "es5",
1528
"typeRoots": ["./node_modules/@types/"]
1629
},

0 commit comments

Comments
 (0)