Minification May No Longer Work if JS Code Contains eval() or with() on DXP 7.0

This article serves as a reference concerning an intended change in the minification process which might break some JavaScript code.

After upgrading from Liferay Portal 6.2 to Liferay DXP 7.0, any JavaScript code that uses eval() or with() may no longer work as expected. After the minification process, variable names have been renamed, causing the data variable inside an eval expression to be undefined.

Here is an example from a view.jsp file:

<script>
var view = {

"mRender": function ( data, type, full ) {
                return eval("myFunction(data)");
}
}

function myFunction(data){
alert(data);
}

view["mRender"]("Test OK")    ;

</script>

Executing this code snippet (which works in Portal 6.2) will then throw the following error in Liferay DXP 7.0:

Uncaught ReferenceError: data is not defined
    at eval (eval at mRender ((index):1), :1:20)
    at Object.mRender ((index):13)
    at (index):13

Resolution

Workarounds Available

There are two possible workarounds you may execute.

  1. Refactor the JavaScript code to avoid using eval() or with().
  2. Set com.liferay.portal.servlet.filters.strip.StripFilter=false in the portal-ext.properties file.

Any one of these workarounds should resolve the issue.

Additional Information

See LPS-44366 for more information about switching to Google Closure Compiler to compress JS files in Liferay DXP 7.0 and Google Closure Compiler restrictions about using eval() and with().

这篇文章有帮助吗?
1 人中有 1 人觉得有帮助