@@ -70,41 +70,59 @@ function webpackImporter(loaderContext, includePaths) {
70
70
mainFiles : [ ] ,
71
71
modules : [ ] ,
72
72
} ) ;
73
+ // TODO implement the `restrictions` option for `enhanced-resolve` and avoid resolution `js` files from the `mainFields`
74
+ // TODO avoid resolsing `_index`, `index` and files without extensions
75
+ // TODO avoid resolving with multiple extensions - `file.sass.sass`/`file.sass.scss`/`file.sass.css`
73
76
const webpackResolve = loaderContext . getResolve ( {
74
77
mainFields : [ 'sass' , 'style' , 'main' , '...' ] ,
75
78
mainFiles : [ '_index' , 'index' , '...' ] ,
76
79
extensions : [ '.sass' , '.scss' , '.css' ] ,
77
80
} ) ;
78
81
79
- return ( url , prev , done ) => {
80
- // The order of import precedence is as follows:
81
- //
82
- // 1. Filesystem imports relative to the base file.
83
- // 2. Custom importer imports.
84
- // 3. Filesystem imports relative to the working directory.
85
- // 4. Filesystem imports relative to an `includePaths` path.
86
- // 5. Filesystem imports relative to a `SASS_PATH` path.
87
- //
88
- // Because `sass`/`node-sass` run custom importers after `3`, `4` and `5` points, we need to emulate this behavior to avoid wrong resolution.
89
- const sassPossibleRequests = getPossibleRequests ( url ) ;
90
- const webpackPossibleRequests = getPossibleRequests ( url , true ) ;
91
- const resolutionMap = [ ]
92
- . concat (
82
+ return ( originalUrl , prev , done ) => {
83
+ let url = originalUrl ;
84
+
85
+ const isFileScheme = originalUrl . startsWith ( 'file:' ) ;
86
+
87
+ if ( isFileScheme ) {
88
+ // eslint-disable-next-line no-param-reassign
89
+ url = originalUrl . slice ( 5 ) ;
90
+ }
91
+
92
+ let resolutionMap = [ ] ;
93
+
94
+ if ( includePaths . length > 0 && ! isFileScheme ) {
95
+ // The order of import precedence is as follows:
96
+ //
97
+ // 1. Filesystem imports relative to the base file.
98
+ // 2. Custom importer imports.
99
+ // 3. Filesystem imports relative to the working directory.
100
+ // 4. Filesystem imports relative to an `includePaths` path.
101
+ // 5. Filesystem imports relative to a `SASS_PATH` path.
102
+ //
103
+ // Because `sass`/`node-sass` run custom importers before `3`, `4` and `5` points, we need to emulate this behavior to avoid wrong resolution.
104
+ const sassPossibleRequests = getPossibleRequests ( url ) ;
105
+
106
+ resolutionMap = resolutionMap . concat (
93
107
includePaths . map ( ( context ) => ( {
94
108
resolve : sassResolve ,
95
109
context,
96
110
possibleRequests : sassPossibleRequests ,
97
111
} ) )
98
- )
99
- . concat ( {
100
- resolve : webpackResolve ,
101
- context : dirContextFrom ( prev ) ,
102
- possibleRequests : webpackPossibleRequests ,
103
- } ) ;
112
+ ) ;
113
+ }
114
+
115
+ const webpackPossibleRequests = getPossibleRequests ( url , true ) ;
116
+
117
+ resolutionMap = resolutionMap . concat ( {
118
+ resolve : webpackResolve ,
119
+ context : isFileScheme ? '/' : dirContextFrom ( prev ) ,
120
+ possibleRequests : webpackPossibleRequests ,
121
+ } ) ;
104
122
105
123
startResolving ( resolutionMap )
106
124
// Catch all resolving errors, return the original file and pass responsibility back to other custom importers
107
- . catch ( ( ) => ( { file : url } ) )
125
+ . catch ( ( ) => ( { file : originalUrl } ) )
108
126
. then ( done ) ;
109
127
} ;
110
128
}
0 commit comments