blakek / addtopage

Inject content to your page how you want—using JavaScript
MIT License
0 stars 0 forks source link

Add "require" functionality #16

Open blakek opened 7 years ago

blakek commented 7 years ago

I want to use as a sort of ponyfill for require to be able to require libraries from, e.g. npm, for super-lazy prototyping.

Example behavior:

var require = window.addtopage.require;
require('moment');
moment().format('dddd');  // Thursday
blakek commented 7 years ago

Looking back, I'm wondering how this would best be implemented. Fetching the package should be done asynchronously, and the example I wrote above won't work.

My first thought is to use a promise:

var require = window.addtopage.require

require('moment').then(function() {
  moment.format('dddd')
})

However, this is targeting browser environment, so we would probably need to either use a fake promise or use a lightweight polyfill. Further, using "require" for the function name may be odd since require() runs "synchronously" in Node.js.