Merge pull request #1668 from jareguo/master

完善 CC_XXX 系列宏以便用户区分发布版本
This commit is contained in:
Jare Guo
2017-06-14 23:01:00 +08:00
committed by GitHub
12 changed files with 205 additions and 101 deletions
+4 -4
View File
@@ -341,10 +341,10 @@ function genLogFunc(func, type) {
return CC_JSB ? function (...args) {
var id = args[0];
if (args.length === 1) {
CC_DEV ? func(cc._LogInfos[id]) : func(type + ' ' + id + ', please go to ' + errorMapUrl + '#' + id + ' to see details.');
CC_DEBUG ? func(cc._LogInfos[id]) : func(type + ' ' + id + ', please go to ' + errorMapUrl + '#' + id + ' to see details.');
return;
}
if (CC_DEV) {
if (CC_DEBUG) {
args[0] = cc._LogInfos[id];
func.apply(cc, args);
} else {
@@ -359,10 +359,10 @@ function genLogFunc(func, type) {
} : function (id) {
'use strict';
if (arguments.length === 1) {
CC_DEV ? func(cc._LogInfos[id]) : func(type + ' ' + id + ', please go to ' + errorMapUrl + '#' + id + ' to see details.');
CC_DEBUG ? func(cc._LogInfos[id]) : func(type + ' ' + id + ', please go to ' + errorMapUrl + '#' + id + ' to see details.');
return;
}
if (CC_DEV) {
if (CC_DEBUG) {
let argsArr = cc.js.shiftArguments.apply(null, arguments);
func.apply(cc, [cc._LogInfos[id]].concat(argsArr));
} else {
+1 -1
View File
@@ -1,4 +1,4 @@
if (CC_DEV) {
if (CC_DEBUG) {
var logs = {
//ActionManager: 1000
"1000": "cc.ActionManager.addAction(): action must be non-null", //addAction
+1 -1
View File
@@ -170,7 +170,7 @@ var NodeActivator = cc.Class({
},
_deactivateNodeRecursively (node) {
if (CC_TEST || CC_DEV) {
if (CC_DEV) {
cc.assert(!(node._objFlags & Deactivating), 'node should not deactivating');
// ensures _activeInHierarchy is always changing when Deactivating flagged
cc.assert(node._activeInHierarchy, 'node should not deactivated');
+27 -31
View File
@@ -25,7 +25,7 @@
'use strict';
const Utils = require('./utils');
const Utils = require('../util/utils');
const Path = require('path');
const Source = require('vinyl-source-stream');
@@ -48,12 +48,7 @@ exports.buildCocosJs = function (sourceFile, outputFile, excludes, callback) {
bundler.ignore(file);
});
var uglifyOption = Utils.uglifyOptions(false, {
CC_EDITOR: false,
CC_DEV: true,
CC_TEST: false,
CC_JSB: false
});
var uglifyOption = Utils.uglifyOptions('build', false, true);
bundler = bundler.bundle();
bundler = bundler.pipe(Source(outFile));
@@ -81,12 +76,7 @@ exports.buildCocosJsMin = function (sourceFile, outputFile, excludes, callback,
bundler.ignore(file);
});
var uglifyOption = Utils.uglifyOptions(true, {
CC_EDITOR: false,
CC_DEV: false,
CC_TEST: false,
CC_JSB: false
});
var uglifyOption = Utils.uglifyOptions('build', false, false);
var Size = null;
try {
@@ -145,12 +135,7 @@ exports.buildPreview = function (sourceFile, outputFile, callback) {
.pipe(Source(outFile))
.pipe(Buffer())
.pipe(Sourcemaps.init({loadMaps: true}))
.pipe(Minifier(Utils.uglifyOptions(false, {
CC_EDITOR: false,
CC_DEV: true,
CC_TEST: false,
CC_JSB: false
}), UglifyHarmony))
.pipe(Minifier(Utils.uglifyOptions('preview', false, false), UglifyHarmony))
.pipe(Optimizejs({
sourceMap: false
}))
@@ -163,6 +148,27 @@ exports.buildPreview = function (sourceFile, outputFile, callback) {
.on('end', callback);
};
exports.buildJsbPreview = function (sourceFile, outputFile, jsbSkipModules, callback) {
var outFile = Path.basename(outputFile);
var outDir = Path.dirname(outputFile);
var bundler = Utils.createBundler(sourceFile);
jsbSkipModules.forEach(function (module) {
bundler.ignore(require.resolve(module));
});
bundler.bundle()
.on('error', HandleErrors.handler)
.pipe(HandleErrors())
.pipe(Source(outFile))
.pipe(Buffer())
.pipe(Minifier(Utils.uglifyOptions('preview', true, false), UglifyHarmony))
.pipe(Optimizejs({
sourceMap: false
}))
.pipe(Gulp.dest(outDir))
.on('end', callback);
};
exports.buildJsb = function (sourceFile, outputFile, jsbSkipModules, callback) {
var outFile = Path.basename(outputFile);
var outDir = Path.dirname(outputFile);
@@ -176,12 +182,7 @@ exports.buildJsb = function (sourceFile, outputFile, jsbSkipModules, callback) {
.pipe(HandleErrors())
.pipe(Source(outFile))
.pipe(Buffer())
.pipe(Minifier(Utils.uglifyOptions(false, {
CC_EDITOR: false,
CC_DEV: false, // CC_DEV should be false after build
CC_TEST: false,
CC_JSB: true
}), UglifyHarmony))
.pipe(Minifier(Utils.uglifyOptions('build', true, true), UglifyHarmony))
.pipe(Optimizejs({
sourceMap: false
}))
@@ -202,12 +203,7 @@ exports.buildJsbMin = function (sourceFile, outputFile, jsbSkipModules, callback
.pipe(HandleErrors())
.pipe(Source(outFile))
.pipe(Buffer())
.pipe(Minifier(Utils.uglifyOptions(true, {
CC_EDITOR: false,
CC_DEV: false,
CC_TEST: false,
CC_JSB: true
}), UglifyHarmony))
.pipe(Minifier(Utils.uglifyOptions('build', true, false), UglifyHarmony))
.pipe(Optimizejs({
sourceMap: false
}))
+2 -7
View File
@@ -40,7 +40,7 @@ const Minifier = require('gulp-uglify/minifier');
const Sourcemaps = require('gulp-sourcemaps');
const UglifyHarmony = require('uglify-js-harmony');
const Utils = require('./utils');
const Utils = require('../util/utils');
exports.build = function (sourceFile, outputFile, sourceFileForExtends, outputFileForExtends, callback) {
var engine = Utils.createBundler(sourceFile)
@@ -51,12 +51,7 @@ exports.build = function (sourceFile, outputFile, sourceFileForExtends, outputFi
.pipe(Buffer())
// remove `..args` used in CC_JSB
.pipe(Sourcemaps.init({loadMaps: true}))
.pipe(Minifier(Utils.uglifyOptions(false, {
CC_EDITOR: false,
CC_DEV: true,
CC_TEST: true,
CC_JSB: false
}), UglifyHarmony))
.pipe(Minifier(Utils.uglifyOptions('test', false, false), UglifyHarmony))
.pipe(Sourcemaps.write('./', {
sourceRoot: '../',
includeContent: false,
+20
View File
@@ -0,0 +1,20 @@
// generate macros for uglify's global_defs
var PLATFORM_MACROS = ['CC_EDITOR', 'CC_PREVIEW', 'CC_BUILD', 'CC_TEST'];
// platform: editor | preview | build | test
module.exports = function (platform, isJSB, isDebugBuild) {
var platformMacro = 'CC_' + platform.toUpperCase();
if (PLATFORM_MACROS.indexOf(platformMacro) === -1) {
throw new Error('Unknown platform: ' + platform);
}
var res = {};
for (var i = 0; i < PLATFORM_MACROS.length; i++) {
var macro = PLATFORM_MACROS[i];
res[macro] = (macro === platformMacro);
}
res['CC_DEV'] = res['CC_EDITOR'] || res['CC_PREVIEW'] || res['CC_TEST'];
res['CC_DEBUG'] = res['CC_DEV'] || !!isDebugBuild;
res['CC_JSB'] = !!isJSB;
return res;
};
+4 -2
View File
@@ -4,12 +4,14 @@ const Path = require('path');
allowReturnOutsideFunctionInBrowserifyTransform();
const Browserify = require('browserify');
const Fs = require('fs');
const Macro = require('./macro');
const preludePath = Path.resolve(__dirname, '../browserify_prelude.js');
const prelude = Fs.readFileSync(preludePath, 'utf8');
exports.uglifyOptions = function (minify, global_defs) {
if (minify) {
exports.uglifyOptions = function (platform, isJSB, isDebugBuild) {
var global_defs = Macro(platform, isJSB, isDebugBuild);
if (!global_defs['CC_DEBUG']) {
return {
compress: {
global_defs: global_defs,
+18 -12
View File
@@ -38,20 +38,20 @@ const Watch = require('./gulp/tasks/watch');
// engine //
/////////////
gulp.task('build-cocos2d-dev', function (done) {
gulp.task('build-html5-dev', function (done) {
Engine.buildCocosJs('./index.js', './bin/cocos2d-js.js', [], done);
});
gulp.task('build-cocos2d-min', function (done) {
gulp.task('build-html5-min', function (done) {
Engine.buildCocosJsMin('./index.js', './bin/cocos2d-js-min.js', [], done);
});
gulp.task('build-html5', ['build-cocos2d-dev', 'build-cocos2d-min']);
gulp.task('build-preview', function (done) {
gulp.task('build-html5-preview', function (done) {
Engine.buildPreview('./index.js', './bin/cocos2d-js-for-preview.js', done);
});
gulp.task('build-html5', ['build-html5-preview', 'build-html5-dev', 'build-html5-min']);
var jsbSkipModules = [
'../../cocos2d/core/CCGame',
'../../cocos2d/core/textures/CCTexture2D',
@@ -105,24 +105,28 @@ var jsbSkipModules = [
'../../cocos2d/render-texture/CCRenderTextureWebGLRenderCmd.js'
];
gulp.task('build-jsb-extends-dev', function (done) {
gulp.task('build-jsb-dev', function (done) {
Engine.buildJsb([
'./jsb/index.js',
'./extends.js'
], './bin/jsb_polyfill.dev.js', jsbSkipModules, done);
});
gulp.task('build-jsb-extends-min', function (done) {
gulp.task('build-jsb-min', function (done) {
Engine.buildJsbMin([
'./jsb/index.js',
'./extends.js'
], './bin/jsb_polyfill.js', jsbSkipModules, done);
});
gulp.task('build-jsb', ['build-jsb-extends-dev', 'build-jsb-extends-min']);
gulp.task('build-min', ['build-html5', 'build-jsb']);
gulp.task('build-jsb-preview', function (done) {
Engine.buildJsbPreview([
'./jsb/index.js',
'./extends.js'
], './bin/jsb_polyfill-for-preview.js', jsbSkipModules, done);
});
gulp.task('build-jsb', ['build-jsb-preview', 'build-jsb-dev', 'build-jsb-min']);
/////////
// test //
@@ -171,12 +175,14 @@ gulp.task('test-no-build', function (done) {
// global //
////////////
gulp.task('build-dev', ['build-preview', 'build-jsb'], function (done) {
// fast build, only for develop
gulp.task('build-dev', ['build-html5-preview', 'build-jsb-preview'], function (done) {
// make dist version dirty
Del(['./bin/.cache'], done);
});
gulp.task('build', ['build-html5', 'build-preview', 'build-jsb'], function (done) {
// only build preview for html5 since it will built by editor
gulp.task('build', ['build-html5-preview', 'build-jsb'], function (done) {
Del(['./bin/.cache'], done);
});
+106 -28
View File
@@ -25,19 +25,106 @@
// MACROS
// if "global_defs" not preprocessed by uglify, just declare them globally,
// this may happened in release version's preview page.
Function(
/* use evaled code to prevent the uglify from renaming symbols */
'if(typeof CC_TEST=="undefined")' +
'CC_TEST=typeof tap=="object"||typeof QUnit=="object";' +
'if(typeof CC_EDITOR=="undefined")' +
'CC_EDITOR=typeof Editor=="object"&&typeof process=="object"&&"electron" in process.versions;' +
'if(typeof CC_DEV=="undefined")' +
'CC_DEV=CC_EDITOR||CC_TEST;' + /* CC_DEV contains CC_TEST and CC_EDITOR */
'if(typeof CC_JSB=="undefined")' +
'CC_JSB=false;'
)();
/**
* !#zh
* 这里是一些用来判断执行环境的宏,这些宏都是全局变量,直接访问即可。<br>
* 在项目构建时,这些宏将会被预处理并根据构建的平台剔除不需要的代码,例如
*
* if (CC_DEBUG) {
* cc.log('debug');
* }
* else {
* cc.log('release');
* }
*
* 在构建后会只剩下
*
* cc.log('release');
*
* (好棒棒)<br>
* <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.
*/
function defineMacro (name, defaultValue) {
Function(
// if "global_defs" not preprocessed by uglify, just declare them globally,
// this may happened in release version's preview page.
// (use evaled code to prevent mangle by uglify)
'if(typeof ' + name + '=="undefined")' +
name + '=' + defaultValue
)();
}
function defined (name) {
return 'typeof ' + name + '=="object"';
}
defineMacro('CC_TEST', defined('tap') + '||' + defined('QUnit'));
defineMacro('CC_EDITOR', defined('Editor') + '&&' + defined('process') + '&&"electron" in process.versions');
defineMacro('CC_PREVIEW', '!CC_EDITOR');
defineMacro('CC_DEV', true); // CC_EDITOR || CC_PREVIEW || CC_TEST
defineMacro('CC_DEBUG', true); // CC_DEV || Debug Build
defineMacro('CC_JSB', defined('jsb'));
defineMacro('CC_BUILD', false);
// PREDEFINE
@@ -45,12 +132,11 @@ require('./predefine');
// load Cocos2d engine code
var isMainProcess = CC_EDITOR && Editor.isMainProcess;
if (!isMainProcess) {
require('./cocos2d/index.js');
if (CC_EDITOR && Editor.isMainProcess) {
cc._initDebugSetting(1); // DEBUG_MODE_INFO
}
else {
cc._initDebugSetting(1); // DEBUG_MODE_INFO
require('./cocos2d/index.js');
}
// LOAD EXTENDS FOR CREATOR
@@ -61,18 +147,10 @@ if (CC_EDITOR) {
// In editor, in addition to the modules defined in cc scope, you can also access to the internal modules by using _require.
// var isDomNode = cc._require('./cocos2d/core/platform/utils').isDomNode;
cc._require = require;
/*
* Checks if the extension is loaded.
* This method is used to make editor code more elegant, so only available in editor.
* @param {string} moduleName - such as 'sp', 'dragonBones', 'TiledMap'... etc.
*/
cc.hasExtension = function (moduleName) {
return moduleName in global || moduleName in cc;
};
}
if (isMainProcess) {
Editor.versions['cocos2d'] = require('./package.json').version;
if (Editor.isMainProcess) {
Editor.versions['cocos2d'] = require('./package.json').version;
}
}
module.exports = cc;
+19 -12
View File
@@ -50,18 +50,25 @@ try {
catch (e) {
}
// Macros, if "global_defs" not preprocessed by uglify, just declare them globally
Function(
/* use evaled code to prevent the uglify from renaming symbols */
'if(typeof CC_TEST=="undefined")' +
'window.CC_TEST=typeof describe!="undefined"||typeof QUnit=="object";' +
'if(typeof CC_EDITOR=="undefined")' +
'window.CC_EDITOR=typeof Editor=="object"&&typeof process=="object"&&"electron" in process.versions;' +
'if(typeof CC_DEV=="undefined")' +
'window.CC_DEV=CC_EDITOR||CC_TEST;' +
'if(typeof CC_JSB=="undefined")' +
'window.CC_JSB=true;'
)();
function defineMacro (name, defaultValue) {
Function(
// if "global_defs" not preprocessed by uglify, just declare them globally,
// this may happened in release version's preview page.
// (use evaled code to prevent mangle by uglify)
'if(typeof ' + name + '=="undefined")' +
name + '=' + defaultValue
)();
}
function defined (name) {
return 'typeof ' + name + '=="object"';
}
defineMacro('CC_TEST', defined('tap') + '||' + defined('QUnit'));
defineMacro('CC_EDITOR', defined('Editor') + '&&' + defined('process') + '&&"electron" in process.versions');
defineMacro('CC_PREVIEW', '!CC_EDITOR');
defineMacro('CC_DEV', true); // CC_EDITOR || CC_PREVIEW || CC_TEST
defineMacro('CC_DEBUG', true); // CC_DEV || Debug Build
defineMacro('CC_JSB', defined('jsb'));
defineMacro('CC_BUILD', false);
require('./jsb-predefine');
require('./jsb-game');
+2 -2
View File
@@ -39,12 +39,12 @@
"gulp-shell": "0.4.1",
"gulp-size": "2.1.0",
"gulp-sourcemaps": "1.5.2",
"gulp-uglify": "1.5.3",
"gulp-uglify": "^2.1.2",
"gulp-util": "3.0.6",
"jshint-stylish": "2.1.0",
"multipipe": "0.3.0",
"require-dir": "0.3.0",
"uglify-js-harmony": "2.7.5",
"uglify-js-harmony": "2.7.7",
"vinyl": "1.1.0",
"vinyl-buffer": "1.0.0",
"vinyl-source-stream": "1.0.0",
+1 -1
View File
@@ -47,7 +47,7 @@ if (CC_DEV) {
// output all info before initialized
require('./CCDebugger');
cc._initDebugSetting(cc.DebugMode.INFO);
if (CC_DEV) {
if (CC_DEBUG) {
require('./DebugInfos');
}