Grunt Add multiple input/output directories
I am using grunt-babel
to transform my react-jsx
files into .js
.
I am planning to write a grunt task for this. Currently, I have below
module.exports = function( grunt ) {
require('load-grunt-tasks')(grunt);
grunt.initConfig( {
babel : {
options : {
plugins : ['transform-react-jsx'],
presets: ['es2015', 'react']
},
client : {
expand : true,
cwd : './react_demo/test/jsx/common',
src : ['*.jsx'],
dest : './react_demo/static/react/components',
ext : '.js'
}
}
} );
grunt.loadNpmTasks('grunt-babel');
grunt.registerTask('default', ['babel:client']);
};
In my above task what I am trying to do is, I am transforming all my JSX
files present in folder ./react_demo/test/jsx/common
into ./react_demo/static/react/components
.
Now, in my application in future we can have multiple folder each having there own-set of JSX files which will be going into different destination folders.
We can have folders like:
Now, how can I specify multiple src/dest
directories and map them together? I tried giving an array to src/dest but it complained with below error:
Warning: Path must be a string. Received [ './react_demo/cartridge/scripts/jsx/common' ] Use --force to continue.
据我所知,你将不得不为每个新目的地指定一个新的babel目标:
module.exports = function( grunt ) {
require('load-grunt-tasks')(grunt);
grunt.initConfig( {
babel : {
options : {
plugins : ['transform-react-jsx'],
presets: ['es2015', 'react']
},
client : {
expand : true,
cwd : './react_demo/test/jsx/common',
src : ['*.jsx'],
dest : './react_demo/static/react/components',
ext : '.js'
},
common1 : {//add more targets
expand : true,
cwd : './react/test/jsx/common1',
src : ['*.jsx'],
dest : './react/static/react/components1',
ext : '.js'
}//etc..
}
} );
grunt.loadNpmTasks('grunt-babel');
grunt.registerTask('default', ['babel:client','babel:common1']);//reg new targets
};
Few days back, I have faced this problem.You can achieve it by using config file.
write a configuration file (Eg : main-folder/grunt/config.js) file to register some shortcut, variables to make your grunt tasks more dynamic.
Example:
var demoConfig = {
'module1': 'script/module1',
'module2': 'script/module2',
'module3': 'script/module3'
};
module.exports = demoConfig ;
}
And import this config in each task with : var config = require('../config');
In gruntfile.js:
module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-task');
var getTasks = function() {
var jsConfig = require('../config/demoConfig'),
jsTasks = {};
for (var key in jsConfig) {
jsTasks[key] = {
files: [{
expand: true,
cwd: './react_demo/test/jsx/' + jsConfig[key],
src: '**/*.jsx',
dest: './react_demo/static/react/' +jsConfig[key],
ext: '.js'
}]
};
}
return jsTasks;
};
grunt.config('babel', getTasks());};
for your scenario, you can change some functionality here and get it done.
I hope this help.
for more info read this: dynamic grunt task with babel
链接地址: http://www.djcxy.com/p/52110.html上一篇: babel不会在父文件夹中转发jsx文件
下一篇: Grunt添加多个输入/输出目录