-
Notifications
You must be signed in to change notification settings - Fork 93
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
Alias resolution #81
Open
OscarBarrett
wants to merge
5
commits into
airbnb:master
Choose a base branch
from
OscarBarrett:feature/resolve
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Alias resolution #81
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b248682
Apply https://github.com/airbnb/babel-plugin-inline-react-svg/pull/17
OscarBarrett 1af8341
Updated path resolution
OscarBarrett ed752f7
Support an array of paths for aliases
OscarBarrett 20f3d8f
Added tests, guard alias object access
OscarBarrett 2c68df8
Updated readme
OscarBarrett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,5 +1,10 @@ | ||||||
import { extname, dirname, parse as parseFilename } from 'path'; | ||||||
import { readFileSync } from 'fs'; | ||||||
import { | ||||||
extname, | ||||||
dirname, | ||||||
parse as parseFilename, | ||||||
resolve as resolvePath, | ||||||
} from 'path'; | ||||||
import { readFileSync, existsSync } from 'fs'; | ||||||
import { parse } from '@babel/parser'; | ||||||
import { declare } from '@babel/helper-plugin-utils'; | ||||||
import resolve from 'resolve'; | ||||||
|
@@ -48,7 +53,13 @@ export default declare(({ | |||||
if (typeof importPath !== 'string') { | ||||||
throw new TypeError('`applyPlugin` `importPath` must be a string'); | ||||||
} | ||||||
const { ignorePattern, caseSensitive, filename: providedFilename } = state.opts; | ||||||
const { | ||||||
ignorePattern, | ||||||
caseSensitive, | ||||||
filename: providedFilename, | ||||||
root, | ||||||
alias, | ||||||
} = state.opts; | ||||||
const { file, filename } = state; | ||||||
if (ignorePattern) { | ||||||
// Only set the ignoreRegex once: | ||||||
|
@@ -61,14 +72,40 @@ export default declare(({ | |||||
// This plugin only applies for SVGs: | ||||||
if (extname(importPath) === '.svg') { | ||||||
const iconPath = filename || providedFilename; | ||||||
const svgPath = resolve.sync(importPath, { basedir: dirname(iconPath) }); | ||||||
if (caseSensitive && !fileExistsWithCaseSync(svgPath)) { | ||||||
throw new Error(`File path didn't match case of file on disk: ${svgPath}`); | ||||||
const aliasPart = importPath.split('/')[0]; | ||||||
const aliasMatch = alias ? alias[aliasPart] : undefined; | ||||||
const svgPaths = []; | ||||||
let chosenPath; | ||||||
|
||||||
if (aliasMatch) { | ||||||
const resolveRoot = resolvePath(process.cwd(), root || './'); | ||||||
const aliasMatches = typeof aliasMatch === 'string' ? [aliasMatch] : aliasMatch; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
aliasMatches.forEach((match) => { | ||||||
const aliasedPath = resolvePath(resolveRoot, match); | ||||||
svgPaths.push(resolvePath(aliasedPath, importPath.replace(`${aliasPart}/`, ''))); | ||||||
}); | ||||||
} else { | ||||||
svgPaths.push(resolve.sync(importPath, { basedir: dirname(iconPath) })); | ||||||
} | ||||||
if (!svgPath) { | ||||||
|
||||||
for (let i = 0; i < svgPaths.length; i += 1) { | ||||||
const svgPath = svgPaths[i]; | ||||||
if (caseSensitive && !fileExistsWithCaseSync(svgPath)) { | ||||||
throw new Error(`File path didn't match case of file on disk: ${svgPath}`); | ||||||
} | ||||||
|
||||||
if (svgPath && existsSync(svgPath)) { | ||||||
chosenPath = svgPath; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like this could use .find ? (or the array.prototype.find package) |
||||||
break; | ||||||
} | ||||||
} | ||||||
|
||||||
if (!chosenPath) { | ||||||
throw new Error(`File path does not exist: ${importPath}`); | ||||||
} | ||||||
const rawSource = readFileSync(svgPath, 'utf8'); | ||||||
|
||||||
const rawSource = readFileSync(chosenPath, 'utf8'); | ||||||
const optimizedSource = state.opts.svgo === false | ||||||
? rawSource | ||||||
: optimize(rawSource, state.opts.svgo); | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import React from 'react'; | ||
|
||
import MySvg from 'myalias/CLOSE.svg'; | ||
|
||
export default function MyFunctionIcon() { | ||
return <MySvg />; | ||
} | ||
|
||
export class MyClassIcon extends React.Component { | ||
render() { | ||
return <MySvg />; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import React from 'react'; | ||
|
||
import MySvg from 'myalias/close.svg'; | ||
import Svg2 from 'myalias/close3.svg'; | ||
|
||
export default function MyFunctionIcon() { | ||
return ( | ||
<> | ||
<MySvg /> | ||
<Svg2 /> | ||
</> | ||
); | ||
} | ||
|
||
export class MyClassIcon extends React.Component { | ||
render() { | ||
return ( | ||
<> | ||
<MySvg /> | ||
<Svg2 /> | ||
</> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import React from 'react'; | ||
|
||
import MySvg from 'myalias/close.svg'; | ||
import Svg2 from 'myalias/close2.svg'; | ||
|
||
export default function MyFunctionIcon() { | ||
return ( | ||
<> | ||
<MySvg /> | ||
<Svg2 /> | ||
</> | ||
); | ||
} | ||
|
||
export class MyClassIcon extends React.Component { | ||
render() { | ||
return ( | ||
<> | ||
<MySvg /> | ||
<Svg2 /> | ||
</> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import React from 'react'; | ||
|
||
import MySvg from 'myalias/close.svg'; | ||
|
||
export default function MyFunctionIcon() { | ||
return <MySvg />; | ||
} | ||
|
||
export class MyClassIcon extends React.Component { | ||
render() { | ||
return <MySvg />; | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if someone imports from
__proto__
or toString ?