Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem using SystemJS/JSPM? #17

Open
nicklasb opened this issue Aug 20, 2015 · 2 comments
Open

Problem using SystemJS/JSPM? #17

nicklasb opened this issue Aug 20, 2015 · 2 comments

Comments

@nicklasb
Copy link

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
Copy link
Author

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

@CodeMan99
Copy link
Collaborator

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants