Skip to content

Commit 5576cc3

Browse files
committed
refactored into lib
1 parent cde23bb commit 5576cc3

File tree

6 files changed

+121
-63
lines changed

6 files changed

+121
-63
lines changed

README.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,23 @@ Reloads changed styles (css|scss|sass|less|styl) without reloading the entire em
66

77
## Installation
88

9-
### As an Ember CLI addon
9+
Run either command below depending on Ember version in your project folder.
1010

11-
In your ember-cli project run
11+
For Ember CLI >= `0.2.3`:
1212

13-
```bash
14-
npm install --save-dev ember-cli-styles-reloader
13+
```shell
14+
ember install ember-cli-styles-reloader
15+
```
16+
17+
For Ember CLI < `0.2.3`:
18+
19+
```shell
20+
ember install:addon ember-cli-styles-reloader
1521
```
1622

1723
## Configurations
1824

19-
* All style changes can be animated to smoothly transition between new/old change sets.
25+
* All style changes can be animated to smoothly transition between old/new change sets.
2026
By default this feature is disabled, in order to not interfere with existing transition(s) defined
2127
in your app. To enable it:
2228

addon/.gitkeep

Whitespace-only changes.

config/environment.js

-5
This file was deleted.

index.js

+46-52
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,54 @@
1-
/* jshint node: true */
2-
'use strict';
3-
4-
/* default reload css extensions */
5-
var styleExtensions = ['css', 'scss', 'sass', 'less', 'styl'];
6-
var reloadCssPattern = new RegExp('.(' + styleExtensions.join('|') + ')$');
7-
8-
function LiveStyleReloader(options){
9-
10-
var noop = function(){},
11-
options = options,
12-
appCssPattern = new RegExp('^' + options.project.root + '/app/styles/*'),
13-
ui = options.ui,
14-
http = null,
15-
liveReloadHost = options.liveReloadHost || options.host
16-
liveReloadHostname = [(options.ssl ? 'https://' :'http://'), liveReloadHost, ':', options.liveReloadPort].join(''),
17-
reloadCssPattern = options.reloadCssPattern;
18-
19-
var getDirtyAsset = function(changedFilePath){
20-
if (changedFilePath.match(appCssPattern)){
21-
return options.project.pkg.name + '.css';
22-
}
23-
return 'vendor.css';
24-
};
25-
26-
var fileDidChange = function(results){
27-
var filePath = results.filePath || '';
28-
var dirtyAsset = getDirtyAsset(filePath);
29-
30-
if (filePath.match(reloadCssPattern)){
31-
ui.writeLine("Reloading " + dirtyAsset + " only");
32-
http.get(liveReloadHostname + '/changed?files=' + dirtyAsset)
33-
.on('error', noop);
34-
}
35-
};
36-
37-
this.startObserving = function(watcher){
38-
if (options.liveReload){
39-
ui.writeLine('StyleReloader watches ' + styleExtensions.join('|'));
40-
http = require('http');
41-
watcher.on('change', fileDidChange.bind(this));
42-
}
43-
};
44-
};
1+
// /* jshint node: true */
2+
// 'use strict';
3+
4+
// /* default reload css extensions */
5+
// var styleExtensions = ['css', 'scss', 'sass', 'less', 'styl'];
6+
// var reloadCssPattern = new RegExp('.(' + styleExtensions.join('|') + ')$');
7+
8+
// function LiveStyleReloader(options){
9+
10+
// var noop = function(){},
11+
// options = options,
12+
// appCssPattern = new RegExp('^' + options.project.root + '/app/styles/*'),
13+
// ui = options.ui,
14+
// http = null,
15+
// liveReloadHost = options.liveReloadHost || options.host
16+
// liveReloadHostname = [(options.ssl ? 'https://' :'http://'), liveReloadHost, ':', options.liveReloadPort].join(''),
17+
// reloadCssPattern = options.reloadCssPattern;
18+
19+
// var getDirtyAsset = function(changedFilePath){
20+
// if (changedFilePath.match(appCssPattern)){
21+
// return options.project.pkg.name + '.css';
22+
// }
23+
// return 'vendor.css';
24+
// };
25+
26+
// var fileDidChange = function(results){
27+
// var filePath = results.filePath || '';
28+
// var dirtyAsset = getDirtyAsset(filePath);
29+
30+
// if (filePath.match(reloadCssPattern)){
31+
// ui.writeLine("Reloading " + dirtyAsset + " only");
32+
// http.get(liveReloadHostname + '/changed?files=' + dirtyAsset)
33+
// .on('error', noop);
34+
// }
35+
// };
36+
37+
// this.startObserving = function(watcher){
38+
// if (options.liveReload){
39+
// ui.writeLine('StyleReloader watches ' + styleExtensions.join('|'));
40+
// http = require('http');
41+
// watcher.on('change', fileDidChange.bind(this));
42+
// }
43+
// };
44+
// };
4545

4646
module.exports = {
4747
name: 'ember-cli-styles-reloader',
4848

4949
serverMiddleware: function(config){
50-
var options = config.options;
51-
52-
// Tell ember-cli to ignore files that match reloadCssPattern
53-
options.project.liveReloadFilterPatterns.push(reloadCssPattern);
54-
options.reloadCssPattern = reloadCssPattern
55-
56-
var lsReloader = new LiveStyleReloader(options);
57-
lsReloader.startObserving(options.watcher);
50+
var lsReloader = require('./lib/styles-reloader')(config.options);
51+
lsReloader.run();
5852
},
5953

6054
contentFor: function(type, config){

lib/styles-reloader.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* jshint node: true */
2+
'use strict';
3+
4+
var path = require('path');
5+
var http = require('http');
6+
7+
/* default reload css extensions */
8+
var styleExtensions = ['css', 'scss', 'sass', 'less', 'styl'];
9+
var reloadCssPattern = new RegExp('\.(' + styleExtensions.join('|') + ')$');
10+
11+
var noop = function(){};
12+
13+
module.exports = function StylesReloader(options){
14+
var options = options;
15+
var liveReloadEnabled = options.liveReload;
16+
var fsWatcher = options.watcher;
17+
var ui = options.ui;
18+
19+
// Build app style pattern
20+
var appStylePath = options.project.root + path.join('/app', 'styles', '*');
21+
var appStylePattern = new RegExp('^' + appStylePath);
22+
var appStyleResource = options.project.pkg.name + '.css';
23+
24+
// Livereload host/port
25+
var liveReloadHostname = [
26+
(options.ssl ? 'https://' :'http://'),
27+
(options.liveReloadHost || options.host),
28+
':',
29+
options.liveReloadPort
30+
].join('');
31+
32+
function getChangedStyle(filePath){
33+
if (filePath.match(appStylePattern)){
34+
return appStyleResource;
35+
}
36+
return 'vendor.css';
37+
};
38+
39+
function fileDidChange(results){
40+
var filePath = results.filePath || '';
41+
var liveReloadFile = getChangedStyle(filePath);
42+
43+
if (filePath.match(reloadCssPattern)){
44+
ui.writeLine('Reloading ' + liveReloadFile + ' only');
45+
http.get(liveReloadHostname + '/changed?files=' + liveReloadFile)
46+
.on('error', noop);
47+
}
48+
};
49+
50+
function updateReloadFilters(){
51+
options.project.liveReloadFilterPatterns.push(reloadCssPattern);
52+
};
53+
54+
return {
55+
run: function(){
56+
if (liveReloadEnabled){
57+
ui.writeLine('StylesReloader watches ' + styleExtensions.join('|'));
58+
updateReloadFilters();
59+
fsWatcher.on('change', fileDidChange.bind(this));
60+
}
61+
}
62+
};
63+
};

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ember-cli-styles-reloader",
3-
"version": "0.1.4",
3+
"version": "0.1.5",
44
"description": "Reloads changed styles (css|scss|sass|less|styl) without reloading the entire ember-cli app",
55
"directories": {
66
"doc": "doc",

0 commit comments

Comments
 (0)