Skip to content

Commit

Permalink
refactor: code
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed Jan 6, 2021
1 parent 997d60e commit 087c394
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 18 deletions.
27 changes: 14 additions & 13 deletions src/utils.js
Expand Up @@ -245,24 +245,25 @@ function getPossibleRequests(
forWebpackResolver = false,
rootContext = false
) {
// Maybe it is server-relative URLs
const root = forWebpackResolver && rootContext;
let request;

if (typeof root === "string" && /^\//.test(url)) {
request = root + url;
} else {
request = url;
}
// In case there is module request, send this to webpack resolver
if (forWebpackResolver) {
// Maybe it is server-relative URLs
request =
typeof rootContext === "string" && /^\//.test(url)
? rootContext + url
: url;

// A `~` makes the url an module
if (moduleRequestRegex.test(request)) {
request = request.replace(moduleRequestRegex, "");
}

// In case there is module request, send this to webpack resolver
if (forWebpackResolver && isModuleImport.test(url)) {
return [...new Set([request, url])];
if (isModuleImport.test(url)) {
request = request[request.length - 1] === "/" ? request : `${request}/`;

return [...new Set([request, url])];
}
} else {
request = url;
}

// Keep in mind: ext can also be something like '.datepicker' when the true extension is omitted and the filename contains a dot.
Expand Down
5 changes: 5 additions & 0 deletions test/helpers/getCodeFromSass.js
Expand Up @@ -217,6 +217,10 @@ function getCodeFromSass(testId, options) {
testFolder,
"node_modules/@org/pkg/index.scss"
);
const pathToScopedNpmFile = path.resolve(
testFolder,
"node_modules/@org/style.scss"
);
const pathToSCSSCustomSassField = path.resolve(
testFolder,
"node_modules/scss-custom-sass-field/nested/style.scss"
Expand Down Expand Up @@ -726,6 +730,7 @@ function getCodeFromSass(testId, options) {
pathToSassIndexAlias
)
.replace(/^~@org\/pkg/, pathToScopedNpmPkg)
.replace(/^@org\/style/, pathToScopedNpmFile)
.replace(/^~bootstrap-sass$/, pathToBootstrap3Entry)
.replace(/^~bootstrap-sass/, pathToBootstrap3Package)
.replace(/^~bootstrap$/, pathToBootstrap4Entry)
Expand Down
2 changes: 1 addition & 1 deletion test/loader.test.js
Expand Up @@ -638,7 +638,7 @@ describe("loader", () => {
});

it(`should work with the "bootstrap" package, import as a package (${implementationName}) (${syntax})`, async () => {
const testId = getTestId("bootstrap-package", syntax);
const testId = getTestId("bootstrap", syntax);
const options = {
implementation: getImplementationByName(implementationName),
};
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion test/sass/imports.sass
Expand Up @@ -25,4 +25,4 @@
/* @import ~another */
@import ~another
// Should prefer `scss`
@import "~@org/style"
@import "@org/style"
2 changes: 1 addition & 1 deletion test/sass/uses.sass
Expand Up @@ -20,4 +20,4 @@
/* @use ~another */
@use "~another" as another
// Should prefer `scss`
@use "~@org/style"
@use "@org/style"
File renamed without changes.
2 changes: 1 addition & 1 deletion test/scss/imports.scss
Expand Up @@ -26,4 +26,4 @@
@import "~another";
/* @import "~@org/style"; */
// Should prefer `scss`
@import "~@org/style";
@import "@org/style";
2 changes: 1 addition & 1 deletion test/scss/uses.scss
Expand Up @@ -20,4 +20,4 @@
@use "~another" as another;
/* @use "~@org/style"; */
// Should prefer `scss`
@use "~@org/style";
@use "@org/style";

0 comments on commit 087c394

Please sign in to comment.