1

I have a problem with Google Closure Compiler. I want to compile NodeJS code but it seems that Google CC have some problems with require.

One example :

'use strict';

var moment = require('moment');
var crypto = require('crypto');
moment.locale('fr');

//TEMP CONFIG
var oddsProb = 0.85;


class Match {

    constructor(game, date, t1, p1, t2, p2){
        this._isLive = false;
        this.t1 = t1;
        this.t2 = t2;
        this.p1 = p1;
        this.p2 = p2;
        this.p0 = 100-p1-p2;
        this.o1 = (p1==0?0:(oddsProb*100/p1)).toFixed(2);
        this.o2 = (p2==0?0:(oddsProb*100/p2)).toFixed(2);
        this.o0 = (this.p0==0?0:(oddsProb*100/this.p0)).toFixed(2);
        this.date = moment(date);
        this.gameType = game;
        this.fromNowString = this.date.fromNow();
        this._hashCode = crypto.createHash('md5').update(t1+t2+this.date.format("YMMDD")+this.gameType).digest("hex");
        //Validation date
        if(moment().isBefore(date)){
            this._isLive = true;
        }
    }

    serialize(){
        return {
            _id: this._hashCode.substring(0,24),
            date: this.date.valueOf(),
            t1: this.t1,
            p1: this.p1,
            t2: this.t2,
            p2: this.p2,
            p0: this.p0,
            o1: this.o1,
            o2: this.o2,
            o0: this.o0,
            gameType: this.gameType
        }
    }

    static deserialize(s){
        return new Match(s.gameType, moment(s.date), s.t1, s.p1, s.t2, s.p2);
    }

    isLive(){
        return this._isLive;
    }
}

module.exports = Match;

And in other file :

var Match = require('./match');

Google CC gives me this error : ERROR - Variable Match first declared in /src/public/routes.js

My gulpfile :

var gulp = require('gulp');
var source = './src'; // dossier de travail
var destination = './dist'; // dossier à livrer
var closureCompiler = require('google-closure-compiler').gulp();


gulp.task('js-compile', function () {
    return gulp.src(source+'/**/*.js', {base: source})
        .pipe(closureCompiler({
            compilation_level: 'SIMPLE',
            warning_level: 'VERBOSE',
            js_output_file: 'output.min.js'
        }))
        .pipe(gulp.dest(destination));
});

Any idea? Thank you!

izanagi_1995
  • 94
  • 1
  • 8
  • 1
    Why are you trying to use Closure Compiler on JS designed to run on NodeJS? – Quentin Jul 18 '16 at 09:33
  • 1
    I want to reduce and optimize my code. I'm wrong? – izanagi_1995 Jul 18 '16 at 09:46
  • Why do you want to reduce it? In what way do you want to optimize it? Normally people do that so it is faster to send it over the network. The code isn't being sent over the network, it is running on the server. – Quentin Jul 18 '16 at 09:47
  • 1
    Yes, I was more thinking about run time reduction. Wrong? EDIT : Saw that Google Closure Compiler also "optimizes" code – izanagi_1995 Jul 18 '16 at 09:49
  • 1
    @Quentin The Closure Compiler is a tool for making JavaScript download **and run faster** https://developers.google.com/closure/compiler/ – Tom Jul 18 '16 at 11:22
  • 1
    There main use case for Closure-Compiler on nodejs code is static analysis including type-checking. It currently does not work well with nodejs style code. – Chad Killingsworth Jul 18 '16 at 12:36
  • "It currently does not work well with nodejs style code" - That's wrong. I use it extensively in Electron with Node.js. – Johann Mar 29 '19 at 09:36

0 Answers0