Skip to content

Commit

Permalink
CLI: Adds support for Custom Functions.
Browse files Browse the repository at this point in the history
* Adds related tests and fixtures.

Issue URL: sass#784.
PR URL: sass#815.
  • Loading branch information
am11 committed Mar 28, 2015
1 parent 9dc4b18 commit d91917a
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
13 changes: 12 additions & 1 deletion bin/node-sass
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}, {
Expand All @@ -55,6 +56,8 @@ var cli = meow({
'source-comments'
],
string: [
'functions',
'importer',
'include-path',
'indent-type',
'linefeed',
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions lib/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});
});
3 changes: 3 additions & 0 deletions test/fixtures/custom-functions/setter-expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
div {
width: 42rem;
height: 84px; }
1 change: 1 addition & 0 deletions test/fixtures/custom-functions/setter.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
div { width: foo(42px); height: bar(42px); }
2 changes: 2 additions & 0 deletions test/fixtures/custom-functions/string-conversion-expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
div {
color: "barbar"; }
1 change: 1 addition & 0 deletions test/fixtures/custom-functions/string-conversion.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
div { color: foo("bar"); }
10 changes: 10 additions & 0 deletions test/fixtures/extras/my_custom_functions_setter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
'foo($a)': function(size) {
size.setUnit('rem');
return size;
},
'bar($a)': function(size) {
size.setValue(size.getValue() * 2);
return size;
}
};
8 changes: 8 additions & 0 deletions test/fixtures/extras/my_custom_functions_string_conversion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var sass = require('../../..');

module.exports = {
'foo($a)': function(str) {
str = str.getValue().replace(/['"]/g, '');
return new sass.types.String('"' + str + str + '"');
}
};

0 comments on commit d91917a

Please sign in to comment.