forked from shakacode/sass-resources-loader
/
rewritePaths.js
47 lines (35 loc) · 1.45 KB
/
rewritePaths.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import path from 'path';
import logger from './logger';
const useRegexp = /@(import|use)\s+(?:'([^']+)'|"([^"]+)"|([^\s;]+))/g;
export const getRelativeUsePath = (oldUsePath, absoluteUsePath, moduleContext) => {
// from node_modules
if ((/^~/).test(oldUsePath)) {
return oldUsePath;
}
return path.relative(moduleContext, absoluteUsePath);
};
export default function rewritePaths(error, file, contents, moduleContext, callback) {
if (error) {
logger.debug('Resources: **not found**');
return callback(error);
}
if (!/\.s[ac]ss$/i.test(file)) {
return callback(null, contents);
}
const rewritten = contents.replace(useRegexp, (entire, importer, single, double, unquoted) => {
// Don't rewrite imports from built-ins
if ([single, double].some(match => match && match.indexOf('sass:') === 0)) {
return entire;
}
const oldUsePath = single || double || unquoted;
const absoluteUsePath = path.join(path.dirname(file), oldUsePath);
const relUsePath = getRelativeUsePath(oldUsePath, absoluteUsePath, moduleContext);
const newUsePath = relUsePath.split(path.sep).join('/');
logger.debug(`Resources: @${importer} of ${oldUsePath} changed to ${newUsePath}`);
const lastCharacter = entire[entire.length - 1];
const quote = lastCharacter === "'" || lastCharacter === '"' ? lastCharacter : '';
return `@${importer} ${quote}${newUsePath}${quote}`;
});
callback(null, rewritten);
return undefined;
}