Skip to content

Commit

Permalink
Respect includePaths when resolving json imports
Browse files Browse the repository at this point in the history
Fixes #2
  • Loading branch information
xzyfer committed Jul 15, 2018
1 parent 9c8f9f8 commit 81a8995
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
24 changes: 21 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,31 @@ module.exports = function(url, prev, done) {
const parts = url.split('/');
const name = path.basename(parts.pop(), '.json');
const cwd = process.cwd();
const resolved = path.join(cwd, url);


const includePaths = this.options.includePaths.split(':')
.map(p => path.resolve(cwd, p));

try {
var json = require(resolved);
let resolved = path.join(cwd, url);
if (exists(resolved)) {
var json = require(resolved);
} else {
for (let i = 0; i < includePaths.length; i++ ) {
resolved = path.join(includePaths[i], url);

if (exists(resolved)) {
var json = require(resolved);
}
}
}
} catch(err) {
return done(err);
}

return done({ contents: `$${name}: (${buildSassMap(json)});` });
if (json) {
return done({ contents: `$${name}: (${buildSassMap(json)});` });
}

return done(null);
};
12 changes: 12 additions & 0 deletions tests/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ exports[`json-importer async should resolve json array values as Sass lists 1`]
"
`;

exports[`json-importer async should resolve json files in includePaths 1`] = `
"output {
contents: (\\"color\\": \\"red\\", \\"size\\": \\"small\\"); }
"
`;

exports[`json-importer async should resolve nested json strings as Sass map 1`] = `
"output {
contents: (\\"colors\\": (\\"red\\": \\"#f00\\", \\"blue\\": \\"#00f\\"), \\"sizes\\": (\\"small\\": \\"16px\\", \\"big\\": \\"30px\\")); }
Expand All @@ -30,6 +36,12 @@ exports[`json-importer sync should resolve json array values as Sass lists 1`] =
"
`;

exports[`json-importer sync should resolve json files in includePaths 1`] = `
"output {
contents: (\\"color\\": \\"red\\", \\"size\\": \\"small\\"); }
"
`;

exports[`json-importer sync should resolve nested json strings as Sass map 1`] = `
"output {
contents: (\\"colors\\": (\\"red\\": \\"#f00\\", \\"blue\\": \\"#00f\\"), \\"sizes\\": (\\"small\\": \\"16px\\", \\"big\\": \\"30px\\")); }
Expand Down
12 changes: 8 additions & 4 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ const generate = (file) => `
output { contents: inspect($${path.basename(file, '.json')}) }
`;

const compile = function(data) {
const compile = function(data, options = {}) {
return new Promise((yeah, nah) => {
return sass.render(
{ data, importer },
Object.assign({ data, importer }, options),
(err, results) => err ? nah(err) : yeah(results.css.toString()),
);
});
}

const compileSync = function(data) {
const compileSync = function(data, options = {}) {
return new Promise((yeah, nah) => {
try {
const results = sass.renderSync({ data, importer });
const results = sass.renderSync(Object.assign({ data, importer }, options));
yeah(results.css.toString());
} catch (err) {
nah(err);
Expand All @@ -42,6 +42,10 @@ describe('json-importer', () => {
func(generate('tests/fixtures/list.json'))
.then(result => expect(result).toMatchSnapshot())
));
it('should resolve json files in includePaths', () => (
func(generate('fixtures/flat.json'), { includePaths: ['tests']})
.then(result => expect(result).toMatchSnapshot())
));
});
});
});

0 comments on commit 81a8995

Please sign in to comment.