@@ -67,10 +67,10 @@ class NodeImporter {
67
67
/// The [previous] URL is the URL of the stylesheet in which the import
68
68
/// appeared. Returns the contents of the stylesheet and the URL to use as
69
69
/// [previous] for imports within the loaded stylesheet.
70
- Tuple2 <String , String > load (String url, Uri previous) {
70
+ Tuple2 <String , String > load (String url, Uri previous, bool forImport ) {
71
71
var parsed = Uri .parse (url);
72
72
if (parsed.scheme == '' || parsed.scheme == 'file' ) {
73
- var result = _resolveRelativePath (p.fromUri (parsed), previous);
73
+ var result = _resolveRelativePath (p.fromUri (parsed), previous, forImport );
74
74
if (result != null ) return result;
75
75
}
76
76
@@ -79,21 +79,24 @@ class NodeImporter {
79
79
previous.scheme == 'file' ? p.fromUri (previous) : previous.toString ();
80
80
for (var importer in _importers) {
81
81
var value = call2 (importer, _context, url, previousString);
82
- if (value != null ) return _handleImportResult (url, previous, value);
82
+ if (value != null ) {
83
+ return _handleImportResult (url, previous, value, forImport);
84
+ }
83
85
}
84
86
85
- return _resolveLoadPathFromUrl (parsed, previous);
87
+ return _resolveLoadPathFromUrl (parsed, previous, forImport );
86
88
}
87
89
88
90
/// Asynchronously loads the stylesheet at [url] .
89
91
///
90
92
/// The [previous] URL is the URL of the stylesheet in which the import
91
93
/// appeared. Returns the contents of the stylesheet and the URL to use as
92
94
/// [previous] for imports within the loaded stylesheet.
93
- Future <Tuple2 <String , String >> loadAsync (String url, Uri previous) async {
95
+ Future <Tuple2 <String , String >> loadAsync (
96
+ String url, Uri previous, bool forImport) async {
94
97
var parsed = Uri .parse (url);
95
98
if (parsed.scheme == '' || parsed.scheme == 'file' ) {
96
- var result = _resolveRelativePath (p.fromUri (parsed), previous);
99
+ var result = _resolveRelativePath (p.fromUri (parsed), previous, forImport );
97
100
if (result != null ) return result;
98
101
}
99
102
@@ -102,22 +105,26 @@ class NodeImporter {
102
105
previous.scheme == 'file' ? p.fromUri (previous) : previous.toString ();
103
106
for (var importer in _importers) {
104
107
var value = await _callImporterAsync (importer, url, previousString);
105
- if (value != null ) return _handleImportResult (url, previous, value);
108
+ if (value != null ) {
109
+ return _handleImportResult (url, previous, value, forImport);
110
+ }
106
111
}
107
112
108
- return _resolveLoadPathFromUrl (parsed, previous);
113
+ return _resolveLoadPathFromUrl (parsed, previous, forImport );
109
114
}
110
115
111
116
/// Tries to load a stylesheet at the given [path] relative to [previous] .
112
117
///
113
118
/// Returns the stylesheet at that path and the URL used to load it, or `null`
114
119
/// if loading failed.
115
- Tuple2 <String , String > _resolveRelativePath (String path, Uri previous) {
116
- if (p.isAbsolute (path)) return _tryPath (path);
120
+ Tuple2 <String , String > _resolveRelativePath (
121
+ String path, Uri previous, bool forImport) {
122
+ if (p.isAbsolute (path)) return _tryPath (path, forImport);
117
123
118
124
// 1: Filesystem imports relative to the base file.
119
125
if (previous.scheme == 'file' ) {
120
- var result = _tryPath (p.join (p.dirname (p.fromUri (previous)), path));
126
+ var result =
127
+ _tryPath (p.join (p.dirname (p.fromUri (previous)), path), forImport);
121
128
if (result != null ) return result;
122
129
}
123
130
return null ;
@@ -128,24 +135,26 @@ class NodeImporter {
128
135
///
129
136
/// Returns the stylesheet at that path and the URL used to load it, or `null`
130
137
/// if loading failed.
131
- Tuple2 <String , String > _resolveLoadPathFromUrl (Uri url, Uri previous) =>
138
+ Tuple2 <String , String > _resolveLoadPathFromUrl (
139
+ Uri url, Uri previous, bool forImport) =>
132
140
url.scheme == '' || url.scheme == 'file'
133
- ? _resolveLoadPath (p.fromUri (url), previous)
141
+ ? _resolveLoadPath (p.fromUri (url), previous, forImport )
134
142
: null ;
135
143
136
144
/// Tries to load a stylesheet at the given [path] from a load path (including
137
145
/// the working directory).
138
146
///
139
147
/// Returns the stylesheet at that path and the URL used to load it, or `null`
140
148
/// if loading failed.
141
- Tuple2 <String , String > _resolveLoadPath (String path, Uri previous) {
149
+ Tuple2 <String , String > _resolveLoadPath (
150
+ String path, Uri previous, bool forImport) {
142
151
// 2: Filesystem imports relative to the working directory.
143
- var cwdResult = _tryPath (p.absolute (path));
152
+ var cwdResult = _tryPath (p.absolute (path), forImport );
144
153
if (cwdResult != null ) return cwdResult;
145
154
146
155
// 3: Filesystem imports relative to [_includePaths].
147
156
for (var includePath in _includePaths) {
148
- var result = _tryPath (p.absolute (p.join (includePath, path)));
157
+ var result = _tryPath (p.absolute (p.join (includePath, path)), forImport );
149
158
if (result != null ) return result;
150
159
}
151
160
@@ -156,8 +165,10 @@ class NodeImporter {
156
165
///
157
166
/// Returns the stylesheet at that path and the URL used to load it, or `null`
158
167
/// if loading failed.
159
- Tuple2 <String , String > _tryPath (String path) {
160
- var resolved = resolveImportPath (path);
168
+ Tuple2 <String , String > _tryPath (String path, bool forImport) {
169
+ var resolved = forImport
170
+ ? inImportRule (() => resolveImportPath (path))
171
+ : resolveImportPath (path);
161
172
return resolved == null
162
173
? null
163
174
: Tuple2 (readFile (resolved), p.toUri (resolved).toString ());
@@ -166,14 +177,14 @@ class NodeImporter {
166
177
/// Converts an importer's return [value] to a tuple that can be returned by
167
178
/// [load] .
168
179
Tuple2 <String , String > _handleImportResult (
169
- String url, Uri previous, Object value) {
180
+ String url, Uri previous, Object value, bool forImport ) {
170
181
if (isJSError (value)) throw value;
171
182
if (value is ! NodeImporterResult ) return null ;
172
183
173
184
var result = value as NodeImporterResult ;
174
185
if (result.file != null ) {
175
- var resolved = _resolveRelativePath (result.file, previous) ??
176
- _resolveLoadPath (result.file, previous);
186
+ var resolved = _resolveRelativePath (result.file, previous, forImport ) ??
187
+ _resolveLoadPath (result.file, previous, forImport );
177
188
if (resolved != null ) return resolved;
178
189
179
190
throw "Can't find stylesheet to import." ;
0 commit comments