Search code examples
dartdart-pub

dart pub build: exclude a file or directory


I am trying to exclude a list of files or directories when building a web application with dart's pub build. Using this, as suggested by the documentation:

transformers:
- simple_transformer:
    $exclude: "**/CVS"

does not work:

Error on line 10, column 3 of pubspec.yaml: "simple_transformer" is not a dependency.

- simple_transformer:

Is there a way to do it (using SDK 1.10.0) ?


Solution

  • Sadly there is currently no support to mark files as ignored by pub build as Günter already mentioned. The .gitignore feature was removed as it was undocumented and caused more trouble than it solved.

    But you can execlude files from the build output. This means that the files are still processed (and still take time to process =/ ) but aren't present in the output directiory. This is useful for generating a deployable copy of your application in one go.

    In our application we use a simple ConsumeTransformer to mark assets as consumed so that they are not written to the output folder:

    library consume_transformer;
    
    import 'package:barback/barback.dart';
    
    class ConsumeTransformer extends Transformer implements LazyTransformer {
      final List<RegExp> patterns = <RegExp>[];
    
      ConsumeTransformer.asPlugin(BarbackSettings settings) {
        if (settings.configuration['patterns'] != null) {
          for (var pattern in settings.configuration['patterns']) {
            patterns.add(new RegExp(pattern));
          }
        }
      }
    
      bool isPrimary(AssetId inputId) =>
          patterns.any((p) => p.hasMatch(inputId.path));
    
      void declareOutputs(DeclaringTransform transform) {}
    
      void apply(Transform transform) => transform.consumePrimary();
    }
    

    The consumer requires a list of regex patterns as an argument an consumes the matched files. You need to add the transformer to you pubspec.yaml file as the last transformer:

    transformers:
    - ... # Your other transformers
    - packagename/consume_transformer:
        patterns: ["\\.psd$"]
    

    The example configuration ignores all files that have the psd extension, but you can add pattern as you need them.

    I created a pub package that contains the transformer, take a look here.