How to achieve a precompiler directive such as functionality

I am developing an angular application, and he recommends using the generated code for many things running in production, namely template caches, expression caches, and a static DI injector. There is currently no good way to switch between different build configurations, so I am using the recommended template here :

In the lib / main.dart file, you can import the imported initializer-prod.dart file, which has the identifier initializer-dev.dart. Switching between these two files will allow you to switch between prod and dev modes. Before using prod mode, you will need to run the script generator.

This results in the following import:

//import 'initializer_prod.dart' as init; // Use in prod/test.
import 'initializer_dev.dart' as init; // Use in dev.

As you can see, switching the import is a manual process. Is there a better, more automatic way to achieve this?

+2
source share
1 answer

I see two possibilities (I have not tried any of them yet)

or

log(String msg) {
  if (const String.fromEnvironment('DEBUG') != null) {
    print('debug: $msg');
  }
}

main() {
  log('In production, I do not exist');
}

Some links about transformers:


dart2js pubspec.yaml,

transformers:
- $dart2js:
    commandLineOptions: [-DDEBUG=true]
    environment:
      DEBUG: "true"
    suppressWarnings: true
    terse: true

pub build , (yaml list formLineOptions, yaml map)
String.fromEnvironment()

: dart2js

dart2js pubspec.yaml

-2

:

transformers: # or dev_transformers
- $dart2js:
  environment: { PROD: "true" }

,

String.fromEnvironment()

main() {
  print('PROD: ${const String.fromEnvironment('PROD')}'); 
  // works in the browser
  // prints 'PROD: null' in Dartium
  // prints 'PROD: true' in Chrome
}

. dart2js Transformer

-3

- assert . assert .

+2

Source: https://habr.com/ru/post/1532570/


All Articles