@@ -63,6 +63,20 @@ namespace Sass {
6363 block_stack.push_back (root);
6464 root->is_root (true );
6565 read_bom ();
66+
67+ if (ctx.queue .size () == 1 ) {
68+ Import* pre = new (ctx.mem ) Import (pstate);
69+ string load_path (ctx.queue [0 ].load_path );
70+ do_import (load_path, pre , ctx.c_headers , false );
71+ ctx.head_imports = ctx.queue .size () - 1 ;
72+ if (!pre ->urls ().empty ()) (*root) << pre ;
73+ if (!pre ->files ().empty ()) {
74+ for (size_t i = 0 , S = pre ->files ().size (); i < S; ++i) {
75+ (*root) << new (ctx.mem ) Import_Stub (pstate, pre ->files ()[i]);
76+ }
77+ }
78+ }
79+
6680 lex< optional_spaces >();
6781 Selector_Lookahead lookahead_result;
6882 while (position < end) {
@@ -193,6 +207,52 @@ namespace Sass {
193207
194208 }
195209
210+ bool Parser::do_import (const string& import_path, Import* imp, vector<Sass_Importer_Entry> importers, bool only_one)
211+ {
212+ bool has_import = false ;
213+ string load_path = unquote (import_path);
214+ for (auto importer : importers) {
215+ // int priority = sass_importer_get_priority(importer);
216+ Sass_Importer_Fn fn = sass_importer_get_function (importer);
217+ if (Sass_Import_List includes =
218+ fn (load_path.c_str (), importer, ctx.c_compiler )
219+ ) {
220+ Sass_Import_List list = includes;
221+ while (*includes) {
222+ Sass_Import_Entry include = *includes;
223+ const char *file = sass_import_get_path (include);
224+ char * source = sass_import_take_source (include);
225+ size_t line = sass_import_get_error_line (include);
226+ size_t column = sass_import_get_error_column (include);
227+ const char * message = sass_import_get_error_message (include);
228+ if (message) {
229+ if (line == string::npos && column == string::npos) error (message, pstate);
230+ else error (message, ParserState (message, source, Position (line, column)));
231+ } else if (source) {
232+ if (file) {
233+ ctx.add_source (file, load_path, source);
234+ imp->files ().push_back (file);
235+ } else {
236+ ctx.add_source (load_path, load_path, source);
237+ imp->files ().push_back (load_path);
238+ }
239+ } else if (file) {
240+ import_single_file (imp, file);
241+ }
242+ ++includes;
243+ }
244+ // deallocate returned memory
245+ sass_delete_import_list (list);
246+ // set success flag
247+ has_import = true ;
248+ // break import chain
249+ if (only_one) return true ;
250+ }
251+ }
252+ // return result
253+ return has_import;
254+ }
255+
196256 Import* Parser::parse_import ()
197257 {
198258 lex< kwd_import >();
@@ -201,52 +261,11 @@ namespace Sass {
201261 do {
202262 while (lex< block_comment >());
203263 if (lex< quoted_string >()) {
204- string import_path (lexed);
205- bool has_custom_import = false ;
206- string load_path = unquote (import_path);
207- for (auto importer : ctx.c_importers ) {
208- if (has_custom_import) break ;
209- Sass_Importer_Fn fn = sass_importer_get_function (importer);
210- // int priority = sass_importer_get_priority(importer);
211- if (Sass_Import_List includes =
212- fn (load_path.c_str (), importer, ctx.c_compiler )
213- ) {
214- Sass_Import_List list = includes;
215- while (*includes) {
216- Sass_Import_Entry include = *includes;
217- const char *file = sass_import_get_path (include);
218- char * source = sass_import_take_source (include);
219- size_t line = sass_import_get_error_line (include);
220- size_t column = sass_import_get_error_column (include);
221- const char * message = sass_import_get_error_message (include);
222- if (message) {
223- if (line == string::npos && column == string::npos) error (message, pstate);
224- else error (message, ParserState (message, source, Position (line, column)));
225- } else if (source) {
226- if (file) {
227- ctx.add_source (file, load_path, source);
228- imp->files ().push_back (file);
229- } else {
230- ctx.add_source (load_path, load_path, source);
231- imp->files ().push_back (load_path);
232- }
233- } else if (file) {
234- import_single_file (imp, file);
235- }
236- ++includes;
237- }
238- // deallocate returned memory
239- sass_delete_import_list (list);
240- // break import chain
241- has_custom_import = true ;
242- }
243- }
244-
245- if (!has_custom_import) {
264+ if (!do_import (lexed, imp, ctx.c_importers , true ))
265+ {
246266 // push single file import
247- import_single_file (imp, import_path );
267+ import_single_file (imp, lexed );
248268 }
249-
250269 }
251270 else if (peek< uri_prefix >()) {
252271 imp->urls ().push_back (parse_value ());
0 commit comments