diff --git a/lib/src/configuration.dart b/lib/src/configuration.dart index fb4138481..a9b254426 100644 --- a/lib/src/configuration.dart +++ b/lib/src/configuration.dart @@ -63,6 +63,8 @@ final class Configuration { bool get isEmpty => values.isEmpty; + int get length => values.length; + /// Removes a variable with [name] from this configuration, returning it. /// /// If no such variable exists in this configuration, returns null. diff --git a/lib/src/visitor/async_evaluate.dart b/lib/src/visitor/async_evaluate.dart index c1db0e72e..ec6fd9482 100644 --- a/lib/src/visitor/async_evaluate.dart +++ b/lib/src/visitor/async_evaluate.dart @@ -853,33 +853,8 @@ final class _EvaluateVisitor bool namesInErrors = false, }) async { var url = stylesheet.span.sourceUrl; - - if (_modules[url] case var alreadyLoaded?) { - var currentConfiguration = configuration ?? _configuration; - if (!_moduleConfigurations[url]!.sameOriginal(currentConfiguration) && - currentConfiguration is ExplicitConfiguration) { - var message = namesInErrors - ? "${p.prettyUri(url)} was already loaded, so it can't be " - "configured using \"with\"." - : "This module was already loaded, so it can't be configured using " - "\"with\"."; - - var existingSpan = _moduleNodes[url]?.span; - var configurationSpan = configuration == null - ? currentConfiguration.nodeWithSpan.span - : null; - var secondarySpans = { - if (existingSpan != null) existingSpan: "original load", - if (configurationSpan != null) configurationSpan: "configuration", - }; - - throw secondarySpans.isEmpty - ? _exception(message) - : _multiSpanException(message, "new load", secondarySpans); - } - - return alreadyLoaded; - } + var moduleUsedOverrides = false; + var currentConfiguration = configuration ?? _configuration; var environment = AsyncEnvironment(); late CssStylesheet css; @@ -916,12 +891,18 @@ final class _EvaluateVisitor _inKeyframes = false; if (configuration != null) _configuration = configuration; + // setup testing if the module accessed (& consumed) any configuration variables. + var configLength1 = _configuration.length; + await visitStylesheet(stylesheet); css = _outOfOrderImports == null ? root : CssStylesheet(_addOutOfOrderImports(), stylesheet.span); preModuleComments = _preModuleComments; + // determine if the module accessed (& consumed) any configuration variables. + moduleUsedOverrides = configLength1 > _configuration.length; + _importer = oldImporter; __stylesheet = oldStylesheet; __root = oldRoot; @@ -939,6 +920,34 @@ final class _EvaluateVisitor _configuration = oldConfiguration; }); + if (_modules[url] case var alreadyLoaded?) { + // note: only throw an error if module used any of the config vars, otherwise treat it as if it was called without "with". + if (moduleUsedOverrides && + !_moduleConfigurations[url]!.sameOriginal(currentConfiguration) && + currentConfiguration is ExplicitConfiguration) { + var message = namesInErrors + ? "${p.prettyUri(url)} was already loaded, so it can't be " + "configured using \"with\"." + : "This module was already loaded, so it can't be configured using " + "\"with\"."; + + var existingSpan = _moduleNodes[url]?.span; + var configurationSpan = configuration == null + ? currentConfiguration.nodeWithSpan.span + : null; + var secondarySpans = { + if (existingSpan != null) existingSpan: "original load", + if (configurationSpan != null) configurationSpan: "configuration", + }; + + throw secondarySpans.isEmpty + ? _exception(message) + : _multiSpanException(message, "new load", secondarySpans); + } + + return alreadyLoaded; + } + var module = environment.toModule( css, preModuleComments ?? const {}, diff --git a/lib/src/visitor/evaluate.dart b/lib/src/visitor/evaluate.dart index 5727e56aa..cc097e278 100644 --- a/lib/src/visitor/evaluate.dart +++ b/lib/src/visitor/evaluate.dart @@ -5,7 +5,7 @@ // DO NOT EDIT. This file was generated from async_evaluate.dart. // See tool/grind/synchronize.dart for details. // -// Checksum: a3068d04660dd2bed34b884aa6e1a21d423dc4e5 +// Checksum: 4c0db6ff92d072b0165af02288d9d3a2782d137d // // ignore_for_file: unused_import @@ -861,33 +861,8 @@ final class _EvaluateVisitor bool namesInErrors = false, }) { var url = stylesheet.span.sourceUrl; - - if (_modules[url] case var alreadyLoaded?) { - var currentConfiguration = configuration ?? _configuration; - if (!_moduleConfigurations[url]!.sameOriginal(currentConfiguration) && - currentConfiguration is ExplicitConfiguration) { - var message = namesInErrors - ? "${p.prettyUri(url)} was already loaded, so it can't be " - "configured using \"with\"." - : "This module was already loaded, so it can't be configured using " - "\"with\"."; - - var existingSpan = _moduleNodes[url]?.span; - var configurationSpan = configuration == null - ? currentConfiguration.nodeWithSpan.span - : null; - var secondarySpans = { - if (existingSpan != null) existingSpan: "original load", - if (configurationSpan != null) configurationSpan: "configuration", - }; - - throw secondarySpans.isEmpty - ? _exception(message) - : _multiSpanException(message, "new load", secondarySpans); - } - - return alreadyLoaded; - } + var moduleUsedOverrides = false; + var currentConfiguration = configuration ?? _configuration; var environment = Environment(); late CssStylesheet css; @@ -924,12 +899,18 @@ final class _EvaluateVisitor _inKeyframes = false; if (configuration != null) _configuration = configuration; + // setup testing if the module accessed (& consumed) any configuration variables. + var configLength1 = _configuration.length; + visitStylesheet(stylesheet); css = _outOfOrderImports == null ? root : CssStylesheet(_addOutOfOrderImports(), stylesheet.span); preModuleComments = _preModuleComments; + // determine if the module accessed (& consumed) any configuration variables. + moduleUsedOverrides = configLength1 > _configuration.length; + _importer = oldImporter; __stylesheet = oldStylesheet; __root = oldRoot; @@ -947,6 +928,34 @@ final class _EvaluateVisitor _configuration = oldConfiguration; }); + if (_modules[url] case var alreadyLoaded?) { + // note: only throw an error if module used any of the config vars, otherwise treat it as if it was called without "with". + if (moduleUsedOverrides && + !_moduleConfigurations[url]!.sameOriginal(currentConfiguration) && + currentConfiguration is ExplicitConfiguration) { + var message = namesInErrors + ? "${p.prettyUri(url)} was already loaded, so it can't be " + "configured using \"with\"." + : "This module was already loaded, so it can't be configured using " + "\"with\"."; + + var existingSpan = _moduleNodes[url]?.span; + var configurationSpan = configuration == null + ? currentConfiguration.nodeWithSpan.span + : null; + var secondarySpans = { + if (existingSpan != null) existingSpan: "original load", + if (configurationSpan != null) configurationSpan: "configuration", + }; + + throw secondarySpans.isEmpty + ? _exception(message) + : _multiSpanException(message, "new load", secondarySpans); + } + + return alreadyLoaded; + } + var module = environment.toModule( css, preModuleComments ?? const {},