How to write a module that works with Node.js, RequireJS as well as without them

前端 未结 3 1188
故里飘歌
故里飘歌 2021-02-14 18:09

I am working on a JavaScript library for JSON/XML processing. My library works in browser as well as Node.js (with xmldom and xmlhttprequest modules).<

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-14 18:47

    Here is a template I'm currently using, it's both AMD and node compatible though not directly loadable stand-alone in the browser...

    The main advantage to this approach is that the domain-specific code does not need to care about what imported it, for the general case.

    /**********************************************************************
    * 
    *
    *
    **********************************************************************/
    ((typeof define)[0]=='u'?function(f){module.exports=f(require)}:define)(
    function(require){ var module={} // makes module AMD/node compatible...
    /*********************************************************************/
    
    
    
    
    /*********************************************************************/
    
    
    
    
    /**********************************************************************
    * vim:set ts=4 sw=4 :                               */ return module })
    

提交回复
热议问题