使用Gulp将浏览器模块设置为外部模块 [英] Make browserify modules external with Gulp

查看:120
本文介绍了使用Gulp将浏览器模块设置为外部模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 lib / a.js 和<$创建一个库 lib.js c $ c> lib / b.js 并且能够使用脚本 client.js 使用 var a = require('lib / a.js'); 并且它在我编译的 lib.js 库之前包含 client.js (因此, lib.js 必须声明 require >函数知道 lib / a.js

I have a library lib.js that I want to create from lib/a.js and lib/b.js and to be able to use it from a script client.js using var a = require('lib/a.js'); and that it works when I just include the compiled lib.js library before client.js (therefore, lib.js has to declare a require function that knows about lib/a.js)

我想我必须使用外部别名但我不确定什么是正确的方法来做到这一点

I guess I have to use external and alias but I am not sure what is the proper way to do it

另外,是否有可能为我的库中的文件夹自动创建所有别名的Gulp文件?例如。为 lib / dir中的所有文件创建一个别名?

Also, is it possible to have a Gulp file that creates all the alias automatically for the folders in my library? eg. creates an alias for all the files in the lib/ dir?

推荐答案

这里有几个gulp任务可以帮助您分别构建常见的lib.js和client.js包。

Here are a couple of gulp tasks that would help to build your common lib.js and the client.js bundles separately.

请注意,您必须将browserify告知b .require()lib / *。js绑定lib.js时,必须将其告知b.external()绑定client.js时将单独加载的库

Note that you have to tell browserify to b.require() lib/*.js when bundling lib.js, and you have to tell it to b.external() the libraries that will be loaded separately when bundling client.js

var path = require('path');
var gulp = require('gulp');
var browserify = require('browserify');
var concat = require('gulp-concat');
var transform = require('vinyl-transform');

gulp.task('build-lib', function () {

  // use `vinyl-transform` to wrap around the regular ReadableStream returned by b.bundle();
  // so that we can use it down a vinyl pipeline as a vinyl file object.
  // `vinyl-transform` takes care of creating both streaming and buffered vinyl file objects.
  var browserified = transform(function(filename) {

    // basename, for eg: 'a.js'
    var basename = path.basename(filename);

    // define the exposed name that your client.js would use to require();
    // for eg: require('lib/a.js'); // -> exposed name should be 'lib/a.js'
    var expose = 'lib/' + basename;

    return browserify(filename)
      .require(filename, { expose: expose})
      .bundle();
  });

  return gulp.src(['./lib/*.js'])
    .pipe(browserified)
    .pipe(concat('lib.js'))
    .pipe(gulp.dest('./dist'));
});

gulp.task('build-client', function () {

  var browserified = transform(function(filename) {
    // filename = './client.js'

    // let browserify know that lib/a.js and and lib/b.js are external files
    // and will be loaded externally (in your case, by loading the bundled lib.js 
    // for eg: <script src='dist/lib.js'>)
    return browserify(filename)
      .external('lib/a.js')
      .external('lib/b.js')
      .bundle();
  });

  return gulp.src(['./client.js'])
    .pipe(browserified)
    .pipe(gulp.dest('./dist'));
});

gulp.task('default', ['build-lib', 'build-client']);

这篇关于使用Gulp将浏览器模块设置为外部模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