Skip to content

Commit

Permalink
Merge pull request #89 from alfredriesen/master
Browse files Browse the repository at this point in the history
feat: accept js files to be able to compose json structure
  • Loading branch information
pmowrer authored Apr 22, 2020
2 parents 2d10a37 + 05479f6 commit b7b0d71
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down

0 comments on commit b7b0d71

Please sign in to comment.