通过读取并分析bower.json文件里override属性里main路径下定义的插件及相关依赖,返回一个文件数组。
Github
https://github.com/ck86/main-bower-files
安装
$ npm install –save-dev main-bower-files
使用
1. 直接使用
var gulp = require('gulp');
var mainBowerFiles = require('main-bower-files');
gulp.task('TASKNAME', function() {
return gulp.src(mainBowerFiles())
.pipe(/* what you want to do with the files */)
});
2.也可以增加配置:
var gulp = require('gulp');
var mainBowerFiles = require('main-bower-files');
gulp.task('TASKNAME', function() {
return gulp.src(mainBowerFiles(/* options */), { base: 'path/to/bower_components' })
.pipe(/* what you want to do with the files */)
});
配置项
1.override option
这块配置是配置到bower.json文件里的
1.1 main Type: String or Array or Object
这个配置能够指定想要选择的文件,特别是根据环境来选择想要的bower文件,例如根据process.env.NODE_ENV,当env=development时选择file.js,当env=production时选择file.min.js
{
"overrides": {
"BOWER-PACKAGE": {
"main": {
"development": "file.js",
"production": "file.min.js"
}
}
}
}
或者使用glob匹配规则来筛选想要的文件
{
"overrides": {
"BOWER-PACKAGE": {
"main": "**/*.js"
}
}
}
1.2 ignore Type: Boolean Default: false
这是个布尔类型,选择true或false来选择是否忽略该包
{
"overrides": {
"BOWER-PACKAGE": {
"ignore": true
}
}
}
1.3 dependencies Type: Object
可以重载需要的依赖,也可以set null来忽略依赖
2.common option
这些配置是配置到main-bower-files的配置中,像这样:mainBowerFiles(/* options*/) ,这些配置一般用的比较少
- debugging
- main
- env
- paths
- checkExistence
- includeDev
- includeSelf
- filter
- overrides
具体意思参见github解释:
debugging Type: boolean Default: false
Set to true to enable debugging output.
main Type: String or Array or Object Default: null
You can specify for all packages a default main property which will be used if the package does not provide a main property.
env Type: String Default: process.env.NODE_ENV
If process.env.NODE_ENV is not set you can use this option.
paths Type: Object or String
You can specify the paths where the following bower specific files are located:
bower_components, .bowerrc and bower.json,For example:
mainBowerFiles({
paths: {
bowerDirectory: 'path/for/bower_components',
bowerrc: 'path/for/.bowerrc',
bowerJson: 'path/for/bower.json'
}
})
.pipe(gulp.dest('client/src/lib'));
If a String is supplied instead, it will become the basepath for default paths.
For example:
mainBowerFiles({ paths: 'path/for/project' });
/*
{
bowerDirectory: 'path/for/project/bower_components',
bowerrc: 'path/for/project/.bowerrc',
bowerJson: 'path/for/project/bower.json'
}
*/
checkExistence Type: boolean Default: false
Set this to true if you want that the plugin checks every file for existence.If enabled and a file does not exists, the plugin will throw an exception.
includeDev Type: mixed Default: false
You can include your devDependencies in two ways:
Set this option to inclusive or true to add the devDependencies to your dependencies
or use exclusive to exclude your dependencies
includeSelf Type: boolean Default: false
Set this to true to add the main files to your dependencies
filter Type: RegExp or function or glob Default: null
You can filter the list of files by a regular expression, glob or callback function (the first and only argument is the file path).
overrides Type: object Default: {}
Set default overrides option which can be overridden in the overrides section of the bower.json
group Type: String or Array Default: null
You can specify a group of dependencies you want to read from bower.json
For example:
{
"dependencies": {
"BOWER-PACKAGE-1": "*",
"BOWER-PACKAGE-2": "*",
"BOWER-PACKAGE-3": "*",
"BOWER-PACKAGE-4": "*"
},
"group": {
"home": [ "BOWER-PACKAGE-1" ],
"contact": [ "BOWER-PACKAGE-4" ],
"admin": [ "BOWER-PACKAGE-1", "BOWER-PACKAGE-2", "BOWER-PACKAGE-3" ]
}
}
mainBowerFiles({ paths: 'path/for/project', group: 'home' });
You can select multiple groups with an array.
mainBowerFiles({ paths: 'path/for/project', group: ['home', 'contact'] });
You can include all packages except for those listed in a group with the ! operator.
mainBowerFiles({ paths: 'path/for/project', group: '!home' });