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

fix: perf #840

Merged
merged 1 commit into from
Apr 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 1 addition & 7 deletions src/index.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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