-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds two additional options for plugins sass: path to the stylesheet within the publicDir. When provided, this will be used to when the plugin is active. Providing false diables loading of any stylesheets for the plugin. styleSheet: path to an SCSS file within the plugins root directory. When in development, this will generate a CSS file to be specified in conjunction with the styleSheet option. Signed-off-by: Tyler Smalley <tyler.smalley@elastic.co>
- Loading branch information
Tyler Smalley
committed
Jun 4, 2018
1 parent
92c9ad7
commit 6c5780d
Showing
18 changed files
with
598 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import path from 'path'; | ||
import fs from 'fs'; | ||
import sass from 'node-sass'; | ||
import { FSWatcher } from 'chokidar'; | ||
import anymatch from 'anymatch'; | ||
|
||
export class SassBuilder { | ||
/** | ||
* @param {String} input - Full path to SASS file | ||
* @param {String} output - Full path to CSS to write to | ||
* @param {Object|null} options | ||
* @property {FSWatcher|null} options.watcher - Instance of Chokidar to use | ||
* @property {Log|null} options.log - Instance of logger | ||
*/ | ||
|
||
constructor(input, output, options = {}) { | ||
this.input = input; | ||
this.output = output; | ||
this.watcher = options.watcher || new FSWatcher({}); | ||
this.logger = options.log; | ||
} | ||
|
||
/** | ||
* Adds glob to instance of Watcher | ||
*/ | ||
|
||
addToWatcher() { | ||
this.watcher.add(this.getGlob()); | ||
} | ||
|
||
/** | ||
* Glob based on input path | ||
*/ | ||
|
||
getGlob() { | ||
return path.join(path.dirname(this.input), '**', '*.s{a,c}ss'); | ||
} | ||
|
||
log(level, ...message) { | ||
if (!this.logger) { | ||
return; | ||
} | ||
|
||
this.logger[level](...message); | ||
} | ||
|
||
async buildIfInPath(path) { | ||
if (anymatch(this.getGlob(), path)) { | ||
await this.build(); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Transpiles SASS and writes CSS to output | ||
*/ | ||
|
||
async build() { | ||
try { | ||
const rendered = await sass.renderSync({ | ||
file: this.input, | ||
outfile: this.output | ||
}); | ||
|
||
fs.writeFileSync(this.output, rendered.css); | ||
this.log('good', 'Compiled SASS into CSS ', this.output); | ||
} catch(e) { | ||
this.log('bad', 'Compiling SCSS failed', e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import sinon from 'sinon'; | ||
import sass from 'node-sass'; | ||
import { SassBuilder } from './sass_builder'; | ||
|
||
describe('SASS builder', () => { | ||
const sandbox = sinon.createSandbox(); | ||
|
||
it('generates a glob', () => { | ||
const builder = new SassBuilder('/foo/style.sass', 'foo/public/style.css'); | ||
expect(builder.getGlob()).toEqual('/foo/**/*.s{a,c}ss'); | ||
}); | ||
|
||
it('adds a watch for SASS files on the basePath', () => { | ||
const watcher = { | ||
add: sinon.stub(), | ||
}; | ||
const builder = new SassBuilder('/foo/style.sass', 'foo/public/style.css', { | ||
watcher, | ||
}); | ||
|
||
builder.addToWatcher(); | ||
|
||
sinon.assert.calledOnce(watcher.add); | ||
sinon.assert.calledWithExactly(watcher.add, builder.getGlob()); | ||
}); | ||
|
||
it('builds SASS', () => { | ||
sandbox.stub(sass, 'renderSync').callsFake(() => { | ||
return { css: 'test' }; | ||
}); | ||
|
||
const builder = new SassBuilder('/foo/style.sass', '/foo/public/style.css'); | ||
builder.build(); | ||
|
||
sinon.assert.calledOnce(sass.renderSync); | ||
sinon.assert.calledWithExactly(sass.renderSync, { | ||
file: '/foo/style.sass', | ||
outfile: '/foo/public/style.css', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.