javascript - Dynamically generating a required module with Browserify and Gulp -


is there way invoke browserify (via gulp) includes different file when requireing module given name?

briefly, end result browserify entry point, main.js:

var myplatformspecificimplmentation = require('./platform'); // go town 

to use contents of ./path/to/platform-a.js when run gulp js:platform-a , ./path/to/platform-b.js when run gulp js:platform-b.

if using requirejs, simple modifying paths option accordingly:

paths: {   platform: './path/to/platform-a' } 

it great if somehow generate these modules dynamically via gulp's built-in streaming mechanism. in case, could, say, pipe file gulp-template , on browserify.

thanks

one solution use pathmodify plugin so:

gulpfile.js

var   pathmod = require('pathmodify'),   paths = {a: '/path/to/platform-a.js', b: '/path/to/platform-b.js'};  function platform (version) {   return function () {     return browserify('./main')       .plugin(pathmod(), {mods: [         pathmod.mod.id('app/platform', paths[version])       ]})       .bundle()       .pipe(...);   }; }  gulp.task('js:platform-a', platform('a')); gulp.task('js:platform-b', platform('b')); 

main.js

var myplatformspecificimplmentation = require('app/platform'); 

i've illustrated require() string changed app/platform because allows simplest implementation of pathmodify without collisions other ./platform relative paths in other files. can implemented pathmodify without risking collision (by testing parent module [main.js in case] pathname). if it's important keep ./platform string i'll illustrate that.

or use transform. take @ makerequiretransform() in benbria/browserify-transform-tools if don't want roll own.

it great if somehow generate these modules dynamically via gulp's built-in streaming mechanism. in case, could, say, pipe file gulp-template , on browserify.

that's not out of question, it's not easy do. without touching disk, you'd need create / gulp.src() vinyl file , run through whatever gulp plugins, convert stream feed browserify.