有没有办法在没有 package.json 的情况下使用 browserify-shim?

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

我需要为我的一些 browserify 依赖项使用 browserify-shim,但我无法编辑我的 package.json。我在 gulp 中使用 browserify。可以从 API 中指定所有 package.json 部分吗?我正在设想这样的事情:

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

然后,我的输出将 var $ = require('jquery'); 替换为 var $ = jQuery;,因为我们现在将 jQuery 作为全局变量使用。这可能吗?当

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

你可以使用 exposify transform in that case. browserify-shim is actially based on it 并且是同一个人写的。

例如:

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