AlexZ
AlexZ

Reputation: 12073

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

Upvotes: 3

Views: 2415

Answers (2)

Kolya Ay
Kolya Ay

Reputation: 353

You could use exposify transform in that case. browserify-shim is actially based on it and is written by the same guy.

E.g.:

return browserify('glob/path/to/my/main/js/file.js', { debug: false })
    .transform('exposify', { expose: {'jquery': 'jQuery' }})

Upvotes: 2

Jasin Yip
Jasin Yip

Reputation: 308

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($);

Upvotes: 0

Related Questions