Browse Source

Merge pull request #3791 from RaananW/modules-revamp

AMD "define" was fixed
Raanan Weber 7 years ago
parent
commit
0bc3f5b7d2
3 changed files with 46 additions and 17 deletions
  1. 32 8
      Tools/Gulp/config.json
  2. 12 8
      Tools/Gulp/gulp-addModuleExports.js
  3. 2 1
      dist/preview release/what's new.md

+ 32 - 8
Tools/Gulp/config.json

@@ -1315,7 +1315,10 @@
             "buildAsModule": true,
             "moduleName": "babylonjs-materials",
             "outputFilename": "babylonjs.materials",
-            "moduleDeclaration": "BJSMaterials",
+            "moduleDeclaration": {
+                "name": "BJSMaterials",
+                "module": "babylonjs-materials"
+            },
             "extendsRoot": true
         }
     },
@@ -1346,7 +1349,10 @@
             "buildAsModule": true,
             "moduleName": "babylonjs-post-process",
             "outputFilename": "babylonjs.postProcess",
-            "moduleDeclaration": "BJSPostProcess",
+            "moduleDeclaration": {
+                "name": "PostProcesses",
+                "module": "babylonjs-post-process"
+            },
             "extendsRoot": true
         }
     },
@@ -1449,7 +1455,10 @@
             "buildAsModule": true,
             "moduleName": "babylonjs-procedural-textures",
             "outputFilename": "babylonjs.proceduralTextures",
-            "moduleDeclaration": "BJSProceduralTextures",
+            "moduleDeclaration": {
+                "name": "ProceduralTextures",
+                "module": "babylonjs-procedural-textures"
+            },
             "extendsRoot": true
         }
     },
@@ -1522,7 +1531,10 @@
             "buildAsModule": true,
             "moduleName": "babylonjs-loaders",
             "outputFilename": "babylonjs.loaders",
-            "moduleDeclaration": "BJSLoaders",
+            "moduleDeclaration": {
+                "name": "Loaders",
+                "module": "babylonjs-loaders"
+            },
             "extraTypesDependencies": [
                 "babylonjs",
                 "babylonjs-gltf2interface"
@@ -1558,7 +1570,10 @@
                 "babylonjs-gltf2interface"
             ],
             "outputFilename": "babylonjs.serializers",
-            "moduleDeclaration": "BJSSerializers",
+            "moduleDeclaration": {
+                "name": "Serializers",
+                "module": "babylonjs-serializers"
+            },
             "extendsRoot": true
         }
     },
@@ -1589,7 +1604,10 @@
                 "output": "babylon.gui.js",
                 "buildAsModule": true,
                 "moduleName": "babylonjs-gui",
-                "moduleDeclaration": "GUI"
+                "moduleDeclaration": {
+                    "name": "GUI",
+                    "module": "babylonjs-gui"
+                }
             }
         ],
         "build": {
@@ -1665,7 +1683,10 @@
                 "output": "babylon.inspector.js",
                 "webpack": "../../inspector/webpack.config.js",
                 "bundle": "true",
-                "moduleDeclaration": "INSPECTOR"
+                "moduleDeclaration": {
+                    "name": "INSPECTOR",
+                    "module": "babylonjs-inspector"
+                }
             }
         ],
         "build": {
@@ -1681,7 +1702,10 @@
                 "output": "babylon.viewer.js",
                 "webpack": "../../Viewer/webpack.gulp.config.js",
                 "bundle": "true",
-                "moduleDeclaration": "BabylonViewer",
+                "moduleDeclaration": {
+                    "name": "BabylonViewer",
+                    "module": "babylonjs-viewer"
+                },
                 "babylonIncluded": true
             }
         ],

+ 12 - 8
Tools/Gulp/gulp-addModuleExports.js

@@ -8,13 +8,20 @@ var through = require('through2');
 module.exports = function (varName, subModule, extendsRoot, externalUsingBabylon, noBabylonInit) {
     return through.obj(function (file, enc, cb) {
 
+        if (typeof varName === 'string') {
+            varName = {
+                base: varName,
+                module: varName
+            }
+        }
+
         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;
 `;
         function moduleExportAddition(varName) {
 
-            let base = subModule ? 'BABYLON' : varName;
+            let base = subModule ? 'BABYLON' : varName.base;
 
             let basicInit = `root["${base}"]${(subModule && !extendsRoot) ? '["' + varName + '"]' : ''} = f;`;
             let sadGlobalPolution = (!subModule) ? `var globalObject = (typeof global !== 'undefined') ? global : ((typeof window !== 'undefined') ? window : this);
@@ -25,21 +32,19 @@ globalObject["${base}"] = f;` : '';
 
             return `\n\n(function universalModuleDefinition(root, factory) {
                 var f = factory();
-                if (root && root["${base}"]) {
-                    return;
-                }
+                
                 ${sadGlobalPolution}
     if(typeof exports === 'object' && typeof module === 'object')
         module.exports = f;
     else if(typeof define === 'function' && define.amd)
-        define(["${varName}"], factory);
+        define("${varName.module}", ${subModule || extendsRoot ? '["BABYLON"],' : ''} factory);
     else if(typeof exports === 'object')
-        exports["${varName}"] = f;
+        exports["${varName.module}"] = f;
     else {
         ${basicInit}
     }
 })(this, function() {
-    return ${base}${(subModule && !extendsRoot) ? '.' + varName : ''};
+    return ${base}${(subModule && !extendsRoot) ? '.' + varName.base : ''};
 });
 `;
         }
@@ -81,7 +86,6 @@ globalObject["${base}"] = f;` : '';
 
         try {
             if (externalUsingBabylon) {
-                //file.contents = new Buffer(optionalRequire.concat(String(file.contents)));
                 file.contents = new Buffer(optionalRequire.concat(new Buffer(String(file.contents).concat(moduleExportAddition(varName)))));
             } else {
                 let pretext = subModule ? optionalRequire : '';

+ 2 - 1
dist/preview release/what's new.md

@@ -78,7 +78,7 @@
 - Added `Tools.WorkerPool` class for web worker management. ([bghgary](https://github.com/bghgary))
 - Support depth maps for multiple active cameras for post processes like depth of field ([trevordev](https://github.com/trevordev))
 - Integrates depth texture support in the engine ([sebavan](https://github.com/sebavan))
-- NPM package now has a dependency system, updated during build ([RaananW](https://github.com/RaananW))
+- NPM package now has a dependency system, updated during build. ([RaananW](https://github.com/RaananW))
 
 ## Bug fixes
 
@@ -90,6 +90,7 @@
 - Enabling teleportation in the vr helper class caused a redundant post process to be added ([trevordev](https://github.com/trevordev))
 - (Viewer) Fixed a bug where loading another mesh positioned it incorrectly ([RaananW](https://github.com/RaananW))
 - (Viewer) Disabling templates now work correctly ([RaananW](https://github.com/RaananW))
+- AMD "define" declaration is no longer anonymous ([RaananW](https://github.com/RaananW))
 
 ## Breaking changes