diff --git a/README.md b/README.md index f05f65d..d7a33b3 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,9 @@ export default { }; ``` -## Importing strings +## Importing + +### Importing strings Since JSON doesn't map directly to SASS's data types, a common source of confusion is how to handle strings. While [SASS allows strings to be both quoted and unquoted](http://sass-lang.com/documentation/file.SASS_REFERENCE.html#sass-script-strings), strings containing spaces, commas and/or other special characters have to be wrapped in quotes. In terms of JSON, this means the string has to be double quoted: ##### Incorrect @@ -112,6 +114,21 @@ See discussion here for more: https://github.com/Updater/node-sass-json-importer/pull/5 +### Importing *.js Files + +You can also import *.js Files. This way you can use javascript to compose and export json structure for node-sass-json-importer. +``` +const xl = require('./variables.json') +const md = require('./variables-md.json') +const xs = require('./variables-xs.json') + +module.exports = { + xl, + md, + xs, +} +``` + ## Custom resolver Should you care to resolve paths, say, starting with `~/` relative to project root or some other arbitrary directory, you can do it as follows: diff --git a/src/index.js b/src/index.js index 83cd949..c4078d9 100644 --- a/src/index.js +++ b/src/index.js @@ -44,7 +44,7 @@ export default function(options = {}) { } export function isJSONfile(url) { - return /\.json5?$/.test(url); + return /\.js(on5?)?$/.test(url); } export function transformJSONtoSass(json) { diff --git a/test/index.js b/test/index.js index 4ec0677..73cf54f 100644 --- a/test/index.js +++ b/test/index.js @@ -313,6 +313,10 @@ describe('isJSONfile', function() { expect(isJSONfile('/test/variables.json5')).to.be.true; }); + it('returns true if the given URL is a JS file', function() { + expect(isJSONfile('/test/composed-variables.js')).to.be.true; + }); + it('returns false if the given URL is not a JSON or JSON5 file', function() { expect(isJSONfile('/test/variables.not-json-or-json5')).to.be.false; });