gulp-uglify 需要一个对象,但未提供对象

问题描述 投票:0回答:1

我有一个旧项目,如果

condition
为 true,则必须添加一个过滤器以从对象数组中删除项目。尝试运行时出现以下错误
gulp build

[14:44:03] Using gulpfile /runner/_work/MyLegacyProject/gulpfile.js
[14:44:03] Starting 'build'...
[14:44:03] Finished 'build' after 9.03 ms
[14:44:06] gulp-uglify expects an object, non-object provided

events.js:174
      throw er; // Unhandled 'error' event
      ^
Error
    at new JS_Parse_Error (eval at <anonymous> (/runner/_work/MyLegacyProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:1534:18)
    at js_error (eval at <anonymous> (/runner/_work/MyLegacyProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:1542:11)
    at croak (eval at <anonymous> (/runner/_work/MyLegacyProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:2089:9)
    at token_error (eval at <anonymous> (/runner/_work/MyLegacyProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:2097:9)
    at unexpected (eval at <anonymous> (/runner/_work/MyLegacyProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:2103:9)
    at expr_atom (eval at <anonymous> (/runner/_work/MyLegacyProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:2630:9)
    at maybe_unary (eval at <anonymous> (/runner/_work/MyLegacyProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:2792:19)
    at expr_ops (eval at <anonymous> (/runner/_work/MyLegacyProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:2827:24)
    at maybe_conditional (eval at <anonymous> (/runner/_work/MyLegacyProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:2832:20)
    at maybe_assign (eval at <anonymous> (/runner/_work/MyLegacyProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:2856:20)
Emitted 'error' event at:
    at Duplexer.onerror (/runner/_work/MyLegacyProject/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:640:52)
    at Duplexer.emit (events.js:198:13)
    at DestroyableTransform.<anonymous> (/runner/_work/MyLegacyProject/node_modules/gulp-streamify/src/index.js:20:12)
    at DestroyableTransform.emit (events.js:203:15)
    at onwriteError (/runner/_work/MyLegacyProject/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:443:12)
    at onwrite (/runner/_work/MyLegacyProject/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:470:11)
    at WritableState.onwrite (/runner/_work/MyLegacyProject/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:180:5)
    at DestroyableTransform.afterTransform (/runner/_work/MyLegacyProject/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:93:3)
    at DestroyableTransform.minify [as _transform] (/runner/_work/MyLegacyProject/node_modules/gulp-uglify/minifier.js:71:14)
    at DestroyableTransform.Transform._read (/runner/_work/MyLegacyProject/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:184:10)

下面是我进行更改的代码段。很明显,

array.filter
导致了这个问题。有什么建议吗?

const statusTypes = [
            {
                "Id": 1,
                "Name": "Status 1"
            },
            {
                "Id": 2,
                "Name": "Status 2"
            },
            {
                "Id": 3,
                "Name": "Status 3"
            }
        ];

console.log(statusTypes);

// throws error when gulp build is run
// const myStatuses = (condition === true) ? statusTypes.filter((i) => i.Id !== 3) : statusTypes;

// works with gulp build
const myStatuses = (condition === true) ? statusTypes : statusTypes;


console.log(myStatuses);

以下是我在package.json中看到的版本

    "browserify": "^10.2.4",
    "gulp-react": "^3.0.1",
    "gulp-streamify": "^1.0.2",
    "gulp-uglify": "^1.4.2"
javascript gulp gulp-uglify
1个回答
0
投票

您的

gulp-uglify
 中的 
package.json 版本已有 9 岁了,因此可能是 ES6 兼容性问题。

您已经演示了 gulp 构建无需

filter()
函数即可工作,但考虑到
Array.filter()
是 ES5,它应该仍然可以工作,因此您应该尝试传递匿名 函数表达式 而不是 箭头函数表达式 ,这是 ES6 .

例如:

const myStatuses = (condition === true) ? statusTypes.filter(function(i) {return i.Id !== 3;} ) : statusTypes;
© www.soinside.com 2019 - 2024. All rights reserved.