-
Notifications
You must be signed in to change notification settings - Fork 3
/
glob-sass-importer.js
28 lines (24 loc) · 994 Bytes
/
glob-sass-importer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const path = require('node:path');
const glob = require('tiny-glob');
/** @typedef {{file: string} | {contents: string} | null } ResolveResult */
/**
* Custom glob importer
* Needed to implement this because none of the sass glob importers on npm output @use
* @param {string} url Raw path in source code
* @param {string} currentPath Path to the current file
* @param {(result: ResolveResult) => void} done Callback function for async resolvers
* @returns {ResolveResult | void}
*/
const globImporter = (url, currentPath, done) => {
// This is a really lazy check but it is easy and good enough
const isGlob = url.includes('*');
if (!isGlob) return null;
glob(url, { cwd: path.dirname(currentPath) }).then((matchingPaths) => {
// Generates a fake file that has the expanded @forward statements
const contents = matchingPaths
.map((filePath) => `@forward "./src/${filePath}";`)
.join('\n');
done({ contents });
});
};
module.exports = globImporter;