问题描述
我有一个库 lib.js
我想从 lib/a.js
和 lib/b.js
创建并能够使用 var a = require('lib/a.js');
从脚本 client.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
)
我想我必须使用 external
和 alias
但我不确定什么是正确的方法
I guess I have to use external
and alias
but I am not sure what is the proper way to do it
另外,是否有可能有一个 Gulp 文件为我的库中的文件夹自动创建所有别名?例如.为 lib/
目录中的所有文件创建别名?
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.
请注意,捆绑 lib.js 时必须将 browserify 告知 b.require() lib/*.js,并且必须告知 b.external() 捆绑客户端时将单独加载的库.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
这篇关于使用 Gulp 使 browserify 模块外部化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!