问题
I would like to skip some specific code on pub build.
example:
Log.print('something ${StackTrace.current}');
I would like that the code above was not transpilled to JS in production.
回答1:
Asserts are only executed in checked mode and won't be included by pub build
in production mode by default:
assert(() {
Log.print('something ${StackTrace.current}');
return true;
})
DartPad example doesn't print it because it builds in production mode.
You can also pass "environment" (not mix up with OS environment variables) to pub build
and read it in code
transformers: # or dev_transformers
- $dart2js:
environment: { PROD: "true" }
const prod = String.fromEnvironment('PROD')
print('PROD: $prod');
// prints 'PROD: null' in Dartium
// prints 'PROD: true' in Chrome
See also https://stackoverflow.com/a/22524258/217408
来源:https://stackoverflow.com/questions/42487428/dart-check-if-is-building