问题描述
这是关于使用 Grunt.js 的动态文件映射的一般问题,但为了示例目的,我将尝试在项目中构建咖啡文件动态结构:
在这里,我可以有多个(动态)目标文件夹具有不同的深度。查找咖啡文件仍然很方便,随时可以匹配 ** / coffee / *。coffee
。
$ b b
我要达到的目的是将 dest 属性相对置于匹配的咖啡文件:
- 查找
** / coffee / *。coffee
- 编译为
../ js / *。js
而不是相对于项目文件夹(Gruntfile)。
coffee:{
compile:{
files:[
{
expand:true,
src:['** / coffee / *。 '],
dest:'../js/',//这将不工作!但我希望它可以:)
ext:'.js'
}
]
}
}
解决方案认为这是一个很好的主意。听起来像每个目标应该是一个自己的存储库。即使我们忽略了,你会有其他问题,如难以告诉目标和非目标文件夹分开。例如,您当前的模式可能匹配
node_modules /
目录中的CoffeeScript文件。
您的目标具有不同的文件夹结构(即
coffee /
和js /
不在同一级别)。再次,它听起来像他们是不同的项目,并应该有自己的回购。
这就是说,如果你真的,真的必须这样做,是实现这一目标的几种方法。
首先,正常方法是手动指定多个目标。我将在这里使用
Gruntfile.coffee
语法:咖啡:
target1:
expand:true
cwd:'target1 / coffee'
src:'** / *。coffee'
dest:'target1 / js'
ext:'.js'
targetX:
expand:true
cwd:'targetX / some-folder / coffee'
src:'** / *。 coffee'
dest:'targetX / some-folder / js'
ext:'.js'
b $ b但是,如果你确定你需要有动态目标,并且不介意将不想要的文件夹列入黑名单,可以尝试这样:
coffee:do - >
targets = {}
目标在grunt.file.expand'** / coffee','!node_modules / **'
targets [target.split('/',1) [0]] =
expand:true
cwd:target
src:'** / *。coffee'
dest:target +'/../js'
ext:'.js'
targets
以前的片段,有一些增加的不确定性。您可以通过使用
targets /
文件夹包含所有目标,并使用targets / ** / coffee $ c>来清除黑名单$ c>作为模式。
或者您可以使用
grunt.file.expandMapping
:coffee:
compile:
files:grunt.file.expandMapping ['** / coffee / ** / *。coffee','!node_modules / **'],'',
expand:true
ext:'.js'
rename:(base,src)
src.replace'/ coffee /','/ js /'
看起来很简单(和丑陋),它是最慢的选择,感觉最错了。
那么。这是可能的,但很可能不是你真正想要的。
This is a generic question about dynamic file mapping with Grunt.js, but for the example purpose, I'll be trying to build coffee files in a project with a dynamic structure :
Here, I can have multiple (dynamic) target folders with a different depth. Finding coffee files remain easy, it will match
**/coffee/*.coffee
anytime.What I'm trying to achieve, is making the dest property relative to the matched coffee file :
- find
**/coffee/*.coffee
- compile to
../js/*.js
Instead of making it relative to the project folder (Gruntfile).
coffee: {
compile: {
files: [
{
expand: true,
src: ['**/coffee/*.coffee'],
dest: '../js/', // This won't work ! But I wish it could :)
ext: '.js'
}
]
}
}
How would you achieve this ?
I don't think that's a very good idea. It sounds like each target should perhaps be a repository of its own. Even if we ignore that, you'll have other problems, such as difficulty telling target and non-target folders apart. For example, your current pattern would potentially match CoffeeScript files in the node_modules/
directory.
I also find it weird that your targets have differing folder structures (i.e. coffee/
and js/
are not on the same level). Again, it sounds like they're different projects, and should have their own repos.
That being said, if you really, really have to do it like that, there are a few ways to accomplish this.
First, the "normal" way to do it would be to simply specify multiple targets manually. I'll be using the Gruntfile.coffee
syntax here:
coffee:
target1:
expand: true
cwd: 'target1/coffee'
src: '**/*.coffee'
dest: 'target1/js'
ext: '.js'
targetX:
expand: true
cwd: 'targetX/some-folder/coffee'
src: '**/*.coffee'
dest: 'targetX/some-folder/js'
ext: '.js'
However if you're certain you need to have dynamic targets, and don't mind blacklisting unwanted folders, perhaps try something like this:
coffee: do ->
targets = {}
for target in grunt.file.expand '**/coffee', '!node_modules/**'
targets[target.split('/', 1)[0]] =
expand: true
cwd: target
src: '**/*.coffee'
dest: target + '/../js'
ext: '.js'
targets
This will essentially do the same as the previous snippet, with some added uncertainty. You could perhaps get rid of the blacklist by having a targets/
folder contain all the targets, and using targets/**/coffee
as the pattern.
Alternatively you could use grunt.file.expandMapping
:
coffee:
compile:
files: grunt.file.expandMapping ['**/coffee/**/*.coffee', '!node_modules/**'], '',
expand: true
ext: '.js'
rename: (base, src) ->
src.replace '/coffee/', '/js/'
While it may look kind of simple (and ugly), it's the slowest option and feels the most wrong.
So there. It's possible, but most likely not what you really want.
这篇关于动态文件映射与相对目的地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!