123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- var gutil = require('gulp-util');
- var through = require('through2');
- /**
- * The parameters for this function has grown during development.
- * Eventually, this function will need to be reorganized.
- */
- 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.base;
- 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) {
- basicInit = `__extends(root["BABYLON"], factory()); `
- }*/
- return `\n\n(function universalModuleDefinition(root, factory) {
- var f = factory();
-
- ${sadGlobalPolution}
- if(typeof exports === 'object' && typeof module === 'object')
- module.exports = f;
- else if(typeof define === 'function' && define.amd)
- define(["${varName.module}"], ${subModule || extendsRoot ? '["BABYLON"],' : ''} factory);
- else if(typeof exports === 'object')
- exports["${varName.module}"] = f;
- else {
- ${basicInit}
- }
- })(this, function() {
- return ${base}${(subModule && !extendsRoot) ? '.' + varName.base : ''};
- });
- `;
- }
- var extendsAddition =
- `var __extends = (this && this.__extends) || (function () {
- var extendStatics = Object.setPrototypeOf ||
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
- function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
- return function (d, b) {
- extendStatics(d, b);
- function __() { this.constructor = d; }
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
- };
- })();
- `;
- var decorateAddition =
- 'var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {\n' +
- 'var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;\n' +
- 'if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);\n' +
- 'else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;\n' +
- 'return c > 3 && r && Object.defineProperty(target, key, r), r;\n' +
- '};\n';
- if (file.isNull()) {
- cb(null, file);
- return;
- }
- if (file.isStream()) {
- //streams not supported, no need for now.
- return;
- }
- if (noBabylonInit) {
- optionalRequire = '';
- }
- try {
- if (externalUsingBabylon) {
- file.contents = new Buffer(optionalRequire.concat(new Buffer(String(file.contents).concat(moduleExportAddition(varName)))));
- } 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);
- } catch (err) {
- this.emit('error', new gutil.PluginError('gulp-add-module-exports', err, { fileName: file.path }));
- }
- cb();
- });
- };
|