In almost any web application can not do without third-party javascript-libraries. The easiest way to add them is to download and add to the project, and also add the file to the git repository. The solution is good, but for me personally, the presence of any static in the project is a bit annoying. There is another method: specify a link to external hosting js-libraries such as google, yandex. In principle, this is an option, but in my practice there were cases when it was necessary to continue development and access to the Internet left much to be desired or it did not exist at all, as a result, the client part did not function. The most suitable solution seems to me to add the js library as a dependency in a project, with a similar approach you could encounter in Ruby on Rails.libraryDependencies ++= Seq( cache, ws, "org.webjars" %% "webjars-play" % "2.3.0-2", "org.webjars" % "requirejs" % "2.1.14-3", "org.webjars" % "requirejs-domready" % "2.0.1-2", "org.webjars" % "jquery" % "2.1.1", "org.webjars" % "bootstrap" % "3.2.0-2" ) GET /lib/*file controllers.WebJarAssets.at(file) <link rel="stylesheet" href="@routes.WebJarAssets.at(WebJarAssets.locate("bootstrap.min.css"))"> <link rel="stylesheet" href="/lib/bootstrap/3.2.0-2/css/bootstrap.min.css"> /*global requirejs */ // Ensure any request for this webjar brings in jQuery. requirejs.config({ paths: { "jquery": webjars.path("jquery", "jquery") }, shim: { "jquery": { "exports": "$" } } }); @Html(org.webjars.RequireJS.getSetupJavaScript(routes.WebJarAssets.at("").url)) import play.api.cache.Cached import views._ import play.api.Play.current def requireJsConfig = Cached("require_js_config") { Action { Ok(html.requireJsConfig()).as("application/javascript") } } GET /files/config.js controllers.Application.requireJsConfig var webjars = { versions: {"requirejs-domready":"2.0.1","requirejs":"2.1.14-3","bootstrap":"3.2.0-2","jquery":"2.1.1"}, path: function(webJarId, path) { console.error('The webjars.path() method of getting a WebJar path has been deprecated. The RequireJS config in the ' + webJarId + ' WebJar may need to be updated. Please file an issue: http://github.com/webjars/' + webJarId + '/issues/new'); return ['/lib/' + webJarId + '/' + webjars.versions[webJarId] + '/' + path]; } }; var require = { callback: function() { // Deprecated WebJars RequireJS plugin loader define('webjars', function() { return { load: function(name, req, onload, config) { if (name.indexOf('.js') >= 0) { console.warn('Detected a legacy file name (' + name + ') as the thing to load. Loading via file name is no longer supported so the .js will be dropped in an effort to resolve the module name instead.'); name = name.replace('.js', ''); } console.error('The webjars plugin loader (eg webjars!' + name + ') has been deprecated. The RequireJS config in the ' + name + ' WebJar may need to be updated. Please file an issue: http://github.com/webjars/webjars/issues/new'); req([name], function() {; onload(); }); } } }); // All of the WebJar configs requirejs.config({"paths":{"requirejs-domready":["/lib/requirejs-domready/2.0.1/domReady","domReady"]}}) requirejs.config({"paths":{}}) requirejs.config({"paths":{"bootstrap":["/lib/bootstrap/3.2.0-2/js/bootstrap","js/bootstrap"],"bootstrap-css":["/lib/bootstrap/3.2.0-2/css/bootstrap","css/bootstrap"]},"shim":{"bootstrap":["jquery"]}}) requirejs.config({"paths":{"jquery":["/lib/jquery/2.1.1/jquery","jquery"]},"shim":{"jquery":{"exports":"$"}}}) } } require ['jquery','requirejs-domready!'],-> $('body').text 'Success' <script type="application/javascript" src="@routes.Application.requireJsConfig()"></script> <script data-main="@routes.Assets.at("js/main.js")" src="@routes.WebJarAssets.at(WebJarAssets.locate("require.min.js"))"></script> Source: https://habr.com/ru/post/241515/
All Articles