意外的令牌关键字"函数",预期的双关语",»



这是我尝试运行应用后的错误消息

[15:32:01]正在启动"default"。。。

[15:32:01]水管工发现未处理的错误:

GulpUglifyError:无法缩小JavaScript

引起原因:SyntaxError:意外的令牌关键字«function»,应为穿孔«,»

文件:/home/*******/myfile.js行:81

我的gull文件:

const gulp = require('gulp')
const uglify = require('gulp-uglify')
const babel = require('gulp-babel')
const plumber = require('gulp-plumber')
const del = require('del')
gulp.task('default', ['conf'], () => gulp.src('src/app/**/*.js')
.pipe(plumber())
.pipe(babel())
.pipe(uglify())
.pipe(gulp.dest('dist')))
gulp.task('conf', ['package'], () => gulp.src('./src/conf/*').pipe(gulp.dest('dist/conf')))
gulp.task('package', ['clean'], () => gulp.src(['./package.json']).pipe(gulp.dest('dist/')))
gulp.task('clean', () => del(['dist/**/*']))

如果我正确理解了这个问题,我的代码必须由babel转换到es5,所以这里是.babelrc

{
"plugins": [
"transform-class-properties", 
"babel-plugin-transform-runtime",
"transform-es2015-shorthand-properties",
["babel-plugin-root-import", {
"rootPathSuffix": "src/app"
}]
],
"presets": ["es2015"]
}

以下是myfile.js.中第81行(注释开始处(的代码

export default class Scraper {
// ...
/**
* Converts a html string to a cheerio object
* @param {String} html The html string
* @return {Object} The cheerio object
*/
htmlToDom(html) {
// https://bugs.chromium.org/p/v8/issues/detail?id=2869
// https://github.com/cheeriojs/cheerio/issues/263
if(typeof global.gc === 'function') {
global.gc()
html = (' ' + html).substr(1)
}
return cheerio.load(html)
}
static absolute(location, relative) {
//...
}

首先,我认为可能是人手不足的版本导致了问题,但我安装了transform-es2015-shorthand属性,在一个旧的项目中,我使用了相同的框架,没有这个插件。

更新

"dependencies": {
"babel-plugin-transform-class-properties": "^6.24.1",
"bunyan": "^1.8.10",
"cheerio": "^1.0.0-rc.2",
"colors": "^1.1.2",
"js-yaml": "^3.12.0",
"pg": "^7.4.3",
"puppeteer": "^1.5.0",
"sequelize": "^4.38.0",
"sequelize-connect": "^2.1.1"
},
"devDependencies": {
"ava": "^0.19.1",
"babel-plugin-root-import": "^6.1.0",
"babel-plugin-transform-es2015-shorthand-properties": "^6.24.1",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-latest": "^6.24.1",
"babel-register": "^6.24.1",
"coveralls": "^2.13.0",
"del": "^2.2.2",
"esdoc": "^0.5.2",
"gulp": "^3.9.1",
"gulp-babel": "^6.1.2",
"gulp-plumber": "^1.1.0",
"gulp-uglify": "^2.1.2",
"mkdir-recursive": "^0.4.0",
"nyc": "^11.2.0",
"sinon": "^2.1.0",
"standard": "^10.0.1",
"wait-on": "^3.2.0"
},

卸载并重新安装gulp-uglify对我来说很有用。

您可能需要尝试添加babel-plugin-transform-async-to-generator

  1. yarn add babel-plugin-transform-async-to-generator -D
  2. .babelrc:"plugins": ["babel-plugin-transform-async-to-generator"]

最新更新