[英]Can I achieve this with Gulp + Browserify? Do I need Webpack instead?
我已经使用Gulp一段时间了。 然后我被介绍给Browserify。 现在,考虑到我正在向React迈进,Webpack即将出现。 我离题了...
我想完成以下步骤:
node
应用程序中require
前端依赖项(例如jQuery和Backbone),以便拥有真实的来源npm
。 dependencies.js
文件中(Browserify,对吗?)。 all.min.js
文件,该文件all.min.js
压缩, 包装在一个自执行的匿名函数中 - (function(){ /*all code here*/})()
-以避免任何全局变量/窗口对象上的变量/全局污染 。 (<-这是关键)。 我很希望能够像我习惯的那样在Gulp中处理所有这些问题,但是整个全球污染问题正在使我丧命。 看一下gulp文件:
var gulp = require('gulp');
var concat = require('gulp-concat-util'); // Makes concat.header, concat.footer available.
var uglify = require('gulp-uglify');
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
gulp.task('browserify', function() {
return browserify('libraries.js') // File containing a list of requires's.
.bundle()
.pipe(source('dependencies.js')) // Concatenated file.
.pipe(buffer())
.pipe(gulp.dest('./dev/dependencies')); // Destination directory.
});
gulp.task('scripts', function() {
return gulp.src([
'dev/dependencies/dependencies.js',
'dev/js/models/**/*.js', // Models before views & collections.
'dev/js/**/!(app|templates)*.js',
'dev/js/templates.js',
'dev/js/app.js'
])
.pipe(concat('all.min.js')) // Final file.
.pipe(concat.header('(function(){')) // Attempts to wrap in a SEAF.
.pipe(concat.footer('\n})();'))
.pipe(uglify())
.pipe(gulp.dest('public'));
});
所有这些代码的确会产生一个最小的文件,并封装在SEAF中,但是我所需的库(例如Backbone和jQuery)仍然可以在全局范围内访问。 我的生活! 这就是它的工作方式吗? 这些库将自己附加到window
(我在代码中进行了查看),但我认为也许有些Browserify魔术可以解决这一问题。 任何想法表示赞赏!
我正在使用webpack处理js和CSS,对此我感到非常满意。
所有库依赖项都由npm install
并保存在package.json中。 结果,我得到了一堆包含所有应用程序的缩小文件。 JS模块存储在函数作用域中,因此不会破坏全局作用域。 易于维护,易于理解和跟踪每个模块中使用的所有依赖项。
我会给你一些基本的例子。
文件:webpack.config.js-webpack的配置
module.exports = {
// entrypoint for webpack to start processing
entry: {
bundle: './js/main.js'
},
output: {
path: __dirname + '/' + 'assets',
filename: '[name].js',
publicPath : '/assets/',
}
};
文件:./ js / main.js-Webpack的入口点
// This is require from node_modules
var $ = require('jquery');
// this is require for my local module
var MyModule1 = require('./modules/module1.js');
文件:./modules/module1.js-本地模块
var OtherLib = require('other-lib');
...
// exporting some data for others
module.exports = {
// export object
};
优点:
我面临的缺点:
--watch
模式下处理许多SASS文件时,速度相当慢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.