Guest User

Untitled

a guest
Apr 25th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. loadModules = (arrayOfFilePaths) ->
  2. new Promise (resolve) ->
  3. require arrayOfFilePaths, (ms...) ->
  4. for module in ms
  5. module ModuleAPI
  6. resolve()
  7.  
  8. # in my library
  9. load = (path_to_file) ->
  10. (require path_to_file).do_something()
  11.  
  12. # in my app (using the 'compiled' libary)
  13. cool_library.load("file_that_exists_in_my_app")
  14.  
  15. # in my library
  16. load = (module) ->
  17. module.do_something()
  18.  
  19. # in my app (using the 'compiled' libary)
  20. module = require("file_that_exists_in_my_app")
  21. cool_library.load(module)
  22.  
  23. function loadInContext(filename) {
  24. return new Promise(function(resolve){
  25. require(['./'+filename], resolve);
  26. })
  27. }
  28.  
  29. function loadModules(namesInContext){
  30. return Promise.all(namesInContext.map(loadInContext));
  31. }
  32.  
  33. loadModules(arrayOfFiles).then(function(){
  34. modules.forEach(function(module){
  35. module(moduleAPI);
  36. })
  37. });
  38.  
  39. // modulesConfig.js
  40. module.exports = [
  41. require(...),
  42. ....
  43. ]
  44.  
  45. // run.js
  46. require('modulesConfig').forEach(function(module){
  47. module(moduleAPI);
  48. })
Add Comment
Please, Sign In to add comment