我想使用大口吞下构建JavaScript文件的包。
例如,我的项目中有以下结构:
有供应商包含(1-2)、本地包含(3-4)和包文件(5-6)。
供应商包含的只是与鲍尔或作曲家一起安装的第三方鲍尔库。它们可以是CommonJS,AMD,或者只是一个普通的jQuery插件。
我希望在这样的包文件中指定依赖项:
/js/bundle1.js
(function() {
// Vendor includes.
include('vendor1');
include('vendor2');
// Local includes.
include('includes/include1.js');
include('includes/include2.js');
// Some code here.
})();我希望大口吞下处理这个源文件,并创建一个最终的分发文件(包),确保所有依赖项(包括)合并到一个文件中。因此,我可以从我的HTML中包含foo.js,所有依赖项都将提供给它。
我希望有一个清晰而健壮的系统来管理项目内部的所有依赖关系,并构建发行文件。
发布于 2014-04-12 20:06:50
你的问题似乎只有一个答案,但却没有。你想要解决的问题是,很多人已经用许多不同的方式解决了这个问题,你已经确定了两个主要的选择: AMD和CommonJS。还有其他方法,但考虑到您可能对Javascript依赖关系管理以及gulp还不熟悉,我建议您使用一些相对简单的方法(尽管这个主题本身并不简单)。
我想对你来说最简单的方法是:
gulp中运行browserify的语句看起来可能如下所示:
var browserify = require('gulp-browserify');
gulp.src('bundles/bundle1.js', {read: false})
.pipe(browserify({
'standalone': true
})
.pipe(rename('bundle1Output.js'))
.pipe(gulp.dest('dist'));这将为您提供一个dist/bundle1Output.js文件。
发布于 2015-12-15 12:42:47
这里有一个gulp插件:
https://www.npmjs.com/package/gulp-include
它应该做您想做的事情,除了在您的包文件中,而不是这样:
(function() {
// Vendor includes.
include('vendor1');
include('vendor2');
// Local includes.
include('includes/include1.js');
include('includes/include2.js');
// Some code here.
})();你必须写:
//=require vendor1/**/*.js
//=require vendor2/**/*.js
//=require includes/include1.js
//=require includes/include2.js
// Some code herehttps://stackoverflow.com/questions/22874894
复制相似问题