我有一个下面的结构:
src/
modules/
module1/
js/
main.js
scss/
main.scss
index.html
module2/
js/
main.js
scss/
main.scss
index.html
我想运行一个简单的任务将这些复制到下面的结构中:
dev/
js/
module1.js
module2.js
css/
module1.css
module2.css
module1.html
module2.html
有没有办法用现有的grunt插件做到这一点?如果没有,我怎样才能做到这一点?
发布于 2013-03-20 14:23:45
这可以使用grunt-contrib-copy插件来完成。
需要注意的主要事情是,您可以通过使用rename函数(它接受每个文件的目标和源)以编程方式更改目标。
下面是一个(有点脆弱的)示例Gruntfile.js
,它应该复制到您想要的结构中:
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
copy: {
main: {
files: [
{
expand: true,
cwd: 'src/modules/',
src: ['**/*.js'],
dest: 'dev/js/',
rename: function(dest, src) {
// use the source directory to create the file
// example with your directory structure
// dest = 'dev/js/'
// src = 'module1/js/main.js'
return dest + src.substring(0, src.indexOf('/')) + '.js';
}
},
{
expand: true,
cwd: 'src/modules/',
src: ['**/*.scss'],
dest: 'dev/css/',
rename: function(dest, src) {
return dest + src.substring(0, src.indexOf('/')) + '.css';
}
},
{
expand: true,
cwd: 'src/modules/',
src: ['**/*.html'],
dest: 'dev/',
rename: function(dest, src) {
return dest + src.substring(0, src.indexOf('/')) + '.html';
}
}
]
}
}
});
grunt.loadNpmTasks('grunt-contrib-copy');
// Default task(s).
grunt.registerTask('default', ['copy']);
};
发布于 2015-01-18 23:04:43
不再需要仅仅使用grunt-contrib-copy
,您现在可以利用grunt.file.expandMapping
,它具有仅更改文件扩展名的选项,或者定义一个返回输出文件名的函数。
以下是jade
任务中的files
对象示例,用于将.jade模板编译为.html文件:
files: [{
expand: true,
src: "**/*.jade",
dest: "<%= distDir %>",
cwd: "<%= assetsDir %>/jade",
rename: function(dest, matchedSrcPath, options) {
// return the destination path and filename:
return (dest + matchedSrcPath).replace('.jade', '.html');
}
}]
在本例中,使用ext: '.html'
选项比使用rename
选项更容易,但我在这里使用的是rename
,这样您就可以看到它是如何工作的。
有关grunt.file docs中的ext
和rename
(以及其他)选项的更多信息。更多的例子是here和here。
发布于 2014-07-15 17:58:32
您可以简单地使用选项: expand : true,flatten: true
无需自定义重命名回调。
https://stackoverflow.com/questions/15271121
复制相似问题