how to use the exclude in browserify?

蓝咒 提交于 2019-12-14 03:51:21

问题


in browserify-handbook, exclude part,it gives an example of using the exclude:

$ npm install jquery
$ browserify -r jquery --standalone jquery > jquery-bundle.js

then we want to just require('jquery') in a main.js:

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

defering to the jquery dist bundle so that we can write:

<script src="jquery-bundle.js"></script>
<script src="bundle.js"></script>

and not have the jquery definition show up in bundle.js, then while compiling the main.js, you can --exclude jquery:

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

but when i try to run this sample,i got an error of "Uncaught Error: Cannot find module 'jquery'"

i know if i use standalone,i can just use 'jquery' as a global variable, but it's not modular, so i still want to do as the sample using "require('jquery')", so,what shall i do to achieve it?


回答1:


I finally got this functionality working using information found here:

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

I wonder if the docs you found are out of date now...

The working solution in the above link uses the '-x' option ('external') rather than the '-u' option ('exclude')

The linked article also demonstrates how to set this up using gulp.

I'm pasting the relevant content from the above-linked web site:

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

For main.js

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

In the browser, you need to include all those 3 files

<script src="foo-out.js"></script>
<script src="d3-browserify-out.js"></script>
<script src="main-bundle.js"></script>

Update: The link I posted does not seem to be working so I'm including my current gulpfile.js. I'm new to gulp and javascript, so there may be a better way to do this stuff. But this current setup does basically work:

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']);


来源:https://stackoverflow.com/questions/34279961/how-to-use-the-exclude-in-browserify

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!