javascript 如何让 Grunt-Contrib-Copy 复制相对于给定源路径的文件/目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22697919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get Grunt-Contrib-Copy to copy files/directories relative to given source path
提问by micahblu
First time using this task and what I'm trying to achieve is the following:
第一次使用这个任务,我想要实现的是以下内容:
copy all directories/files from src/js/bower_components/*
to build/assets/js/vendor/
将所有目录/文件复制src/js/bower_components/*
到build/assets/js/vendor/
I've tried using cwd
property but it doesn't work at all when I use it.. I've set it to: src/js/bower_components/
我试过使用cwd
属性,但是当我使用它时它根本不起作用..我已将其设置为:src/js/bower_components/
From src
从源
.
├── Gruntfile
└── src
└── js
└── bower_components
└── jquery
I currently get:
我目前得到:
.
├── Gruntfile
└── build
└── assets
└── js
└── vendor
src
└── js
└── bower_components
└── jquery
What I'd like
我想要什么
.
├── Gruntfile
└── build
└── assets
└── js
└── vendor
└──jquery
Here's my current grunt task
这是我目前的 grunt 任务
copy: {
main: {
src: 'src/js/bower_components/*',
dest: 'build/assets/js/vendor/',
expand: true,
}
},
Thanks for any help
谢谢你的帮助
回答by Kosmotaur
I've set up an example project with tree like this:
我已经用这样的树建立了一个示例项目:
.
├── Gruntfile.js
├── package.json
└── src
└── js
└── foo.js
Using the below Gruntfile:
使用以下 Gruntfile:
module.exports = function(grunt) {
require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);
grunt.initConfig({
copy : {
foo : {
files : [
{
expand : true,
dest : 'dist',
cwd : 'src',
src : [
'**/*.js'
]
}
]
}
}
});
grunt.registerTask('build', function(target) {
grunt.task.run('copy');
});
};
This gave me this structure:
这给了我这个结构:
.
├── Gruntfile.js
├── dist
│?? └── js
│?? └── foo.js
├── package.json
└── src
└── js
└── foo.js
When I had changed cwd
so that the Gruntfile read:
当我更改cwd
以便 Gruntfile 读取时:
module.exports = function(grunt) {
require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);
grunt.initConfig({
copy : {
foo : {
files : [
{
expand : true,
dest : 'dist',
cwd : 'src/js',
src : [
'**/*.js'
]
}
]
}
}
});
grunt.registerTask('build', function(target) {
grunt.task.run('copy');
});
};
I got this dir structure:
我得到了这个目录结构:
.
├── Gruntfile.js
├── dist
│?? └── foo.js
├── package.json
└── src
└── js
└── foo.js
So it seems like cwd
does what you need. Maybe you left src
at src/js/bower_components/*
when setting cwd
to src/js/bower_components
? In that case, src
should read something like **/*.js
, but depending on what you really need.
所以它似乎cwd
可以满足您的需求。也许你src
在src/js/bower_components/*
设置为时离开cwd
了src/js/bower_components
?在这种情况下,src
应该阅读类似**/*.js
,但取决于您真正需要的内容。