如何使babel咕噜咕噜?

时间:2016-02-25 09:53:13

标签: javascript gruntjs babeljs

我安装了grunt-babel。设置gruntfile。 babel任务成功运行(我从grunt收到通知,没有错误),但它不会产生任何输出。有什么想法吗?

Gruntfile.js:

grunt.loadNpmTasks('grunt-babel');

grunt.initConfig({
...
babel: {
  options: {
    sourceMap: true,
    presets: ['es2015']
  },
  babel: {
    es6: {
      files: [
        {
          expand: true,
          cwd: '<%= yeoman.app %>/scripts/',
          src: ['**/*.es6'],
          dest: '<%= yeoman.app %>/scripts/es6-build'
          //ext: '.es6'
        }
      ]
    }
  }
},
...
});

grunt.registerTask('serve', 'Compile then start a connect web server', function (target) {
if (target === 'dist') {
  return grunt.task.run(['build', 'connect:dist:keepalive']);
}

grunt.task.run([
  'clean:server',
  'wiredep',
  'concurrent:server',
  'autoprefixer:server',
  'connect:livereload',
  'babel',
  'watch'
  ]);
 });

Package.json:

{
"name": "yo1",
"version": "0.0.0",
"dependencies": {},
"repository": {},
"devDependencies": {
  "babel-preset-es2015": "^6.5.0",
  "grunt": "^0.4.5",
  "grunt-babel": "^6.0.0",
...
}

.babelrc

{
 "presets": [
   "es2015"
 ]

}

1 个答案:

答案 0 :(得分:0)

我没有测试过该解决方案,但与我的Gruntfile.js相比,我建议删除es6babel键。例如,如果您有多个目标(服务器),请删除es6密钥并将babel替换为server

grunt.loadNpmTasks('grunt-babel');

grunt.initConfig({
...
babel: {
  options: {
    sourceMap: true,
    presets: ['es2015']
  },
  files: [
    {
      expand: true,
      cwd: '<%= yeoman.app %>/scripts/',
      src: ['**/*.js'],
      dest: '<%= yeoman.app %>/scripts/es6-build'
      //ext: '.es6'
    }
  ]
},
...
});

grunt.registerTask('serve', 'Compile then start a connect web server', function (target) {
if (target === 'dist') {
  return grunt.task.run(['build', 'connect:dist:keepalive']);
}

grunt.task.run([
  'clean:server',
  'wiredep',
  'concurrent:server',
  'autoprefixer:server',
  'connect:livereload',
  'babel',
  'watch'
  ]);
 });