1 | class LessPlugin {
|
2 | constructor (window, host) {
|
3 | this.window = window;
|
4 | this.host = host;
|
5 | }
|
6 |
|
7 | reload (path, options) {
|
8 | if (this.window.less && this.window.less.refresh) {
|
9 | if (path.match(/\.less$/i)) {
|
10 | return this.reloadLess(path);
|
11 | }
|
12 |
|
13 | if (options.originalPath.match(/\.less$/i)) {
|
14 | return this.reloadLess(options.originalPath);
|
15 | }
|
16 | }
|
17 |
|
18 | return false;
|
19 | }
|
20 |
|
21 | reloadLess (path) {
|
22 | let link;
|
23 |
|
24 | const links = ((() => {
|
25 | const result = [];
|
26 |
|
27 | for (link of Array.from(document.getElementsByTagName('link'))) {
|
28 | if ((link.href && link.rel.match(/^stylesheet\/less$/i)) || (link.rel.match(/stylesheet/i) && link.type.match(/^text\/(x-)?less$/i))) {
|
29 | result.push(link);
|
30 | }
|
31 | }
|
32 |
|
33 | return result;
|
34 | })());
|
35 |
|
36 | if (links.length === 0) {
|
37 | return false;
|
38 | }
|
39 |
|
40 | for (link of Array.from(links)) {
|
41 | link.href = this.host.generateCacheBustUrl(link.href);
|
42 | }
|
43 |
|
44 | this.host.console.log('LiveReload is asking LESS to recompile all stylesheets');
|
45 | this.window.less.refresh(true);
|
46 |
|
47 | return true;
|
48 | }
|
49 |
|
50 | analyze () {
|
51 | return {
|
52 | disable: !!(this.window.less && this.window.less.refresh)
|
53 | };
|
54 | }
|
55 | };
|
56 |
|
57 | LessPlugin.identifier = 'less';
|
58 | LessPlugin.version = '1.0';
|
59 |
|
60 | module.exports = LessPlugin;
|