Skip to content

Commit c315eab

Browse files
committed
feat(liferay-cli): add UrlResolver to application
This gives the application an opportunity to map static resource URLs (mainly HTML files) to Portal URLs so that the developer can decide where to deploy things.
1 parent 6d0e296 commit c315eab

File tree

5 files changed

+67
-5
lines changed

5 files changed

+67
-5
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ const facet: Facet = {
5555
await renderer.render('src/app/app.component.ts', context);
5656
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/app/app.url.resolver.ts', context);
59+
await renderer.render('src/types/base.url.map.ts', context);
5860
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);

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ import { LiferayParams } from '../types/liferay.params';
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 {
1311
params?: LiferayParams;
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+
}

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
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';
67
import { AppDynamicLoader } from './app/app.dynamic.loader';
78
import { AppModule } from './app/app.module';
9+
import { AppUrlResolver } from './app/app.url.resolver';
810
import { LiferayParams } from './types/liferay.params';
911

1012
/**
@@ -14,8 +16,21 @@ import { LiferayParams } from './types/liferay.params';
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
@@ -24,5 +39,6 @@ export default function(params: LiferayParams) {
2439
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+
}

0 commit comments

Comments
 (0)