Bootstrap

CocosCreator 源码-predefine.js讲解

本文基于 Cocos Creator 2.4.1 撰写。

文件目录:

/**

 * !#zh

 * 这里是一些用来判断执行环境的宏,这些宏都是全局变量,直接访问即可。<br>

 * 在项目构建时,这些宏将会被预处理并根据构建的平台剔除不需要的代码,例如

 *

 *     if (CC_DEBUG) {

 *         cc.log('debug');

 *     }

 *     else {

 *         cc.log('release');

 *     }

 *

 * 在构建后会只剩下

 *

 *     cc.log('release');

 *

 * <br>

 * 如需判断脚本是否运行于指定平台,可以用如下表达式:

 *

 *     {

 *         "编辑器":  CC_EDITOR,

 *         "编辑器 或 预览":  CC_DEV,

 *         "编辑器 或 预览 或 构建调试":  CC_DEBUG,

 *         "网页预览":  CC_PREVIEW && !CC_JSB,

 *         "模拟器预览":  CC_PREVIEW && CC_JSB,

 *         "构建调试":  CC_BUILD && CC_DEBUG,

 *         "构建发行":  CC_BUILD && !CC_DEBUG,

 *     }

 *

 * !#en

 * Here are some of the macro used to determine the execution environment, these macros are global variables, can be accessed directly.<br>

 * When the project is built, these macros will be preprocessed and discard unreachable code based on the built platform, for example:

 *

 *     if (CC_DEBUG) {

 *         cc.log('debug');

 *     }

 *     else {

 *         cc.log('release');

 *     }

 *

 * After build will become:

 *

 *     cc.log('release');

 *

 * <br>

 * To determine whether the script is running on the specified platform, you can use the following expression:

 *

 *     {

 *         "editor":  CC_EDITOR,

 *         "editor or preview":  CC_DEV,

 *         "editor or preview or build in debug mode":  CC_DEBUG,

 *         "web preview":  CC_PREVIEW && !CC_JSB,

 *         "simulator preview":  CC_PREVIEW && CC_JSB,

 *         "build in debug mode":  CC_BUILD && CC_DEBUG,

 *         "build in release mode":  CC_BUILD && !CC_DEBUG,

 *     }

 *

 * @module GLOBAL-MACROS

 */

/**

 * @property {Boolean} CC_EDITOR - Running in the editor.

 */

/**

 * @property {Boolean} CC_PREVIEW - Preview in browser or simulator.

 */

/**

 * @property {Boolean} CC_DEV - Running in the editor or preview.

 */

/**

 * @property {Boolean} CC_DEBUG - Running in the editor or preview, or build in debug mode.

 */

/**

 * @property {Boolean} CC_BUILD - Running in published project.

 */

/**

 * @property {Boolean} CC_JSB - Running in native platform (mobile app, desktop app, or simulator).

 */

/**

 * @property {Boolean} CC_TEST - Running in the engine's unit test.

 */

/**

 * @property {Boolean} CC_RUNTIME - Running in runtime environments.

 */

// window may be undefined when first load engine from editor

var _global = typeof window === 'undefined' ? global : window;

//再次声明一个_global 没关系,最终都指向了global 作为这个js的局部变量用【指向全局的global】

/*

 * @param defaultValue - The default value is only used in the editor or preview.

 */

/* 在global下面添加默认变量的值:定义全局宏

主要有下面几个,具体说明在下面进行列出

CC_DEV

CC_DEBUG

CC_JSB

CC_NATIVERENDERER

CC_SUPPORT_JIT

CC_PHYSICS_BUILTIN

CC_PHYSICS_CANNON

CC_EDITOR

CC_PREVIEW

CC_TEST

CC_RUNTIME

CC_JSB

*/

function defineMacro (name, defaultValue) {

    // if "global_defs" not preprocessed by uglify, just declare them globally,

    // this may happened in release version's preview page.

    if (typeof _global[name] === 'undefined') {

        _global[name] = defaultValue;

    }

}

/* 以下进行数据绑定,当获取name位某个平台的时候 进行提示输出 */

/* 

这里会在都require完事后,才会执行,此时CCDebug已经初始化,所以cc.warnID可以用。

index.js里面require('./cocos2d/core/predefine');

-->require('./asset-manager');--->require('./deprecated');--->require('../CCDirector');-->const game = require('./CCGame');

--->const debug = require('./CCDebug');

的过程里面会require

这里的require顺序可以不用太关注。

*/

