Commit dc237d98 by Adeel Mujahid

Merge pull request #815 from am11/master

CLI: Adds support for Custom Functions
parents 9dc4b18e d91917ab
...@@ -452,7 +452,8 @@ Output will be saved with the same name as input SASS file into the current work ...@@ -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 --source-map-root Base path, will be emitted in source-map as is
--include-path Path to look for imported files --include-path Path to look for imported files
--precision The amount of precision allowed in decimal numbers --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 --help Print usage info
``` ```
......
...@@ -42,7 +42,8 @@ var cli = meow({ ...@@ -42,7 +42,8 @@ var cli = meow({
' --source-map-root Base path, will be emitted in source-map as is', ' --source-map-root Base path, will be emitted in source-map as is',
' --include-path Path to look for imported files', ' --include-path Path to look for imported files',
' --precision The amount of precision allowed in decimal numbers', ' --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' ' --help Print usage info'
].join('\n') ].join('\n')
}, { }, {
...@@ -55,6 +56,8 @@ var cli = meow({ ...@@ -55,6 +56,8 @@ var cli = meow({
'source-comments' 'source-comments'
], ],
string: [ string: [
'functions',
'importer',
'include-path', 'include-path',
'indent-type', 'indent-type',
'linefeed', 'linefeed',
...@@ -227,6 +230,14 @@ function run(options, emitter) { ...@@ -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) { if (options.watch) {
watch(options, emitter); watch(options, emitter);
} else { } else {
......
...@@ -30,6 +30,7 @@ module.exports = function(options, emitter) { ...@@ -30,6 +30,7 @@ module.exports = function(options, emitter) {
sourceMap: options.sourceMap, sourceMap: options.sourceMap,
sourceMapRoot: options.sourceMapRoot, sourceMapRoot: options.sourceMapRoot,
importer: options.importer, importer: options.importer,
functions: options.functions,
indentWidth: options.indentWidth, indentWidth: options.indentWidth,
indentType: options.indentType, indentType: options.indentType,
linefeed: options.linefeed linefeed: options.linefeed
......
...@@ -400,4 +400,38 @@ describe('cli', function() { ...@@ -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