Skip to content

Commit 81a8995

Browse files
committed
Respect includePaths when resolving json imports
Fixes #2
1 parent 9c8f9f8 commit 81a8995

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

index.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,31 @@ module.exports = function(url, prev, done) {
3737
const parts = url.split('/');
3838
const name = path.basename(parts.pop(), '.json');
3939
const cwd = process.cwd();
40-
const resolved = path.join(cwd, url);
40+
41+
42+
const includePaths = this.options.includePaths.split(':')
43+
.map(p => path.resolve(cwd, p));
4144

4245
try {
43-
var json = require(resolved);
46+
let resolved = path.join(cwd, url);
47+
if (exists(resolved)) {
48+
var json = require(resolved);
49+
} else {
50+
for (let i = 0; i < includePaths.length; i++ ) {
51+
resolved = path.join(includePaths[i], url);
52+
53+
if (exists(resolved)) {
54+
var json = require(resolved);
55+
}
56+
}
57+
}
4458
} catch(err) {
4559
return done(err);
4660
}
4761

48-
return done({ contents: `$${name}: (${buildSassMap(json)});` });
62+
if (json) {
63+
return done({ contents: `$${name}: (${buildSassMap(json)});` });
64+
}
65+
66+
return done(null);
4967
};

tests/__snapshots__/index.test.js.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ exports[`json-importer async should resolve json array values as Sass lists 1`]
1212
"
1313
`;
1414

15+
exports[`json-importer async should resolve json files in includePaths 1`] = `
16+
"output {
17+
contents: (\\"color\\": \\"red\\", \\"size\\": \\"small\\"); }
18+
"
19+
`;
20+
1521
exports[`json-importer async should resolve nested json strings as Sass map 1`] = `
1622
"output {
1723
contents: (\\"colors\\": (\\"red\\": \\"#f00\\", \\"blue\\": \\"#00f\\"), \\"sizes\\": (\\"small\\": \\"16px\\", \\"big\\": \\"30px\\")); }
@@ -30,6 +36,12 @@ exports[`json-importer sync should resolve json array values as Sass lists 1`] =
3036
"
3137
`;
3238

39+
exports[`json-importer sync should resolve json files in includePaths 1`] = `
40+
"output {
41+
contents: (\\"color\\": \\"red\\", \\"size\\": \\"small\\"); }
42+
"
43+
`;
44+
3345
exports[`json-importer sync should resolve nested json strings as Sass map 1`] = `
3446
"output {
3547
contents: (\\"colors\\": (\\"red\\": \\"#f00\\", \\"blue\\": \\"#00f\\"), \\"sizes\\": (\\"small\\": \\"16px\\", \\"big\\": \\"30px\\")); }

tests/index.test.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ const generate = (file) => `
77
output { contents: inspect($${path.basename(file, '.json')}) }
88
`;
99

10-
const compile = function(data) {
10+
const compile = function(data, options = {}) {
1111
return new Promise((yeah, nah) => {
1212
return sass.render(
13-
{ data, importer },
13+
Object.assign({ data, importer }, options),
1414
(err, results) => err ? nah(err) : yeah(results.css.toString()),
1515
);
1616
});
1717
}
1818

19-
const compileSync = function(data) {
19+
const compileSync = function(data, options = {}) {
2020
return new Promise((yeah, nah) => {
2121
try {
22-
const results = sass.renderSync({ data, importer });
22+
const results = sass.renderSync(Object.assign({ data, importer }, options));
2323
yeah(results.css.toString());
2424
} catch (err) {
2525
nah(err);
@@ -42,6 +42,10 @@ describe('json-importer', () => {
4242
func(generate('tests/fixtures/list.json'))
4343
.then(result => expect(result).toMatchSnapshot())
4444
));
45+
it('should resolve json files in includePaths', () => (
46+
func(generate('fixtures/flat.json'), { includePaths: ['tests']})
47+
.then(result => expect(result).toMatchSnapshot())
48+
));
4549
});
4650
});
4751
});

0 commit comments

Comments
 (0)