如何实现预编译器指令像功能 [英] How to achieve precompiler directive like functionality

查看:119
本文介绍了如何实现预编译器指令像功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个有角度的应用程序,建议对生产中运行的很多事情使用生成的代码,即模板缓存,表达式缓存和静态DI注入器。目前没有良好的方式在不同的构建配置之间切换,所以我使用的模式推荐


在lib / main.dart中,您可以看到正在导入的initializer-prod.dart文件有initializer-dev.dart对应。在这两个文件之间切换将允许您在prod和dev模式之间切换。您需要在使用prod模式之前运行生成器脚本。


这将导致以下导入:

  // import'initializer_prod.dart'as init; //在prod / test中使用。 
import'initializer_dev.dart'as init; // Use in dev。

如您所见,切换导入是一个手动过程。是否有更好的,更自动的方式来实现这一点?

我看到两种可能性)









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

main(){
log('在生产中,我不存在');
}

有关变压器的一些链接:





EDIT

我可以在pubspec.yaml中配置 dart2js 选项,例如



< pre class =lang-yaml prettyprint-override> transformers:
- $ dart2js:
commandLineOptions:[-DDEBUG = true]
环境:
DEBUG:true
suppressWarnings:true
terse:true

如果提供了未知选项,或者不是预期的格式( yaml list for commandLineOptions),则验证和 pub build yaml map 表单环境)

BUT String.fromEnvironment()没有获得值



根据这个问题,这是支持的:
在pub构建期间将参数传递给dart2js



a href =https://code.google.com/p/dart/issues/detail?id=16298 =nofollow>如何将选项从pubspec.yaml传递到dart2js



EDIT-2



我试过了, strong>





 变压器:#或dev_transformers 
- $ dart2js:
环境:{PROD:true}

p>

  String.fromEnvironment()

main(){
print('PROD:$ {const String.fromEnvironment('PROD')}');
//在浏览器中工作
//在Dartium中打印'PROD:null'
//在Chrome中打印'PROD:true'
}

另请参阅配置内置dart2js Transformer



EDIT-3



另一种方法是使用 assert 设置变量。
assert 在生产中被忽略。


I'm developing an angular app, and it's recommended to use generated code for a lot of things running in production, namely template caches, expression caches, and a static DI injector. There's currently no nice way to switch between different build configurations, so I'm using the pattern recommended here:

In lib/main.dart you can see initializer-prod.dart file being imported, which has initializer-dev.dart counterpart. Switching between those two file will allow you to switch between prod and dev modes. You will need to run the generator script before using the prod mode.

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?

解决方案

I see two possibilities (haven't tried any of these myself 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:

EDIT
I was able to configure dart2js options in pubspec.yaml like

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

They are validate and pub build fails if an unknown option is provided or if it's not the expected format (yaml list for commandLineOptions, yaml map form environment)
BUT String.fromEnvironment() didn't get a value

According to this issue, this is supported: Passing in arguments to dart2js during pub build

I filed a bug How to pass options to dart2js from pubspec.yaml

EDIT-2

I tried it and it is working now:

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

access it from the code like

String.fromEnvironment()

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

see also Configuring the Built-in dart2js Transformer

EDIT-3

Another way is to use assert to set variables. assert is ignored in production.

这篇关于如何实现预编译器指令像功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