Skip to content

Commit

Permalink
fix: perf (#840)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Apr 20, 2020
1 parent aeb86f0 commit 744112d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
8 changes: 1 addition & 7 deletions src/index.js
Expand Up @@ -33,15 +33,9 @@ function loader(content) {
: true;

if (shouldUseWebpackImporter) {
const resolve = this.getResolve({
mainFields: ['sass', 'style', 'main', '...'],
mainFiles: ['_index', 'index', '...'],
extensions: ['.sass', '.scss', '.css'],
});

const { includePaths } = sassOptions;

sassOptions.importer.push(webpackImporter(this, resolve, includePaths));
sassOptions.importer.push(webpackImporter(this, includePaths));
}

const callback = this.async();
Expand Down
22 changes: 20 additions & 2 deletions src/webpackImporter.js
Expand Up @@ -19,7 +19,7 @@ const matchCss = /\.css$/i;
* (based on whether the call is sync or async) because otherwise node-sass doesn't exit.
*
*/
function webpackImporter(loaderContext, resolve, includePaths) {
function webpackImporter(loaderContext, includePaths) {
function dirContextFrom(fileContext) {
return path.dirname(
// The first file is 'stdin' when we're using the data option
Expand All @@ -32,7 +32,7 @@ function webpackImporter(loaderContext, resolve, includePaths) {
return Promise.reject();
}

const [{ context, possibleRequests }] = resolutionMap;
const [{ resolve, context, possibleRequests }] = resolutionMap;

return resolve(context, possibleRequests[0])
.then((result) => {
Expand Down Expand Up @@ -60,6 +60,22 @@ function webpackImporter(loaderContext, resolve, includePaths) {
});
}

// We can't use `loaderContext.resolve` because it resolves values from default `extensions`/`mainFields`/etc from webpack configuration
const sassResolve = loaderContext.getResolve({
alias: [],
aliasFields: [],
descriptionFiles: [],
extensions: [],
mainFields: [],
mainFiles: [],
modules: [],
});
const webpackResolve = loaderContext.getResolve({
mainFields: ['sass', 'style', 'main', '...'],
mainFiles: ['_index', 'index', '...'],
extensions: ['.sass', '.scss', '.css'],
});

return (url, prev, done) => {
// The order of import precedence is as follows:
//
Expand All @@ -75,11 +91,13 @@ function webpackImporter(loaderContext, resolve, includePaths) {
const resolutionMap = []
.concat(
includePaths.map((context) => ({
resolve: sassResolve,
context,
possibleRequests: sassPossibleRequests,
}))
)
.concat({
resolve: webpackResolve,
context: dirContextFrom(prev),
possibleRequests: webpackPossibleRequests,
});
Expand Down

0 comments on commit 744112d

Please sign in to comment.