Rails Gem Uglifier 4.2 不支持 async/await,如何修复

huangapple go评论63阅读模式
英文:

Rails Gem Uglifier 4.2 does not support async/await, how to fix

问题

I have a Ruby on Rails project which uses some Javascript in the frontend. In development mode this runs fine, but when trying to build a Docker container (which compiles the assets) Uglifier gives an error Uglifier::Error: Unexpected token: operator (=). The offending code is:

var MyController = (function() {
    let self = undefined;
    MyController = class MyController {

        constructor() {
            self = this;
        }

        init() {...}
        ... // lots more code

        loadAsync = async (targetElement, num) => {       // <= this is the offending line
            await this.delay(5);
            $.ajax({
                url: `...`,
                method: 'GET',
                dataType: 'HTML',
                complete(response) {
                    ...
                }
            });
        };

        delay = ms => new Promise(res => setTimeout(res, ms));

        ... 
英文:

I have a Ruby on Rails project which uses some Javascript in the frontend. In development mode this runs fine, but when trying to build a Docker container (which compiles the assets) Uglifier gives an error Uglifier::Error: Unexpected token: operator (=). The offending code is:

var MyController = (function() {
    let self = undefined;
    MyController = class MyController {

        constructor() {
            self = this;
        }

        init() {...}
        ... // lots more code

        loadAsync = async (targetElement, num) =&gt; {       // &lt;= this is the offending line
            await this.delay(5);
            $.ajax({
                url: `...`,
                method: &#39;GET&#39;,
                dataType: &#39;HTML&#39;,
                complete(response) {
                    ...
                }
            });
        };

        delay = ms =&gt; new Promise(res =&gt; setTimeout(res, ms));
     
        ... 

I know that Uglifier uses UglifyJS under the hood, and that this used to have problems with async/await. Are there versions available now that have this problem fixed, and if so, how do I install a working version? I use the newest Uglifier gem version available (4.2), using the options harmony: true, mangle: false.

答案1

得分: 2

Uglifier readme 中写道:

> UglifyJS 仅支持 ES5。如果需要压缩 ES6,ruby-terser 是一个更好的选择。

切换到 ruby-terser 是一种选择。您还可以使用 Babel 将 ES6 转译为 ES5。

英文:

The Uglifier readme says

> UglifyJS only works with ES5. If you need to compress ES6, ruby-terser
> is a better option.

Switching to ruby-terser is an option. You could also use Babel to transpile ES6 into ES5.

huangapple
  • 本文由 发表于 2023年5月17日 19:02:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76271387.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定