@@ -27,31 +27,35 @@ module.exports = class {
27
27
// Embed Source Map in Development
28
28
async compile ( config ) {
29
29
return new Promise ( ( resolve , reject ) => {
30
- if ( ! isProd ) {
31
- config . sourceMap = true
32
- config . sourceMapEmbed = true
33
- config . outputStyle = 'expanded'
30
+ const sassOptions = {
31
+ loadPaths : [ path . join ( __dirname , 'scss' ) ] ,
32
+ style : isProd ? 'compressed' : 'expanded' ,
33
+ sourceMap : ! isProd ,
34
+ sourceMapIncludeSources : ! isProd ,
35
+ quietDeps : true
34
36
}
35
37
36
- const result = sass . renderSync ( config )
37
- return resolve ( result . css . toString ( ) )
38
+ try {
39
+ const result = sass . compile ( config . filePath , sassOptions )
40
+ return resolve ( result . css . toString ( ) )
41
+ } catch ( error ) {
42
+ return reject ( error )
43
+ }
38
44
} )
39
45
}
40
46
41
- // Minify & Optimize with CleanCSS in Production
42
- async minify ( css ) {
43
- return new Promise ( ( resolve , reject ) => {
44
- if ( ! isProd ) {
45
- resolve ( css )
46
- }
47
-
48
- const minified = new CleanCSS ( ) . minify ( css )
49
- if ( ! minified . styles ) {
50
- return reject ( minified . error )
47
+ // Process the file
48
+ async render ( { filePath } ) {
49
+ try {
50
+ const css = await this . compile ( { filePath } )
51
+ if ( isProd ) {
52
+ const minified = new CleanCSS ( { } ) . minify ( css ) . styles
53
+ return minified
51
54
}
52
-
53
- resolve ( minified . styles )
54
- } )
55
+ return css
56
+ } catch ( error ) {
57
+ return this . renderError ( error )
58
+ }
55
59
}
56
60
57
61
// Display an error overlay when CSS build fails.
@@ -85,7 +89,7 @@ module.exports = class {
85
89
position: fixed;
86
90
}
87
91
body::after {
88
- content: '${ cssesc ( error ) } ';
92
+ content: '${ cssesc ( error . toString ( ) ) } ';
89
93
white-space: pre;
90
94
display: block;
91
95
top: 0;
@@ -98,24 +102,4 @@ module.exports = class {
98
102
position: fixed;
99
103
}`
100
104
}
101
-
102
- // Render the CSS file
103
- async render ( { filePath } ) {
104
- try {
105
- const css = await this . compile ( { file : filePath } )
106
- const result = await this . minify ( css )
107
- return result
108
- } catch ( error ) {
109
- // If things go wrong
110
- if ( isProd ) {
111
- // Throw and abort in production
112
- throw new Error ( error )
113
- } else {
114
- // Otherwise display the error overly
115
- console . error ( error )
116
- const message = error . formatted || error . message
117
- return this . renderError ( message )
118
- }
119
- }
120
- }
121
105
}
0 commit comments