function defineDeprecatedMacroGetter (name, defaultValue) {

    if (typeof _global[name] === 'undefined') {

        Object.defineProperty(_global, name, {

            get: function () {

                let recommandedUsage;

                if (name === 'CC_WECHATGAMESUB') {

                    recommandedUsage = 'cc.sys.platform === cc.sys.WECHAT_GAME_SUB';

                }

                else if (name === 'CC_WECHATGAME') {

                    recommandedUsage = 'cc.sys.platform === cc.sys.WECHAT_GAME';                    

                }

                else if (name === 'CC_QQPLAY') {

                    recommandedUsage = 'cc.sys.platform === cc.sys.QQ_PLAY';

                }

                cc.warnID(1400, name, recommandedUsage);

                return defaultValue;

            }

        });

    }

}

function defined (name) {

    return typeof _global[name] === 'object';

}

// ensure CC_BUILD is defined

// should not use window.CC_BUILD because we need get global_defs defined in uglify

defineMacro('CC_BUILD', false);

// These default values can only be defined after building

// If you need to modify them

// please modify the `global_defs` in the option returned by `gulp/util/utils.js: getUglifyOptions`.

/* 

设置多个编译宏,在构建的时候进行裁剪代码用的

*/

/* 拓展执行说明:

defined('jsb')  具体执行  typeof jsb === 'object' 是否为 true

*/

if (CC_BUILD) {

    _global.CC_BUILD = CC_BUILD;

    _global.CC_DEV = CC_DEV;

    _global.CC_DEBUG = CC_DEBUG;

    _global.CC_JSB = CC_JSB;

    _global.CC_NATIVERENDERER = CC_NATIVERENDERER;

    _global.CC_SUPPORT_JIT = CC_SUPPORT_JIT;

    _global.CC_PHYSICS_BUILTIN = CC_PHYSICS_BUILTIN;

    _global.CC_PHYSICS_CANNON = CC_PHYSICS_CANNON;

    _global.CC_EDITOR = CC_EDITOR;

    _global.CC_PREVIEW = CC_PREVIEW;

    _global.CC_TEST = CC_TEST;

    _global.CC_RUNTIME = CC_RUNTIME;

    _global.CC_JSB = CC_JSB;

}

else {//这里定义的宏都是 bool类型

    defineMacro('CC_DEV', true); //编辑器 或 预览   // (CC_EDITOR && !CC_BUILD) || CC_PREVIEW || CC_TEST

    defineMacro('CC_DEBUG', true);//编辑器 或 预览 或者debug模式的build  // CC_DEV || Debug Build

    defineMacro('CC_JSB', defined('jsb'));//CC_JSB 来判断是否为 native 环境 了解和机器的桥接jsb==在本机平台(移动应用程序、桌面应用程序或模拟器)上运行

    defineMacro('CC_NATIVERENDERER', defined('jsb'));//

    defineMacro('CC_SUPPORT_JIT', true);//是否支持jit 

    defineMacro('CC_PHYSICS_BUILTIN', false);//物理引擎相关

    defineMacro('CC_PHYSICS_CANNON', true);//物理引擎相关

    defineMacro('CC_EDITOR', defined('Editor') && defined('process') && ('electron' in process.versions));//编辑器内部

    defineMacro('CC_PREVIEW', !CC_EDITOR);//预览模式,

    defineMacro('CC_TEST', defined('tap') || defined('QUnit'));//单元测试

    defineMacro('CC_RUNTIME', 'function' === typeof loadRuntime);//在runtime环境中运行

    defineMacro('CC_JSB', defined('jsb') && !CC_RUNTIME);//为什么重复定义了一个CC_JSB ???不知道为啥欢迎补充

}

/* 

定义几个宏判断是微信 或者qqplay平台

*/

// deprecated 

const WECHATGAMESUB = !!(defined('wx') && wx.getSharedCanvas);

const WECHATGAME = !!(defined('wx') && (wx.getSystemInfoSync || wx.getSharedCanvas));

const QQPLAY = defined('bk');

defineDeprecatedMacroGetter('CC_WECHATGAMESUB', WECHATGAMESUB);

/*

宏定义微信平台 CC_WECHATGAME 

*/

defineDeprecatedMacroGetter('CC_WECHATGAME', WECHATGAME);

defineDeprecatedMacroGetter('CC_QQPLAY', QQPLAY);

if (CC_DEV) {

    /**

     * contains internal apis for unit tests

     * @expose

     */

    cc._Test = {};

}

/**

 * @module cc

 */

/**

 * The current version of Cocos2d being used.<br/>

 * Please DO NOT remove this String, it is an important flag for bug tracking.<br/>

 * If you post a bug to forum, please attach this flag.

 * @property {String} ENGINE_VERSION

 * 定义引擎版本号

 */

const engineVersion = '2.4.1';

_global['CocosEngine'] = cc.ENGINE_VERSION = engineVersion;

 

;