I have an old project and have to add a filter to remove an item from the array of objects if the condition
is true. I am getting the below error when trying to run 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)
Below is the code segment where I am making the changes. It is pretty obvious that, array.filter
is causing the issue. Any suggestions?
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);
Below are the versions I see in package.json
"browserify": "^10.2.4",
"gulp-react": "^3.0.1",
"gulp-streamify": "^1.0.2",
"gulp-uglify": "^1.4.2"
The gulp-uglify
version in your package.json
is 9 years old so it could be an ES6 compatibility issue.
You have demonstrated the gulp build works without the filter()
function but given that Array.filter()
is ES5 it should still work so you should try to pass an anonymous function expression instead of the arrow function expression, which is ES6.
For example:
const myStatuses = (condition === true) ? statusTypes.filter(function(i) {return i.Id !== 3;} ) : statusTypes;