[英]Dynamic dest for ES6 Grunt configuration
I'm trying to configurate Grunt to start using ES6, so that it transpire each component's JS file to it's own folder. 我正在尝试配置Grunt以开始使用ES6,以便将每个组件的JS文件传输到其自己的文件夹中。
I have the next directory structure: 我有下一个目录结构:
Components └──footer │ └──js │ └──footer.jsx └──header │ └──js │ └──header.jsx └──slider └──js └──slider.jsx
and I need to have the next result: 我需要得到下一个结果:
Components └──footer │ └──js │ │ └──footer.jsx │ └──compiled │ └──footer.js └──header │ └──js │ │ └──header.jsx │ └──compiled │ └──header.js └──slider └──js │ └──slider.jsx └──compiled └──slider.js
Currently I have the following configuration: 目前,我有以下配置:
babel: {
options: {
sourceMap: true,
presets: ['env']
},
dist: {
files: [{
expand: true,
cwd: "src/Components",
src: ["**/*.jsx"],
dest: 'compiled',
ext: '.js',
extDot: 'first'
}]
}
}
and it puts all compiled files into one common folder. 并将所有编译文件放入一个公用文件夹。
How it should be configured to have compiled JS for each own component directory? 应该如何配置它为每个自己的组件目录编译JS?
When building the files object dynamically you can utilize grunts rename property to generate and return a new destination path. 动态构建文件对象时 ,可以利用grunts 重命名属性来生成并返回新的目标路径。 The
rename
function is where you can handle the logic for this this type of requirement. 您可以在其中使用
rename
功能来处理此类需求的逻辑。
Gruntfile.js Gruntfile.js
// ...
babel: {
options: {
sourceMap: true,
presets: ['env']
},
dist: {
files: [{
expand: true,
cwd: 'src/Components',
src: ["**/*.jsx"],
dest: 'src/Components', // <--- This should be the same value as `cwd`
rename: function (dest, src) {
var destParts = dest.split('/'),
srcParts = src.split('/');
srcParts.splice((srcParts.length - 2), 1, 'compiled');
return destParts.concat(srcParts).join('/');
},
ext: '.js',
extDot: 'first'
}]
}
}
// ...
Notes 笔记
rename
function accepts two arguments, namely src
and dest
. rename
函数接受两个参数,即src
和dest
。 dest
and src
path Strings are split
into Arrays of items. dest
和src
路径字符串均被split
为项目数组。 Each item of each resultant Array is part of the path because split('/')
uses /
as the separator. split('/')
使用/
作为分隔符。 We then utilize splice()
to replace the second from last item in the Array, (which is the js
part of the src
path), with the new directory name, ie compiled
. 然后,我们使用
splice()
将Array中最后一项(即src
路径的js
部分splice()
中的第二项替换为新的目录名称,即compiled
。
Finally we concat
'enate all items of the new srcParts
Array with the parts of the dest
Array. 最后,我们
concat
“enate新的所有项目srcParts
磁盘阵列和的部分dest
数组。 This Array is turned back to a String joining items with /
to form the new destination path. 此数组转回一个字符串,该字符串用
/
连接各项以形成新的目标路径。 This new path String is returned. 返回此新路径String。
The value of the dest
property must match that of the cwd
property in the files
Object. dest
属性的值必须与files
Object中的cwd
属性的值匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.