【发布时间】:2016-08-13 12:24:42
【问题描述】:
在我注入 bower 组件的 gulp 文件中,我有这种糟糕的代码重复。但我不知道如何摆脱它。
一般来说,我们不能只说bower_components/**/*.js,因为我们不想导入所有文件,而且对于生产,我们只想导入.min 文件。再次。我不能保证我使用的每个包都有.js 和.min.js 文件。所以只是*.js 和*.min.js 可能不起作用。
gulp.task('inject', () => {
let sources = gulp.src([
// jquery
'public/bower_components/jquery/dist/jquery.js',
// bootstrap
'public/bower_components/bootstrap/dist/js/bootstrap.js',
'public/bower_components/bootstrap/dist/css/bootstrap.css',
// angular
'public/bower_components/angular/angular.js',
'public/bower_components/angular/angular-csp.css',
// angular route
'public/bower_components/angular-route/angular-route.js',
],{read: false});
let min_sources = gulp.src([
// jquery
'public/bower_components/jquery/dist/jquery.min.js',
// bootstrap
'public/bower_components/bootstrap/dist/js/bootstrap.min.js',
'public/bower_components/bootstrap/dist/css/bootstrap.min.css',
// angular
'public/bower_components/angular/angular.min.js',
'public/bower_components/angular/angular-csp.css',
// angular route
'public/bower_components/angular-route/angular-route.min.js',
],{read: false});
return gulp.src('public/build/index.html')
.pipe(gulpif(!argv.production, inject(sources, {relative: true})))
.pipe(gulpif(argv.production, inject(min_sources, {relative: true})))
.pipe(gulp.dest('public/build/'));
});
但是这种代码重复不是解决方案。我认为。除了在bower.js 文件中移动这两个数组之外,我该如何改进这部分?
【问题讨论】:
-
为什么不总是使用第三方组件的缩小版?您可以使用main-bower-files 而不是自己列出所有这些。
-
用于调试目的。使用缩小版本,即使您只是忘记导入某些组件,您也无法说出问题所在。因此,在开发过程中,我更喜欢使用未缩小版本的组件。
-
你应该看看source maps。
-
好的。所以显然我可以只提供 .js 供应商文件的列表。并从此列表中创建带有地图的文件的缩小版本。
标签: javascript gulp bower