@@ -24,21 +24,72 @@ export async function optimizeCssWorker(inputOpts: d.OptimizeCssInput) {
2424
2525 const processor = postcss ( plugins ) ;
2626
27- const result = await processor . process ( inputOpts . css , {
28- map : null ,
29- from : inputOpts . filePath
30- } ) ;
31-
32- result . warnings ( ) . forEach ( warning => {
33- output . diagnostics . push ( {
34- header : `Optimize CSS: ${ warning . plugin } ` ,
35- messageText : warning . text ,
36- level : 'warn' ,
37- type : 'css'
27+ try {
28+ const result = await processor . process ( inputOpts . css , {
29+ map : null ,
30+ from : inputOpts . filePath
3831 } ) ;
39- } ) ;
4032
41- output . css = result . css ;
33+ result . warnings ( ) . forEach ( warning => {
34+ output . diagnostics . push ( {
35+ header : `Optimize CSS: ${ warning . plugin } ` ,
36+ messageText : warning . text ,
37+ level : 'warn' ,
38+ type : 'css' ,
39+ absFilePath : inputOpts . filePath
40+ } ) ;
41+ } ) ;
42+
43+ output . css = result . css ;
44+
45+ } catch ( e ) {
46+ const diagnostic : d . Diagnostic = {
47+ header : `Optimize CSS` ,
48+ messageText : `CSS Error` ,
49+ level : `error` ,
50+ type : `css` ,
51+ absFilePath : inputOpts . filePath
52+ } ;
53+
54+ if ( typeof e . name === 'string' ) {
55+ diagnostic . header = e . name ;
56+ }
57+
58+ if ( typeof e . reason === 'string' ) {
59+ diagnostic . messageText = e . reason ;
60+ }
61+
62+ if ( typeof e . source === 'string' && typeof e . line === 'number' ) {
63+ const lines = ( e . source as string ) . replace ( / \r / g, '\n' ) . split ( '\n' ) ;
64+
65+ if ( lines . length > 0 ) {
66+ const addLine = ( lineNumber : number ) => {
67+ const line = lines [ lineNumber ] ;
68+ if ( typeof line === 'string' ) {
69+ const printLine : d . PrintLine = {
70+ lineIndex : - 1 ,
71+ lineNumber : - 1 ,
72+ text : line ,
73+ errorCharStart : - 1 ,
74+ errorLength : - 1
75+ } ;
76+ diagnostic . lines = diagnostic . lines || [ ] ;
77+ diagnostic . lines . push ( printLine ) ;
78+ }
79+ } ;
80+
81+ addLine ( e . line - 3 ) ;
82+ addLine ( e . line - 2 ) ;
83+ addLine ( e . line - 1 ) ;
84+ addLine ( e . line ) ;
85+ addLine ( e . line + 1 ) ;
86+ addLine ( e . line + 2 ) ;
87+ addLine ( e . line + 3 ) ;
88+ }
89+ }
90+
91+ output . diagnostics . push ( diagnostic ) ;
92+ }
4293
4394 } catch ( e ) {
4495 catchError ( output . diagnostics , e ) ;
0 commit comments