Skip to content

Instantly share code, notes, and snippets.

@ethanve
Last active September 18, 2021 09:16
Show Gist options
  • Select an option

  • Save ethanve/598b614f2d0f1b8530c27757cdc08b21 to your computer and use it in GitHub Desktop.

Select an option

Save ethanve/598b614f2d0f1b8530c27757cdc08b21 to your computer and use it in GitHub Desktop.
Custom Webpack loader that prepends a Sass import to each imported Sass file
/**
* Loader that automatically prepends a reference to sassIncludes in the Webpack configuration files
*/
import fs from 'fs';
import path from 'path';
import config from './config';
let fileFound = true;
if (!config.sassIncludes || !fs.existsSync(config.sassIncludes)) {
console.warn('Can not find the sassIncludes files');
fileFound = false;
}
module.exports = function (content) {
this.cacheable && this.cacheable();
if (!fileFound) {
return content;
}
const relativePath = path.relative(path.dirname(this.resourcePath), config.sassIncludes);
return `import "${relativePath}"; ${content}`;
};
@danilvalov
Copy link

@ethanve: Please, change fs.exists to fs.existsSync. fs.exists returns undefined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment