1
1
const less = require ( 'less' ) ;
2
2
const loaderUtils = require ( 'loader-utils' ) ;
3
3
const cloneDeep = require ( 'clone-deep' ) ;
4
+ const pify = require ( 'pify' ) ;
5
+ const removeSourceMappingUrl = require ( './removeSourceMappingUrl' ) ;
4
6
5
7
const trailingSlash = / [ \\ / ] $ / ;
8
+ const render = pify ( less . render . bind ( less ) ) ;
6
9
7
10
function lessLoader ( source ) {
8
11
const loaderContext = this ;
9
- const options = Object . assign (
10
- {
11
- filename : this . resource ,
12
- paths : [ ] ,
13
- plugins : [ ] ,
14
- relativeUrls : true ,
15
- compress : Boolean ( this . minimize ) ,
16
- } ,
17
- cloneDeep ( loaderUtils . getOptions ( loaderContext ) ) ,
18
- ) ;
19
- const cb = loaderContext . async ( ) ;
20
- const isSync = typeof cb !== 'function' ;
21
- let finalCb = cb || loaderContext . callback ;
12
+ const options = {
13
+ plugins : [ ] ,
14
+ relativeUrls : true ,
15
+ compress : Boolean ( this . minimize ) ,
16
+ ...cloneDeep ( loaderUtils . getOptions ( loaderContext ) ) ,
17
+ } ;
18
+ const done = loaderContext . async ( ) ;
19
+ const isSync = typeof done !== 'function' ;
22
20
const webpackPlugin = {
23
21
install ( lessInstance , pluginManager ) {
24
22
const WebpackFileManager = getWebpackFileManager ( loaderContext , options ) ;
@@ -32,32 +30,26 @@ function lessLoader(source) {
32
30
throw new Error ( 'Synchronous compilation is not supported anymore. See https://github.com/webpack-contrib/less-loader/issues/84' ) ;
33
31
}
34
32
33
+ // We need to set the filename because otherwise our WebpackFileManager will receive an undefined path for the entry
34
+ options . filename = loaderContext . resource ;
35
+
35
36
// It's safe to mutate the array now because it has already been cloned
36
37
options . plugins . push ( webpackPlugin ) ;
37
38
38
- if ( options . sourceMap ) {
39
- options . sourceMap = {
40
- outputSourceFiles : true ,
41
- } ;
42
- }
43
-
44
- less . render ( source , options , ( err , result ) => {
45
- const cb = finalCb ;
46
-
47
- // Less is giving us double callbacks sometimes :(
48
- // Thus we need to mark the callback as "has been called"
49
- if ( ! finalCb ) {
50
- return ;
51
- }
52
- finalCb = null ;
53
-
54
- if ( err ) {
55
- cb ( formatLessRenderError ( err ) ) ;
56
- return ;
57
- }
58
-
59
- cb ( null , result . css , result . map ) ;
60
- } ) ;
39
+ render ( source , options )
40
+ . then ( ( { css, map } ) => {
41
+ return {
42
+ // Removing the sourceMappingURL comment.
43
+ // See removeSourceMappingUrl.js for the reasoning behind this.
44
+ css : removeSourceMappingUrl ( css ) ,
45
+ map : typeof map === 'string' ? JSON . parse ( map ) : map ,
46
+ } ;
47
+ } , ( lessError ) => {
48
+ throw formatLessRenderError ( lessError ) ;
49
+ } )
50
+ . then ( ( { css, map } ) => {
51
+ done ( null , css , map ) ;
52
+ } , done ) ;
61
53
}
62
54
63
55
function getWebpackFileManager ( loaderContext , query ) {
0 commit comments