From a772bdf730747c40b02876d03ee4ab18ab736e2e Mon Sep 17 00:00:00 2001 From: ViusualSJ Date: Thu, 23 Jun 2016 14:05:13 +0800 Subject: [PATCH 1/8] Modular gulp command and define module entry --- gulp/tasks/build-cocos2d.js | 173 ----------------------- gulp/tasks/build.js | 269 ------------------------------------ gulp/tasks/clean.js | 32 ----- gulp/tasks/engine.js | 161 +++++++++++++++++++++ gulp/tasks/modular.js | 205 +++++++++++++++++++++++++++ gulp/tasks/test.js | 92 +++++++----- gulp/tasks/utils.js | 57 ++++++++ gulp/tasks/watch.js | 33 ++--- gulpfile.js | 183 ++++++++++++++++++------ index.js | 6 +- module.json | 147 ++++++++++++++++++++ 11 files changed, 787 insertions(+), 571 deletions(-) delete mode 100644 gulp/tasks/build-cocos2d.js delete mode 100644 gulp/tasks/build.js delete mode 100644 gulp/tasks/clean.js create mode 100644 gulp/tasks/engine.js create mode 100644 gulp/tasks/modular.js create mode 100644 gulp/tasks/utils.js create mode 100644 module.json diff --git a/gulp/tasks/build-cocos2d.js b/gulp/tasks/build-cocos2d.js deleted file mode 100644 index 16e491a06e..0000000000 --- a/gulp/tasks/build-cocos2d.js +++ /dev/null @@ -1,173 +0,0 @@ -/**************************************************************************** - Copyright (c) 2013-2016 Chukong Technologies Inc. - - http://www.cocos.com - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated engine source code (the "Software"), a limited, - worldwide, royalty-free, non-assignable, revocable and non-exclusive license - to use Cocos Creator solely to develop games on your target platforms. You shall - not use Cocos Creator software for developing other software or tools that's - used for developing games. You are not granted to publish, distribute, - sublicense, and/or sell copies of Cocos Creator. - - The software or tools in this License Agreement are licensed, not sold. - Chukong Aipu reserves all rights not expressly granted to you. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. - ****************************************************************************/ - -'use strict'; - -var Path = require('path'); -var gulp = require('gulp'); -var concat = require('gulp-concat'); -var es = require('event-stream'); -var sourcemaps = require('gulp-sourcemaps'); - -var srcs = [ - - './cocos2d/core/base-nodes/BaseNodesPropertyDefine.js', - './cocos2d/core/base-nodes/CCSGNode.js', - './cocos2d/core/base-nodes/CCSGNodeCanvasRenderCmd.js', - './cocos2d/core/base-nodes/CCSGNodeWebGLRenderCmd.js', - - './cocos2d/core/scenes/CCSGScene.js', - './cocos2d/core/scenes/CCLoaderScene.js', - - './cocos2d/core/layers/CCLayer.js', - './cocos2d/core/layers/CCLayerCanvasRenderCmd.js', - './cocos2d/core/layers/CCLayerWebGLRenderCmd.js', - - './cocos2d/core/sprites/SpritesPropertyDefine.js', - './cocos2d/core/sprites/CCSGSprite.js', - './cocos2d/core/sprites/CCSGSpriteCanvasRenderCmd.js', - './cocos2d/core/sprites/CCSGSpriteWebGLRenderCmd.js', - './cocos2d/core/sprites/CCBakeSprite.js', - './cocos2d/core/sprites/CCSpriteBatchNode.js', - './cocos2d/core/sprites/CCSpriteBatchNodeCanvasRenderCmd.js', - './cocos2d/core/sprites/CCSpriteBatchNodeWebGLRenderCmd.js', - - './cocos2d/core/sprites/CCScale9Sprite.js', - './cocos2d/core/sprites/CCScale9SpriteCanvasRenderCmd.js', - './cocos2d/core/sprites/CCScale9SpriteWebGLRenderCmd.js', - - './cocos2d/core/CCConfiguration.js', - - './cocos2d/core/CCDrawingPrimitivesCanvas.js', - './cocos2d/core/CCDrawingPrimitivesWebGL.js', - - './cocos2d/core/label/CCSGLabel.js', - './cocos2d/core/label/CCSGLabelCanvasRenderCmd.js', - './cocos2d/core/label/CCSGLabelWebGLRenderCmd.js', - - './cocos2d/core/editbox/CCSGEditBox.js', - './cocos2d/core/editBox/CCdomNode.js', - - './cocos2d/core/videoplayer/CCSGVideoPlayer.js', - - './cocos2d/render-texture/CCRenderTexture.js', - './cocos2d/render-texture/CCRenderTextureCanvasRenderCmd.js', - './cocos2d/render-texture/CCRenderTextureWebGLRenderCmd.js', - - './cocos2d/shape-nodes/CCDrawNode.js', - './cocos2d/shape-nodes/CCDrawNodeCanvasRenderCmd.js', - './cocos2d/shape-nodes/CCDrawNodeWebGLRenderCmd.js', - - './cocos2d/clipping-nodes/CCClippingNode.js', - './cocos2d/clipping-nodes/CCClippingNodeCanvasRenderCmd.js', - './cocos2d/clipping-nodes/CCClippingNodeWebGLRenderCmd.js', - - './cocos2d/particle/CCSGParticleSystem.js', - './cocos2d/particle/CCSGParticleSystemCanvasRenderCmd.js', - './cocos2d/particle/CCSGParticleSystemWebGLRenderCmd.js', - // './cocos2d/particle/CCSGParticleExamples.js', - './cocos2d/particle/CCParticleBatchNode.js', - './cocos2d/particle/CCParticleBatchNodeCanvasRenderCmd.js', - './cocos2d/particle/CCParticleBatchNodeWebGLRenderCmd.js', - - './cocos2d/tilemap/CCSGTMXTiledMap.js', - './cocos2d/tilemap/CCTMXXMLParser.js', - './cocos2d/tilemap/CCTMXObjectGroup.js', - './cocos2d/tilemap/CCSGTMXLayer.js', - './cocos2d/tilemap/CCTMXLayerCanvasRenderCmd.js', - './cocos2d/tilemap/CCTMXLayerWebGLRenderCmd.js', - - './cocos2d/parallax/CCParallaxNode.js', - './cocos2d/parallax/CCParallaxNodeRenderCmd.js', - - './cocos2d/physics/CCPhysicsDebugNode.js', - './cocos2d/physics/CCPhysicsDebugNodeCanvasRenderCmd.js', - './cocos2d/physics/CCPhysicsDebugNodeWebGLRenderCmd.js', - - './cocos2d/motion-streak/CCSGMotionStreak.js', - './cocos2d/motion-streak/CCSGMotionStreakWebGLRenderCmd.js' - -]; - -var header = new Buffer('(function (cc, ccui, ccs, cp) {\n'); -var footer = new Buffer('\n}).call(window, cc, ccui, ccs, cp);\n'); - -//function wrap (header, footer) { -// return es.through(function (file) { -// file.contents = Buffer.concat([header, file.contents, footer]); -// this.emit('data', file); -// }); -//} - -var File = require('vinyl'); -function wrapFile (header, footer) { - var headerFile = new File({ - cwd: process.cwd(), - base: process.cwd(), - path: Path.resolve('header.js'), - contents: header - }); - var footerFile = new File({ - cwd: process.cwd(), - base: process.cwd(), - path: Path.resolve('footer.js'), - contents: footer - }); - var isHeaderInserted = false; - return es.through( - function (file) { - if (!isHeaderInserted) { - this.emit('data', headerFile); - isHeaderInserted = true; - } - //file.contents = Buffer.concat([header, file.contents, footer]); - this.emit('data', file); - }, - function () { - this.emit('data', footerFile); - this.emit('end'); - } - ); -} - -gulp.task('build-modular-cocos2d', function () { - return gulp.src(srcs) - .pipe(wrapFile(header, footer)) - .pipe(sourcemaps.init()) - .pipe(concat(Path.basename(paths.modularCocos2d))) - .pipe(sourcemaps.write('./')) - //.pipe(wrap(header, footer)) - - .pipe(es.through( - function (file) { - var content = file.contents.toString(); - content = content.replace(/\r\n/g , '\n'); - file.contents = new Buffer(content); - this.emit('data', file); - } - )) - - .pipe(gulp.dest(Path.dirname(paths.modularCocos2d))); -}); diff --git a/gulp/tasks/build.js b/gulp/tasks/build.js deleted file mode 100644 index c50033cc38..0000000000 --- a/gulp/tasks/build.js +++ /dev/null @@ -1,269 +0,0 @@ -/**************************************************************************** - Copyright (c) 2013-2016 Chukong Technologies Inc. - - http://www.cocos.com - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated engine source code (the "Software"), a limited, - worldwide, royalty-free, non-assignable, revocable and non-exclusive license - to use Cocos Creator solely to develop games on your target platforms. You shall - not use Cocos Creator software for developing other software or tools that's - used for developing games. You are not granted to publish, distribute, - sublicense, and/or sell copies of Cocos Creator. - - The software or tools in this License Agreement are licensed, not sold. - Chukong Aipu reserves all rights not expressly granted to you. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. - ****************************************************************************/ - -var Path = require('path'); -var Fs = require('fire-fs'); -var Del = require('del'); - -var gulp = require('gulp'); -var mirror = require('gulp-mirror'); -var pipe = require('multipipe'); -var uglify = require('gulp-uglify'); -var rename = require('gulp-rename'); -var sourcemaps = require('gulp-sourcemaps'); -var es = require('event-stream'); -var size = require('gulp-size'); -var chalk = require('chalk'); - -var browserify = require('browserify'); -var source = require('vinyl-source-stream'); -var buffer = require('vinyl-buffer'); -var handleErrors = require('../util/handleErrors'); -//var babelify = require('babelify'); - -require('./build-cocos2d'); - -function getUglifyOptions (minify, global_defs) { - if (minify) { - return { - compress: { - global_defs: global_defs, - } - }; - } - else { - var compress = { - // http://lisperator.net/uglifyjs/compress - global_defs: global_defs, - sequences: false, // join consecutive statements with the “comma operator” - properties: false, // optimize property access: a["foo"] → a.foo - //dead_code: true, // discard unreachable code - drop_debugger: false, // discard “debugger” statements - unsafe: false, // some unsafe optimizations (see below) - conditionals: false, // optimize if-s and conditional expressions - comparisons: false, // optimize comparisons - //evaluate: true, // evaluate constant expressions - booleans: false, // optimize boolean expressions - loops: false, // optimize loops - unused: false, // drop unused variables/functions - hoist_funs: false, // hoist function declarations - hoist_vars: false, // hoist variable declarations - if_return: false, // optimize if-s followed by return/continue - join_vars: false, // join var declarations - cascade: false, // try to cascade `right` into `left` in sequences - side_effects: false // drop side-effect-free statements - //warnings: true // warn about potentially dangerous optimizations/code - }; - return { - mangle: false, - //preserveComments: 'all', - output: { - // http://lisperator.net/uglifyjs/codegen - beautify: true, - bracketize: true - }, - compress: compress - }; - } -} - -function printSizes (sizeGetter, minSizeGetter, zippedSizeGetter) { - return es.through(null, function () { - console.log('Size of web engine: zipped: ' + chalk.cyan(zippedSizeGetter.size.toLocaleString() + 'B') + - ', minimized: ' + chalk.cyan(minSizeGetter.size.toLocaleString() + 'B') + - ', raw: ' + chalk.cyan(sizeGetter.size.toLocaleString() + 'B') + - ', compression ratio: ' + chalk.magenta((zippedSizeGetter.size / sizeGetter.size * 100).toFixed(2) + '%')); - this.emit('end'); - }); -} - -function rebundle_html_dev_min(bundler) { - console.log('Compiling ' + paths.outFileDev + ' and ' + paths.outFile + ' ...'); - - function createSizeGetter (gzip) { - return size({ - gzip: gzip, - pretty: false, - showTotal: false, - showFiles: false, - }); - } - - var sizeGetter = createSizeGetter(false); - var minSizeGetter = createSizeGetter(false); - var zippedSizeGetter = createSizeGetter(true); - - var devPipes = [ - uglify(getUglifyOptions(false, { - CC_EDITOR: false, - CC_DEV: true, - CC_TEST: false, - CC_JSB: false - })), - sizeGetter - ]; - var minPipes = [ - rename(paths.outFile), - uglify(getUglifyOptions(true, { - CC_EDITOR: false, - CC_DEV: false, - CC_TEST: false, - CC_JSB: false - })), - minSizeGetter, - zippedSizeGetter, - ]; - - return bundler.bundle() - .on('error', handleErrors.handler) - .pipe(handleErrors()) - .pipe(source(paths.outFileDev)) - .pipe(buffer()) - .pipe(sourcemaps.init({loadMaps: true})) - .pipe(mirror(pipe(devPipes), pipe(minPipes))) - .pipe(printSizes(sizeGetter, minSizeGetter, zippedSizeGetter)) - .pipe(sourcemaps.write('./', { - sourceRoot: './', - includeContent: true, - addComment: true - })) - .pipe(gulp.dest(paths.outDir)); -} - -function rebundle(bundler, name, options) { - var macros = options && options.macros; // default false - // only if macros // default false - var sourceMaps = !!((options && ('sourceMaps' in options)) ? options.sourceMaps : macros); - var minify = !!(options && options.minify); // default false - - var bundle = bundler.bundle() - .on('error', handleErrors.handler) - .pipe(handleErrors()) - .pipe(source(name)) - .pipe(buffer()); - - if (macros) { - console.log('Compiling ' + name + ' ...'); - if (sourceMaps) { - bundle = bundle.pipe(sourcemaps.init({loadMaps: true})); - } - bundle = bundle.pipe(uglify(getUglifyOptions(minify, macros))); - if (sourceMaps) { - bundle = bundle.pipe(sourcemaps.write('./', { - sourceRoot: '../', - includeContent: false, - addComment: true - })); - } - } - bundle = bundle.pipe(gulp.dest(paths.outDir)); - - return bundle; -} - -function createBundler(entryFiles) { - // https://github.com/substack/node-browserify#methods - var options = { - debug: true, //temporarily disable due to an asar packing bug - detectGlobals: false, // dont insert `process`, `global`, `__filename`, and `__dirname` - bundleExternal: false // dont bundle external modules - //standalone: 'engine-framework', - //basedir: tempScriptDir - }; - return new browserify(entryFiles, options) - .exclude('package.json'); -} - -gulp.task('build-html5', ['build-modular-cocos2d'], function () { - return rebundle_html_dev_min(createBundler(paths.jsEntry)); -}); - -gulp.task('clean-test', function (done) { - Del([paths.test.destEditorExtends, paths.test.dest], - // sometimes it may need to delay 100ms to ensure build-test will be execute... - setTimeout(function () { - done(); - }, 100)); -}); - -gulp.task('build-test', ['build-modular-cocos2d', 'clean-test'], function () { - var engine = rebundle(createBundler(paths.jsEntry), Path.basename(paths.test.dest)); - if (Fs.existsSync(paths.test.jsEntryEditorExtends)) { - var bundler = createBundler(paths.test.jsEntryEditorExtends); - //bundler = bundler.transform(babelify.configure({ - // presets: ["es2015"] - //})); - var editorExtends = rebundle(bundler, Path.basename(paths.test.destEditorExtends)); - return es.merge(engine, editorExtends); - } - else { - // not in editor, skip editor extend - return engine; - } -}); - -gulp.task('build-preview', ['build-modular-cocos2d'], function () { - return rebundle(createBundler(paths.jsEntry), Path.basename(paths.preview.dest), { - macros: { - CC_EDITOR: false, - CC_DEV: true, - CC_TEST: false, - CC_JSB: false - }, - sourceMaps: true - }); -}); - -function rebundle_jsb(bundler, name, minify) { - var skips = paths.jsb.skipModules; - for (var i = 0; i < skips.length; ++i) { - bundler.ignore(require.resolve(skips[i])); - } - rebundle(bundler, name, { - minify: minify, - macros: { - CC_EDITOR: false, - CC_DEV: false, - CC_TEST: false, - CC_JSB: true - } - }); -} - -gulp.task('build-jsb-extends-min', function () { - var jsbPolyfill = rebundle_jsb(createBundler(paths.jsb.entries), paths.jsb.outFile, true); - return jsbPolyfill; -}); -gulp.task('build-jsb-extends-dev', function () { - var jsbPolyfill = rebundle_jsb(createBundler(paths.jsb.entries), paths.jsb.outFileDev, false); - return jsbPolyfill; -}); - -gulp.task('build', ['build-html5', 'build-preview', 'build-jsb-extends-min', 'build-jsb-extends-dev']); -gulp.task('build-min', ['build-html5', 'build-jsb-extends-min']); -gulp.task('build-dev', ['build-preview', 'build-jsb-extends-dev'], function (done) { - // make dist version dirty - Del(['./bin/cocos2d-js.js', './bin/cocos2d-js-min.js', './bin/jsb_polyfill.js'], done); -}); diff --git a/gulp/tasks/clean.js b/gulp/tasks/clean.js deleted file mode 100644 index ca28011700..0000000000 --- a/gulp/tasks/clean.js +++ /dev/null @@ -1,32 +0,0 @@ -/**************************************************************************** - Copyright (c) 2013-2016 Chukong Technologies Inc. - - http://www.cocos.com - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated engine source code (the "Software"), a limited, - worldwide, royalty-free, non-assignable, revocable and non-exclusive license - to use Cocos Creator solely to develop games on your target platforms. You shall - not use Cocos Creator software for developing other software or tools that's - used for developing games. You are not granted to publish, distribute, - sublicense, and/or sell copies of Cocos Creator. - - The software or tools in this License Agreement are licensed, not sold. - Chukong Aipu reserves all rights not expressly granted to you. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. - ****************************************************************************/ - -var Path = require('path'); -var gulp = require('gulp'); -var del = require('del'); - -gulp.task('clean', function (done) { - del(Path.join(paths.outDir, '**/*')); -}); diff --git a/gulp/tasks/engine.js b/gulp/tasks/engine.js new file mode 100644 index 0000000000..c701c8737e --- /dev/null +++ b/gulp/tasks/engine.js @@ -0,0 +1,161 @@ +/**************************************************************************** + Copyright (c) 2013-2016 Chukong Technologies Inc. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Chukong Aipu reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +'use strict'; + +const Utils = require('./utils'); +const Path = require('path'); + +const Source = require('vinyl-source-stream'); +const Gulp = require('gulp'); +const Buffer = require('vinyl-buffer'); +const Uglify = require('gulp-uglify'); +const Sourcemaps = require('gulp-sourcemaps'); +const HandleErrors = require('../util/handleErrors'); + +exports.buildCocosJs = function (sourceFile, outputFile, callback) { + var outDir = Path.dirname(outputFile); + var outFile = Path.basename(outputFile); + var bundler = Utils.createBundler(sourceFile); + bundler.exclude('./bin/modular-cocos2d-cut.js'); + var uglifyOption = Utils.uglifyOptions(false, { + CC_EDITOR: false, + CC_DEV: true, + CC_TEST: false, + CC_JSB: false + }); + + bundler.bundle() + .pipe(Source(outFile)) + .pipe(Buffer()) + .pipe(Sourcemaps.init({loadMaps: true})) + .pipe(Uglify(uglifyOption)) + .pipe(Sourcemaps.write('./', { + sourceRoot: './', + includeContent: true, + addComment: true + })) + .pipe(Gulp.dest(outDir)) + .on('end', callback); +}; + +exports.buildCocosJsMin = function (sourceFile, outputFile, callback) { + var outDir = Path.dirname(outputFile); + var outFile = Path.basename(outputFile); + var bundler = Utils.createBundler(sourceFile); + bundler.exclude('./bin/modular-cocos2d-cut.js'); + var uglifyOption = Utils.uglifyOptions(true, { + CC_EDITOR: false, + CC_DEV: false, + CC_TEST: false, + CC_JSB: false + }); + + bundler.bundle() + .pipe(Source(outFile)) + .pipe(Buffer()) + .pipe(Sourcemaps.init({loadMaps: true})) + .pipe(Uglify(uglifyOption)) + .pipe(Sourcemaps.write('./', { + sourceRoot: './', + includeContent: true, + addComment: true + })) + .pipe(Gulp.dest(outDir)) + .on('end', callback); +}; + +exports.buildPreview = function (sourceFile, outputFile) { + + var outFile = Path.basename(outputFile); + var outDir = Path.dirname(outputFile); + + var bundler = Utils.createBundler(sourceFile); + bundler.exclude('./bin/modular-cocos2d-cut.js') + .bundle() + .on('error', HandleErrors.handler) + .pipe(HandleErrors()) + .pipe(Source(outFile)) + .pipe(Buffer()) + .pipe(Sourcemaps.init({loadMaps: true})) + .pipe(Uglify(Utils.uglifyOptions(false, { + CC_EDITOR: false, + CC_DEV: true, + CC_TEST: false, + CC_JSB: false + }))) + .pipe(Sourcemaps.write('./', { + sourceRoot: '../', + includeContent: false, + addComment: true + })) + .pipe(Gulp.dest(outDir)); +}; + +exports.buildJsbDev = 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(Uglify(Utils.uglifyOptions(false, { + CC_EDITOR: false, + CC_DEV: false, + CC_TEST: false, + CC_JSB: true + }))) + .pipe(Gulp.dest(outDir)) + .on('end', callback); +}; + +exports.buildJsbMin = 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(Uglify(Utils.uglifyOptions(true, { + CC_EDITOR: false, + CC_DEV: false, + CC_TEST: false, + CC_JSB: true + }))) + .pipe(Gulp.dest(outDir)) + .on('end', callback); +}; \ No newline at end of file diff --git a/gulp/tasks/modular.js b/gulp/tasks/modular.js new file mode 100644 index 0000000000..7da5d47983 --- /dev/null +++ b/gulp/tasks/modular.js @@ -0,0 +1,205 @@ +/**************************************************************************** + Copyright (c) 2013-2016 Chukong Technologies Inc. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Chukong Aipu reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +'use strict'; + +const Path = require('path'); +const Async = require('async'); +const gulp = require('gulp'); +const File = require('vinyl'); +const concat = require('gulp-concat'); +const es = require('event-stream'); +const sourcemaps = require('gulp-sourcemaps'); + +//'./cocos2d/parallax/CCParallaxNode.js', +//'./cocos2d/parallax/CCParallaxNodeRenderCmd.js', +//'./cocos2d/physics/CCPhysicsDebugNode.js', +//'./cocos2d/physics/CCPhysicsDebugNodeCanvasRenderCmd.js', +//'./cocos2d/physics/CCPhysicsDebugNodeWebGLRenderCmd.js', + +var modules = { + 'Core': [ + './cocos2d/core/base-nodes/BaseNodesPropertyDefine.js', + './cocos2d/core/base-nodes/CCSGNode.js', + './cocos2d/core/base-nodes/CCSGNodeCanvasRenderCmd.js', + './cocos2d/core/base-nodes/CCSGNodeWebGLRenderCmd.js', + './cocos2d/core/scenes/CCSGScene.js', + './cocos2d/core/scenes/CCLoaderScene.js', + './cocos2d/core/layers/CCLayer.js', + './cocos2d/core/layers/CCLayerCanvasRenderCmd.js', + './cocos2d/core/layers/CCLayerWebGLRenderCmd.js', + './cocos2d/core/CCConfiguration.js', + './cocos2d/core/CCDrawingPrimitivesCanvas.js', + './cocos2d/core/CCDrawingPrimitivesWebGL.js' + ], + + 'Sprite': [ + './cocos2d/core/sprites/SpritesPropertyDefine.js', + './cocos2d/core/sprites/CCSGSprite.js', + './cocos2d/core/sprites/CCSGSpriteCanvasRenderCmd.js', + './cocos2d/core/sprites/CCSGSpriteWebGLRenderCmd.js', + './cocos2d/core/sprites/CCBakeSprite.js', + './cocos2d/core/sprites/CCSpriteBatchNode.js', + './cocos2d/core/sprites/CCSpriteBatchNodeCanvasRenderCmd.js', + './cocos2d/core/sprites/CCSpriteBatchNodeWebGLRenderCmd.js', + './cocos2d/core/sprites/CCScale9Sprite.js', + './cocos2d/core/sprites/CCScale9SpriteCanvasRenderCmd.js', + './cocos2d/core/sprites/CCScale9SpriteWebGLRenderCmd.js' + ], + + 'Label': [ + './cocos2d/core/label/CCSGLabel.js', + './cocos2d/core/label/CCSGLabelCanvasRenderCmd.js', + './cocos2d/core/label/CCSGLabelWebGLRenderCmd.js' + ], + + 'Mask': [ + './cocos2d/shape-nodes/CCDrawNode.js', + './cocos2d/shape-nodes/CCDrawNodeCanvasRenderCmd.js', + './cocos2d/shape-nodes/CCDrawNodeWebGLRenderCmd.js', + './cocos2d/clipping-nodes/CCClippingNode.js', + './cocos2d/clipping-nodes/CCClippingNodeCanvasRenderCmd.js', + './cocos2d/clipping-nodes/CCClippingNodeWebGLRenderCmd.js' + ], + + 'ParticleSystem': [ + './cocos2d/particle/CCSGParticleSystem.js', + './cocos2d/particle/CCSGParticleSystemCanvasRenderCmd.js', + './cocos2d/particle/CCSGParticleSystemWebGLRenderCmd.js', + './cocos2d/particle/CCParticleBatchNode.js', + './cocos2d/particle/CCParticleBatchNodeCanvasRenderCmd.js', + './cocos2d/particle/CCParticleBatchNodeWebGLRenderCmd.js' + ], + + 'TiledMap': [ + './cocos2d/tilemap/CCSGTMXTiledMap.js', + './cocos2d/tilemap/CCTMXXMLParser.js', + './cocos2d/tilemap/CCTMXObjectGroup.js', + './cocos2d/tilemap/CCSGTMXLayer.js', + './cocos2d/tilemap/CCTMXLayerCanvasRenderCmd.js', + './cocos2d/tilemap/CCTMXLayerWebGLRenderCmd.js' + ], + + 'EditorBox': [ + './cocos2d/core/editbox/CCSGEditBox.js', + './cocos2d/core/editBox/CCdomNode.js' + ], + + 'VideoPlayer': [ + './cocos2d/core/videoplayer/CCSGVideoPlayer.js' + ], + + 'MotionStreak': [ + './cocos2d/motion-streak/CCSGMotionStreak.js', + './cocos2d/motion-streak/CCSGMotionStreakWebGLRenderCmd.js', + './cocos2d/render-texture/CCRenderTexture.js', + './cocos2d/render-texture/CCRenderTextureCanvasRenderCmd.js', + './cocos2d/render-texture/CCRenderTextureWebGLRenderCmd.js' + ] + +}; + +/** + * 在生成合并文件的时候插入模块头尾 + * @returns {*} + */ +function wrapFile() { + var cwd = process.cwd(); + var headerFile = new File({ + cwd: cwd, base: cwd, path: Path.resolve('header.js'), + contents: new Buffer('(function (cc, ccui, ccs, cp) {\n') + }); + var footerFile = new File({ + cwd: cwd, base: cwd, path: Path.resolve('footer.js'), + contents: new Buffer('\n}).call(window, cc, ccui, ccs, cp);\n') + }); + var isHeaderInserted = false; + return es.through(function (file) { + if (!isHeaderInserted) { + this.emit('data', headerFile); + isHeaderInserted = true; + } + //file.contents = Buffer.concat([header, file.contents, footer]); + this.emit('data', file); + }, function () { + this.emit('data', footerFile); + this.emit('end'); + }); +} + +exports.buildModular = function ( outFile, outDir, exModule, callback ) { + + var list = []; + for (let name in modules) { + if (exModule.indexOf(name) !== -1) continue; + let files = modules[name]; + files.forEach((file) => { + list.push(file); + }); + } + + list = list.map(function (file) { + return Path.join(__dirname, '../../', file); + }); + + return gulp.src(list) + .pipe(wrapFile()) + .pipe(sourcemaps.init()) + .pipe(concat(outFile)) + .pipe(sourcemaps.write('./')) + .pipe(es.through(function (file) { + var content = file.contents.toString(); + content = content.replace(/\r\n/g, '\n'); + file.contents = new Buffer(content); + this.emit('data', file); + })) + .pipe(gulp.dest(outDir)) + .on('end', callback); +}; + +exports.buildFileWithoutModular = function ( outDir, done ) { + var list = []; + + for (let name in modules) { + let files = modules[name]; + list.push({ + name: name, + files: files + }); + } + + outDir = Path.join(__dirname, '../../', outDir, 'modules'); + Async.eachSeries(list, function ( item, next ) { + return gulp.src(item.files.map(function ( path ) { + console.log(Path.join(__dirname, '../../', path)); + return Path.join(__dirname, '../../', path) + })) + .pipe(concat(item.name + '.js')) + .pipe(gulp.dest(outDir)) + .on('end', next); + }, function () { + done(); + }); +}; \ No newline at end of file diff --git a/gulp/tasks/test.js b/gulp/tasks/test.js index 24d9e5258c..f68603a8d2 100644 --- a/gulp/tasks/test.js +++ b/gulp/tasks/test.js @@ -5,11 +5,11 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated engine source code (the "Software"), a limited, - worldwide, royalty-free, non-assignable, revocable and non-exclusive license + worldwide, royalty-free, non-assignable, revocable and non-exclusive license to use Cocos Creator solely to develop games on your target platforms. You shall - not use Cocos Creator software for developing other software or tools that's - used for developing games. You are not granted to publish, distribute, - sublicense, and/or sell copies of Cocos Creator. + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. The software or tools in this License Agreement are licensed, not sold. Chukong Aipu reserves all rights not expressly granted to you. @@ -23,43 +23,71 @@ THE SOFTWARE. ****************************************************************************/ -var Fs = require('fire-fs'); -var gulp = require('gulp'); -var fb = require('gulp-fb'); -var shell = require('gulp-shell'); +'use strict'; -var TimeOutInSeconds = 5; +const Path = require('fire-path'); +const Fs = require('fire-fs'); +const Del = require('del'); +const Source = require('vinyl-source-stream'); +const Gulp = require('gulp'); +const Fb = require('gulp-fb'); +const Buffer = require('vinyl-buffer'); +const HandleErrors = require('../util/handleErrors'); +const EventStream = require('event-stream'); -gulp.task('unit-runner', function() { - var js = paths.test.src; - var dest = paths.outDir; - var libs = [paths.test.dest]; +const Utils = require('./utils'); + +exports.build = function (sourceFile, outputFile, callback) { + var jsEntryEditorExtends = '../editor/test-utils/engine-extends-entry.js'; + + var bundler = Utils.createBundler(sourceFile); + var engine = bundler.bundle() + .on('error', HandleErrors.handler) + .pipe(HandleErrors()) + .pipe(Source(Path.basename(outputFile))) + .pipe(Buffer()) + .pipe(Gulp.dest(Path.dirname(outputFile))); + + if (Fs.existsSync(jsEntryEditorExtends)) { + var editorExtends = Utils.createBundler(jsEntryEditorExtends) + .bundle() + .on('error', HandleErrors.handler) + .pipe(HandleErrors()) + .pipe(Source(Path.basename(jsEntryEditorExtends))) + .pipe(Buffer()) + .pipe(Gulp.dest(Path.dirname(outputFile))); + EventStream.merge(engine, editorExtends) + .on('end', callback); + } else { + callback(); + } +}; + +exports.clean = function (list, callback) { + Del(list, callback) +}; + +exports.unit = function (outDir, libs, callback) { var title = 'Cocos2d-JS Test Suite'; - if (Fs.existsSync(paths.test.destEditorExtends)) { - libs.push(paths.test.destEditorExtends); + if (Fs.existsSync('./bin/cocos2d-js-extends-for-test.js')) { + libs.push('./bin/cocos2d-js-extends-for-test.js'); title += ' (Editor Extends Included)' } - return gulp.src(js, { read: false, base: './' }) - .pipe(fb.toFileList()) - .pipe(fb.generateRunner(paths.test.runner, dest, title, libs)) - .pipe(gulp.dest(dest)); -}); + return Gulp.src('test/qunit/unit/**/*.js', { read: false, base: './' }) + .pipe(Fb.toFileList()) + .pipe(Fb.generateRunner('test/qunit/lib/qunit-runner.html', outDir, title, libs)) + .pipe(Gulp.dest(outDir)) + .on('end', callback); +}; -function test () { +exports.test = function () { var qunit; try { qunit = require('gulp-qunit'); - } - catch (e) { + } catch (e) { console.error('Please run "npm install gulp-qunit" before running "gulp test".'); throw e; } - return gulp.src('bin/qunit-runner.html', { read: false }) - .pipe(qunit({ timeout: TimeOutInSeconds })); -} - -gulp.task('test', ['build-test', 'unit-runner'], test); -gulp.task('visual-test', ['build-test'], shell.task([ - 'sh ./test/visual-tests/run.sh' -])); -gulp.task('test-no-build', test); + return Gulp.src('bin/qunit-runner.html', { read: false }) + .pipe(qunit({ timeout: 5 })); +}; \ No newline at end of file diff --git a/gulp/tasks/utils.js b/gulp/tasks/utils.js new file mode 100644 index 0000000000..6aa4955eea --- /dev/null +++ b/gulp/tasks/utils.js @@ -0,0 +1,57 @@ +'use strict'; + +const Path = require('path'); +const Browserify = require('browserify'); + +exports.uglifyOptions = function (minify, global_defs) { + if (minify) { + return { + compress: {global_defs: global_defs} + }; + } + + return { + mangle: false, + //preserveComments: 'all', + output: { + // http://lisperator.net/uglifyjs/codegen + beautify: true, + bracketize: true + }, + compress: { + // http://lisperator.net/uglifyjs/compress + global_defs: global_defs, + sequences: false, // join consecutive statements with the “comma operator” + properties: false, // optimize property access: a["foo"] → a.foo + //dead_code: true, // discard unreachable code + drop_debugger: false, // discard “debugger” statements + unsafe: false, // some unsafe optimizations (see below) + conditionals: false, // optimize if-s and conditional expressions + comparisons: false, // optimize comparisons + //evaluate: true, // evaluate constant expressions + booleans: false, // optimize boolean expressions + loops: false, // optimize loops + unused: false, // drop unused variables/functions + hoist_funs: false, // hoist function declarations + hoist_vars: false, // hoist variable declarations + if_return: false, // optimize if-s followed by return/continue + join_vars: false, // join var declarations + cascade: false, // try to cascade `right` into `left` in sequences + side_effects: false // drop side-effect-free statements + //warnings: true // warn about potentially dangerous optimizations/code + } + }; +}; + +exports.createBundler = function createBundler(entryFiles) { + // https://github.com/substack/node-browserify#methods + var options = { + debug: true, //temporarily disable due to an asar packing bug + detectGlobals: false, // dont insert `process`, `global`, `__filename`, and `__dirname` + bundleExternal: false // dont bundle external modules + //standalone: 'engine-framework', + //basedir: tempScriptDir + }; + return new Browserify(entryFiles, options) + .exclude(Path.join(__dirname, '../../package.json')); +}; \ No newline at end of file diff --git a/gulp/tasks/watch.js b/gulp/tasks/watch.js index 00d2483d0c..c5e613c0b9 100644 --- a/gulp/tasks/watch.js +++ b/gulp/tasks/watch.js @@ -1,17 +1,16 @@ var gulp = require('gulp'); -var watchify = require('watchify'); -var browserify = require('browserify'); -var Fs = require('fs'); -var Path = require('path'); +var Watchify = require('watchify'); +var Browserify = require('browserify'); +var Fs = require('fire-fs'); function watchFile(opts) { - var bundler = new browserify(opts.entries, { + var bundler = new Browserify(opts.entries, { cache: {}, packageCache: {}, debug: true, detectGlobals: false, // dont insert `process`, `global`, `__filename`, and `__dirname` bundleExternal: false, // dont bundle external modules - plugin: [watchify] + plugin: [Watchify] }); bundler.on('update', bundle); @@ -46,26 +45,24 @@ function watchFile(opts) { return bundle(); } -gulp.task('watch-preview', function () { +exports.preview = function (sourceFile, outputFile) { return watchFile({ - entries: paths.jsEntry, - dest: paths.preview.dest, + entries: sourceFile, + dest: outputFile, prefix: 'CC_DEV = true;\n' }); -}); +}; -gulp.task('watch-jsb-polyfill', function () { +exports.jsbPolyfill = function (sourceFile, outputFile, skipModules) { return watchFile({ - entries: paths.jsb.entries, - dest: Path.join(paths.outDir, paths.jsb.outFileDev), + entries: sourceFile, + dest: outputFile, prefix: function () { var prefix = 'CC_DEV = true;\n'; prefix += 'CC_JSB = true;\n'; - + return prefix; }, - skips: paths.jsb.skipModules + skips: skipModules }); -}); - -gulp.task('watch-dev-files', ['watch-preview', 'watch-jsb-polyfill']); +}; \ No newline at end of file diff --git a/gulpfile.js b/gulpfile.js index c1036f20e1..fd54cbc19b 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -23,59 +23,150 @@ THE SOFTWARE. ****************************************************************************/ -var gulp = require('gulp'); -var requireDir = require('require-dir'); -var Path = require('path'); +'use strict'; -MinifyOriginCocos2d = false; // true - compile by closure, false - just concat +const Path = require('fire-path'); +const gulp = require('gulp'); +const Del = require('del'); +const Shell = require('gulp-shell'); -// specify game project paths for tasks. -paths = { - src: './src', - jsEntry: './index.js', - outDir: './bin', - outFileDev: 'cocos2d-js.js', - outFile: 'cocos2d-js-min.js', +const Modular = require('./gulp/tasks/modular'); +const Engine = require('./gulp/tasks/engine'); +const Test = require('./gulp/tasks/test'); +const Watch = require('./gulp/tasks/watch'); - jsb: { - entries: [ - './jsb/index.js', - './extends.js' - ], - outFile: 'jsb_polyfill.js', - outFileDev: 'jsb_polyfill.dev.js', - skipModules: [ - '../../cocos2d/core/CCGame', - '../../cocos2d/core/textures/CCTexture2D', - '../../cocos2d/core/sprites/CCSpriteFrame', - '../../cocos2d/core/event/event', - '../../cocos2d/core/load-pipeline/audio-downloader', - '../../cocos2d/audio/CCAudio', - '../../extensions/spine/SGSkeleton', - '../../extensions/spine/SGSkeletonAnimation', - '../../extensions/spine/SGSkeletonCanvasRenderCmd', - '../../extensions/spine/SGSkeletonWebGLRenderCmd', - '../../extensions/spine/lib/spine', - ], - }, +/////////////// +// modular // +/////////////// - test: { - src: 'test/qunit/unit/**/*.js', - runner: 'test/qunit/lib/qunit-runner.html', - jsEntryEditorExtends: '../editor/test-utils/engine-extends-entry.js', // only available in editor - dest: 'bin/cocos2d-js-for-test.js', - destEditorExtends: 'bin/cocos2d-js-extends-for-test.js' - }, +//gulp.task('build-file-without-module', function (done) { +// Modular.buildFileWithoutModular(paths.outDir, done); +//}); +gulp.task('build-all-modular', function (done) { + Modular.buildModular('modular-cocos2d.js', './bin', [], done); +}); +gulp.task('build-cut-modular', function (done) { + Modular.buildModular('modular-cocos2d-cut.js', './bin', [], done); +}); - preview: { - dest: 'bin/cocos2d-js-for-preview.js', - }, +gulp.task('build-modular-cocos2d', ['build-all-modular', 'build-cut-modular']); - modularCocos2d: './bin/modular-cocos2d.js', -}; -// require all tasks in gulp/tasks, including sub-folders -requireDir('./gulp/tasks', { recurse: true }); +///////////// +// engine // +///////////// + +gulp.task('build-cocos2d-dev', ['build-modular-cocos2d'], function (done) { + Engine.buildCocosJs('./index.js', './bin/cocos2d-js.js', done); +}); + +gulp.task('build-cocos2d-min', ['build-modular-cocos2d'], 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', ['build-modular-cocos2d'], function () { + Engine.buildPreview('./index.js', './bin/cocos2d-js-for-preview.js'); +}); + +var jsbSkipModules = [ + '../../cocos2d/core/CCGame', + '../../cocos2d/core/textures/CCTexture2D', + '../../cocos2d/core/sprites/CCSpriteFrame', + '../../cocos2d/core/event/event', + '../../cocos2d/core/load-pipeline/audio-downloader', + '../../cocos2d/audio/CCAudio', + '../../extensions/spine/SGSkeleton', + '../../extensions/spine/SGSkeletonAnimation', + '../../extensions/spine/SGSkeletonCanvasRenderCmd', + '../../extensions/spine/SGSkeletonWebGLRenderCmd', + '../../extensions/spine/lib/spine' +]; + +gulp.task('build-jsb-extends-dev', function (done) { + Engine.buildJsbDev([ + './jsb/index.js', + './extends.js' + ], './bin/jsb_polyfill.dev.js', jsbSkipModules, done); +}); + +gulp.task('build-jsb-extends-min', function () { + 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']); + + +///////// +// test // +///////// + +gulp.task('clean-test', function (done) { + Test.clean([ + './bin/cocos2d-js-extends-for-test.js', + 'bin/cocos2d-js-for-test.js' + ], done); +}); + +gulp.task('build-test', ['build-modular-cocos2d', 'clean-test'], function (done) { + Test.build('./index.js', './bin/cocos2d-js-for-test.js', done); +}); + +gulp.task('unit-runner', [], function (done) { + Test.unit('./bin', [ + './bin/cocos2d-js-for-test.js' + ], done); +}); + +gulp.task('test', ['build-test', 'unit-runner'], function () { + Test.test(); +}); + +gulp.task('visual-test', ['build-test'], Shell.task([ + 'sh ./test/visual-tests/run.sh' +])); + +gulp.task('test-no-build', function () { + Test.test(); +}); + +//////////// +// global // +//////////// + +gulp.task('build-dev', ['build-preview', 'build-jsb-extends-dev'], function (done) { + // make dist version dirty + Del(['./bin/cocos2d-js.js', './bin/cocos2d-js-min.js', './bin/jsb_polyfill.js'], done); +}); + +gulp.task('build', ['build-html5', 'build-preview', 'build-jsb-extends-min', 'build-jsb-extends-dev']); // default task gulp.task('default', ['build']); + +gulp.task('clean', function (done) { + Del(Path.join('./bin', '**/*'), done); +}); + +//////////// +// watch // +//////////// + +gulp.task('watch-preview', function () { + Watch.preview('./index.js', './bin/cocos2d-js-for-preview.js'); +}); + +gulp.task('watch-jsb-polyfill', function () { + Watch.jsbPolyfill([ + './jsb/index.js', + './extends.js' + ], './bin/jsb_polyfill.dev.js', jsbSkipModules); +}); + +gulp.task('watch-dev-files', ['watch-preview', 'watch-jsb-polyfill']); diff --git a/index.js b/index.js index a86cc314e1..e8a2723955 100644 --- a/index.js +++ b/index.js @@ -48,7 +48,11 @@ require('./predefine'); var isMainProcess = CC_EDITOR && Editor.isMainProcess; if (!isMainProcess) { // LOAD ORIGIN COCOS2D COMPILED BY CLOSURE - require('./bin/modular-cocos2d'); + try { + require('./bin/modular-cocos2d'); + } catch (error) { + require('./bin/modular-cocos2d-cut'); + } } else { // load modules for editor's core-level which included in modular-cocos2d.js diff --git a/module.json b/module.json new file mode 100644 index 0000000000..3e729eae5c --- /dev/null +++ b/module.json @@ -0,0 +1,147 @@ +[ + { + "name": "Core", + "locked": true, + "includes": [] + }, + { + "name": "Canvas", + "locked": true, + "includes": [ + "./cocos2d/core/components/CCCanvas.js" + ] + }, + { + "name": "Sprite", + "locked": true, + "includes": [ + "./cocos2d/core/components/CCSprite.js" + ] + }, + { + "name": "Label", + "includes": [ + "./cocos2d/core/components/CCLabel.js" + ] + }, + { + "name": "Mask", + "includes": [ + "./cocos2d/core/components/CCMask.js" + ] + }, + { + "name": "CCSpriteDistortion", + "includes": [ + "./cocos2d/core/components/CCSpriteDistortion.js" + ] + }, + { + "name": "LabelOutline", + "includes": [ + "./cocos2d/core/components/CCLabelOutline.js" + ] + }, + { + "name": "ParticleSystem", + "includes": [ + "./cocos2d/particle.CCParticleSystem.js" + ] + }, + { + "name": "TiledMap", + "includes": [ + "./cocos2d/tilemap/CCTiledMap.js" + ] + }, + { + "name": "Spine Skeleton", + "includes": [ + "./extensions/spine/index.js" + ] + }, + { + "name": "Widget", + "includes": [ + "./cocos2d/core/components/CCWidget.js" + ] + }, + { + "name": "Button", + "includes": [ + "./cocos2d/core/components/CCButton.js" + ] + }, + { + "name": "ProgressBar", + "includes": [ + "./cocos2d/core/components/CCProgressBar.js" + ] + }, + { + "name": "ScrollBar", + "includes": [ + "./cocos2d/core/components/CCScrollBar.js" + ] + }, + { + "name": "ScrollView", + "includes": [ + "./cocos2d/core/components/CCScrollView.js" + ] + }, + { + "name": "Layout", + "includes": [ + "./cocos2d/core/components/CCLayout.js" + ] + }, + { + "name": "EditorBox", + "includes": [ + "./cocos2d/core/components/CCEditBox.js" + ] + }, + { + "name": "VideoPlayer", + "includes": [ + "./cocos2d/core/components/CCVideoPlayer.js" + ] + }, + { + "name": "AudioSource", + "includes": [ + "./cocos2d/core/components/CCAudioSource.js" + ] + }, + { + "name": "Animation", + "includes": [ + "./cocos2d/core/components/CCAnimation.js" + ] + }, + { + "name": "MotionStreak", + "includes": [ + "./cocos2d/motion-streak/CCMotionStreak.js" + ] + }, + { + "name": "BoxCollider", + "includes": [ + "./cocos2d/core/collider/CCBoxCollider.js" + ] + }, + { + "name": "CircleCollider", + "includes": [ + "./cocos2d/core/collider/CCCircleCollider.js" + ] + }, + { + "name": "PolygonCollider", + "includes": [ + "./cocos2d/core/collider/CCPolygonCollider.js" + ] + } +] \ No newline at end of file From cd3f9aa4ca02e2b8cf68a22113b32eedc7768853 Mon Sep 17 00:00:00 2001 From: ViusualSJ Date: Thu, 23 Jun 2016 15:16:39 +0800 Subject: [PATCH 2/8] review code --- gulpfile.js | 4 ++-- index.js | 6 ++++- module.json => modules.json | 48 ++++++++++++++++++------------------- 3 files changed, 31 insertions(+), 27 deletions(-) rename module.json => modules.json (83%) diff --git a/gulpfile.js b/gulpfile.js index fd54cbc19b..c423946f31 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -91,7 +91,7 @@ gulp.task('build-jsb-extends-dev', function (done) { ], './bin/jsb_polyfill.dev.js', jsbSkipModules, done); }); -gulp.task('build-jsb-extends-min', function () { +gulp.task('build-jsb-extends-min', function (done) { Engine.buildJsbMin([ './jsb/index.js', './extends.js' @@ -110,7 +110,7 @@ gulp.task('build-min', ['build-html5', 'build-jsb']); gulp.task('clean-test', function (done) { Test.clean([ './bin/cocos2d-js-extends-for-test.js', - 'bin/cocos2d-js-for-test.js' + './bin/cocos2d-js-for-test.js' ], done); }); diff --git a/index.js b/index.js index e8a2723955..0054173420 100644 --- a/index.js +++ b/index.js @@ -51,7 +51,11 @@ if (!isMainProcess) { try { require('./bin/modular-cocos2d'); } catch (error) { - require('./bin/modular-cocos2d-cut'); + if (error.code === 'MODULE_NOT_FOUND') { + require('./bin/modular-cocos2d-cut'); + } else { + throw error; + } } } else { diff --git a/module.json b/modules.json similarity index 83% rename from module.json rename to modules.json index 3e729eae5c..20110ca0d8 100644 --- a/module.json +++ b/modules.json @@ -2,145 +2,145 @@ { "name": "Core", "locked": true, - "includes": [] + "entries": [] }, { "name": "Canvas", "locked": true, - "includes": [ + "entries": [ "./cocos2d/core/components/CCCanvas.js" ] }, { "name": "Sprite", "locked": true, - "includes": [ + "entries": [ "./cocos2d/core/components/CCSprite.js" ] }, { "name": "Label", - "includes": [ + "entries": [ "./cocos2d/core/components/CCLabel.js" ] }, { "name": "Mask", - "includes": [ + "entries": [ "./cocos2d/core/components/CCMask.js" ] }, { "name": "CCSpriteDistortion", - "includes": [ + "entries": [ "./cocos2d/core/components/CCSpriteDistortion.js" ] }, { "name": "LabelOutline", - "includes": [ + "entries": [ "./cocos2d/core/components/CCLabelOutline.js" ] }, { "name": "ParticleSystem", - "includes": [ + "entries": [ "./cocos2d/particle.CCParticleSystem.js" ] }, { "name": "TiledMap", - "includes": [ + "entries": [ "./cocos2d/tilemap/CCTiledMap.js" ] }, { "name": "Spine Skeleton", - "includes": [ + "entries": [ "./extensions/spine/index.js" ] }, { "name": "Widget", - "includes": [ + "entries": [ "./cocos2d/core/components/CCWidget.js" ] }, { "name": "Button", - "includes": [ + "entries": [ "./cocos2d/core/components/CCButton.js" ] }, { "name": "ProgressBar", - "includes": [ + "entries": [ "./cocos2d/core/components/CCProgressBar.js" ] }, { "name": "ScrollBar", - "includes": [ + "entries": [ "./cocos2d/core/components/CCScrollBar.js" ] }, { "name": "ScrollView", - "includes": [ + "entries": [ "./cocos2d/core/components/CCScrollView.js" ] }, { "name": "Layout", - "includes": [ + "entries": [ "./cocos2d/core/components/CCLayout.js" ] }, { "name": "EditorBox", - "includes": [ + "entries": [ "./cocos2d/core/components/CCEditBox.js" ] }, { "name": "VideoPlayer", - "includes": [ + "entries": [ "./cocos2d/core/components/CCVideoPlayer.js" ] }, { "name": "AudioSource", - "includes": [ + "entries": [ "./cocos2d/core/components/CCAudioSource.js" ] }, { "name": "Animation", - "includes": [ + "entries": [ "./cocos2d/core/components/CCAnimation.js" ] }, { "name": "MotionStreak", - "includes": [ + "entries": [ "./cocos2d/motion-streak/CCMotionStreak.js" ] }, { "name": "BoxCollider", - "includes": [ + "entries": [ "./cocos2d/core/collider/CCBoxCollider.js" ] }, { "name": "CircleCollider", - "includes": [ + "entries": [ "./cocos2d/core/collider/CCCircleCollider.js" ] }, { "name": "PolygonCollider", - "includes": [ + "entries": [ "./cocos2d/core/collider/CCPolygonCollider.js" ] } From d0192a8b19fa5973a83ebc5a27d652f11046eeb7 Mon Sep 17 00:00:00 2001 From: ViusualSJ Date: Thu, 23 Jun 2016 15:39:27 +0800 Subject: [PATCH 3/8] review --- gulp/tasks/engine.js | 9 +++++---- gulp/tasks/test.js | 7 ++++--- gulpfile.js | 14 +++++++------- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/gulp/tasks/engine.js b/gulp/tasks/engine.js index c701c8737e..9963b5154d 100644 --- a/gulp/tasks/engine.js +++ b/gulp/tasks/engine.js @@ -26,7 +26,7 @@ 'use strict'; const Utils = require('./utils'); -const Path = require('path'); +const Path = require('fire-path'); const Source = require('vinyl-source-stream'); const Gulp = require('gulp'); @@ -87,7 +87,7 @@ exports.buildCocosJsMin = function (sourceFile, outputFile, callback) { .on('end', callback); }; -exports.buildPreview = function (sourceFile, outputFile) { +exports.buildPreview = function (sourceFile, outputFile, callback) { var outFile = Path.basename(outputFile); var outDir = Path.dirname(outputFile); @@ -111,10 +111,11 @@ exports.buildPreview = function (sourceFile, outputFile) { includeContent: false, addComment: true })) - .pipe(Gulp.dest(outDir)); + .pipe(Gulp.dest(outDir)) + .on('end', callback); }; -exports.buildJsbDev = function (sourceFile, outputFile, jsbSkipModules, callback) { +exports.buildJsb = function (sourceFile, outputFile, jsbSkipModules, callback) { var outFile = Path.basename(outputFile); var outDir = Path.dirname(outputFile); diff --git a/gulp/tasks/test.js b/gulp/tasks/test.js index f68603a8d2..eb4ac53280 100644 --- a/gulp/tasks/test.js +++ b/gulp/tasks/test.js @@ -59,7 +59,7 @@ exports.build = function (sourceFile, outputFile, callback) { EventStream.merge(engine, editorExtends) .on('end', callback); } else { - callback(); + engine.on('end', callback); } }; @@ -80,7 +80,7 @@ exports.unit = function (outDir, libs, callback) { .on('end', callback); }; -exports.test = function () { +exports.test = function (callback) { var qunit; try { qunit = require('gulp-qunit'); @@ -89,5 +89,6 @@ exports.test = function () { throw e; } return Gulp.src('bin/qunit-runner.html', { read: false }) - .pipe(qunit({ timeout: 5 })); + .pipe(qunit({ timeout: 5 })) + .on('end', callback); }; \ No newline at end of file diff --git a/gulpfile.js b/gulpfile.js index c423946f31..7a8f5c5df5 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -66,8 +66,8 @@ gulp.task('build-cocos2d-min', ['build-modular-cocos2d'], function (done) { gulp.task('build-html5', ['build-cocos2d-dev', 'build-cocos2d-min']); -gulp.task('build-preview', ['build-modular-cocos2d'], function () { - Engine.buildPreview('./index.js', './bin/cocos2d-js-for-preview.js'); +gulp.task('build-preview', ['build-modular-cocos2d'], function (done) { + Engine.buildPreview('./index.js', './bin/cocos2d-js-for-preview.js', done); }); var jsbSkipModules = [ @@ -85,7 +85,7 @@ var jsbSkipModules = [ ]; gulp.task('build-jsb-extends-dev', function (done) { - Engine.buildJsbDev([ + Engine.buildJsb([ './jsb/index.js', './extends.js' ], './bin/jsb_polyfill.dev.js', jsbSkipModules, done); @@ -124,16 +124,16 @@ gulp.task('unit-runner', [], function (done) { ], done); }); -gulp.task('test', ['build-test', 'unit-runner'], function () { - Test.test(); +gulp.task('test', ['build-test', 'unit-runner'], function (done) { + Test.test(done); }); gulp.task('visual-test', ['build-test'], Shell.task([ 'sh ./test/visual-tests/run.sh' ])); -gulp.task('test-no-build', function () { - Test.test(); +gulp.task('test-no-build', function (done) { + Test.test(done); }); //////////// From d2d2dcbc80dd4718fb7e05519ee4ed5a3f864322 Mon Sep 17 00:00:00 2001 From: ViusualSJ Date: Thu, 23 Jun 2016 15:55:00 +0800 Subject: [PATCH 4/8] Merge Collider --- modules.json | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/modules.json b/modules.json index 20110ca0d8..40b365ded9 100644 --- a/modules.json +++ b/modules.json @@ -127,20 +127,10 @@ ] }, { - "name": "BoxCollider", - "entries": [ - "./cocos2d/core/collider/CCBoxCollider.js" - ] - }, - { - "name": "CircleCollider", - "entries": [ - "./cocos2d/core/collider/CCCircleCollider.js" - ] - }, - { - "name": "PolygonCollider", + "name": "Collider", "entries": [ + "./cocos2d/core/collider/CCBoxCollider.js", + "./cocos2d/core/collider/CCCircleCollider.js", "./cocos2d/core/collider/CCPolygonCollider.js" ] } From e88b424b69380f49ac5c51833dc15f00db09e172 Mon Sep 17 00:00:00 2001 From: ViusualSJ Date: Thu, 23 Jun 2016 16:07:09 +0800 Subject: [PATCH 5/8] add modules entries --- modules.json | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/modules.json b/modules.json index 40b365ded9..be9205984d 100644 --- a/modules.json +++ b/modules.json @@ -117,7 +117,8 @@ { "name": "Animation", "entries": [ - "./cocos2d/core/components/CCAnimation.js" + "./cocos2d/core/components/CCAnimation.js", + "./cocos2d/animation/index.js" ] }, { @@ -129,9 +130,7 @@ { "name": "Collider", "entries": [ - "./cocos2d/core/collider/CCBoxCollider.js", - "./cocos2d/core/collider/CCCircleCollider.js", - "./cocos2d/core/collider/CCPolygonCollider.js" + "./cocos2d/core/collider/index.js" ] } ] \ No newline at end of file From 69ec6aa46076c52c44c06b1d7e1ee8c02de0b8d3 Mon Sep 17 00:00:00 2001 From: VisualSJ Date: Mon, 27 Jun 2016 15:00:21 +0800 Subject: [PATCH 6/8] add Modular print size info --- gulp/tasks/engine.js | 43 +++++++++++++++++++++++++++++++++++++++---- gulpfile.js | 4 ++-- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/gulp/tasks/engine.js b/gulp/tasks/engine.js index 9963b5154d..f376a51afd 100644 --- a/gulp/tasks/engine.js +++ b/gulp/tasks/engine.js @@ -33,13 +33,20 @@ const Gulp = require('gulp'); const Buffer = require('vinyl-buffer'); const Uglify = require('gulp-uglify'); const Sourcemaps = require('gulp-sourcemaps'); +const Size = require('gulp-size'); +const EventStream = require('event-stream'); +const Chalk = require('chalk'); const HandleErrors = require('../util/handleErrors'); -exports.buildCocosJs = function (sourceFile, outputFile, callback) { +exports.buildCocosJs = function (sourceFile, outputFile, excludes, callback) { var outDir = Path.dirname(outputFile); var outFile = Path.basename(outputFile); var bundler = Utils.createBundler(sourceFile); - bundler.exclude('./bin/modular-cocos2d-cut.js'); + + excludes && excludes.forEach(function (file) { + bundler.exclude(file); + }); + var uglifyOption = Utils.uglifyOptions(false, { CC_EDITOR: false, CC_DEV: true, @@ -47,11 +54,23 @@ exports.buildCocosJs = function (sourceFile, outputFile, callback) { CC_JSB: false }); + var rawSize = Size({ gzip: false, pretty: false, showTotal: false, showFiles: false }); + var zippedSize = Size({ gzip: true, pretty: false, showTotal: false, showFiles: false }); + bundler.bundle() .pipe(Source(outFile)) .pipe(Buffer()) .pipe(Sourcemaps.init({loadMaps: true})) .pipe(Uglify(uglifyOption)) + .pipe(rawSize) + .pipe(zippedSize) + .pipe(EventStream.through(null, function () { + var raw = rawSize.size; + var zipped = zippedSize.size; + var percent = ((zipped / raw) * 100).toFiexed(2); + console.log(`Size of ${outputFile}: raw: ${Chalk.cyan(raw + 'B')} zipped: ${Chalk.cyan(zipped + 'B')}, compression ratio: ${percent}%`); + this.emit('end'); + })) .pipe(Sourcemaps.write('./', { sourceRoot: './', includeContent: true, @@ -61,11 +80,15 @@ exports.buildCocosJs = function (sourceFile, outputFile, callback) { .on('end', callback); }; -exports.buildCocosJsMin = function (sourceFile, outputFile, callback) { +exports.buildCocosJsMin = function (sourceFile, outputFile, excludes, callback) { var outDir = Path.dirname(outputFile); var outFile = Path.basename(outputFile); var bundler = Utils.createBundler(sourceFile); - bundler.exclude('./bin/modular-cocos2d-cut.js'); + + excludes && excludes.forEach(function (file) { + bundler.exclude(file); + }); + var uglifyOption = Utils.uglifyOptions(true, { CC_EDITOR: false, CC_DEV: false, @@ -73,11 +96,23 @@ exports.buildCocosJsMin = function (sourceFile, outputFile, callback) { CC_JSB: false }); + var rawSize = Size({ gzip: false, pretty: false, showTotal: false, showFiles: false }); + var zippedSize = Size({ gzip: true, pretty: false, showTotal: false, showFiles: false }); + bundler.bundle() .pipe(Source(outFile)) .pipe(Buffer()) .pipe(Sourcemaps.init({loadMaps: true})) .pipe(Uglify(uglifyOption)) + .pipe(rawSize) + .pipe(zippedSize) + .pipe(EventStream.through(null, function () { + var raw = rawSize.size; + var zipped = zippedSize.size; + var percent = ((zipped / raw) * 100).toFiexed(2); + console.log(`Size of ${outputFile}: raw: ${Chalk.cyan(raw + 'B')} zipped: ${Chalk.cyan(zipped + 'B')}, compression ratio: ${percent}%`); + this.emit('end'); + })) .pipe(Sourcemaps.write('./', { sourceRoot: './', includeContent: true, diff --git a/gulpfile.js b/gulpfile.js index 7a8f5c5df5..ea28701316 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -57,11 +57,11 @@ gulp.task('build-modular-cocos2d', ['build-all-modular', 'build-cut-modular']); ///////////// gulp.task('build-cocos2d-dev', ['build-modular-cocos2d'], function (done) { - Engine.buildCocosJs('./index.js', './bin/cocos2d-js.js', done); + Engine.buildCocosJs('./index.js', './bin/cocos2d-js.js', ['./bin/modular-cocos2d-cut.js'], done); }); gulp.task('build-cocos2d-min', ['build-modular-cocos2d'], function (done) { - Engine.buildCocosJsMin('./index.js', './bin/cocos2d-js-min.js', done); + Engine.buildCocosJsMin('./index.js', './bin/cocos2d-js-min.js', ['./bin/modular-cocos2d-cut.js'], done); }); gulp.task('build-html5', ['build-cocos2d-dev', 'build-cocos2d-min']); From b7f4a276f12dcebdf3cec565037db3b2b831483e Mon Sep 17 00:00:00 2001 From: VisualSJ Date: Mon, 27 Jun 2016 15:01:24 +0800 Subject: [PATCH 7/8] add Modular print size info --- gulp/tasks/engine.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gulp/tasks/engine.js b/gulp/tasks/engine.js index f376a51afd..6873985bc8 100644 --- a/gulp/tasks/engine.js +++ b/gulp/tasks/engine.js @@ -67,7 +67,7 @@ exports.buildCocosJs = function (sourceFile, outputFile, excludes, callback) { .pipe(EventStream.through(null, function () { var raw = rawSize.size; var zipped = zippedSize.size; - var percent = ((zipped / raw) * 100).toFiexed(2); + var percent = ((zipped / raw) * 100).toFixed(2); console.log(`Size of ${outputFile}: raw: ${Chalk.cyan(raw + 'B')} zipped: ${Chalk.cyan(zipped + 'B')}, compression ratio: ${percent}%`); this.emit('end'); })) @@ -109,7 +109,7 @@ exports.buildCocosJsMin = function (sourceFile, outputFile, excludes, callback) .pipe(EventStream.through(null, function () { var raw = rawSize.size; var zipped = zippedSize.size; - var percent = ((zipped / raw) * 100).toFiexed(2); + var percent = ((zipped / raw) * 100).toFixed(2); console.log(`Size of ${outputFile}: raw: ${Chalk.cyan(raw + 'B')} zipped: ${Chalk.cyan(zipped + 'B')}, compression ratio: ${percent}%`); this.emit('end'); })) From 18d87556763057ccda21d1cda6a9717ec11e0a95 Mon Sep 17 00:00:00 2001 From: VisualSJ Date: Mon, 27 Jun 2016 15:13:20 +0800 Subject: [PATCH 8/8] add Modular print size info --- gulp/tasks/engine.js | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/gulp/tasks/engine.js b/gulp/tasks/engine.js index 6873985bc8..9c183eccbb 100644 --- a/gulp/tasks/engine.js +++ b/gulp/tasks/engine.js @@ -54,23 +54,11 @@ exports.buildCocosJs = function (sourceFile, outputFile, excludes, callback) { CC_JSB: false }); - var rawSize = Size({ gzip: false, pretty: false, showTotal: false, showFiles: false }); - var zippedSize = Size({ gzip: true, pretty: false, showTotal: false, showFiles: false }); - bundler.bundle() .pipe(Source(outFile)) .pipe(Buffer()) .pipe(Sourcemaps.init({loadMaps: true})) .pipe(Uglify(uglifyOption)) - .pipe(rawSize) - .pipe(zippedSize) - .pipe(EventStream.through(null, function () { - var raw = rawSize.size; - var zipped = zippedSize.size; - var percent = ((zipped / raw) * 100).toFixed(2); - console.log(`Size of ${outputFile}: raw: ${Chalk.cyan(raw + 'B')} zipped: ${Chalk.cyan(zipped + 'B')}, compression ratio: ${percent}%`); - this.emit('end'); - })) .pipe(Sourcemaps.write('./', { sourceRoot: './', includeContent: true, @@ -110,7 +98,7 @@ exports.buildCocosJsMin = function (sourceFile, outputFile, excludes, callback) var raw = rawSize.size; var zipped = zippedSize.size; var percent = ((zipped / raw) * 100).toFixed(2); - console.log(`Size of ${outputFile}: raw: ${Chalk.cyan(raw + 'B')} zipped: ${Chalk.cyan(zipped + 'B')}, compression ratio: ${percent}%`); + console.log(`Size of ${outputFile}: minimized: ${Chalk.cyan(raw + 'B')} zipped: ${Chalk.cyan(zipped + 'B')}, compression ratio: ${percent}%`); this.emit('end'); })) .pipe(Sourcemaps.write('./', {