Nodejs 关于grunt配置中的通配符
Nodejs 关于grunt配置中的通配符
{,/} 不像是正则表达式, eg: scripts/{,/}.js 意思是匹配scripts目录下和后代目录下所有的js吗?
当然可以。下面是一个关于Node.js中Grunt配置中的通配符的详细解释及示例代码。
Node.js Grunt配置中的通配符
Grunt是一款流行的JavaScript任务运行器,它允许你自动化一些常见的开发任务,如文件监控、文件合并、压缩等。在Grunt配置中,通配符用于指定需要处理的文件路径模式。
常见通配符
*
匹配任意单个字符。**
匹配任意数量的字符,包括路径分隔符(即可以跨目录)。{a,b}
匹配a
或b
。{a..c}
匹配a
到c
之间的所有字符(仅限于数字)。
示例
假设我们有以下文件结构:
project/
├── Gruntfile.js
├── scripts/
│ ├── util.js
│ └── main.js
└── lib/
├── helper.js
└── utils.js
我们想要配置一个任务来处理所有的.js
文件,包括子目录中的文件。我们可以使用**/*.js
这样的模式来匹配所有.js
文件。
module.exports = function(grunt) {
// 初始化Grunt配置
grunt.initConfig({
// 配置一个任务,比如copy
copy: {
main: {
files: [
{
expand: true,
cwd: 'scripts/',
src: ['**/*.js'],
dest: 'dist/scripts/'
},
{
expand: true,
cwd: 'lib/',
src: ['**/*.js'],
dest: 'dist/lib/'
}
]
}
}
});
// 加载插件以启用特定任务
grunt.loadNpmTasks('grunt-contrib-copy');
// 注册一个默认任务
grunt.registerTask('default', ['copy']);
};
在这个例子中:
expand: true
表示开启扩展模式,这样可以使用通配符。cwd: 'scripts/'
设置源文件的根目录。src: ['**/*.js']
使用通配符模式来匹配所有.js
文件。dest: 'dist/scripts/'
设置目标目录。
通过这种方式,你可以轻松地配置Grunt来处理各种复杂的文件模式。希望这能帮助你更好地理解和使用Grunt中的通配符。
应该是这样的: scripts/{ , * / } *.js , 晕,原来cnnode 把星号过滤了
###Globbing patterns
It is often impractical to specify all source filepaths individually, so Grunt supports filename expansion (also know as globbing) via the built-in node-glob and minimatch libraries.
While this isn’t a comprehensive tutorial on globbing patterns, know that in a filepath:
*
matches any number of characters, but not/
?
matches a single character, but not/
**
matches any number of characters, including/
, as long as it’s the only thing in a path part{}
allows for a comma-separated list of “or” expressions!
at the beginning of a pattern will negate the match All most people need to know is thatfoo/*.js
will match all files ending with.js
in thefoo/
subdirectory, butfoo/**/*.js
will match all files ending with.js
in thefoo/
subdirectory and all of its subdirectories.
Also, in order to simplify otherwise complicated globbing patterns, Grunt allows arrays of file paths or globbing patterns to be specified. Patterns are processed in-order, with !
-prefixed matches excluding matched files from the result set. The result set is uniqued.
For example:
// You can specify single files:
{src: 'foo/this.js', dest: ...}
// Or arrays of files:
{src: ['foo/this.js', 'foo/that.js', 'foo/the-other.js'], dest: ...}
// Or you can generalize with a glob pattern:
{src: 'foo/th*.js', dest: ...}
// This single node-glob pattern:
{src: ‘foo/{a,b}.js’, dest: …}
// Could also be written like this:
{src: ['foo/a.js’, ‘foo/b*.js’], dest: …}
// All .js files, in foo/, in alpha order:
{src: [‘foo/.js’], dest: …}
// Here, bar.js is first, followed by the remaining files, in alpha order:
{src: [‘foo/bar.js’, 'foo/.js’], dest: …}
// All files except for bar.js, in alpha order:
{src: [‘foo/.js’, ‘!foo/bar.js’], dest: …}
// All files in alpha order, but with bar.js at the end.
{src: ['foo/.js’, ‘!foo/bar.js’, ‘foo/bar.js’], dest: …}
// Templates may be used in filepaths or glob patterns:
{src: [‘src/<%= basename %>.js’], dest: ‘build/<%= basename %>.min.js’}
// But they may also reference file lists defined elsewhere in the config:
{src: [‘foo/*.js’, ‘<%= jshint.all.src %>’], dest: …}
{ , * / } 这个比较怪,逗号写在前面, 按上面的说的,应该指的是匹配该目录下的js和子目录下的js,只匹配两级目录的文件了.谢谢!
当然可以。在Grunt配置中使用通配符是一种非常方便的方式来指定文件路径模式。这些通配符通常用于files
属性,以简化文件路径的定义。
示例
假设你有一个项目结构如下:
project-root/
├── Gruntfile.js
├── scripts/
│ ├── main.js
│ └── utils/
│ └── helper.js
└── styles/
└── style.css
如果你想匹配所有在scripts/
目录及其子目录下的.js
文件,你可以这样配置Grunt任务:
module.exports = function(grunt) {
// 项目配置
grunt.initConfig({
// 配置一个任务,例如:'uglify'
uglify: {
options: {
banner: '/*! <%= grunt.template.today("dd-mm-yyyy") %> */\n'
},
build: {
files: {
'dist/scripts.min.js': ['scripts/**/*.js'] // 使用通配符
}
}
}
});
// 加载包含任务的插件
grunt.loadNpmTasks('grunt-contrib-uglify');
// 默认任务
grunt.registerTask('default', ['uglify']);
};
在这个例子中,'scripts/**/*.js'
这个通配符路径会匹配到:
scripts/main.js
scripts/utils/helper.js
但不会匹配到styles/
目录下的任何文件。
通配符解释
**
表示任意数量的子目录。*
表示任意数量的文件名(不包括目录)。
所以,scripts/**/*.js
会匹配scripts/
目录下的所有.js
文件,无论它们位于哪一层子目录中。
希望这能帮助你更好地理解和使用Grunt配置中的通配符!如果你有任何进一步的问题,请随时告诉我。