4
4
*/
5
5
'use strict' ;
6
6
7
- var os = require ( 'os' ) ,
8
- path = require ( 'path' ) ,
9
- fs = require ( 'fs' ) ,
10
- util = require ( 'util' ) ,
11
- loaderUtils = require ( 'loader-utils' ) ,
12
- SourceMapConsumer = require ( 'source-map' ) . SourceMapConsumer ;
7
+ const os = require ( 'os' ) ;
8
+ const path = require ( 'path' ) ;
9
+ const fs = require ( 'fs' ) ;
10
+ const util = require ( 'util' ) ;
11
+ const loaderUtils = require ( 'loader-utils' ) ;
12
+ const SourceMapConsumer = require ( 'source-map' ) . SourceMapConsumer ;
13
13
14
- var adjustSourceMap = require ( 'adjust-sourcemap-loader/lib/process' ) ;
14
+ const adjustSourceMap = require ( 'adjust-sourcemap-loader/lib/process' ) ;
15
15
16
- var valueProcessor = require ( './lib/value-processor' ) ,
17
- joinFn = require ( './lib/join-function' ) ,
18
- logToTestHarness = require ( './lib/log-to-test-harness' ) ;
16
+ const valueProcessor = require ( './lib/value-processor' ) ;
17
+ const joinFn = require ( './lib/join-function' ) ;
18
+ const logToTestHarness = require ( './lib/log-to-test-harness' ) ;
19
19
20
20
const DEPRECATED_OPTIONS = {
21
21
engine : [
22
22
'DEP_RESOLVE_URL_LOADER_OPTION_ENGINE' ,
23
- 'the "engine" option is deprecated , "postcss" engine is the default, there are no other available engines '
23
+ 'the "engine" option has been removed , "postcss" is the only available parser '
24
24
] ,
25
25
keepQuery : [
26
26
'DEP_RESOLVE_URL_LOADER_OPTION_KEEP_QUERY' ,
@@ -55,7 +55,7 @@ function resolveUrlLoader(content, sourceMap) {
55
55
/* jshint validthis:true */
56
56
57
57
// details of the file being processed
58
- var loader = this ;
58
+ const loader = this ;
59
59
60
60
// a relative loader.context is a problem
61
61
if ( / ^ \. / . test ( loader . context ) ) {
@@ -66,24 +66,20 @@ function resolveUrlLoader(content, sourceMap) {
66
66
}
67
67
68
68
// infer webpack version from new loader features
69
- var isWebpackGte5 = 'getOptions' in loader && typeof loader . getOptions === 'function' ;
69
+ const isWebpackGte5 = 'getOptions' in loader && typeof loader . getOptions === 'function' ;
70
70
71
- // webpack 1: prefer loader query, else options object
72
- // webpack 2: prefer loader options
73
- // webpack 3: deprecate loader.options object
74
- // webpack 4: loader.options no longer defined
75
- var rawOptions = loaderUtils . getOptions ( loader ) ,
76
- options = Object . assign (
71
+ // use loader.getOptions where available otherwise use loaderUtils
72
+ const rawOptions = isWebpackGte5 ? loader . getOptions ( ) : loaderUtils . getOptions ( loader ) ;
73
+ const options = Object . assign (
77
74
{
78
75
sourceMap : loader . sourceMap ,
79
- engine : 'postcss' ,
80
76
silent : false ,
81
77
removeCR : os . EOL . includes ( '\r' ) ,
82
78
root : false ,
83
79
debug : false ,
84
80
join : joinFn . defaultJoin
85
81
} ,
86
- rawOptions
82
+ rawOptions ,
87
83
) ;
88
84
89
85
// maybe log options for the test harness
@@ -95,7 +91,7 @@ function resolveUrlLoader(content, sourceMap) {
95
91
}
96
92
97
93
// deprecated options
98
- var deprecatedItems = Object . entries ( DEPRECATED_OPTIONS ) . filter ( ( [ key ] ) => key in rawOptions ) ;
94
+ const deprecatedItems = Object . entries ( DEPRECATED_OPTIONS ) . filter ( ( [ key ] ) => key in rawOptions ) ;
99
95
if ( deprecatedItems . length ) {
100
96
deprecatedItems . forEach ( ( [ , value ] ) => handleAsDeprecated ( ...value ) ) ;
101
97
}
@@ -114,7 +110,7 @@ function resolveUrlLoader(content, sourceMap) {
114
110
}
115
111
116
112
// validate the result of calling the join option
117
- var joinProper = options . join ( options , loader ) ;
113
+ const joinProper = options . join ( options , loader ) ;
118
114
if ( typeof joinProper !== 'function' ) {
119
115
return handleAsError (
120
116
'loader misconfiguration' ,
@@ -129,7 +125,7 @@ function resolveUrlLoader(content, sourceMap) {
129
125
130
126
// validate root option
131
127
if ( typeof options . root === 'string' ) {
132
- var isValid = ( options . root === '' ) ||
128
+ const isValid = ( options . root === '' ) ||
133
129
( path . isAbsolute ( options . root ) && fs . existsSync ( options . root ) && fs . statSync ( options . root ) . isDirectory ( ) ) ;
134
130
135
131
if ( ! isValid ) {
@@ -149,7 +145,8 @@ function resolveUrlLoader(content, sourceMap) {
149
145
loader . cacheable ( ) ;
150
146
151
147
// incoming source-map
152
- var sourceMapConsumer , absSourceMap ;
148
+ let absSourceMap = null ;
149
+ let sourceMapConsumer = null ;
153
150
if ( sourceMap ) {
154
151
155
152
// support non-standard string encoded source-map (per less-loader)
@@ -186,20 +183,10 @@ function resolveUrlLoader(content, sourceMap) {
186
183
) ;
187
184
}
188
185
189
- // choose a CSS engine
190
- var enginePath = / ^ [ \w - ] + $ / . test ( options . engine ) && path . join ( __dirname , 'lib' , 'engine' , options . engine + '.js' ) ;
191
- var isValidEngine = fs . existsSync ( enginePath ) ;
192
- if ( ! isValidEngine ) {
193
- return handleAsError (
194
- 'loader misconfiguration' ,
195
- '"engine" option is not valid'
196
- ) ;
197
- }
198
-
199
186
// allow engine to throw at initialisation
200
- var engine ;
187
+ let engine = null ;
201
188
try {
202
- engine = require ( enginePath ) ;
189
+ engine = require ( './lib/engine/postcss' ) ;
203
190
} catch ( error ) {
204
191
return handleAsError (
205
192
'error initialising' ,
@@ -208,7 +195,7 @@ function resolveUrlLoader(content, sourceMap) {
208
195
}
209
196
210
197
// process async
211
- var callback = loader . async ( ) ;
198
+ const callback = loader . async ( ) ;
212
199
Promise
213
200
. resolve ( engine ( loader . resourcePath , content , {
214
201
outputSourceMap : ! ! options . sourceMap ,
@@ -234,7 +221,7 @@ function resolveUrlLoader(content, sourceMap) {
234
221
// webpack4 and earlier: source-map sources are relative to the file being processed
235
222
// webpack5: source-map sources are relative to the project root but without a leading slash
236
223
if ( options . sourceMap ) {
237
- var finalMap = adjustSourceMap ( loader , {
224
+ const finalMap = adjustSourceMap ( loader , {
238
225
format : isWebpackGte5 ? 'projectRelative' : 'sourceRelative'
239
226
} , result . map ) ;
240
227
callback ( null , result . content , finalMap ) ;
0 commit comments