Skip to content

Commit

Permalink
Changes to accommodate Play 2.3 and sbt-web
Browse files Browse the repository at this point in the history
  • Loading branch information
huntc committed Apr 2, 2014
1 parent fcc45e7 commit 095e97c
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 17 deletions.
2 changes: 1 addition & 1 deletion app/views/Index.scala.html
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8">
<title>My AngularJS App</title>
<link rel="stylesheet" href="@routes.Assets.at("css/app.css")">
@Html(org.webjars.play.RequireJS.setup("js/app"))
<script data-main="js/main" src="@routes.Assets.at("lib/requirejs/require.js")"></script>

This comment has been minimized.

Copy link
@jamesward

jamesward Apr 2, 2014

Contributor

This should be a versioned URL.

This comment has been minimized.

Copy link
@huntc

huntc Apr 2, 2014

Author Owner

Yes indeed. I need to enable the digest plugin and make some changes to the router. This will absolutely happen although probably not in time for Play 2.3-M1.

I've also added the locating of js/main.

This comment has been minimized.

Copy link
@huntc

huntc Apr 3, 2014

Author Owner

Actually, just thinking about it, it will be versioned. When you do a stage the rjs optimiser kicks in and transforms that path to the CDN path. In addition if there's a .min.js then that will be used instead.

So I believe we're good. :-)

This comment has been minimized.

Copy link
@jamesward

jamesward Apr 3, 2014

Contributor

Woah. How does the optimizer change the path that routes.Assets.at generates? And how to I change that behavior so that I can specify my own CDN URL? In the case of Typesafe.com I still want these kinds of files served from the Play app but proxied on my own CDN with versioned URLs and far future expires.

This comment has been minimized.

Copy link
@huntc

huntc Apr 3, 2014

Author Owner

Its pure rjs all the way mate! We have a standard template.app.js file that does the legwork, but you can easily replace the WebJar CDN like this:

RjsKeys.webjarCdn := "http://whatever"

If you need more control then any configuration specified in main.js takes precedence (this is an rjs thing).

As a final option, you can provide your own app.build.js file in the project's base directory. The only requirement we have is the last line which is replaced with values we derive from the build:

(function (appDir, baseUrl, dir, paths, buildWriter) {
    return {
        appDir: appDir,
        baseUrl: baseUrl,
        dir: dir,
        generateSourceMaps: true,
        mainConfigFile: appDir + "/" + baseUrl + "/main.js",
        modules: [
            {
                name: "main"
            }
        ],
        onBuildWrite: buildWriter,
        optimize: "uglify2",
        paths: paths,
        preserveLicenseComments: false
    }
}(undefined, undefined, undefined, undefined, undefined))

This comment has been minimized.

Copy link
@jamesward

jamesward Apr 3, 2014

Contributor

I'm referring to @routes.Assets.at("lib/requirejs/require.js") which isn't yet in rjs land so I'm wondering how we are changing the URL there to a versioned and CDN'able URL.

This comment has been minimized.

Copy link
@huntc

huntc Apr 3, 2014

Author Owner

Ah yes, good point. sbt-digest and route changes to the rescue. Thanks for the clarification.

This comment has been minimized.

Copy link
@huntc

huntc Apr 3, 2014

Author Owner

I don't think it'll make 2.3-M1 though, just FYI.

</head>
<body>
<ul class="menu">
Expand Down
11 changes: 3 additions & 8 deletions build.sbt
Expand Up @@ -3,13 +3,8 @@ name := "angular-seed-play"
version := "1.0-SNAPSHOT"

libraryDependencies ++= Seq(
"org.webjars" %% "webjars-play" % "2.2.1-2",
"org.webjars" % "angularjs" % "1.2.13"
"org.webjars" % "angularjs" % "1.3.0-beta.2",
"org.webjars" % "requirejs" % "2.1.11-1"
)

play.Project.playScalaSettings

requireJs += "app.js"

requireJsShim += "app.js"

