简体   繁体   English

如何使用Gulp有效管理和处理Bower包? (VS2015,Visual Studio 2015)

[英]How to efficiently manage and process Bower packages with Gulp? (VS2015, Visual Studio 2015)

Background: 背景:

Visual Studio 2015 has introduced Gulp and Bower for client side package management. Visual Studio 2015引入了Gulp和Bower用于客户端包管理。 .Net previously had a very efficient method of bundling / minification and package management , but for an unspecified reason this has been removed in ASP.Net 5 / MVC 6, and the advice is to use Gulp and Bower instead. .Net之前有一种非常有效的捆绑/缩小和包管理方法 ,但由于一个未指明的原因,这已在ASP.Net 5 / MVC 6中删除,建议使用Gulp和Bower。

I have a number of vendor files that I wish to use in my project, including jquery.appear, isotope, owl-carousel etc, etc; 我有许多供应商文件,我希望在我的项目中使用,包括jquery.appear,isotope,owl-carousel等等; some are simple JavaScript files, others have CSS, still others have assets such as fonts, images. 一些是简单的JavaScript文件,另一些是CSS,还有一些是资源,如字体,图像。

Scenario: 场景:

At the moment I am evaluating how to best utilise Bower to manage versions of packages, while using Gulp to extract only the necessary files from bower_components, and uglify / minify / concat them into bundles. 目前我正在评估如何最好地利用Bower来管理软件包的版本,同时使用Gulp仅从bower_components中提取必要的文件,并将uglify / minify / concat连接到bundle中。

I am currently using CDN available versions of scripts, but best practice would suggest I implement fail-over to local copies - IF I can find a way to manage them using Bower / Gulp OR just download them locally, and forgo package management. 我目前正在使用CDN可用版本的脚本,但最佳实践建议我实现故障转移到本地副本 - 如果我能找到一种方法来管理它们使用Bower / Gulp 或者只是在本地下载它们,并放弃包管理。

Package management would be my preferred approach, but not if this is high maintenance in terms of scripts, configurations, overrides etc. 包管理将是我的首选方法,但如果这是脚本,配置,覆盖等方面的高维护,则不是。

What I have tried: 我尝试过的:

I have looked at Gulp packages such as bower-main-files , gulp-bower-src (which apparently is blacklisted by Gulp), and I am currently using gulp-concat-vendor ; 我看过Gulp软件包,例如bower-main-filesgulp-bower-src (显然被Gulp列入黑名单),我目前正在使用gulp-concat-vendor ; with this I can process basic packages which contain only single JavaScript files (ie not CSS, not related assets such as images). 有了这个,我可以处理只包含单个JavaScript文件的基本包(即不包含CSS,不包括图像等相关资产)。

Problems: 问题:

Some of the bower packages do not include correct information for exporting their main files (some have no main declarations at all). 一些凉亭包不包含用于导出其主文件的正确信息(一些根本没有主要声明)。

Some of the packages download dependencies into bower_components at the top level, which becomes cluttered with files I do not need (I want only the main (core) exported files, and the dependencies are usually already met elsewhere). 有些软件包会将依赖项下载到顶层的bower_components中,这会使我不需要的文件变得混乱(我只想要主(核心)导出的文件,而且依赖项通常已在其他地方得到满足)。 These additional packages need yet more configuration to exclude them from being processed as part of 'Bower Main Files'. 这些额外的软件包需要更多配置,以排除它们作为“Bower Main Files”的一部分进行处理。

In general, Bower 'standards' are loose, and are not adhered to, even for popular packages. 一般来说,Bower的“标准”是松散的,并且即使对于流行的包装也不遵守。

During concatenation, sometimes a specific order needs to be achieved. 在连接期间,有时需要实现特定的顺序。 I have been unable to find an elegant way to do this automatically - I have created an array of source files, but this is not ideal, as it requires manually checking and editing for each package, which mostly negates the whole concept of package management. 我一直无法找到一种优雅的方法来自动执行此操作 - 我创建了一系列源文件,但这并不理想,因为它需要手动检查和编辑每个包,这大多否定了包管理的整个概念。

Questions: 问题:

  1. Do experienced front-end developers attempt to follow the same approach as I am attempting (using bower_components as a source), or simply manually copy required files from GitHub? 有经验的前端开发人员是否尝试按照我尝试的相同方法(使用bower_components作为源),或者只是从GitHub手动复制所需的文件?

  2. If you do use bower-components, can you please outline the workflow with Gulp, and what plug-ins you use to filter out only the files you need. 如果你确实使用了bower-components,你能否概述一下Gulp的工作流程,以及你用来过滤掉你需要的文件的插件。

  3. Is it possible to prevent unneeded dependencies , tests , etc from being downloaded by Bower in the first place? 是否有可能防止Bower首先下载不需要的依赖项测试等?

  4. When processing files that include relative references (eg CSS containing a reference to an image), is it possible to correct the relative path, to be relative to the specified output directory for such assets? 处理包含相对引用的文件(例如,包含对图像的引用的CSS)时,是否可以更正相对路径,相对于此类资产的指定输出目录?

  1. Yes. 是。

  2. See below. 见下文。

  3. Well, bower package is package, you get whats included. 好吧,凉亭包是包,你得到什么包括。 For your build you either rely on components bower.json which specifies main files or do filtering yourself. 对于您的构建,您要么依赖于指定主文件的组件bower.json,要么自己进行过滤。 It is simple enough. 这很简单。

You can use filter = require('gulp-filter') to filter files like that: 您可以使用filter = require('gulp-filter')来过滤这样的文件:

var gulp = require('gulp'),
    bower = require('gulp-main-bower-files'),
    filter = require('gulp-filter'),
    concat = require('gulp-concat'),
    rename = require('gulp-rename'),
    srcmaps = require('gulp-sourcemaps'),
    jsminify = require('gulp-uglify')
    cssminify = require('gulp-csso'),
    del = require('del');

var src = {
    js: 'app/**/*.js',
    css: 'app/**/*.css',
    content: ['app/**/*.jpg', 'app/**/*.svg', 'app/**/*.png', 'app/**/*.ico', 'app/**/*.html']
}

var dst = {
    pub: 'pub/',
    lib: 'pub/lib/'
}

gulp.task('bower', ['start-build'], function () {
    var jsfilter = filter('**/*.js')
    var cssfilter = filter('**/*.css')
    return gulp.src('bower.json')
        .pipe(bower())
    .pipe(jsfilter)
    .pipe(concat('lib.min.js'))
        .pipe(jsminify())
    .pipe(gulp.dest(dst.lib))
    .pipe(jsfilter.restore())
    .pipe(cssfilter)
    .pipe(concat('lib.min.css'))
        .pipe(cssminify())
    .pipe(gulp.dest(dst.lib))
    .pipe(cssfilter.restore())
    .pipe(rename(function (path) {
        if (~path.dirname.indexOf('fonts')) {
            path.dirname = '/fonts'
        }
    }))
    .pipe(gulp.dest(dst.lib));
})

gulp.task('js', ['start-build'], function () {
    return gulp.src([src.js])
        .pipe(srcmaps.init())
        .pipe(concat('app.min.js'))
        .pipe(jsminify())
        .pipe(srcmaps.write())
        .pipe(gulp.dest(dst.pub));
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM