gulp-babel 在删除 node_modules 和 运行 后再次出现语法错误 npm install

gulp-babel get syntax error after removed node_modules and run npm install again

我的 gulp 构建脚本中有 gulp-babel 任务,在我删除 node_modules 目录并再次 运行 npm install 之前它运行良好。 它返回

SyntaxError: j.js: Unexpected token (790:10)

错误日志指出错误标记如下:

  789 |     data,
> 790 |     async = true,
      |           ^
  791 |     cache = 'no-cache',
  792 |     method = 'GET',
  793 |     headers = {},

如果我直接使用 cli babel j.js 编译 j.js,将不会收到错误消息并按预期返回编译代码。

我的gulp任务代码:

const gulp = require( 'gulp' );
const { babel } = require( 'gulp-load-plugins' )();

gulp.task( 'babel', () => {
    return gulp.src( [
        '.tmp/j.js'
    ] ).pipe( babel() ).pipe( gulp.dest( '.tmp' ) );
} );

我的.babelrc :

{
    "presets" : [ "es2016" ],
    "plugins" : [
        "transform-es2015-arrow-functions",
        "transform-es2015-object-super",
        "transform-es2015-parameters",
        "transform-object-assign",
        "transform-es2015-block-scoping",
        "transform-es2015-shorthand-properties",
        "transform-es2015-block-scoped-functions",
        "transform-es2015-for-of",
        "transform-es2015-destructuring",
        [ "transform-es2015-classes", { "loose" : true } ],
        [ "transform-es2015-spread", { "loose" : true } ],
        [ "transform-es2015-template-literals", { "loose" : true } ]
    ]
}

我的 npm 依赖项:

"devDependencies": {
    "babel-cli": "^6.14.0",
    "gulp-babel": "^6.1.2",
    "babel-plugin-transform-es2015-arrow-functions": "^6.8.0",
    "babel-plugin-transform-es2015-block-scoped-functions": "^6.8.0",
    "babel-plugin-transform-es2015-block-scoping": "^6.10.1",
    "babel-plugin-transform-es2015-classes": "^6.9.0",
    "babel-plugin-transform-es2015-destructuring": "^6.9.0",
    "babel-plugin-transform-es2015-for-of": "^6.8.0",
    "babel-plugin-transform-es2015-object-super": "^6.8.0",
    "babel-plugin-transform-es2015-parameters": "^6.11.4",
    "babel-plugin-transform-es2015-shorthand-properties": "^6.8.0",
    "babel-plugin-transform-es2015-spread": "^6.8.0",
    "babel-plugin-transform-es2015-template-literals": "^6.8.0",
    "babel-plugin-transform-object-assign": "^6.8.0",
    "babel-preset-es2016": "^6.11.3",
    "colors": "^1.1.2",
    "del": "^2.2.2",
    "gulp": "^3.9.1",
    "gulp-concat": "^2.6.0",
    "gulp-file-include": "^0.14.0",
    "gulp-load-plugins": "^1.2.4",
    "gulp-uglify": "^2.0.0",
    "gulp-watch": "^4.3.9",
    "require-dir": "^0.3.0",
    "run-sequence": "^1.2.2",
    "uglify-js": "github:mishoo/UglifyJS2#harmony"
  },

我又来答题了

Babeljs 6.14.0 开始支持在 ES7 中声明的 "async function"。单词 "async" 成为编译器的关键字,所以我不能使用 "async" 作为变量名。

那我把"async = true"改成"sync = false"就解决了这个问题

我在 github 上向 Babeljs 报告了一个问题。我认为这个问题已经解决了。