lazy val root = (project in file(".")).addPlugins(PlayScala)

This comment has been minimized.

Copy link
@jamesward

jamesward Apr 2, 2014

Contributor

What is this for?

This comment has been minimized.

Copy link
@huntc

huntc Apr 2, 2014

Author Owner

That's a requirement of sbt 0.13.5. We should talk about that as it is really powerful stuff.

This comment has been minimized.

Copy link
@huntc

huntc Apr 3, 2014

Author Owner

Lemme explain more, for the record. :-) The method of using playScalaSettings is replaced by explicitly adding the PlayScala or PlayJava plugins. When you do that, your project has settings automatically enabled. It also means that you can control which plugins apply to which modules in your project... the way it was before with sbt was that plugins are available to all modules, which may not be desirable.

In addition by adding PlayScala or PlayJava, because they require SbtWeb, SbtWeb becomes enabled. When SbtWeb becomes enabled, all sbt-web plugins become enabled (they are "triggered").

HTH.

This comment has been minimized.

Copy link
@jamesward

jamesward Apr 3, 2014

Contributor

cc @jsuereth I think we should really try to come up with a better DX for this. Putting this kind of stuff in the default experience makes people say wtf and makes sbt seem complex. Why not just addPlugins(PlayScala)?

This comment has been minimized.

Copy link
@huntc

huntc Apr 3, 2014

Author Owner

Indeed. sbt/sbt#1213

This comment has been minimized.

Copy link
@jsuereth

jsuereth Apr 3, 2014

We might be able to make "addPlugins" work. I'll add it to the next sprint.

2 changes: 0 additions & 2 deletions conf/routes
Expand Up @@ -2,8 +2,6 @@
# This file defines all application routes (Higher priority routes first)
# ~~~~

# Enable webjar based resources to be returned
GET /webjars/*file controllers.WebJarAssets.at(file)

# Home page
GET / controllers.Application.index()
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
@@ -1 +1 @@
sbt.version=0.13.0
sbt.version=0.13.5-M2
4 changes: 3 additions & 1 deletion project/plugins.sbt
Expand Up @@ -5,4 +5,6 @@ logLevel := Level.Warn
resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"

// Use the Play sbt plugin for Play projects
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.2.1")
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.3-SNAPSHOT")

addSbtPlugin("com.typesafe.sbt" % "sbt-rjs" % "1.0.0-SNAPSHOT")
24 changes: 20 additions & 4 deletions public/js/app.js → public/js/main.js
@@ -1,19 +1,35 @@
/*global define, angular */
/*global require, requirejs */

'use strict';

requirejs.config({

This comment has been minimized.

Copy link
@jamesward

jamesward Apr 2, 2014

Contributor

Can't we get this config out of the WebJar?

This comment has been minimized.

Copy link
@huntc

huntc Apr 2, 2014

Author Owner

Yes, potentially. In fact one of the first things that I did was to bring in the webjars-requirejs.js file via a script statement. That doesn't work straight off though as you set up a few functions that are not available of course. But in answer to your question, yes it could potentially do that.

Just one thought though... the rjs optimizer expects configuration within this file so I don't know how that will go...

paths: {
'angular': ['../lib/angularjs/angular'],
'angular-route': ['../lib/angularjs/angular-route']
},
shim: {
'angular': {
exports : 'angular'
},
'angular-route': {
deps: ['angular'],
exports : 'angular'
}
}
});

require(['angular', './controllers', './directives', './filters', './services', 'angular-route'],
function(angular, controllers) {

// Declare app level module which depends on filters, and services

angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives', 'ngRoute']).
config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/view1', {templateUrl: 'partials/partial1.html', controller: controllers.MyCtrl1});
$routeProvider.when('/view2', {templateUrl: 'partials/partial2.html', controller: controllers.MyCtrl2});
$routeProvider.otherwise({redirectTo: '/view1'});
}]);
}]);

angular.bootstrap(document, ['myApp']);

});

0 comments on commit 095e97c

Please sign in to comment.