Skip to content

Commit 1c0dfcf

Browse files
Oscar BarrettZaggen
authored andcommitted
[New] Support passing aliases to fix issues with babel module resolver
Co-authored-by: Zaggen <[email protected]> Co-authored-by: Oscar Barrett <[email protected]>
1 parent d94d268 commit 1c0dfcf

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

src/index.js

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { extname, dirname, parse as parseFilename } from 'path';
2-
import { readFileSync } from 'fs';
1+
import { extname, dirname, parse as parseFilename, resolve as resolvePath } from 'path';
2+
import { readFileSync, existsSync } from 'fs';
33
import { parse } from '@babel/parser';
44
import { declare } from '@babel/helper-plugin-utils';
55
import resolve from 'resolve';
@@ -48,7 +48,7 @@ export default declare(({
4848
if (typeof importPath !== 'string') {
4949
throw new TypeError('`applyPlugin` `importPath` must be a string');
5050
}
51-
const { ignorePattern, caseSensitive, filename: providedFilename } = state.opts;
51+
const { ignorePattern, caseSensitive, filename: providedFilename, root, alias } = state.opts;
5252
const { file, filename } = state;
5353
if (ignorePattern) {
5454
// Only set the ignoreRegex once:
@@ -61,14 +61,40 @@ export default declare(({
6161
// This plugin only applies for SVGs:
6262
if (extname(importPath) === '.svg') {
6363
const iconPath = filename || providedFilename;
64-
const svgPath = resolve.sync(importPath, { basedir: dirname(iconPath) });
65-
if (caseSensitive && !fileExistsWithCaseSync(svgPath)) {
66-
throw new Error(`File path didn't match case of file on disk: ${svgPath}`);
64+
const aliasPart = importPath.split('/')[0];
65+
const aliasMatch = alias[aliasPart];
66+
const svgPaths = [];
67+
let chosenPath;
68+
69+
if (aliasMatch) {
70+
const resolveRoot = resolvePath(process.cwd(), root || './');
71+
const aliasMatches = typeof aliasMatch === 'string' ? [aliasMatch] : aliasMatch;
72+
73+
aliasMatches.forEach((match) => {
74+
const aliasedPath = resolvePath(resolveRoot, match);
75+
svgPaths.push(resolvePath(aliasedPath, importPath.replace(`${aliasPart}/`, '')));
76+
});
77+
} else {
78+
svgPaths.push(resolve.sync(importPath, { basedir: dirname(iconPath) }));
6779
}
68-
if (!svgPath) {
80+
81+
for (let i = 0; i < svgPaths.length; i += 1) {
82+
const svgPath = svgPaths[i];
83+
if (caseSensitive && !fileExistsWithCaseSync(svgPath)) {
84+
throw new Error(`File path didn't match case of file on disk: ${svgPath}`);
85+
}
86+
87+
if (svgPath && existsSync(svgPath)) {
88+
chosenPath = svgPath;
89+
break;
90+
}
91+
}
92+
93+
if (!chosenPath) {
6994
throw new Error(`File path does not exist: ${importPath}`);
7095
}
71-
const rawSource = readFileSync(svgPath, 'utf8');
96+
97+
const rawSource = readFileSync(chosenPath, 'utf8');
7298
const optimizedSource = state.opts.svgo === false
7399
? rawSource
74100
: optimize(rawSource, state.opts.svgo);

0 commit comments

Comments
 (0)