1

Gooday.

What i try to achieve: Use play while being able to edit javascript and lessfiles live in the chrome dev tool.

What i figured out: if you add the "public" folder of the project to the workspace in the ressources tab of the chrome dev tool, everything works as expected. Changes made in the chrome dev tool are saved to the according css and javascriptfiles and the page reflects the changes in realtime. The same happens if you add the 'app/assets/stylesheets' directory to the chrome dev workspace --> changes made to the file in the console are saved to the file saved on the disc.

Further if you run 'activator ~run' from the console, changes made on any file that require compilation trigger a recompilation. I got to that point that i could change my less code in the dev tool. Changes where ritten to the local file and made play recompile. All fine and dandy, buuut sadly - there it all ended :/

Problem: i work with less, and less is compiled to some misterious place (I haven't figured out where exactly yet). I can't add the css file (which was compiled from the less file) to the workspace in the chrome dev console if I don't know the directory - this means chrome doesnt know when to repaint the page and i get a 'ressource not available' error in the console. First i thought the css which actually styles the page is stored in the 'target' folder of the project, but this is not the case. (I added the target folder to the workspace, but changing the css files in this folder didn't reflect on the page)

My question: where is the css after being compiled from the less file? Or is there an other way to enable liveediting of less (or any compiled script/style source) in the browser?

2 Answers2

1

Ok I figured it out. For all of you wondering how you can edit the less files of your play project live and save the changes to the source files, heres how I achived it. This Solution is for playframework developers who use less for their styling - Though i think you should be able to solve the issue in a similar manner with every css pre-processor. Note that I use the Chrome Dev Tool to live edit my code:

first of all you have to add this line of code to your build.sbt:

//enable source Map for lesscompilation
LessKeys.sourceMap in (Assets, LessKeys.less) := true

You need the source Map so that the Chrome Dev Tool can map the css to the corresponding lessfiles. After recompilation, reload the project in chrome and hit f12 to bring up the Chrome Dev Tool. Your Less files should be mapped correctly:

https://s3.amazonaws.com/robdodson/images/less-preview.png

Note that the sidepannel points to "modules.less" --> a click on this pointer prings up the Sources panel and opens the corresponding file. If you go in, edit the file and hit ctrl s for saving, nothing happens though.Thats because the Chrome Dev Tool doesn't know where to save the changes to the disk.


To tell the Chrome Dev Tool where to save the changes, you need to set up a Workspace. To do that, open the "Sources" pannel in the Chrome Dev Tool. On the left side, you have a file-tree. Right click in that pannel and click on "add folder to Workspace". A Workspace mapps your local files to an Url and visa-versa. heres a link to an in-depth explanation of the Chrome Dev Tool Workspace:

https://developer.chrome.com/devtools/docs/workspaces

The folder I had to pick was located here :

pathToYourProject/target/web/public/main

now this is confusing, because as @Bass Jobsen mentioned in his post:

https://www.playframework.com/documentation/2.0/AssetsLess tells you:

Note that managed resources are not copied directly into your application public folder, but maintained in a separate folder in target/scala-2.9.1/resources_managed.

But in my case, there was no such folder as "resources_managed" in "target/sacal-2.9.1". After some hours of confusion, i found that the files used by the page corresponded to the files located in "pathToYourProject/target/web/public/main". Probably it's just me, feel free to comment on that.


After configuring the Workspace, live editing still won't work. This is due to the fact that less needs compilation. So how can the css know that something has changed if you don't recompile the less files? To enable live editing, you need to watch your less files and compile them to css if changes occure. I used grunt for that task:

http://gruntjs.com/getting-started

I won't go in to any details on grunt, it's well documented. I'll just post the gruntfile i use.As you can see, grunt listens for all changes on the less files in the folder "target/web/public/main/stylesheets". If changes occure, the task 'less' is initiated which recompiles the file "final.less" (this is the file that imports all other less-files). After recompilation, grunt copys all .less files to the "app/assets/stylesheets" directory of the project. This is nessecery to bring the changes made to the less files in the Chrome Dev Tool to the actual less files of the project. I used

Here's the file:

module.exports = function(grunt){
    grunt.initConfig({
        less: {
              development:{
                options:{
                    sourceMap: true,
              },
                files:{
                    'target/web/public/main/stylesheets/final.css':'target/web/public/main/stylesheets/final.less',
                }
              }
        },
        watch: {
            styles:{
                options:{
                    livereload: true,
                    spawn: false,
                    event: ['added','deleted','changed']
                },
                files:['target/web/public/main/stylesheets/**/*.less'],
                tasks:['less','copy']
            }
        },
        copy: {
            main: {
                files: [
                    {expand: true, cwd: 'target/web/public/main/stylesheets/', src:['**/*.less'], dest:'app/assets/stylesheets/'}
                ]
            }
        }

    });

    grunt.loadNpmTasks('grunt-contrib-copy');
    grunt.loadNpmTasks('grunt-contrib-less');
    grunt.loadNpmTasks('grunt-contrib-watch');
}


Thats how i set up live editing of less with play in the Chrome Dev Tool. Note that you can't edit the less file in the Elements pannel of the Dev Tool. Changes made there will remove the mapping of the less and css files. If you ctrl-click on the properties or classes however, you will be taken to the corresponding file and class/property in the sources pannel. There saved changes will trigger the recompilation of the less file and boom --> you're live editing your page :) I hope i could help someone with this post.

Links and sources:

0

https://www.playframework.com/documentation/2.0/AssetsLess tells you:

Note that managed resources are not copied directly into your application public folder, but maintained in a separate folder in target/scala-2.9.1/resources_managed.

Bass Jobsen
  • 48,736
  • 16
  • 143
  • 224