Is there a compiler preprocessor in Dart?

后端 未结 4 1842
广开言路
广开言路 2020-12-01 19:43

Since a compilation is required before launching a dart application, i wonder if compiler preprocessor is available, or is scheduled in a near future for Dart.
My search

4条回答
  •  北海茫月
    2020-12-01 20:02

    From http://blog.sethladd.com/2013/12/compile-time-dead-code-elimination-with.html

    Suppose you have code like this:

    log(String msg) {
      if (const String.fromEnvironment('DEBUG') != null) {
        print('debug: $msg');
      }
    }
    
    main() {
      log('In production, I do not exist');
    }
    

    When compiled with dart2js -DDEBUG=true app.dart, the output includes the logging behavior:

      main: function() {
        H.printString("debug: In the production release, I do not exist");
      }
    

    (the log function is inlined, but the print still happens)

    However, if -DDEBUG is not set, the logging behavior is not included in the generated JavaScript code:

      main: function() {
      }
    

提交回复
热议问题