@@ -8,13 +8,16 @@ class LazyLoader {
8
8
this . settings = {
9
9
cssDir : "css" ,
10
10
jsDir : "js" ,
11
+ default : "lazy" ,
11
12
} ;
12
13
this . io = new IntersectionObserver ( this . intersectionCallback ) ;
13
14
}
14
15
15
16
public configure ( settings :Partial < LazyLoaderSettings > = { } ) {
16
17
this . settings = Object . assign ( this . settings , settings ) ;
18
+ // @ts -ignore
17
19
this . settings . cssDir = this . settings . cssDir . replace ( / ^ \/ | \/ $ / g, "" ) . trim ( ) ;
20
+ // @ts -ignore
18
21
this . settings . jsDir = this . settings . jsDir . replace ( / ^ \/ | \/ $ / g, "" ) . trim ( )
19
22
this . main ( ) ;
20
23
}
@@ -42,8 +45,8 @@ class LazyLoader {
42
45
} else {
43
46
href = `${ location . origin } /${ this . settings . cssDir } /${ file . replace ( / \. c s s $ / g, "" ) . trim ( ) } .css` ;
44
47
}
45
- let stylesheet : HTMLLinkElement = document . head . querySelector ( `link[href="${ href } "]` ) ;
46
- if ( ! stylesheet ) {
48
+ let stylesheet = document . head . querySelector ( `link[href="${ href } "]` ) as HTMLLinkElement ;
49
+ if ( stylesheet === null ) {
47
50
new Promise < void > ( resolve => {
48
51
stylesheet = document . createElement ( "link" ) ;
49
52
stylesheet . href = href ;
@@ -72,7 +75,7 @@ class LazyLoader {
72
75
} ) ;
73
76
}
74
77
75
- public async mount ( tagName :string , constructor :CustomElementConstructor = null ) :Promise < void > {
78
+ public async mount ( tagName :string , constructor :CustomElementConstructor | null = null ) :Promise < void > {
76
79
if ( ! customElements . get ( tagName ) ) {
77
80
if ( constructor === null ) {
78
81
try {
@@ -107,6 +110,7 @@ class LazyLoader {
107
110
let files :string [ ] = [ ] ;
108
111
const elements = Array . from ( document . body . querySelectorAll ( "[css]" ) ) ;
109
112
for ( let i = 0 ; i < elements . length ; i ++ ) {
113
+ // @ts -ignore
110
114
const attrValue = elements [ i ] . getAttribute ( "css" ) . trim ( ) . replace ( / \s + / , " " ) ;
111
115
const fileNames = attrValue . split ( " " ) ;
112
116
files = [ ...files , ...fileNames ] ;
@@ -141,7 +145,7 @@ class LazyLoader {
141
145
142
146
private getModuleUrl ( element :HTMLElement , tagName :string ) :string {
143
147
let url = `${ location . origin } /${ this . settings . jsDir } /${ tagName } .js` ;
144
- const attrValue = element . getAttribute ( "web-component" ) ;
148
+ const attrValue = element . getAttribute ( "web-component" ) || "" ;
145
149
if ( attrValue !== "" ) {
146
150
if ( attrValue . indexOf ( "https://" ) === 0 || attrValue . indexOf ( "http://" ) === 0 ) {
147
151
url = attrValue ;
@@ -168,7 +172,14 @@ class LazyLoader {
168
172
private async upgradeOrTrack ( elements :Array < HTMLElement > ) :Promise < void > {
169
173
for ( const element of elements ) {
170
174
const loadType = element . getAttribute ( "loading" ) as Loading ;
171
- if ( loadType === "eager" ) {
175
+ if ( loadType === null ) {
176
+ if ( this . settings . default === "eager" ) {
177
+ await this . upgrade ( element ) ;
178
+ element . setAttribute ( "web-component-state" , "mounted" ) ;
179
+ } else {
180
+ this . io . observe ( element ) ;
181
+ }
182
+ } else if ( loadType === "eager" ) {
172
183
await this . upgrade ( element ) ;
173
184
element . setAttribute ( "web-component-state" , "mounted" ) ;
174
185
} else {
@@ -190,4 +201,4 @@ const configure = loader.configure.bind(loader);
190
201
const update = loader . update . bind ( loader ) ;
191
202
const mount = loader . mount . bind ( loader ) ;
192
203
const css = loader . css . bind ( loader ) ;
193
- export { configure , update , mount , css } ;
204
+ export { configure , update , mount , css } ;
0 commit comments