Skip to content

Commit

Permalink
refactor: code (#877)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Aug 5, 2020
1 parent 38d4fc1 commit a255a9c
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions src/utils.js
Expand Up @@ -268,37 +268,39 @@ const isSpecialModuleImport = /^~[^/]+$/;
const isNativeWin32Path = /^[a-zA-Z]:[/\\]|^\\\\/i;

function getWebpackImporter(loaderContext, implementation, includePaths) {
function startResolving(resolutionMap) {
async function startResolving(resolutionMap) {
if (resolutionMap.length === 0) {
return Promise.reject();
}

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

return resolve(context, possibleRequests[0])
.then((result) => {
// Add the result as dependency.
// Although we're also using stats.includedFiles, this might come in handy when an error occurs.
// In this case, we don't get stats.includedFiles from node-sass/sass.
loaderContext.addDependency(path.normalize(result));
let result;

// By removing the CSS file extension, we trigger node-sass to include the CSS file instead of just linking it.
return { file: result.replace(matchCss, '') };
})
.catch(() => {
const [, ...tailResult] = possibleRequests;
try {
result = await resolve(context, possibleRequests[0]);
} catch (_ignoreError) {
const [, ...tailResult] = possibleRequests;

if (tailResult.length === 0) {
const [, ...tailResolutionMap] = resolutionMap;
if (tailResult.length === 0) {
const [, ...tailResolutionMap] = resolutionMap;

return startResolving(tailResolutionMap);
}
return startResolving(tailResolutionMap);
}

// eslint-disable-next-line no-param-reassign
resolutionMap[0].possibleRequests = tailResult;
// eslint-disable-next-line no-param-reassign
resolutionMap[0].possibleRequests = tailResult;

return startResolving(resolutionMap);
}

// Add the result as dependency.
// Although we're also using stats.includedFiles, this might come in handy when an error occurs.
// In this case, we don't get stats.includedFiles from node-sass/sass.
loaderContext.addDependency(path.normalize(result));

return startResolving(resolutionMap);
});
// By removing the CSS file extension, we trigger node-sass to include the CSS file instead of just linking it.
return { file: result.replace(matchCss, '') };
}

const sassResolve = loaderContext.getResolve({
Expand Down Expand Up @@ -394,7 +396,7 @@ function getWebpackImporter(loaderContext, implementation, includePaths) {
startResolving(resolutionMap)
// Catch all resolving errors, return the original file and pass responsibility back to other custom importers
.catch(() => ({ file: originalUrl }))
.then(done);
.then((result) => done(result));
};
}

Expand Down

0 comments on commit a255a9c

Please sign in to comment.