Переглянути джерело

build the viewer's declaration without modules

Raanan Weber 7 роки тому
батько
коміт
2aa8d46587

+ 6 - 2
Tools/Gulp/config.json

@@ -1759,7 +1759,7 @@
                 "name": "babylonjs-viewer",
                 "main": "../../Viewer/src/index.d.ts",
                 "out": "../../dist/preview release/viewer/babylon.viewer.module.d.ts",
-                "prependText": "/// <reference path=\"./babylon.d.ts\"/>"
+                "prependText": "/// <reference path=\"./babylon.d.ts\"/>\n/// <reference path=\"./babylon.glTF2Interface.d.ts\"/>\n/// <reference path=\"./babylonjs.loaders.d.ts\"/>\n"
             },
             "outputs": [
                 {
@@ -1771,7 +1771,11 @@
                         {
                             "filename": "babylon.viewer.js",
                             "outputDirectory": "/viewer/",
-                            "addBabylonDeclaration": true
+                            "addBabylonDeclaration": [
+                                "babylon.d.ts",
+                                "loaders/babylonjs.loaders.d.ts",
+                                "gltf2Interface/babylon.glTF2Interface.d.ts"
+                            ]
                         }
                     ],
                     "minified": true

+ 17 - 3
Tools/Gulp/gulpfile.js

@@ -34,6 +34,9 @@ var dtsBundle = require('dts-bundle');
 const through = require('through2');
 var karmaServer = require('karma').Server;
 
+//viewer declaration
+var processViewerDeclaration = require('./processViewerDeclaration');
+
 var config = require("./config.json");
 
 var del = require("del");
@@ -467,6 +470,8 @@ var buildExternalLibrary = function (library, settings, watch) {
                             if (err) throw err;
                             data = settings.build.dtsBundle.prependText + '\n' + data.toString();
                             fs.writeFile(settings.build.dtsBundle.out, data);
+                            var newData = processViewerDeclaration(data);
+                            fs.writeFile(settings.build.dtsBundle.out.replace('.module', ''), newData);
                         });
                     });
                 }
@@ -502,7 +507,16 @@ var buildExternalLibrary = function (library, settings, watch) {
 
                     if (library.babylonIncluded && dest.addBabylonDeclaration) {
                         // include the babylon declaration
-                        sequence.unshift(gulp.src(config.build.outputDirectory + '/' + config.build.declarationFilename)
+                        if (dest.addBabylonDeclaration === true) {
+                            dest.addBabylonDeclaration = [config.build.declarationFilename];
+                        }
+                        var decsToAdd = dest.addBabylonDeclaration.map(function (dec) {
+                            return config.build.outputDirectory + '/' + dec;
+                        });
+                        sequence.unshift(gulp.src(decsToAdd)
+                            .pipe(rename(function (path) {
+                                path.dirname = '';
+                            }))
                             .pipe(gulp.dest(outputDirectory)))
                     }
                 }
@@ -884,8 +898,8 @@ gulp.task("modules", ["prepare-dependency-tree"], function () {
  */
 gulp.task("typedoc-generate", function () {
     return gulp
-        .src(["../../dist/preview release/babylon.d.ts", 
-            "../../dist/preview release/loaders/babylon.glTF2FileLoader.d.ts", 
+        .src(["../../dist/preview release/babylon.d.ts",
+            "../../dist/preview release/loaders/babylon.glTF2FileLoader.d.ts",
             "../../dist/preview release/serializers/babylon.glTF2Serializer.d.ts",
             "../../dist/preview release/gltf2Interface/babylon.glTF2Interface.d.ts"])
         .pipe(typedoc({

+ 40 - 0
Tools/Gulp/processViewerDeclaration.js

@@ -0,0 +1,40 @@
+module.exports = function (data) {
+
+    var str = "" + data;
+
+    // this regex is not working on node 6 for some reason:
+    // str = str.replace(/declare module 'babylonjs-viewer\/' {((?!(declare))(.|\n))*\n}/g, '');
+
+    let lines = str.split('\n');
+    var firstIndex = lines.findIndex((line => { return line.indexOf("'babylonjs-viewer/'") !== -1 }));
+    var lastIndex = lines.findIndex(((line, idx) => { return line.trim() === '}' && idx > firstIndex }));
+    lines.splice(firstIndex, lastIndex - firstIndex + 1);
+    str = lines.join('\n');
+
+    str = str.replace(/declare module (.*) {/g, 'declare module BabylonViewer {').replace("import * as BABYLON from 'babylonjs';", "");
+    str = str.replace(/import {(.*)} from ['"]babylonjs-viewer(.*)['"];/g, '').replace(/import 'babylonjs-loaders';/, '').replace(/import 'pep';/, '');
+
+    //find all used BABYLON and BABYLON-Loaders classes:
+
+    var babylonRegex = /import {(.*)} from ['"](babylonjs|babylonjs-loaders)['"];/g
+
+    var match = babylonRegex.exec(str);
+    let classes = new Set();
+    while (match != null) {
+        if (match[1]) {
+            match[1].split(",").forEach(element => {
+                classes.add(element.trim());
+            });
+        }
+        match = babylonRegex.exec(str);
+    }
+    str = str.replace(babylonRegex, '');
+    classes.forEach(cls => {
+        let rg = new RegExp(`([ <])(${cls})([^\\w])`, "g")
+        str = str.replace(rg, "$1BABYLON.$2$3");
+    });
+
+    str = str.replace(/export {(.*)};/g, '')
+
+    return str;
+}

+ 2 - 0
dist/preview release/viewer/package.json

@@ -13,6 +13,8 @@
     "files": [
         "babylon.viewer.js",
         "babylon.viewer.module.d.ts",
+        "babylon.glTF2Interface.d.ts",
+        "babylonjs.loaders.d.ts",
         "babylon.d.ts",
         "readme.md",
         "package.json"