dependencies - Browser or server version of module -


using requirejs , javascript modules, i'd share code between server , browser, there 4 situations.

1/ module server

define([], function() {    if(typeof window != 'undefined') return null; // server    //module code }) 

2/ module browser

define([], function() {    if(typeof window == 'undefined') return null; // browser    //module code }) 

3/ module shared

define([], function() {    //module code }) 

4/ module different code server/browser

define(['require'], function(require) {    if(typeof window == 'undefined') // server version    {       return require('nodejs module');    }     // browser version }) 

what want solve better way including module code browser, have past whole code of browser version. nodejs require sync, works. in browser have use like

define(['require'], function(require) {    if(typeof window == 'undefined') // server version    {       return require('nodejs_module');    }     require(['module_for_browser'], function(module_for_browser) {       return module_for_browser;    }) } 

but can't return it!

most libraries have same code both , manage internally checking window, not. want use same eventemitter api server require('events').eventemitter , browser compatible version https://github.com/hij1nx/eventemitter2


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -