Open hotyhuang opened 2 years ago
@hotyhuang From my own digging, it looks like this plugin doesn't properly handle relative paths. It seems to have been coded with the assumption that it will always receive absolute paths from Rollup, so it fails silently when it encounters an SVG import with a relative path (as outputted by the alias plugin). Since the SVGR plugin fails to handle the import, Rollup ends up trying to do so itself and treats the SVG file as JS, leading to the "Unexpected token"
error.
The fact that Rollup itself clearly seems to resolve the relative path just fine serves as evidence that the problem is with this plugin. In the meantime, this should fix the immediate issue for you:
alias({
entries: {
"@src": path.resolve(__dirname, "./src")
}
})
@MastaAaron THANK YOU. I was debugging the alias plugin + SVGr + rollup for like 2 hours and your tip about relative paths finally helped me fix my problem
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This is still an issue, FYI.
I confirm that this is still an issue FYI @gregberge
Still an issue for me.
I finally solved this issue by using alias plugin as follow:
import alias from '@rollup/plugin-alias';
import svgr from '@svgr/rollup';
import path from 'path';
// ....
export default [
{
//...
plugins: [
// other plugins ...
alias({
entries: {
"@": path.resolve(import.meta.dirname, "./src")
}
}),
svgr(),
],
// ...
},
];
The images are in the folder "assets" under "src".
I use them like this in the source files:
import MyIcon from '@/assets/images/my_icon.svg';
<MyIcon />
Even if you have the path defined in tsconfig.json for the source files, it does not apply for *.svg files, that's why you need the alias plugin too.
Hope it will help.
🐛 Bug Report
When I use
@svgr/rollup
and@rollup/plugin-alias
together, looks like it cannot understand the alias that I have set up. Here is the error:To Reproduce
Below is my configs & code snippet. Then I get the error after typing cmd
npm run build
.rollup.config.js
SomeComponent.tsx
in package.json
Expected behavior
The rollup cmd should not fail.
Link to repl or repo (highly encouraged)
Sry I cannot share the link, bcs that belongs to my current employer's org... You should be able to easily reproduce by adding
@rollup/plugin-alias
and use the alias for the svg imports. But let me know if a POC repo is really needed.Workaround
The errors are gone after I replaced the alias (for svg files only) with relative paths.
Run
npx envinfo --system --binaries --npmPackages @svgr/core,@svgr/cli,@svgr/webpack,@svgr/rollup --markdown --clipboard
Paste the results here: