Skip to content

Commit

Permalink
Support loading with a module loader.
Browse files Browse the repository at this point in the history
Babylon.js currently uses namespaces and its types can be pulled in using

    /// <reference path="babylonjs/babylon.d.ts" />

It's generated JavaScript can be loaded using either a script tag, or using a module loader from JavaScript.
However, what was not yet supported is loading types and implementation using a module loader from Typescript,
because babylon.d.ts was not a module.

This adds support for that, following the example here:
https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#support-for-umd-module-definitions
  • Loading branch information
rehmsen committed Sep 29, 2016
1 parent 69b2e64 commit b921855
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Tools/Gulp/gulp-addDtsExport.js
@@ -0,0 +1,31 @@
var gutil = require('gulp-util');
var through = require('through2');

module.exports = function (varName) {
return through.obj(function (file, enc, cb) {

var moduleExportsAddition =
'\nexport as namespace ' + varName + ';\n' +
'\nexport = ' + varName + ';\n';

if (file.isNull()) {
cb(null, file);
return;
}

if (file.isStream()) {
//streams not supported, no need for now.
return;
}

try {
file.contents = new Buffer(String(file.contents) + moduleExportsAddition);
this.push(file);

} catch (err) {
this.emit('error', new gutil.PluginError('gulp-add-module-exports', err, { fileName: file.path }));
}
cb();
});
};

2 changes: 2 additions & 0 deletions Tools/Gulp/gulpfile.js
Expand Up @@ -3,6 +3,7 @@ var uglify = require("gulp-uglify");
var typescript = require("gulp-typescript");
var sourcemaps = require("gulp-sourcemaps");
var srcToVariable = require("gulp-content-to-variable");
var addDtsExport = require("./gulp-addDtsExport");
var addModuleExports = require("./gulp-addModuleExports");
var merge2 = require("merge2");
var concat = require("gulp-concat");
Expand Down Expand Up @@ -98,6 +99,7 @@ gulp.task('typescript-compile', function () {
return merge2([
tsResult.dts
.pipe(concat(config.build.declarationFilename))
.pipe(addDtsExport("BABYLON"))
.pipe(gulp.dest(config.build.outputDirectory)),
tsResult.js
.pipe(gulp.dest(config.build.srcOutputDirectory))
Expand Down

3 comments on commit b921855

@deltakosh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rehmsen we've just found an issue with your hack :(
If you go to loaders folders and try to compile them, you'll see that what you added breaks the compilation:
https://github.com/BabylonJS/Babylon.js/tree/master/loaders

@deltakosh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm reverting it for now

@rehmsen
Copy link
Contributor Author

@rehmsen rehmsen commented on b921855 Oct 5, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.