Saturday, 8 April 2017

Wrapper around browserify require?

I have a server in NodeJS and a heavy JS front end.

The server and the client share a lot of the same code but right now I'm requiring them with relative paths.

I looked up all the different methods of overcoming this problem and I really like the idea of creating a global custom require method that wraps around the built in require method. So now for example I can call "MyRequireFunction()" instead of "require()" and it works the same way.

This works great in NodeJS, but now I'm trying to figure out how to do it for the client too. Because they share the same code I want it to run the same function ("MyRequireFunction()").

I don't want if statements that check if it's node or the browser. I'm looking for a clean solution where I can require a file without a relative path on a single line that works on both platforms. The custom require method is perfect for this, I'm just not sure how to go about doing it on the client using browserify and gulp.

If I wrap require in a custom javascript function it doesn't treat it the way it's supposed to because I guess it thinks the function is part of the code, so how do I go about solving this in the way I'm looking for?



via user1157885

No comments:

Post a Comment