Francesco Bellavita
Francesco Bellavita

Reputation: 145

express-minify can not uglify es6 script, how to use babel to pass transpiled script to express-minify?

I'm trying to minify my scripts with express-minify middleware but i get an error from uglify module: SyntaxError: Unexpected token: name (n) The problem is that uglify can not parse es2015 scripts. There is a way to transpile my script in a middleware before minifying? my code:

    app.use(compression());
    app.use(minify({
      cache: "./cache",
      uglifyJsModule: uglifyJs,
      errorHandler: function (errorInfo, callback) {
        console.log(errorInfo);
        if (errorInfo.stage === 'compile') {
          callback(errorInfo.error, JSON.stringify(errorInfo.error));
          return;
        }
        callback(errorInfo.error, errorInfo.body);
      }
    }));
    app.use(express.static('public', config.staticOptions));

Upvotes: 1

Views: 251

Answers (1)

Francesco Bellavita
Francesco Bellavita

Reputation: 145

Solved with uglify-es:

var uglifyEs = require('uglify-es');
app.use(compression());
app.use(minify({
  cache: "./cache",
  uglifyJsModule: uglifyEs,
  errorHandler: function (errorInfo, callback) {
    console.log(errorInfo);
    if (errorInfo.stage === 'compile') {
      callback(errorInfo.error, JSON.stringify(errorInfo.error));
      return;
    }
    callback(errorInfo.error, errorInfo.body);
  }
}));
app.use(express.static('public', config.staticOptions));

Upvotes: 2

Related Questions