@@ -2,6 +2,7 @@ import parse from 'posthtml-parser';
22import api from 'posthtml/lib/api' ;
33
44import isUrl from './is-url' ;
5+ import { buildWorkerError } from '../../../transpilers/utils/worker-error-handler' ;
56
67// A list of all attributes that may produce a dependency
78// Based on https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes
@@ -57,114 +58,119 @@ const META = {
5758} ;
5859
5960self . addEventListener ( 'message' , async event => {
60- const { code } = event . data ;
61+ try {
62+ const { code } = event . data ;
6163
62- const resources = [ ] ;
64+ const resources = [ ] ;
6365
64- function addDependency ( depPath : string ) {
65- if ( ! isUrl ( depPath ) ) {
66- let assetPath = decodeURIComponent ( depPath ) ;
67- if ( / ^ \w / . test ( assetPath ) ) {
68- assetPath = `./${ assetPath } ` ;
69- }
66+ function addDependency ( depPath : string ) {
67+ if ( ! isUrl ( depPath ) ) {
68+ let assetPath = decodeURIComponent ( depPath ) ;
69+ if ( / ^ \w / . test ( assetPath ) ) {
70+ assetPath = `./${ assetPath } ` ;
71+ }
7072
71- self . postMessage ( {
72- type : 'add-dependency' ,
73- path : assetPath ,
74- isEntry : true ,
75- } ) ;
73+ self . postMessage ( {
74+ type : 'add-dependency' ,
75+ path : assetPath ,
76+ isEntry : true ,
77+ } ) ;
7678
77- resources . push ( assetPath ) ;
79+ resources . push ( assetPath ) ;
7880
79- return assetPath ;
81+ return assetPath ;
82+ }
83+ return false ;
8084 }
81- return false ;
82- }
8385
84- function addSrcSetDependencies ( srcset : string ) {
85- const newSources = [ ] ;
86+ function addSrcSetDependencies ( srcset : string ) {
87+ const newSources = [ ] ;
8688
87- srcset . split ( ',' ) . forEach ( source => {
88- const pair = source . trim ( ) . split ( ' ' ) ;
89- if ( pair . length === 0 ) return ;
90- pair [ 0 ] = addDependency ( pair [ 0 ] ) ;
91- newSources . push ( pair . join ( ' ' ) ) ;
92- } ) ;
93- return newSources . join ( ',' ) ;
94- }
89+ srcset . split ( ',' ) . forEach ( source => {
90+ const pair = source . trim ( ) . split ( ' ' ) ;
91+ if ( pair . length === 0 ) return ;
92+ pair [ 0 ] = addDependency ( pair [ 0 ] ) ;
93+ newSources . push ( pair . join ( ' ' ) ) ;
94+ } ) ;
95+ return newSources . join ( ',' ) ;
96+ }
9597
96- const res = parse ( code , { lowerCaseAttributeNames : true } ) ;
97- res . walk = api . walk ;
98- res . match = api . match ;
99-
100- res . walk ( node => {
101- if ( node . attrs ) {
102- if ( node . tag === 'meta' ) {
103- if (
104- ! Object . keys ( node . attrs ) . some ( attr => {
105- const values = META [ attr ] ;
106- return values && values . includes ( node . attrs [ attr ] ) ;
107- } )
108- ) {
109- return node ;
110- }
98+ const res = parse ( code , { lowerCaseAttributeNames : true } ) ;
99+ res . walk = api . walk ;
100+ res . match = api . match ;
101+
102+ res . walk ( node => {
103+ if ( node == null ) {
104+ return node ;
111105 }
112106
113- /* eslint-disable no-param-reassign no-continue */
114- // eslint-disable-next-line no-restricted-syntax
115- for ( const attr in node . attrs ) {
116- if ( node . tag === 'img' && attr === 'srcset' ) {
117- node . attrs [ attr ] = addSrcSetDependencies ( node . attrs [ attr ] ) ;
118- continue ;
119- }
120- const elements = ATTRS [ attr ] ;
121- // Check for virtual paths
122- if (
123- ( node . tag === 'a' && node . attrs [ attr ] . lastIndexOf ( '.' ) < 1 ) ||
124- node . attrs [ attr ] . endsWith ( '.html' )
125- ) {
126- continue ;
107+ if ( node . attrs ) {
108+ if ( node . tag === 'meta' ) {
109+ if (
110+ ! Object . keys ( node . attrs ) . some ( attr => {
111+ const values = META [ attr ] ;
112+ return values && values . includes ( node . attrs [ attr ] ) ;
113+ } )
114+ ) {
115+ return node ;
116+ }
127117 }
128118
129- if (
130- node . tag === 'html' &&
131- node . attrs [ attr ] . endsWith ( '.html' ) &&
132- attr === 'href'
133- ) {
134- // Another HTML file, we'll compile it when the user goes to it
135- continue ;
136- }
119+ /* eslint-disable no-param-reassign no-continue */
120+ // eslint-disable-next-line no-restricted-syntax
121+ for ( const attr in node . attrs ) {
122+ if ( node . tag === 'img' && attr === 'srcset' ) {
123+ node . attrs [ attr ] = addSrcSetDependencies ( node . attrs [ attr ] ) ;
124+ continue ;
125+ }
126+ const elements = ATTRS [ attr ] ;
127+ // Check for virtual paths
128+ if (
129+ ( node . tag === 'a' && node . attrs [ attr ] . lastIndexOf ( '.' ) < 1 ) ||
130+ node . attrs [ attr ] . endsWith ( '.html' )
131+ ) {
132+ continue ;
133+ }
134+
135+ if (
136+ node . tag === 'html' &&
137+ node . attrs [ attr ] . endsWith ( '.html' ) &&
138+ attr === 'href'
139+ ) {
140+ // Another HTML file, we'll compile it when the user goes to it
141+ continue ;
142+ }
137143
138- if ( elements && elements . includes ( node . tag ) ) {
139- const result = addDependency ( node . attrs [ attr ] ) ;
144+ if ( elements && elements . includes ( node . tag ) ) {
145+ const result = addDependency ( node . attrs [ attr ] ) ;
140146
141- if ( result ) {
142- if ( node . tag === 'link' || node . tag === 'script' ) {
143- node . tag = false ;
144- node . content = [ ] ;
145- } else {
146- node . attrs [ attr ] = result ;
147+ if ( result ) {
148+ if ( node . tag === 'link' || node . tag === 'script' ) {
149+ node . tag = false ;
150+ node . content = [ ] ;
151+ } else {
152+ node . attrs [ attr ] = result ;
153+ }
147154 }
148155 }
149156 }
150157 }
151- }
152158
153- return node ;
154- } ) ;
159+ return node ;
160+ } ) ;
155161
156- let compiledCode = `` ;
162+ let compiledCode = `` ;
157163
158- compiledCode += '\n' ;
159- compiledCode += 'function loadResources() {' ;
160- resources . forEach ( resource => {
161- const resourcePath = JSON . stringify ( resource ) ;
162- compiledCode += `\n` ;
163- compiledCode += `\trequire(${ resourcePath } );\n` ;
164- } ) ;
165- compiledCode += '\n}' ;
164+ compiledCode += '\n' ;
165+ compiledCode += 'function loadResources() {' ;
166+ resources . forEach ( resource => {
167+ const resourcePath = JSON . stringify ( resource ) ;
168+ compiledCode += `\n` ;
169+ compiledCode += `\trequire(${ resourcePath } );\n` ;
170+ } ) ;
171+ compiledCode += '\n}' ;
166172
167- compiledCode += `
173+ compiledCode += `
168174if (document.readyState !== 'complete') {
169175 window.addEventListener('load', function() { loadResources() });
170176} else {
@@ -173,8 +179,14 @@ if (document.readyState !== 'complete') {
173179
174180` ;
175181
176- self . postMessage ( {
177- type : 'result' ,
178- transpiledCode : compiledCode ,
179- } ) ;
182+ self . postMessage ( {
183+ type : 'result' ,
184+ transpiledCode : compiledCode ,
185+ } ) ;
186+ } catch ( e ) {
187+ self . postMessage ( {
188+ type : 'error' ,
189+ error : buildWorkerError ( e ) ,
190+ } ) ;
191+ }
180192} ) ;
0 commit comments