Navigation Menu

Skip to content

Commit

Permalink
Adding global() function
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkpiano committed Feb 1, 2016
1 parent e3ddf12 commit c181eb2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
15 changes: 10 additions & 5 deletions dist/index.js
Expand Up @@ -91,7 +91,8 @@ var Sassport = function () {
*/ */


function Sassport(name) { function Sassport(name) {
var _this = this; var _this = this,
_functions;


var modules = arguments.length <= 1 || arguments[1] === undefined ? [] : arguments[1]; var modules = arguments.length <= 1 || arguments[1] === undefined ? [] : arguments[1];
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
Expand Down Expand Up @@ -121,8 +122,8 @@ var Sassport = function () {
}; };


this._exports = {}; this._exports = {};

this._loaders = {}; this._loaders = {};
this._variables = {};


this._localPath = _path2.default.resolve('./'); this._localPath = _path2.default.resolve('./');
this._localAssetPath = null; this._localAssetPath = null;
Expand All @@ -131,7 +132,7 @@ var Sassport = function () {
this._onRequire = options.onRequire.bind(this); this._onRequire = options.onRequire.bind(this);


this.options = { this.options = {
functions: _defineProperty({ functions: (_functions = {
'resolve-path($source, $module: null)': function resolvePath$source$moduleNull(source, module) { 'resolve-path($source, $module: null)': function resolvePath$source$moduleNull(source, module) {
var modulePath = _utils2.default.isNull(module) ? '' : module.getValue(); var modulePath = _utils2.default.isNull(module) ? '' : module.getValue();
var assetPath = source.getValue(); var assetPath = source.getValue();
Expand All @@ -152,7 +153,7 @@ var Sassport = function () {


return _nodeSass2.default.types.String(assetUrl); return _nodeSass2.default.types.String(assetUrl);
} }
}, 'require($path, $propPath: null, $infer: ' + options.infer + ')', function undefined(file, propPath, infer, done) { }, _defineProperty(_functions, 'require($path, $propPath: null, $infer: ' + options.infer + ')', function undefined(file, propPath, infer, done) {
file = file.getValue(); file = file.getValue();
propPath = _utils2.default.isNull(propPath) ? false : propPath.getValue(); propPath = _utils2.default.isNull(propPath) ? false : propPath.getValue();


Expand All @@ -165,7 +166,9 @@ var Sassport = function () {
return _utils2.default.toSass(data, { return _utils2.default.toSass(data, {
infer: _utils2.default.castToJs(infer) infer: _utils2.default.castToJs(infer)
}); });
}), }), _defineProperty(_functions, 'global($variable)', (0, _wrap2.default)(function (variable) {
return _this._variables[variable];
})), _functions),
importer: this._importer, importer: this._importer,
includePaths: ['node_modules'], includePaths: ['node_modules'],
sassportModules: modules // carried over to node-sass sassportModules: modules // carried over to node-sass
Expand Down Expand Up @@ -253,6 +256,8 @@ var Sassport = function () {
}, { }, {
key: 'globals', key: 'globals',
value: function globals(variableMap) { value: function globals(variableMap) {
this._variables = variableMap;

for (var key in variableMap) { for (var key in variableMap) {
var value = variableMap[key]; var value = variableMap[key];
var sassValue = _utils2.default.sassString(_utils2.default.castToSass(value)); var sassValue = _utils2.default.sassString(_utils2.default.castToSass(value));
Expand Down
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -5,7 +5,9 @@
"main": "dist/index.js", "main": "dist/index.js",
"scripts": { "scripts": {
"start": "babel -w ./src -d ./dist", "start": "babel -w ./src -d ./dist",
"test": "mocha -w --require babel-core/register" "build": "babel ./src -d ./dist",
"test": "npm run build && mocha --require babel-core/register",
"test:watch": "mocha -w --require babel-core/register"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
Expand Down
7 changes: 5 additions & 2 deletions src/index.js
Expand Up @@ -76,8 +76,8 @@ class Sassport {
}; };


this._exports = {}; this._exports = {};

this._loaders = {}; this._loaders = {};
this._variables = {};


this._localPath = path.resolve('./'); this._localPath = path.resolve('./');
this._localAssetPath = null; this._localAssetPath = null;
Expand Down Expand Up @@ -124,7 +124,8 @@ class Sassport {
return utils.toSass(data, { return utils.toSass(data, {
infer: utils.castToJs(infer) infer: utils.castToJs(infer)
}); });
} },
'global($variable)': wrap((variable) => this._variables[variable])
}, },
importer: this._importer, importer: this._importer,
includePaths: ['node_modules'], includePaths: ['node_modules'],
Expand Down Expand Up @@ -204,6 +205,8 @@ class Sassport {
} }


globals(variableMap) { globals(variableMap) {
this._variables = variableMap;

for (let key in variableMap) { for (let key in variableMap) {
let value = variableMap[key]; let value = variableMap[key];
let sassValue = utils.sassString(utils.castToSass(value)); let sassValue = utils.sassString(utils.castToSass(value));
Expand Down
19 changes: 19 additions & 0 deletions test/globals.js
Expand Up @@ -99,4 +99,23 @@ describe('Sassport.globals', function() {
done); done);
}); });
}); });

describe('global() Sass function', () => {
let testModule = sassport()
.globals({
'$foo': 'foo value',
'$bar': 'bar value'
});

it('should retrieve the proper global variables from the root module', (done) => {
testModule.render({
data: 'test { foo: global("$foo"); }',
outputStyle: 'compressed'
}, (err, result) => {
done(assert.equal(
result.css.toString(),
'test{foo:foo value}\n'));
});
});
});
}); });

0 comments on commit c181eb2

Please sign in to comment.