From 20225e8d6b381e882cd8c885db84b3b7b0eae9af Mon Sep 17 00:00:00 2001 From: Matthias Osswald Date: Fri, 21 Nov 2014 16:35:08 +0100 Subject: [PATCH] openui5_connect: inject additional livereload plugin The plugin will handle the case when a .less file got changed but only .css files are in the browser (server-side less compilation). It will now reload all css files instead of reloading the complete page --- package.json | 2 ++ tasks/connect.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/package.json b/package.json index 77777535..ae51de3d 100644 --- a/package.json +++ b/package.json @@ -27,9 +27,11 @@ }, "dependencies": { "async": "^0.9.0", + "connect-inject": "^0.3.2", "connect-openui5": "^0.3.0", "cors": "^2.0.0", "less-openui5": "^0.1.1", + "multiline": "^1.0.1", "object-assign": "^1.0.0", "pretty-data": "^0.40.0", "uglify-js": "^2.4.15", diff --git a/tasks/connect.js b/tasks/connect.js index 43cd1429..bf879342 100644 --- a/tasks/connect.js +++ b/tasks/connect.js @@ -17,8 +17,38 @@ var openui5 = { connect: require('connect-openui5') }; +var inject = require('connect-inject'); var cors = require('cors'); var urljoin = require('url-join'); +var multiline = require('multiline'); + +var liveReloadLessCssPlugin = multiline(function() {/* +var LiveReloadPluginLessCss = function(window, host) { + this.window = window; + this.host = host; +}; +LiveReloadPluginLessCss.identifier = 'less_css'; +LiveReloadPluginLessCss.version = '1.0'; +LiveReloadPluginLessCss.prototype.reload = function(path, options) { + // do only run this if less is not loaded (LiveReload LessPlugin will handle this) + if (!this.window.less) { + // reload stylesheets (css) also if a less file was changed + if (path.match(/\.less$/i)) { + return this.window.LiveReload.reloader.reloadStylesheet(path); + } + if (options.originalPath.match(/\.less$/i)) { + return this.window.LiveReload.reloader.reloadStylesheet(options.originalPath); + } + } + return false; +}; +LiveReloadPluginLessCss.prototype.analyze = function() { + // disable the plugin if less is loaded (LiveReload LessPlugin will handle this) + return { + disable: (this.window.less && this.window.less.refresh) + }; +}; +*/}); module.exports = function(grunt, config) { @@ -70,6 +100,18 @@ module.exports = function(grunt, config) { }; } + // handle livereload option including css/less files (reload css if less files got changed) + if (connectOptions.livereload !== false) { + var port = (connectOptions.livereload === true) ? 35729 : connectOptions.livereload; + connectOptions.livereload = false; // prevent grunt-contrib-connect from inserting the livereload script + mountMiddleware(inject({ + snippet: [ + '\n\n', + '\n\n' + ] + })); + } + // fix serving *.properties files (encoding) mountMiddleware(openui5.connect.properties());