Skip to content

Commit

Permalink
test: more
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Dec 4, 2020
1 parent 307b955 commit 698d1b0
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/utils.js
Expand Up @@ -454,8 +454,6 @@ function getWebpackResolver(
};
}

const matchCss = /\.css$/i;

function getWebpackImporter(loaderContext, implementation, includePaths) {
const resolve = getWebpackResolver(
loaderContext.getResolve,
Expand All @@ -467,12 +465,31 @@ function getWebpackImporter(loaderContext, implementation, includePaths) {
return (originalUrl, prev, done) => {
resolve(prev, originalUrl)
.then((result) => {
let normalizedResult = path.normalize(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));
// By removing the CSS file extension, we trigger node-sass to include the CSS file instead of just linking it.
done({ file: result.replace(matchCss, "") });
loaderContext.addDependency(normalizedResult);

const extension = path.extname(normalizedResult);

if (extension.toLowerCase() === ".css") {
// By removing the CSS file extension, we trigger node-sass to include the CSS file instead of just linking it.
normalizedResult = normalizedResult.replace(/\.css$/i, "");
} else {
// Allows `dart-sass` to resolve `.import` files
const basename = path.basename(normalizedResult, extension);

if (path.extname(basename) === ".import") {
normalizedResult = normalizedResult.replace(
new RegExp(`.import${extension}$`),
extension
);
}
}

done({ file: normalizedResult });
})
// Catch all resolving errors, return the original file and pass responsibility back to other custom importers
.catch(() => {
Expand Down
18 changes: 18 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Expand Up @@ -120,6 +120,24 @@ exports[`loader should load only sass/scss files for the "mainFiles" (node-sass)

exports[`loader should load only sass/scss files for the "mainFiles" (node-sass) (scss): warnings 1`] = `Array []`;

exports[`loader should not use .import.sass files (dart-sass) (sass): errors 1`] = `
Array [
"ModuleBuildError: Module build failed (from ../src/cjs.js):
SassError: Can't find stylesheet to import.",
]
`;

exports[`loader should not use .import.sass files (dart-sass) (sass): warnings 1`] = `Array []`;

exports[`loader should not use .import.scss files (dart-sass) (scss): errors 1`] = `
Array [
"ModuleBuildError: Module build failed (from ../src/cjs.js):
SassError: Can't find stylesheet to import.",
]
`;

exports[`loader should not use .import.scss files (dart-sass) (scss): warnings 1`] = `Array []`;

exports[`loader should output an understandable error (dart-sass) (sass): errors 1`] = `
Array [
"ModuleBuildError: Module build failed (from ../src/cjs.js):
Expand Down
12 changes: 12 additions & 0 deletions test/loader.test.js
Expand Up @@ -1486,6 +1486,18 @@ describe("loader", () => {
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});

it(`should not use .import.${syntax} files (${implementationName}) (${syntax})`, async () => {
const testId = getTestId("use-index-import", syntax);
const options = {
implementation: getImplementationByName(implementationName),
};
const compiler = getCompiler(testId, { loader: { options } });
const stats = await compile(compiler);

expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});
}
});
});
Expand Down
1 change: 1 addition & 0 deletions test/sass/use-index-import.sass
@@ -0,0 +1 @@
@use 'index-import'
1 change: 1 addition & 0 deletions test/scss/use-index-import.scss
@@ -0,0 +1 @@
@use 'index-import';

0 comments on commit 698d1b0

Please sign in to comment.