@@ -8,13 +8,16 @@ class LazyLoader {
88 this . settings = {
99 cssDir : "css" ,
1010 jsDir : "js" ,
11+ default : "lazy" ,
1112 } ;
1213 this . io = new IntersectionObserver ( this . intersectionCallback ) ;
1314 }
1415
1516 public configure ( settings :Partial < LazyLoaderSettings > = { } ) {
1617 this . settings = Object . assign ( this . settings , settings ) ;
18+ // @ts -ignore
1719 this . settings . cssDir = this . settings . cssDir . replace ( / ^ \/ | \/ $ / g, "" ) . trim ( ) ;
20+ // @ts -ignore
1821 this . settings . jsDir = this . settings . jsDir . replace ( / ^ \/ | \/ $ / g, "" ) . trim ( )
1922 this . main ( ) ;
2023 }
@@ -42,8 +45,8 @@ class LazyLoader {
4245 } else {
4346 href = `${ location . origin } /${ this . settings . cssDir } /${ file . replace ( / \. c s s $ / g, "" ) . trim ( ) } .css` ;
4447 }
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 ) {
4750 new Promise < void > ( resolve => {
4851 stylesheet = document . createElement ( "link" ) ;
4952 stylesheet . href = href ;
@@ -72,7 +75,7 @@ class LazyLoader {
7275 } ) ;
7376 }
7477
75- public async mount ( tagName :string , constructor :CustomElementConstructor = null ) :Promise < void > {
78+ public async mount ( tagName :string , constructor :CustomElementConstructor | null = null ) :Promise < void > {
7679 if ( ! customElements . get ( tagName ) ) {
7780 if ( constructor === null ) {
7881 try {
@@ -107,6 +110,7 @@ class LazyLoader {
107110 let files :string [ ] = [ ] ;
108111 const elements = Array . from ( document . body . querySelectorAll ( "[css]" ) ) ;
109112 for ( let i = 0 ; i < elements . length ; i ++ ) {
113+ // @ts -ignore
110114 const attrValue = elements [ i ] . getAttribute ( "css" ) . trim ( ) . replace ( / \s + / , " " ) ;
111115 const fileNames = attrValue . split ( " " ) ;
112116 files = [ ...files , ...fileNames ] ;
@@ -141,7 +145,7 @@ class LazyLoader {
141145
142146 private getModuleUrl ( element :HTMLElement , tagName :string ) :string {
143147 let url = `${ location . origin } /${ this . settings . jsDir } /${ tagName } .js` ;
144- const attrValue = element . getAttribute ( "web-component" ) ;
148+ const attrValue = element . getAttribute ( "web-component" ) || "" ;
145149 if ( attrValue !== "" ) {
146150 if ( attrValue . indexOf ( "https://" ) === 0 || attrValue . indexOf ( "http://" ) === 0 ) {
147151 url = attrValue ;
@@ -168,7 +172,14 @@ class LazyLoader {
168172 private async upgradeOrTrack ( elements :Array < HTMLElement > ) :Promise < void > {
169173 for ( const element of elements ) {
170174 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" ) {
172183 await this . upgrade ( element ) ;
173184 element . setAttribute ( "web-component-state" , "mounted" ) ;
174185 } else {
@@ -190,4 +201,4 @@ const configure = loader.configure.bind(loader);
190201const update = loader . update . bind ( loader ) ;
191202const mount = loader . mount . bind ( loader ) ;
192203const css = loader . css . bind ( loader ) ;
193- export { configure , update , mount , css } ;
204+ export { configure , update , mount , css } ;
0 commit comments