Browse Source

Preparing for including the inspector in npm
Another major change is that the BABYLON object is now assigned to the global object so that the inspector can find it without a problem.
This way we can also develop including any version of BABYLON we wish (instead of the npm package).
See the nullEngine test as an example.

Raanan Weber 7 years ago
parent
commit
d94541fb9c

+ 6 - 5
Tools/Gulp/config.json

@@ -242,7 +242,7 @@
             "dependUpon": [
             "dependUpon": [
                 "core"
                 "core"
             ]
             ]
-        },    
+        },
         "instrumentation": {
         "instrumentation": {
             "files": [
             "files": [
                 "../../src/Instrumentation/babylon.engineInstrumentation.js",
                 "../../src/Instrumentation/babylon.engineInstrumentation.js",
@@ -252,7 +252,7 @@
             "dependUpon": [
             "dependUpon": [
                 "core"
                 "core"
             ]
             ]
-        },      
+        },
         "cameraBehaviors": {
         "cameraBehaviors": {
             "files": [
             "files": [
                 "../../src/Behaviors/Cameras/babylon.framingBehavior.js",
                 "../../src/Behaviors/Cameras/babylon.framingBehavior.js",
@@ -405,7 +405,7 @@
                 "defaultUboDeclaration",
                 "defaultUboDeclaration",
                 "shadowsFragmentFunctions",
                 "shadowsFragmentFunctions",
                 "fresnelFunction",
                 "fresnelFunction",
-                "reflectionFunction",                
+                "reflectionFunction",
                 "imageProcessingDeclaration",
                 "imageProcessingDeclaration",
                 "imageProcessingFunctions",
                 "imageProcessingFunctions",
                 "bumpFragmentFunctions",
                 "bumpFragmentFunctions",
@@ -461,7 +461,7 @@
                 "shadowsFragmentFunctions",
                 "shadowsFragmentFunctions",
                 "pbrFunctions",
                 "pbrFunctions",
                 "imageProcessingDeclaration",
                 "imageProcessingDeclaration",
-                "imageProcessingFunctions",                
+                "imageProcessingFunctions",
                 "harmonicsFunctions",
                 "harmonicsFunctions",
                 "pbrLightFunctions",
                 "pbrLightFunctions",
                 "helperFunctions",
                 "helperFunctions",
@@ -1594,7 +1594,8 @@
                 ],
                 ],
                 "output": "babylon.inspector.js",
                 "output": "babylon.inspector.js",
                 "webpack": "../../inspector/webpack.config.js",
                 "webpack": "../../inspector/webpack.config.js",
-                "bundle": "true"
+                "bundle": "true",
+                "moduleDeclaration": "GUI"
             }
             }
         ],
         ],
         "build": {
         "build": {

+ 24 - 10
Tools/Gulp/gulp-addModuleExports.js

@@ -1,34 +1,45 @@
 var gutil = require('gulp-util');
 var gutil = require('gulp-util');
 var through = require('through2');
 var through = require('through2');
 
 
-module.exports = function (varName, subModule, extendsRoot) {
+/**
+ * The parameters for this function has grown during development.
+ * Eventually, this function will need to be reorganized. 
+ */
+module.exports = function (varName, subModule, extendsRoot, requireOnly) {
     return through.obj(function (file, enc, cb) {
     return through.obj(function (file, enc, cb) {
 
 
-        var optionalRequire = `var babylonDependency; try { babylonDependency = BABYLON || (typeof require !== 'undefined' && require("../babylon.max")); } catch (e) { babylonDependency = BABYLON || (typeof require !== 'undefined' && require("babylonjs")); } 
+        var optionalRequire = `var globalObject = (typeof global !== 'undefined') ? global : ((typeof window !== 'undefined') ? window : this);
+var babylonDependency = (globalObject && globalObject.BABYLON) || BABYLON || (typeof require !== 'undefined' && require("babylonjs"));
 var BABYLON = babylonDependency;
 var BABYLON = babylonDependency;
 `;
 `;
         function moduleExportAddition(varName) {
         function moduleExportAddition(varName) {
 
 
-            let basicInit = `root["BABYLON"]${(subModule && !extendsRoot) ? '["' + varName + '"]' : ''} = factory();`;
+            let base = subModule ? 'BABYLON' : varName;
+
+            let basicInit = `root["${base}"]${(subModule && !extendsRoot) ? '["' + varName + '"]' : ''} = f;`;
+            let sadGlobalPolution = (!subModule) ? `var globalObject = (typeof global !== 'undefined') ? global : ((typeof window !== 'undefined') ? window : this);
+globalObject["${base}"] = f;` : '';
             /*if (extendsRoot) {
             /*if (extendsRoot) {
                 basicInit = `__extends(root["BABYLON"], factory()); `
                 basicInit = `__extends(root["BABYLON"], factory()); `
             }*/
             }*/
 
 
             return `\n\n(function universalModuleDefinition(root, factory) {
             return `\n\n(function universalModuleDefinition(root, factory) {
-                if (root && root["BABYLON"]) {
+                if (root && root["${base}"]) {
                     return;
                     return;
                 }
                 }
+                var f = factory();
+                ${sadGlobalPolution}
     if(typeof exports === 'object' && typeof module === 'object')
     if(typeof exports === 'object' && typeof module === 'object')
-        module.exports = factory();
+        module.exports = f;
     else if(typeof define === 'function' && define.amd)
     else if(typeof define === 'function' && define.amd)
         define([], factory);
         define([], factory);
     else if(typeof exports === 'object')
     else if(typeof exports === 'object')
-        exports["${varName}"] = factory();
+        exports["${varName}"] = f;
     else {
     else {
         ${basicInit}
         ${basicInit}
     }
     }
 })(this, function() {
 })(this, function() {
-    return BABYLON${(subModule && !extendsRoot) ? '.' + varName : ''};
+    return ${base}${(subModule && !extendsRoot) ? '.' + varName : ''};
 });
 });
 `;
 `;
         }
         }
@@ -65,10 +76,13 @@ var BABYLON = babylonDependency;
         }
         }
 
 
         try {
         try {
-            let pretext = subModule ? optionalRequire : '';
-            file.contents = new Buffer(pretext.concat(decorateAddition).concat(new Buffer(extendsAddition.concat(String(file.contents)).concat(moduleExportAddition(varName)))));
+            if (requireOnly) {
+                file.contents = new Buffer(optionalRequire.concat(String(file.contents)));
+            } else {
+                let pretext = subModule ? optionalRequire : '';
+                file.contents = new Buffer(pretext.concat(decorateAddition).concat(new Buffer(extendsAddition.concat(String(file.contents)).concat(moduleExportAddition(varName)))));
+            }
             this.push(file);
             this.push(file);
-
         } catch (err) {
         } catch (err) {
             this.emit('error', new gutil.PluginError('gulp-add-module-exports', err, { fileName: file.path }));
             this.emit('error', new gutil.PluginError('gulp-add-module-exports', err, { fileName: file.path }));
         }
         }

+ 5 - 4
Tools/Gulp/gulpfile.js

@@ -402,6 +402,7 @@ var buildExternalLibrary = function (library, settings, watch) {
             return waitAll.on("end", function () {
             return waitAll.on("end", function () {
                 webpack(require(library.webpack))
                 webpack(require(library.webpack))
                     .pipe(rename(library.output.replace(".js", ".bundle.js")))
                     .pipe(rename(library.output.replace(".js", ".bundle.js")))
+                    .pipe(addModuleExports(library.moduleDeclaration, false, false, true))
                     .pipe(gulp.dest(outputDirectory))
                     .pipe(gulp.dest(outputDirectory))
             });
             });
         }
         }
@@ -474,21 +475,21 @@ gulp.task("typescript-all", function (cb) {
  */
  */
 gulp.task("watch", [], function () {
 gulp.task("watch", [], function () {
     var interval = 1000;
     var interval = 1000;
-    var tasks = [gulp.watch(config.typescript, { interval: interval}, ["typescript-compile"])];
+    var tasks = [gulp.watch(config.typescript, { interval: interval }, ["typescript-compile"])];
 
 
     config.modules.map(function (module) {
     config.modules.map(function (module) {
         config[module].libraries.map(function (library) {
         config[module].libraries.map(function (library) {
-            tasks.push(gulp.watch(library.files, { interval: interval}, function () {
+            tasks.push(gulp.watch(library.files, { interval: interval }, function () {
                 console.log(library.output);
                 console.log(library.output);
                 return buildExternalLibrary(library, config[module], true)
                 return buildExternalLibrary(library, config[module], true)
                     .pipe(debug());
                     .pipe(debug());
             }));
             }));
-            tasks.push(gulp.watch(library.shaderFiles, { interval: interval}, function () {
+            tasks.push(gulp.watch(library.shaderFiles, { interval: interval }, function () {
                 console.log(library.output);
                 console.log(library.output);
                 return buildExternalLibrary(library, config[module], true)
                 return buildExternalLibrary(library, config[module], true)
                     .pipe(debug())
                     .pipe(debug())
             }));
             }));
-            tasks.push(gulp.watch(library.sassFiles, { interval: interval}, function () {
+            tasks.push(gulp.watch(library.sassFiles, { interval: interval }, function () {
                 console.log(library.output);
                 console.log(library.output);
                 return buildExternalLibrary(library, config[module], true)
                 return buildExternalLibrary(library, config[module], true)
                     .pipe(debug())
                     .pipe(debug())

+ 1 - 1
inspector/webpack.config.js

@@ -4,7 +4,7 @@ module.exports = {
         "../../dist/preview release/inspector/babylon.inspector.min.js"
         "../../dist/preview release/inspector/babylon.inspector.min.js"
     ],
     ],
     output: {
     output: {
-        libraryTarget: "var",
+        libraryTarget: "umd",
         library: "INSPECTOR",
         library: "INSPECTOR",
         umdNamedDefine: true
         umdNamedDefine: true
     },
     },