Commit d91917ab by Adeel

CLI: Adds support for Custom Functions.

* Adds related tests and fixtures.

Issue URL: #784.
PR URL: #815.
parent 9dc4b18e
......@@ -452,7 +452,8 @@ Output will be saved with the same name as input SASS file into the current work
--source-map-root Base path, will be emitted in source-map as is
--include-path Path to look for imported files
--precision The amount of precision allowed in decimal numbers
--importer Path to custom importer
--importer Path to .js file containing custom importer
--functions Path to .js file containing custom functions
--help Print usage info
```
......
......@@ -42,7 +42,8 @@ var cli = meow({
' --source-map-root Base path, will be emitted in source-map as is',
' --include-path Path to look for imported files',
' --precision The amount of precision allowed in decimal numbers',
' --importer Path to custom importer',
' --importer Path to .js file containing custom importer',
' --functions Path to .js file containing custom functions',
' --help Print usage info'
].join('\n')
}, {
......@@ -55,6 +56,8 @@ var cli = meow({
'source-comments'
],
string: [
'functions',
'importer',
'include-path',
'indent-type',
'linefeed',
......@@ -227,6 +230,14 @@ function run(options, emitter) {
}
}
if (options.functions) {
if ((path.resolve(options.functions) === path.normalize(options.functions).replace(/(.+)([\/|\\])$/, '$1'))) {
options.functions = require(options.functions);
} else {
options.functions = require(path.resolve(process.cwd(), options.functions));
}
}
if (options.watch) {
watch(options, emitter);
} else {
......
......@@ -30,6 +30,7 @@ module.exports = function(options, emitter) {
sourceMap: options.sourceMap,
sourceMapRoot: options.sourceMapRoot,
importer: options.importer,
functions: options.functions,
indentWidth: options.indentWidth,
indentType: options.indentType,
linefeed: options.linefeed
......
......@@ -400,4 +400,38 @@ describe('cli', function() {
});
});
});
describe('functions', function() {
it('should let custom functions call setter methods on wrapped sass values (number)', function(done) {
var dest = fixture('custom-functions/setter.css');
var src = fixture('custom-functions/setter.scss');
var expected = read(fixture('custom-functions/setter-expected.css'), 'utf8').trim().replace(/\r\n/g, '\n');
var bin = spawn(cli, [
src, '--output', path.dirname(dest),
'--functions', fixture('extras/my_custom_functions_setter.js')
]);
bin.once('close', function() {
assert.equal(read(dest, 'utf8').trim(), expected);
fs.unlinkSync(dest);
done();
});
});
it('should properly convert strings when calling custom functions', function(done) {
var dest = fixture('custom-functions/string-conversion.css');
var src = fixture('custom-functions/string-conversion.scss');
var expected = read(fixture('custom-functions/string-conversion-expected.css'), 'utf8').trim().replace(/\r\n/g, '\n');
var bin = spawn(cli, [
src, '--output', path.dirname(dest),
'--functions', fixture('extras/my_custom_functions_string_conversion.js')
]);
bin.once('close', function() {
assert.equal(read(dest, 'utf8').trim(), expected);
fs.unlinkSync(dest);
done();
});
});
});
});
div {
width: 42rem;
height: 84px; }
div { width: foo(42px); height: bar(42px); }
module.exports = {
'foo($a)': function(size) {
size.setUnit('rem');
return size;
},
'bar($a)': function(size) {
size.setValue(size.getValue() * 2);
return size;
}
};
var sass = require('../../..');
module.exports = {
'foo($a)': function(str) {
str = str.getValue().replace(/['"]/g, '');
return new sass.types.String('"' + str + str + '"');
}
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment