简体   繁体   中英

play framework and requirejs development javascript code

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

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.

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

\n

 
 
 
 
  
  
  uglify: { drop_console: true }
 
 
  

uglify2: {
     compress: { drop_console: true }
}

it removes all console.log messages from minified script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM