【问题标题】:Using the null propagation operator fails in gulp tasks在 gulp 任务中使用空传播运算符失败
【发布时间】:2021-01-23 13:44:16
【问题描述】:

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

我的表情

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

gulp 错误:

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"

提前感谢您的帮助

【问题讨论】:

    标签: javascript gulp null-propagation-operator


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-25
      • 1970-01-01
      • 1970-01-01
      • 2015-02-26
      • 2015-06-13
      • 2023-03-26
      • 2017-12-04
      • 2021-05-10
      相关资源
      最近更新 更多