Write conditional comments in the project code first, and then execute define-cli
before building the project. Cli will activate or comment out the code blocks based on the current define conditions.
Inspired by C#
Conditional Compile,define-cli suports several features:
// '#define' is used to define a condition symbol'
// It can be used in any place (.ts comment)
// For example:
// #define DEBUG = true
// #define CONDITION_2 = true
// #define UPPER_CASE = false
// #define CUSTOM_CONDITION = true
// Simple example:
// #if DEBUG
console.log('active');
// #else
// -- console.log('inactive');
// #endif
// Full example:
// #if DEBUG && UPPER_CASE
// -- console.log('inactive');
// #elif (CUSTOM_CONDITION && UPPER_CASE) || !CONDITION_2
// -- console.log('inactive');
// #elif DEBUG
console.log('active');
// #endif
// Nested example:
// #if DEBUG
console.log('active');
// #if CONDITION_2
console.log('active');
// #else
// -- console.log('inactive');
// #endif
// #endif
The same way can be used to control import packages. 同样的方式可以控制导入包。
// #if DEBUG
import path from 'path';
// #else
// -- import path from 'path';
// #endif
The same way can be used to controll function block and class. 可以控制函数块和类。
// #if DEBUG
test(){
console.log('test a');
}
// #else
// -- test(){
// -- console.log('test b');
// -- }
// #endif
Currently, only Windows is supported. 仅支持Windows。
npm install -g conditional-compile-helper
// see help
// 查看帮助
define-cli -h
// -i input .ts file or directory
// -i 输入 .ts 文件或文件夹
define-cli -i /path/to/your-file.ts
// multiple file or directories
// 同时输入多个文件或者文件夹
define-cli -i /path/to1/ -i /path/to2/
// -d set or override condition
// -d 设置或者覆盖编译条件
define-cli -i /path/to/your-file.ts -d DEBUG=false
// override multiple conditions
// 同时覆盖多个条件
define-cli -i /path/to/your-project-lib-dir -d DEBUG=false,CONDITION_2=true
// -a active all condition controls code
// -a 激活所有条件控制的代码
define-cli -a -i /path/to/
// -c disactive all condition controls code
// -c 注释掉所有条件控制的代码
define-cli -c -i /path/to/
// -t test mode,print all conditions solve result and does not modify your code
// -t 测试模式,打印所有条件控制代码的解决结果,不会修改你的代码
define-cli -t -i /path/to/
-l, --language The project's programming language
[dart] Suitable for Dart or Flutter projects
[typescript] (default) Suitable for TypeScript project
-i, --input The path to a handled can be specified with multiple -i options for multiple files or folders.
-d, --define=<KEY=true/false> Setting compile condition will override any values defined in the code. eg. -d DEBUG=true,TEST=false
The cli reads default configuration file in the current working directory (if it exists): define.settings.yaml
--settings=<path> Read predefined command-line parameters from a specified YAML configuration file.
--settings-init Generate a default configuration file in the current working directory: define.settings.yaml
active-all and comment-all are mutually exclusive. If both parameters are used simultaneously, active-all takes precedence.
-a, --active-all Ignore all compilation conditions and activate all conditional compilation code (remove comments).
-c, --comment-all Ignore all compilation conditions and comment out all conditional compilation code.
-t, --test Just test, and it will not modify the code in any way.
The default mode is strict, and compilation conditions must be in all uppercase.
-s, --disable-strict In --disable-strict mode, there will not be an error when using undefined conditions. However, it will handle them falsely.
-e, --enable-lowercase The compilation condition allow lowercase letters, but when there is no flag for this purpose, the compilation condition must be completely uppercase.
-h, --help See help
Email: 527204190@qq.com