0

I am using requirejs to build frontend on play 2.2 framework. play provides great development/stage code difference for this case. In devel mode I am working with browser based requirejs and in stage I am using precompiled with r.js version of the project. But one feature fails - is it possible to distinguish on javascript side is it development mode or not and remove part of code during compilation or something like:

#ifdef DEVELOPMENT
code in Development only
#endif
2
  • 1
    javascript code doesn't run on the server side... you can include different versions of your scripts during page generation, depending on the development mode, as a workaround. Commented Aug 24, 2014 at 23:05
  • r.js compilator runs on server side in stageing prcess and produces one javascript file Commented Aug 25, 2014 at 6:38

2 Answers 2

1

By default r.js uses UglifyJS to optimize your modules. In r.js' configuration you can use the uglify option to send configuration options to UglifyJS. For instance,

uglify: {
    defines: {
        DEV: ['name', 'false']
    }
},

This would tell uglifyjs replace every instance of the symbol DEV with the name false. Then, parts like this:

if (DEV) {
    // ....
}

would be automatically removed by uglifyjs as being unreachable.

See uglifyjs' documentation for the details of how that works.

You might also want to look at UglifyJS2, because it perhaps does more than UglifyJS. You can tell r.js to use it by setting the optimize option to uglify2, and use the uglify2 option to control what it does.

Sign up to request clarification or add additional context in comments.

Comments

0

UglifyJS2 from sbt-rjs 1.0.7 already has some other usefull option:

uglify: {
     drop_console: true
}

uglify2: {
     compress: { drop_console: true }
}

it removes all console.log messages from minified script.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.