Skip to content

Commit a52fe1e

Browse files
committed
feat(lib): add support for urls with type module
1 parent 45baa9b commit a52fe1e

File tree

4 files changed

+71
-12
lines changed

4 files changed

+71
-12
lines changed

projects/elements/src/lib/lazy-elements/lazy-element/lazy-element.directive.spec.ts

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,22 @@ import { LazyElementDirective } from './lazy-element.directive';
2626
*axLazyElement="'http://elements.com/some-element'; loading: loading"
2727
></some-element>
2828
</div>
29+
<div *ngIf="useModule">
30+
<some-element
31+
*axLazyElement="'http://elements.com/some-element-module'; module: true"
32+
></some-element>
33+
</div>
2934
`
3035
})
3136
class TestHostComponent {
3237
addSameElement = false;
3338
addOtherElement = false;
3439
useLoadingTemplate = false;
40+
useModule = false;
3541
}
3642

3743
describe('LazyElementDirective', () => {
38-
let component: TestHostComponent;
44+
let testHostComponent: TestHostComponent;
3945
let fixture: ComponentFixture<TestHostComponent>;
4046
let appendChildSpy: jasmine.Spy;
4147

@@ -48,13 +54,13 @@ describe('LazyElementDirective', () => {
4854

4955
beforeEach(() => {
5056
fixture = TestBed.createComponent(TestHostComponent);
51-
component = fixture.componentInstance;
57+
testHostComponent = fixture.componentInstance;
5258
appendChildSpy = spyOn(document.body, 'appendChild').and.stub();
5359
fixture.detectChanges();
5460
});
5561

5662
it('should create', () => {
57-
expect(component).toBeTruthy();
63+
expect(testHostComponent).toBeTruthy();
5864
});
5965

6066
it('adds a script tag into dom to load element bundle', () => {
@@ -65,7 +71,7 @@ describe('LazyElementDirective', () => {
6571
});
6672

6773
it('adds a script tag only once for elements with same url', () => {
68-
component.addSameElement = true;
74+
testHostComponent.addSameElement = true;
6975
fixture.detectChanges();
7076

7177
expect(appendChildSpy).toHaveBeenCalledTimes(1);
@@ -75,7 +81,7 @@ describe('LazyElementDirective', () => {
7581
});
7682

7783
it('adds multiple script tags if elements have different bundle url', () => {
78-
component.addOtherElement = true;
84+
testHostComponent.addOtherElement = true;
7985
fixture.detectChanges();
8086

8187
expect(appendChildSpy).toHaveBeenCalledTimes(2);
@@ -90,7 +96,7 @@ describe('LazyElementDirective', () => {
9096
it('renders loading template', () => {
9197
expect(document.querySelector('.loading')).toBe(null);
9298

93-
component.useLoadingTemplate = true;
99+
testHostComponent.useLoadingTemplate = true;
94100
fixture.detectChanges();
95101

96102
expect(document.querySelector('.loading').textContent).toBe('Loading...');
@@ -99,7 +105,7 @@ describe('LazyElementDirective', () => {
99105
it('removes loading template when element is loaded', done => {
100106
expect(document.querySelector('.loading')).toBe(null);
101107

102-
component.useLoadingTemplate = true;
108+
testHostComponent.useLoadingTemplate = true;
103109
fixture.detectChanges();
104110

105111
expect(document.querySelector('.loading').textContent).toBe('Loading...');
@@ -113,4 +119,23 @@ describe('LazyElementDirective', () => {
113119
done();
114120
});
115121
});
122+
123+
it('uses type module on script tag when specified', () => {
124+
fixture.detectChanges();
125+
126+
expect(appendChildSpy).toHaveBeenCalledTimes(1);
127+
expect(appendChildSpy.calls.argsFor(0)[0].src).toBe(
128+
'http://elements.com/some-element'
129+
);
130+
expect(appendChildSpy.calls.argsFor(0)[0].type).toBe('');
131+
132+
testHostComponent.useModule = true;
133+
fixture.detectChanges();
134+
135+
expect(appendChildSpy).toHaveBeenCalledTimes(2);
136+
expect(appendChildSpy.calls.argsFor(1)[0].src).toBe(
137+
'http://elements.com/some-element-module'
138+
);
139+
expect(appendChildSpy.calls.argsFor(1)[0].type).toBe('module');
140+
});
116141
});

projects/elements/src/lib/lazy-elements/lazy-element/lazy-element.directive.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { LazyElementsLoaderService } from '../lazy-elements-loader.service';
1414
export class LazyElementDirective implements OnInit {
1515
@Input('axLazyElement') url: string;
1616
@Input('axLazyElementLoading') loadingTemplateRef: TemplateRef<any>; // tslint:disable-line:no-input-rename
17+
@Input('axLazyElementModule') isModule: boolean; // tslint:disable-line:no-input-rename
1718

1819
constructor(
1920
private vcr: ViewContainerRef,
@@ -29,9 +30,11 @@ export class LazyElementDirective implements OnInit {
2930
this.vcr.createEmbeddedView(this.loadingTemplateRef);
3031
}
3132

32-
this.elementsLoaderService.loadElement(this.url, elementTag).then(() => {
33-
this.vcr.clear();
34-
this.vcr.createEmbeddedView(this.template);
35-
});
33+
this.elementsLoaderService
34+
.loadElement(this.url, elementTag, this.isModule)
35+
.then(() => {
36+
this.vcr.clear();
37+
this.vcr.createEmbeddedView(this.template);
38+
});
3639
}
3740
}

projects/elements/src/lib/lazy-elements/lazy-elements-loader.service.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,28 @@ describe('LazyElementsLoaderService', () => {
8080
done();
8181
});
8282
});
83+
84+
it('adds a script tag without module type', () => {
85+
service.loadElement('http://elements.com/some-element', 'some-element');
86+
87+
expect(appendChildSpy).toHaveBeenCalledTimes(1);
88+
expect(appendChildSpy.calls.argsFor(0)[0].src).toBe(
89+
'http://elements.com/some-element'
90+
);
91+
expect(appendChildSpy.calls.argsFor(0)[0].type).toBe('');
92+
});
93+
94+
it('adds a script tag with module type', () => {
95+
service.loadElement(
96+
'http://elements.com/some-element',
97+
'some-element',
98+
true
99+
);
100+
101+
expect(appendChildSpy).toHaveBeenCalledTimes(1);
102+
expect(appendChildSpy.calls.argsFor(0)[0].src).toBe(
103+
'http://elements.com/some-element'
104+
);
105+
expect(appendChildSpy.calls.argsFor(0)[0].type).toBe('module');
106+
});
83107
});

projects/elements/src/lib/lazy-elements/lazy-elements-loader.service.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ export class LazyElementsLoaderService {
1010

1111
constructor() {}
1212

13-
loadElement(url: string, tag: string): Promise<void> {
13+
loadElement(
14+
url: string,
15+
tag: string,
16+
isModule: boolean = false
17+
): Promise<void> {
1418
if (!url) {
1519
throw new Error(`${LOG_PREFIX} - url for <${tag}> not found`);
1620
}
@@ -24,6 +28,9 @@ export class LazyElementsLoaderService {
2428
if (!this.hasElement(url)) {
2529
const notifier = this.addElement(url);
2630
const script = document.createElement('script') as HTMLScriptElement;
31+
if (isModule) {
32+
script.type = 'module';
33+
}
2734
script.src = url;
2835
script.onload = notifier;
2936
document.body.appendChild(script);

0 commit comments

Comments
 (0)