eduardolundgren / gulp-umd

Gulp plugin for build JavaScript files as Universal Module Definition, aka UMD
MIT License
126 stars 14 forks source link

Problem using SystemJS/JSPM? #17

Open nicklasb opened 9 years ago

nicklasb commented 9 years ago

Hi, I am using SystemJS to load my modules, this means that the wrapper sets module.exports, and then exits the conditions. That leaves the root.angularSchemaFormDynamicSelect undefined, which causes an "Uncaught (in promise) Uncaught ReferenceError: angularSchemaFormDynamicSelect is not defined" when the wrapper tries to return it in the end, code:

;(function(root, factory) {
  if (typeof define === 'function' && define.amd) {
    define(['angular-schema-form'], factory);
  } else if (typeof exports === 'object') {
    module.exports = factory(require('angular-schema-form')); //Happens
  } else {
    root.angularSchemaFormDynamicSelect = factory(root.schemaForm);  //Never happens
  }
}(this, function(schemaForm) {

.........my code...............

return angularSchemaFormDynamicSelect; // This is undefined.
}));

That said, I am not sure I understand the UMD model completely..

nicklasb commented 8 years ago

@eduardolundgren This seems to annoy RequireJS as well. Sort of defies the UMD thoughts.

CodeMan99 commented 6 years ago

@nicklasb Hopefully you have identified a pattern that works for you.

The pattern you have in the original issue is that somewhere in "......my code......" you should define an export variable. Usually this is an object or constructor.

Say you have a class you named "Engine". You write a file like this named "engine.js".

function Engine(valves) {
   this.valves = valves;
}

Engine.prototype.start = function() {
   // make the starter motor turn and send fuel into the injection system.
};

In your dev environment Engine is a global. But when you ship you want to make it private if possible.

;(function(root, factory) {
   if (typeof define === 'function' && define.amd) {
      define(['starter', 'fuel-injection'], factory);
   } else if (typeof exports === 'object') {
      module.exports = factory(require('starter'), require('fuel-injection'));
   } else {
      root.Engine = factory(root.starter, root.fuelInjection);
   }
}(this, function(starter, fuelInjection) {
   function Engine(valves) {
      this.valves = valves;
   }

   Engine.prototype.start = function() {
      // make the starter motor turn and send fuel into the injection system.
   };

   return Engine;
}));

You can also change what value is returned by the "factory" function by providing the name via exports configuration option.

Say instead of Engine you called your class CombustionEngine.

var options = {
   exports: function(file) {
      if (file.basename === 'engine.js') {
         return 'CombustionEngine';
      }
      // file is a vinyl instance: https://github.com/gulpjs/vinyl#filestem
      return file.stem;
   }
};
gulp.src('*.js')
   .pipe(umd(options))
   .pipe(gulp.dest('dist'));

By the way, I am a new maintainer here, so at some point I will go about improving the documentation.