File tree Expand file tree Collapse file tree 5 files changed +67
-5
lines changed
projects/js-toolkit/packages/liferay-cli/src/new/facet-angular Expand file tree Collapse file tree 5 files changed +67
-5
lines changed Original file line number Diff line number Diff 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 ) ;
Original file line number Diff line number Diff line change @@ -5,9 +5,7 @@ import { LiferayParams } from '../types/liferay.params';
55declare 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})
1210export class AppComponent {
1311 params?: LiferayParams;
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 11import './polyfills';
22
3+ import { UrlResolver } from '@angular/compiler';
34import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
45
56import { AppComponent } from './app/app.component';
67import { AppDynamicLoader } from './app/app.dynamic.loader';
78import { AppModule } from './app/app.module';
9+ import { AppUrlResolver } from './app/app.url.resolver';
810import { 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 */
1618export 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}
Original file line number Diff line number Diff line change 1+ /**
2+ * Map from `baseUrl`s to server URLs
3+ */
4+ export interface BaseURLMap {
5+ [baseUrl: string]: string;
6+ }
You can’t perform that action at this time.
0 commit comments