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
Fix transformer-svg-react not finding .svgrrc's #7741
Merged
Merged
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
72702d9
Fix transformer-svg-react not finding .svgrrc's
coolreader18 279d8f8
Use config.loadConfig to load config
coolreader18 e10c41b
Deduplicate getComponentName, svgr has the same function
coolreader18 f2086a3
Add test for svg-react .svgrrc
coolreader18 8193567
Merge branch 'v2' into patch-1
devongovett 54f3aaf
Merge branch 'v2' into patch-1
mischnic 5bfb823
Merge branch 'v2' into 7741
mischnic 3fa24c8
Fix assertion
mischnic c1e2817
Apply svgo config
mischnic ea2c9ee
Merge branch 'v2' into patch-1
mischnic a2d6262
invalidateOnStartup with cjs for various transfomers
mischnic f2fbb0e
Unify valid svgo config names
mischnic 6e96f75
Fixup test
mischnic d036ab4
Merge branch 'v2' into 7741
mischnic File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
3 changes: 3 additions & 0 deletions
3
packages/core/integration-tests/test/integration/svg-react-config/.svgorc.json
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,3 @@ | ||
{ | ||
"floatPrecision": 0 | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/core/integration-tests/test/integration/svg-react-config/.svgrrc.json
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,4 @@ | ||
{ | ||
"icon": true, | ||
"jsxRuntime": "classic-preact" | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/core/integration-tests/test/integration/svg-react-config/icon.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions
1
packages/core/integration-tests/test/integration/svg-react-config/index.html
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 @@ | ||
<img src="icon.svg" /> |
5 changes: 5 additions & 0 deletions
5
packages/core/integration-tests/test/integration/svg-react-config/package.json
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,5 @@ | ||
{ | ||
"dependencies": { | ||
"preact": "*" | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/core/integration-tests/test/integration/svg-react-config/react.js
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,4 @@ | ||
const { h } = require('preact'); | ||
const PreactIcon = require('./icon.svg'); | ||
|
||
module.exports = <PreactIcon />; |
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
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 |
---|---|---|
|
@@ -3,41 +3,61 @@ | |
import {Transformer} from '@parcel/plugin'; | ||
|
||
import path from 'path'; | ||
import camelcase from 'camelcase'; | ||
import svgoPlugin from '@svgr/plugin-svgo'; | ||
import jsxPlugin from '@svgr/plugin-jsx'; | ||
import {transform} from '@svgr/core'; | ||
|
||
function getComponentName(filePath) { | ||
let validCharacters = /[^a-zA-Z0-9_-]/g; | ||
let name = path.parse(filePath).name.replace(validCharacters, ''); | ||
return camelcase(name, { | ||
pascalCase: true, | ||
}); | ||
} | ||
|
||
export default (new Transformer({ | ||
async loadConfig({config}) { | ||
let conf = await config.getConfig(['.svgrrc.json', '.svgrrc']); | ||
return conf?.contents; | ||
let svgrResult = await config.getConfig([ | ||
'.svgrrc', | ||
'.svgrrc.json', | ||
'.svgrrc.js', | ||
'.svgrrc.cjs', | ||
'svgr.config.json', | ||
'svgr.config.js', | ||
'svgr.config.cjs', | ||
]); | ||
let svgoResult = await config.getConfig([ | ||
'.svgorc', | ||
'.svgorc.json', | ||
'.svgorc.js', | ||
'.svgorc.cjs', | ||
'svgo.config.json', | ||
'svgo.config.js', | ||
'svgo.config.cjs', | ||
]); | ||
if (svgrResult) { | ||
let isJavascript = path.extname(svgrResult.filePath) === '.js'; | ||
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. These should also handle |
||
if (isJavascript) { | ||
config.invalidateOnStartup(); | ||
} | ||
} | ||
if (svgoResult) { | ||
let isJavascript = path.extname(svgoResult.filePath) === '.js'; | ||
if (isJavascript) { | ||
config.invalidateOnStartup(); | ||
} | ||
} | ||
return {svgr: svgrResult?.contents, svgo: svgoResult?.contents}; | ||
}, | ||
|
||
async transform({asset, config}) { | ||
let code = await asset.getCode(); | ||
let componentName = getComponentName(asset.filePath); | ||
|
||
const jsx = await transform( | ||
code, | ||
{}, | ||
{svgoConfig: config.svgo, ...config.svgr, runtimeConfig: false}, | ||
{ | ||
caller: { | ||
name: '@parcel/transformer-svg-react', | ||
defaultPlugins: [svgoPlugin, jsxPlugin], | ||
}, | ||
filePath: componentName, | ||
filePath: asset.filePath, | ||
}, | ||
); | ||
|
||
asset.type = config?.typescript ? 'tsx' : 'jsx'; | ||
asset.type = config.svgr?.typescript ? 'tsx' : 'jsx'; | ||
asset.bundleBehavior = null; | ||
asset.setCode(jsx); | ||
|
||
|
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.
Looks like the SVG optimizer only supports
svgo.config.*
. Maybe we should match that here?