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

feat: pass loaderContext to custom importer #513

Closed
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/normalizeOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function normalizeOptions(loaderContext, content, webpackImporter) {
}

// Allow passing custom importers to `node-sass`. Accepts `Function` or an array of `Function`s.
options.importer = options.importer ? proxyCustomImporters(options.importer, resourcePath) : [];
options.importer = options.importer ? proxyCustomImporters(options.importer, resourcePath, loaderContext) : [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case this is lands resourcePath && loaderContext should be merged here, since resourcePath is already on the loaderContext

options.importer.push(webpackImporter);

// `node-sass` uses `includePaths` to resolve `@import` paths. Append the currently processed file.
Expand Down
6 changes: 5 additions & 1 deletion lib/proxyCustomImporters.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@
*
* @param {function|Array<function>} importer
* @param {string} resourcePath
* @param {LoaderContext} loaderContext
* @returns {Array<function>}
*/
function proxyCustomImporters(importer, resourcePath) {
function proxyCustomImporters(importer, resourcePath, loaderContext) {
return [].concat(importer).map((importer) => {
return function (url, prev, done) {
// eslint-disable-next-line no-invalid-this
this.loaderContext = loaderContext;

return importer.apply(
this, // eslint-disable-line no-invalid-this
Array.from(arguments)
Expand Down