From c181eb2097824cdee655a2ae1d1a839ec410944c Mon Sep 17 00:00:00 2001 From: David Khourshid Date: Sun, 31 Jan 2016 20:41:28 -0500 Subject: [PATCH] Adding global() function --- dist/index.js | 15 ++++++++++----- package.json | 4 +++- src/index.js | 7 +++++-- test/globals.js | 19 +++++++++++++++++++ 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/dist/index.js b/dist/index.js index 80438c2..b295349 100644 --- a/dist/index.js +++ b/dist/index.js @@ -91,7 +91,8 @@ var Sassport = function () { */ function Sassport(name) { - var _this = this; + var _this = this, + _functions; var modules = arguments.length <= 1 || arguments[1] === undefined ? [] : arguments[1]; var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; @@ -121,8 +122,8 @@ var Sassport = function () { }; this._exports = {}; - this._loaders = {}; + this._variables = {}; this._localPath = _path2.default.resolve('./'); this._localAssetPath = null; @@ -131,7 +132,7 @@ var Sassport = function () { this._onRequire = options.onRequire.bind(this); this.options = { - functions: _defineProperty({ + functions: (_functions = { 'resolve-path($source, $module: null)': function resolvePath$source$moduleNull(source, module) { var modulePath = _utils2.default.isNull(module) ? '' : module.getValue(); var assetPath = source.getValue(); @@ -152,7 +153,7 @@ var Sassport = function () { 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(); propPath = _utils2.default.isNull(propPath) ? false : propPath.getValue(); @@ -165,7 +166,9 @@ var Sassport = function () { return _utils2.default.toSass(data, { infer: _utils2.default.castToJs(infer) }); - }), + }), _defineProperty(_functions, 'global($variable)', (0, _wrap2.default)(function (variable) { + return _this._variables[variable]; + })), _functions), importer: this._importer, includePaths: ['node_modules'], sassportModules: modules // carried over to node-sass @@ -253,6 +256,8 @@ var Sassport = function () { }, { key: 'globals', value: function globals(variableMap) { + this._variables = variableMap; + for (var key in variableMap) { var value = variableMap[key]; var sassValue = _utils2.default.sassString(_utils2.default.castToSass(value)); diff --git a/package.json b/package.json index 1c4ce01..713943d 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,9 @@ "main": "dist/index.js", "scripts": { "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": { "type": "git", diff --git a/src/index.js b/src/index.js index 46806df..799b1a5 100644 --- a/src/index.js +++ b/src/index.js @@ -76,8 +76,8 @@ class Sassport { }; this._exports = {}; - this._loaders = {}; + this._variables = {}; this._localPath = path.resolve('./'); this._localAssetPath = null; @@ -124,7 +124,8 @@ class Sassport { return utils.toSass(data, { infer: utils.castToJs(infer) }); - } + }, + 'global($variable)': wrap((variable) => this._variables[variable]) }, importer: this._importer, includePaths: ['node_modules'], @@ -204,6 +205,8 @@ class Sassport { } globals(variableMap) { + this._variables = variableMap; + for (let key in variableMap) { let value = variableMap[key]; let sassValue = utils.sassString(utils.castToSass(value)); diff --git a/test/globals.js b/test/globals.js index ce4f3d8..7131ebd 100644 --- a/test/globals.js +++ b/test/globals.js @@ -99,4 +99,23 @@ describe('Sassport.globals', function() { 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')); + }); + }); + }); });