dart check if is building

自古美人都是妖i 提交于 2019-12-23 18:42:13

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!