gulpbrowserifybrowserify-shim

Is there a way to use browserify-shim without package.json?


I need to use browserify-shim for some of my browserify dependencies, but I can't edit my package.json. I'm using browserify inside of gulp. It is possible to specify all of the package.json portion exclusibely form the API? I'm envisioning something like this:

return gulp.src('glob/path/to/my/main/js/file.js')
    .pipe(browserify({
        debug: false,
        transform: ['browserify-shim'],
        shim: {
            'jquery': {
                exports: 'jQuery'
            }
        }
    }));

Then, my output with repalce var $ = require('jquery'); with var $ = jQuery;, since we are now utilizing jQuery as a global. Is this possible? Whne


Solution

  • gulpfile.js

    gulp       = require('gulp')
    browserify = require('gulp-browserify')
    
    gulp.task('browserify', function(){
        var src = 'test.js'
    
        gulp.src(src)
            .pipe(browserify({
                shim: {
                    jQuery: {
                        path: './bowser_components/jquery/dist/jquery.min.js',
                        exports: '$'
                    }
                }
            }))
            .pipe(gulp.dest('./build/js'))
    })
    

    test.js

    $ = require('jQuery')
    
    console.log($);