javascript - grunt-babel 挂起并且不返回错误消息

标签 javascript gruntjs ecmascript-6 babeljs grunt-babel

我正在尝试使用 grunt-babel 将我的 es6 编译为 es5。当我在命令行中输入 grunt babel 时,它挂起并且从不运行 babel。它不会返回错误或崩溃只是挂起。我的 Gruntfile.js 中还有其他任务,它们运行得很好,因此 Gruntfile.js 的结构是正确的。

这是我的 Gruntfile:

'use strict';

module.exports = function(grunt) {
  require('load-grunt-tasks')(grunt);

  // initialize Grunt
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),

    'babel': {
      options: {
        sourceMap: true,
        presets: ['babel-preset-es2015']
      },
      dist: {
        files: {
          'js/survey.js': 'build/survey.js'
        }
      }
    },
    // create jshint task
    jshint: {
      dev: {
        // tell jshint what check
        src: ['Gruntfile.js', 'server.js', 'js/**/*.js', 'models/**/*.js', 'routes/**/*.js', '!build/**', '!tests/client/bundle.js', '!tests/karma_tests/bundle.js', '!js/imageMapResizer.min.js', '!js/kickstart.js', '!js/form-validator.js', '!js/imageMapResizer.js', '!js/jquery-ui.min.js', '!js/jquery.base64.js', '!js/kickstart.js'],
        options: {
          node: true,
          globals: {
            describe: true,
            it: true,
            before: true,
            after: true,
            beforeEach: true,
            afterEach: true,
            res: true
          }
        }
      },

      mocha: {
        // tell mocha where test files are
        src: ['tests/test_entry.js', '!tests/client/bundle.js', '!tests/karma_tests/bundle.js'],
        options: {
          node: true,
          globals: {
            describe: true,
            it: true,
            before: true,
            after: true,
            beforeEach: true,
            afterEach: true,
            res: true,
            expect: true
          }
        }
      },
      jasmine: {
        src: ['<%= jshint.dev.src %>', '<%= jshint.mocha.src %>'],
        options: {
          node: true,
          jasmine: true,
          globals: {
            describe: true,
            it: true,
            before: true,
            after: true,
            beforeEach: true,
            afterEach: true,
            expect: true,
            react: true
          }
        }
      },

      // create jscs task
      jscs: {
        dev: {
          // tell jscs to test the same files as jshint
          src: ['<%= jshint.dev.src %>', '<%= jshint.mocha.src %>']
        }
      }
    },

    mocha: {
      // tell mocha where the test file is
      src: ['tests/test_entry.js'],
      options: {
        node: true,
        globals: {
          describe: true,
          it: true,
          before: true,
          after: true,
          beforeEach: true,
          afterEach: true,
          res: true,
          expect: true
        }
      }
    },

    // create simplemocha task
    simplemocha: {
      dev: {
        src: ['tests/test_entry.js']
      }
    }
  });

  // register linting task
  grunt.registerTask('lint', ['jshint:dev', 'jshint:mocha', 'jshint:jasmine']);
  // register mocha test task
  grunt.registerTask('test', ['simplemocha:dev']);
  grunt.registerTask('babel', ['babel']);
  grunt.registerTask('default', ['test']);
};

这是我的 package.json:

{
  "name": "event_site_bootstrap",
  "version": "0.1.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "test": "mocha tests/test_entry.js",
    "start": "node server.js"
  },
  "author": "",
  "license": "MS-PL",
  "dependencies": {
    "bcrypt-nodejs": "latest",
    "body-parser": "^1.15.0",
    "cookie-parser": "^1.4.1",
    "dotenv": "^1.2.0",
    "eat": "^0.1.1",
    "express": "^4.13.4",
    "flash": "^1.1.0",
    "jquery": "^2.2.1",
    "multer": "^1.1.0",
    "passport": "^0.3.2",
    "passport-http": "^0.3.0",
    "sequelize": "^3.19.3",
    "sequelize-encrypted": "^0.1.0",
    "tedious": "^1.13.2"
  },
  "devDependencies": {
    "babel-cli": "^6.7.5",
    "babel-core": "^6.7.6",
    "babel-polyfill": "^6.7.4",
    "babel-preset-es2015": "^6.6.0",
    "babylon": "^6.7.0",
    "chai": "^3.2.0",
    "chai-http": "^1.0.0",
    "cli-color": "^1.1.0",
    "colors": "^1.1.2",
    "expect": "^1.9.0",
    "grunt": "^0.4.5",
    "grunt-babel": "^6.0.0",
    "grunt-cli": "^0.1.13",
    "grunt-contrib-jshint": "^0.11.3",
    "grunt-jscs": "^2.1.0",
    "grunt-mocha-cli": "^2.0.0",
    "grunt-simple-mocha": "^0.4.0",
    "load-grunt-tasks": "^3.5.0",
    "mocha": "^2.3.4"
  }
}

.babelrc 看起来像这样:

{
  "presets": ["es2015"]
}

从昨天开始我就一直在尝试解决这个问题,但是没有错误消息就很难知道问题出在哪里。我将 babel 用作字符串,但我也尝试过不使用引号,但效果完全相同。我还尝试将 presets: ['babel-preset-es2015'] 添加到 'babel' 下的选项,但它没有改变任何东西。

任何帮助将不胜感激,如果您需要更多信息或想查看其他文件,请告诉我。在此先感谢您的所有帮助!

最佳答案

找到了!删除行

grunt.registerTask('babel', ['babel']);

它会导致无限循环。您已经有一个名为 babel 的任务,因此该行只是用无限任务重新定义了它。

此外,您可能希望 'js/survey.js': 'build/survey.js''build/survey.js': 'js/survey.js'。格式为target: source

关于javascript - grunt-babel 挂起并且不返回错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36581567/

相关文章:

javascript - Eclipse Neon 中的 ES6 支持在哪里?

Javascript 继续或取消提交按钮

javascript - 如何在 Firefox 扩展中更改状态栏面板的背景颜色

javascript - jQuery函数仅执行一次?

gruntjs - 从我的 Gruntfile 中的 JSHint 中排除子目录?

character-encoding - grunt-contrib-sass : incompatible character encodings: UTF-8 and CP850

javascript - 为什么在下面的示例中我必须同时使用剩余参数和扩展运算符?

Javascript - 简化为对象

javascript - Grunt watch -- 长时间延迟

javascript - 前导下划线在 es6 类中转译错误