0

我有一个包含空传播运算符的 JS 表达式,但是在应用 gulp 作业来缩小 JS 时失败。有谁知道如何保持这种语法(不做 if/else 或使用三元运算符(表达式?真:假)?

我的表情

let tableTitle = $(config.$table[0])?.siblings('.title-section')[0]?.innerText;
    

吞咽错误:

TypeError in plugin "gulp-minify"
Message: dev\js\app_delivery.js
Cannot read property 'replace' of undefined (line: undefined, col: undefined, pos: undefined
Details:
    domainEmitter: [object Object]
    domain: [object Object]
    domainThrown: false

我的 gulp 任务导致错误:

gulp.task('JS-libs', function() {
    return gulp.src(config.modules.JS.libs)
    .pipe(concat('libs.js'))
    .pipe(minifyJS())
    .pipe(gulp.dest(config.path.dest.rootJS))
});

minifyJS 函数:

function minifyJS() {
    return gulpMinifyJS({
        ext:{
            src: '.js',
            min:'.min.js'
        }
    });
}

还有我关于 gulp 的 package.json 配置:

"gulp": "^4.0.2",
    "gulp-babel": "^8.0.0",
    "gulp-concat": "^2.6.1",
    "gulp-cssmin": "^0.2.0",
    "gulp-minify": "^3.1.0",
    "gulp-remove-logging": "^1.2.0",
    "gulp-rename": "^1.4.0",
    "gulp-replace": "^1.0.0",
    "uglifyjs": "^2.4.11"

在此先感谢您的帮助

4

1 回答 1

0

我找到了一种解决方法,它的好处是 gulp 没有错误,并且当找不到要搜索的 HTML dom 元素时,一个表达式不会崩溃:

try {
            let tableTitle = $(config.$table[0]).siblings('.title-section')[0].firstChild.nodeValue;
            ...
        }
        catch(error) {
            ...
            console.error(error);
        }

但是,如果有人知道如何回答我最初的问题,我将不胜感激。

于 2020-10-08T15:18:31.543 回答