Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overwrite sass imports with parcel's resolver #1256

Merged
merged 11 commits into from
Apr 27, 2018
Merged
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this fall back to the default SASS resolver when Parcel doesn't find anything?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this falls back to the default sass resolver. Mainly for built-in sass modules.
Tested this with the example repo of #1182 . In this example bootstrap uses mixins which is catched by the sass resolver

}
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;'));
});
});