如何在Browserify中使用exclude?

3
在browserify-handbook中,排除部分给出了使用exclude的示例:(链接)
$ npm install jquery
$ browserify -r jquery --standalone jquery > jquery-bundle.js

然后我们只需要在main.js中使用require('jquery'):

var $ = require('jquery');
$(window).click(function () { document.body.bgColor = 'red' });

将权利转交给jQuery分发包,以便我们可以编写以下内容:
<script src="jquery-bundle.js"></script>
<script src="bundle.js"></script>

如果不希望在bundle.js中出现jquery的定义,那么在编译main.js时,可以使用--exclude jquery命令来排除它:

browserify main.js --exclude jquery > bundle.js

但是当我尝试运行这个示例时,出现了一个错误:“Uncaught Error: Cannot find module 'jquery'”。
我知道如果我使用独立模式,我可以将 'jquery' 作为全局变量使用,但这样不够模块化,所以我仍然想按照示例使用“require('jquery')”,那么我该怎么做呢?
1个回答

6

我终于通过在这里找到的信息使这个功能运作起来了:

https://truongtx.me/2014/03/20/browserify-bring-nodejs-modules-to-browser/

我想知道你找到的文档现在是否已经过时了...

上面链接中的解决方案使用'-x'选项('外部')而不是'-u'选项('排除')

所链接的文章还演示了如何使用gulp进行设置。

我将上面链接网站的相关内容粘贴如下:

$ browserify -r foo.js > foo-out.js
$ browserify -r d3-browserify > d3-browserify-out.js

对于 main.js

$ browserify -x ./foo.js -x d3-browserify main.js > main-bundle.js

在浏览器中,您需要包含这三个文件。
<script src="foo-out.js"></script>
<script src="d3-browserify-out.js"></script>
<script src="main-bundle.js"></script>

更新:我发布的链接似乎无法使用,因此我包含了我的当前gulpfile.js。我对gulp和javascript都很陌生,所以可能有更好的方法来完成这些工作。但是这个当前设置基本上是有效的:

var browserify = require('browserify');

var gulp = require('gulp');

var watchify = require('watchify');

var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');

var jshint = require('gulp-jshint');
var uglify = require('gulp-uglify');
var sourcemaps = require('gulp-sourcemaps');
var gutil = require('gulp-util');

var del = require('del');

const PATH_OPTS = {
    src_dir: './client/js/src/',
    main_file_path: './client/js/src/main.js',
    output_file_name: 'disco.js',
    output_dir: './public/js/',
    common_lib_file_name: 'common.js'
};

const LIBS = ['paper', 'jquery', 'tocktimer'];

gulp.task("clientlib", function() {
    var bundler = browserify({
        debug: false
    });

    LIBS.forEach(function(lib) {
        bundler.require(lib);  
    });

    bundler.bundle()
    .pipe(source(PATH_OPTS.common_lib_file_name))
    // uglify seems to need a buffered stream...
    .pipe(buffer())
    .pipe(uglify())
        .on('error', gutil.log)
    .pipe(gulp.dest(PATH_OPTS.output_dir));
});

gulp.task('client', function () {
    var bundler = browserify({
        debug: true,
        cache: {},
        packageCache: {},
        fullPaths: true
    });

    bundler = watchify(bundler);

    bundler.on('update', function(){
        bundle(bundler);
    });

    bundler.on('log', function(msg) {
        gutil.log(msg);
    });

    bundler.add(PATH_OPTS.main_file_path);

    LIBS.forEach(function(lib) {
        bundler.external(require.resolve(lib, { expose: lib }));
    });

    return bundle(bundler);
});

function bundle(bundler) {
    return bundler.bundle()
        .pipe(source(PATH_OPTS.output_file_name)) 
        .pipe(buffer())
        .pipe(sourcemaps.init({loadMaps: true}))
        // Add transformation tasks to the pipeline here.
        .pipe(uglify())
            .on('error', gutil.log)
        .pipe(sourcemaps.write('./'))
        .pipe(gulp.dest(PATH_OPTS.output_dir));
}

gulp.task('lint', function() {
    return gulp.src(PATH_OPTS.src_dir + '*.js')
        .pipe(jshint())
        .pipe(jshint.reporter('default'));
});

gulp.task('clean', function () {
    return del([
        PATH_OPTS.output_dir + '/*.*'
    ]);
});

gulp.task('full', ['lint', 'clean', 'clientlib', 'client']);

gulp.task('default', ['lint', 'client']);

我刚刚尝试了这个,生成的文件看起来很好,但是在使用它时出现了问题,我得到了“未捕获的错误:找不到模块'react'”的信息。我已经将react.js列在主应用程序脚本上面了。 - Keith Jackson
我现在已经通过使用供应商文件使其工作(我错过了那一部分),但是如果没有供应商文件,它将无法找到React - 使用供应商文件会抛出一个关于不应该缩小React的错误。这太疯狂了! - Keith Jackson
我最近没有关注过这个问题,但是这个解决方案现在似乎已经过时了。请参见 https://truongtx.me/2015/11/03/using-gulp-with-browserify-and-watchify-update-nov-2015 - Nested Software

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接