webjars - How to use RequireJS optimizer in Play framework? -


as advertised, rjs in play can

ensure javascript resources referenced within webjar automatically referenced jsdelivr cdn. in addition if .min.js file found used in place of .js. added bonus here there no change required html!

however, cannot seem of work.

  1. i tried running play app in production mode, , webjar javascripts still being referenced local.
  2. i not see .min version of javascript files being used in production.
  3. i cannot dependency injection work in production mode. example, when want inject jquery in code this

    define(['jquery'], function ($) { 'use strict'; console.log($.grep); return { sum: function (a, b) { return + b; } }; });

i can work fine in dev mode, in production mode, rjs failed saying

[info] error: enoent, no such file or directory '/users/khanguyen/desktop/rjsdemo/target/web/rjs/build/js/jquery.js' [info] in module tree: [info]     main [info]       app [info]  [info] error: error: enoent, no such file or directory '/users/khanguyen/desktop/rjsdemo/target/web/rjs/build/js/jquery.js' [info] in module tree: [info]     main [info]       app [info]  [info]     @ error (native) 

obviously looking @ wrong location jquery, despite config setup generated webjar

requirejs.config({"paths":{"jquery":["/webjars/jquery/1.11.1/jquery","jquery"]},"shim":{"jquery":{"exports":"$"}},"packages":[]})    } 

to have correct location jquery.

i using play 2.4.0, pipelinestages := seq(rjs, digest) setup in build.sbt.

please let me know got wrong.

thanks!

it turns out requirejs optimization support not apply webjars, rather limited classic webjars. enter image description here

even then, webjar build file has included regular module in order rjs work. enter image description here

if @ jquery classic webjar, example, see special webjar build instruction included. take @ file information. enter image description here

once have identify webjar requirejs ready, can let sbt-rjs thing. here setup reference:

/** javascripts/main.js **/ 'use strict';  requirejs.config({     paths:{         'jquery': ['../lib/jquery/jquery'],         'react': ['../lib/react/react'],         'bootstrap': ['../lib/bootstrap/js/bootstrap'],         'react-bootstrap': ['../lib/react-bootstrap/react-bootstrap']     },     shim: {         'bootstrap': {             deps: ['jquery']         },         'react-bootstrap': {             deps: ['react']         }     } }); 

remember have square brackets, otherwise cdn replacement not happen.

for non-requirejs ready scripts, should not have square brackets when declaring paths. otherwise, rjs refuse build error path fallback not supported. of course, won't cdn benefit. side note, requirejs css optimization works, too. limited inlining css, regular requirejs does.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -