Skip to content

Commit

Permalink
use resolver for imports (#1256)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasper De Moor authored and devongovett committed Apr 27, 2018
1 parent e9b249c commit 31190cf
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/assets/SASSAsset.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const localRequire = require('../utils/localRequire');
const promisify = require('../utils/promisify');
const path = require('path');
const os = require('os');
const Resolver = require('../Resolver');
const syncPromise = require('../utils/syncPromise');

class SASSAsset extends Asset {
constructor(name, pkg, options) {
Expand All @@ -14,6 +16,10 @@ class SASSAsset extends Asset {
// node-sass should be installed locally in the module that's being required
let sass = await localRequire('node-sass', this.name);
let render = promisify(sass.render.bind(sass));
const resolver = new Resolver({
extensions: ['.scss', '.sass'],
rootDir: this.options.rootDir
});

let opts =
this.package.sass ||
Expand All @@ -35,6 +41,20 @@ class SASSAsset extends Asset {
}
});

opts.importer = (url, prev, done) => {
let resolved;
try {
resolved = syncPromise(
resolver.resolve(url, prev === 'stdin' ? this.name : prev)
).path;
} catch (e) {
resolved = url;
}
return done({
file: resolved
});
};

return await render(opts);
}

Expand Down
2 changes: 2 additions & 0 deletions test/integration/sass-advanced-import/hello.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.hello
color: blue;
5 changes: 5 additions & 0 deletions test/integration/sass-advanced-import/index.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import '~/hello.sass';

.index
background: #ffffff;
color: #000000;
14 changes: 14 additions & 0 deletions test/sass.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,18 @@ describe('sass', function() {
let css = fs.readFileSync(__dirname + '/dist/index.css', 'utf8');
assert(css.includes('._index_1a1ih_1'));
});

it('should support advanced import syntax', async function() {
let b = await bundle(
__dirname + '/integration/sass-advanced-import/index.sass'
);

assertBundleTree(b, {
name: 'index.css',
assets: ['index.sass']
});

let css = fs.readFileSync(__dirname + '/dist/index.css', 'utf8');
assert(css.includes('color: blue;'));
});
});

0 comments on commit 31190cf

Please sign in to comment.