Skip to content

Commit

Permalink
feat(extract): allow existing custom defined functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jgranstrom committed Jul 14, 2017
1 parent cc79568 commit 23090de
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ function makeExtractionCompileOptions(compileOptions, entryFilename, extractions
Object.assign(extractionFunctions, extractions[extractionKey].injectedFunctions);
});

extractionCompileOptions.functions = extractionFunctions;
extractionCompileOptions.functions = Object.assign(extractionFunctions, compileOptions.functions);
extractionCompileOptions.data = extractions[entryFilename].injectedData;
extractionCompileOptions.importer = importer;
if(!makeExtractionCompileOptions.imported) {
extractionCompileOptions.importer = importer;
}

return extractionCompileOptions;
}
Expand Down
56 changes: 56 additions & 0 deletions test/functions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const { expect } = require('chai');
const path = require('path');
const { render, renderSync } = require('../lib');
const { normalizePath } = require('../lib/extract');
const { types } = require('node-sass');

const functionsFile = path.join(__dirname, 'sass', 'functions.scss');

function verifyFunctions(rendered, sourceFile) {
expect(rendered.vars).to.exist;
expect(rendered.vars).to.have.property('global');
expect(rendered.vars.global).to.have.property('$fColor');
expect(rendered.vars.global).to.have.property('$fSize');

expect(rendered.vars.global.$fColor.value.r).to.equal(0);
expect(rendered.vars.global.$fColor.value.g).to.equal(255);
expect(rendered.vars.global.$fColor.value.b).to.equal(0);
expect(rendered.vars.global.$fColor.value.a).to.equal(1);
expect(rendered.vars.global.$fColor.value.hex).to.equal('#00ff00');
expect(rendered.vars.global.$fColor.type).to.equal('SassColor');
expect(rendered.vars.global.$fColor.sources).to.have.length(1);
expect(rendered.vars.global.$fColor.sources[0]).to.equal(normalizePath(sourceFile));
expect(rendered.vars.global.$fColor.expressions).to.have.length(1);
expect(rendered.vars.global.$fColor.expressions[0]).to.equal('fn-color()');

expect(rendered.vars.global.$fSize.value).to.equal(20);
expect(rendered.vars.global.$fSize.unit).to.equal('px');
expect(rendered.vars.global.$fSize.type).to.equal('SassNumber');
expect(rendered.vars.global.$fSize.sources).to.have.length(1);
expect(rendered.vars.global.$fSize.sources[0]).to.equal(normalizePath(sourceFile));
expect(rendered.vars.global.$fSize.expressions).to.have.length(1);
expect(rendered.vars.global.$fSize.expressions[0]).to.equal('fn-size(2)');
}

const functions = {
'fn-color()': () => new types.Color(0, 255, 0),
'fn-size($multiplier)': (multiplier) => new types.Number(10 * multiplier.getValue(), 'px'),
}

describe('functions', () => {
describe('sync', () => {
it('should extract all variables', () => {
const rendered = renderSync({ file: functionsFile, functions });
verifyFunctions(rendered, functionsFile);
});
});

describe('async', () => {
it('should extract all variables', () => {
return render({ file: functionsFile, functions })
.then(rendered => {
verifyFunctions(rendered, functionsFile);
});
});
});
});
2 changes: 2 additions & 0 deletions test/sass/functions.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$fColor: fn-color();
$fSize: fn-size(2);

0 comments on commit 23090de

Please sign in to comment.