Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/normalizeOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,16 @@ function normalizeOptions(loaderContext, content, webpackImporter) {
options.importer = options.importer
? proxyCustomImporters(options.importer, resourcePath)
: [];
options.importer.push(webpackImporter);

if (options.resolver === false) {
// disable webpack resolved in favor built-in node-sass/sass resolver
} else if (typeof options.resolver === 'function') {
options.importer = options.importer.concat(
proxyCustomImporters(options.resolver, resourcePath)
);
} else {
options.importer.push(webpackImporter);
}

// `node-sass` uses `includePaths` to resolve `@import` paths. Append the currently processed file.
options.includePaths = options.includePaths || [];
Expand Down
41 changes: 11 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const dartSass = require('sass');
const mockRequire = require('mock-require');

const customImporter = require('./tools/customImporter.js');
const customResolver = require('./tools/customResolver.js');
const customFunctions = require('./tools/customFunctions.js');

const pathToSassLoader = require.resolve('../lib/loader.js');
Expand Down Expand Up @@ -185,6 +186,19 @@ implementations.forEach((implementation) => {
importer: customImporter,
}));
});
describe('custom resolver', () => {
it('should use custom resolver', () =>
execTest('custom-resolver', {
resolver: customResolver,
}));
});
describe('no resolver', () => {
it('should use custom resolver', () =>
execTest('no-resolver', {
resolver: false,
includePaths: [path.join(__dirname, 'node_modules', 'scss')],
}));
});
describe('custom functions', () => {
it('should expose custom functions', () =>
execTest('custom-functions', {
Expand Down
3 changes: 3 additions & 0 deletions test/node_modules/scss/unresolvable.scss

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/sass/custom-resolver.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import import-with-custom-resolver
1 change: 1 addition & 0 deletions test/sass/no-resolver.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import unresolvable
1 change: 1 addition & 0 deletions test/scss/custom-resolver.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import 'import-with-custom-resolver';
1 change: 1 addition & 0 deletions test/scss/no-resolver.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import 'unresolvable';
9 changes: 9 additions & 0 deletions test/tools/createSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const dartSass = require('sass');
const nodeSass = require('node-sass');

const customImporter = require('./customImporter.js');
const customResolver = require('./customResolver.js');
const customFunctions = require('./customFunctions.js');

const implementations = [nodeSass, dartSass];
Expand All @@ -22,6 +23,10 @@ function createSpec(ext) {
basePath,
path.resolve(testFolder, '..', 'node_modules', 'bootstrap-sass')
);
const pathToUnresolvable = path.relative(
basePath,
path.resolve(testFolder, 'node_modules', 'scss', 'unresolvable.scss')
);
const pathToScopedNpmPkg = path.relative(
basePath,
path.resolve(testFolder, 'node_modules', '@org', 'pkg', './index.scss')
Expand Down Expand Up @@ -73,6 +78,9 @@ function createSpec(ext) {
if (url === 'import-with-custom-logic') {
return customImporter.returnValue;
}
if (url === 'import-with-custom-resolver') {
return customResolver.returnValue;
}
// Do not transform css imports
if (/\.css$/.test(url) === false) {
// eslint-disable-next-line no-param-reassign
Expand All @@ -83,6 +91,7 @@ function createSpec(ext) {
.replace(/^~@org\/pkg/, pathToScopedNpmPkg)
.replace(/^~module/, pathToModule)
.replace(/^~another/, pathToAnother)
.replace(/^unresolvable/, pathToUnresolvable)
.replace(/^~/, testNodeModules)
.replace(/^path-to-alias/, pathToFooAlias);
}
Expand Down
18 changes: 18 additions & 0 deletions test/tools/customResolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

require('should');

function customResolver(path, prev) {
path.should.equal('import-with-custom-resolver');
prev.should.match(/(sass|scss)[/\\]custom-resolver\.(scss|sass)/);

this.should.have.property('options'); // eslint-disable-line no-invalid-this

return customResolver.returnValue;
}

customResolver.returnValue = {
contents: '.custom-resolver {}',
};

module.exports = customResolver;