0

Let's say I have an old school 3rd party javascript library in the following format, which effectively creates the global SomeModule:

SomeModule.js

(function() {

  this.SomeModule = ...

})();

I'd rather avoid the global and use ES6 syntax to import SomeModule into just the files where I need it. I figure I can wrap the 3rd party library using something like this:

SomeModuleES6.js

var dummy;

(function(){
  require("SomeModule.js");
}.bind(dummy))();

export default dummy.SomeModule;

Files where SomeModule is needed

import SomeModule from "SomeModuleES6";

There's gotta be a better way though. Is there a standard practice for this situation? How would this work through WebPack and package.json? Should I just fork the 3rd party library and use my own updated version instead?

Anson Kao
  • 5,256
  • 4
  • 28
  • 37
  • 1
    Possible duplicate that went cold http://stackoverflow.com/questions/25840770/convert-closure-to-es6-module – Anson Kao Jan 03 '16 at 03:42
  • Your wrapper wouldn't do anything. The object context in the anonymous function in your `SomeModule.js` would still be `global`. – MinusFour Jan 03 '16 at 03:56
  • @MinusFour cool - any suggestions? – Anson Kao Jan 03 '16 at 03:59
  • 1
    I'd say rewriting that piece of code would be in everyone best interest. You could rewrite that in common js. – MinusFour Jan 03 '16 at 04:02
  • You can of course simply alter the object context from the IIFE in your `SomeModule.js` and have it called on an object you can export. However, if they did treat `this` as global then some stuff might not work. For example, if they are expecting to pickup other things from the global object using `this`, then it's problematic. – MinusFour Jan 03 '16 at 04:09
  • @MinusFour hmm good suggestion... everything is tacked onto that 1 global so it should be fine. Investigating this now: https://github.com/umdjs/umd – Anson Kao Jan 03 '16 at 15:59

0 Answers0