Skip to content

Commit dc237d9

Browse files
committed
Merge pull request sass#815 from am11/master
CLI: Adds support for Custom Functions
2 parents 9dc4b18 + d91917a commit dc237d9

File tree

10 files changed

+74
-2
lines changed

10 files changed

+74
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,8 @@ Output will be saved with the same name as input SASS file into the current work
452452
--source-map-root Base path, will be emitted in source-map as is
453453
--include-path Path to look for imported files
454454
--precision The amount of precision allowed in decimal numbers
455-
--importer Path to custom importer
455+
--importer Path to .js file containing custom importer
456+
--functions Path to .js file containing custom functions
456457
--help Print usage info
457458
```
458459

bin/node-sass

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ var cli = meow({
4242
' --source-map-root Base path, will be emitted in source-map as is',
4343
' --include-path Path to look for imported files',
4444
' --precision The amount of precision allowed in decimal numbers',
45-
' --importer Path to custom importer',
45+
' --importer Path to .js file containing custom importer',
46+
' --functions Path to .js file containing custom functions',
4647
' --help Print usage info'
4748
].join('\n')
4849
}, {
@@ -55,6 +56,8 @@ var cli = meow({
5556
'source-comments'
5657
],
5758
string: [
59+
'functions',
60+
'importer',
5861
'include-path',
5962
'indent-type',
6063
'linefeed',
@@ -227,6 +230,14 @@ function run(options, emitter) {
227230
}
228231
}
229232

233+
if (options.functions) {
234+
if ((path.resolve(options.functions) === path.normalize(options.functions).replace(/(.+)([\/|\\])$/, '$1'))) {
235+
options.functions = require(options.functions);
236+
} else {
237+
options.functions = require(path.resolve(process.cwd(), options.functions));
238+
}
239+
}
240+
230241
if (options.watch) {
231242
watch(options, emitter);
232243
} else {

lib/render.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ module.exports = function(options, emitter) {
3030
sourceMap: options.sourceMap,
3131
sourceMapRoot: options.sourceMapRoot,
3232
importer: options.importer,
33+
functions: options.functions,
3334
indentWidth: options.indentWidth,
3435
indentType: options.indentType,
3536
linefeed: options.linefeed

test/cli.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,4 +400,38 @@ describe('cli', function() {
400400
});
401401
});
402402
});
403+
404+
describe('functions', function() {
405+
it('should let custom functions call setter methods on wrapped sass values (number)', function(done) {
406+
var dest = fixture('custom-functions/setter.css');
407+
var src = fixture('custom-functions/setter.scss');
408+
var expected = read(fixture('custom-functions/setter-expected.css'), 'utf8').trim().replace(/\r\n/g, '\n');
409+
var bin = spawn(cli, [
410+
src, '--output', path.dirname(dest),
411+
'--functions', fixture('extras/my_custom_functions_setter.js')
412+
]);
413+
414+
bin.once('close', function() {
415+
assert.equal(read(dest, 'utf8').trim(), expected);
416+
fs.unlinkSync(dest);
417+
done();
418+
});
419+
});
420+
421+
it('should properly convert strings when calling custom functions', function(done) {
422+
var dest = fixture('custom-functions/string-conversion.css');
423+
var src = fixture('custom-functions/string-conversion.scss');
424+
var expected = read(fixture('custom-functions/string-conversion-expected.css'), 'utf8').trim().replace(/\r\n/g, '\n');
425+
var bin = spawn(cli, [
426+
src, '--output', path.dirname(dest),
427+
'--functions', fixture('extras/my_custom_functions_string_conversion.js')
428+
]);
429+
430+
bin.once('close', function() {
431+
assert.equal(read(dest, 'utf8').trim(), expected);
432+
fs.unlinkSync(dest);
433+
done();
434+
});
435+
});
436+
});
403437
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
div {
2+
width: 42rem;
3+
height: 84px; }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
div { width: foo(42px); height: bar(42px); }
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
div {
2+
color: "barbar"; }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
div { color: foo("bar"); }
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
'foo($a)': function(size) {
3+
size.setUnit('rem');
4+
return size;
5+
},
6+
'bar($a)': function(size) {
7+
size.setValue(size.getValue() * 2);
8+
return size;
9+
}
10+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var sass = require('../../..');
2+
3+
module.exports = {
4+
'foo($a)': function(str) {
5+
str = str.getValue().replace(/['"]/g, '');
6+
return new sass.types.String('"' + str + str + '"');
7+
}
8+
};

0 commit comments

Comments
 (0)