Closed wood3n closed 3 years ago
That can't work, because a webpack alias is applied long after babel is run. Instead, don't use any webpack aliases, and use a babel transform instead - and then, if it runs before this plugin, everything should work fine.
That can't work, because a webpack alias is applied long after babel is run. Instead, don't use any webpack aliases, and use a babel transform instead - and then, if it runs before this plugin, everything should work fine.
Thank you for your answers and suggestions, I tried a new babel plugin —— babel-plugin-module-resolver to solve webpack resolve.alias
problem.This is my solution:
yarn add babel-plugin-module-resolver -D
and then apply babel-plugin-module-resolver
and babel-plugin-inline-react-svg
in webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.m?jsx?$/,
exclude: /(node_modules)/,
use: {
loader: "babel-loader",
options: {
presets: ["@babel/preset-env", "@babel/preset-react"],
plugins: [
"@babel/plugin-proposal-class-properties",
"inline-react-svg", // apply babel-plugin-inline-react-svg
[
"module-resolver", // apply babel-plugin-module-resolver
{
alias: {
"@": "./src",
},
},
],
],
},
},
...
}
}
That should indeed work, as long as the order of the two transforms is correct (i forget if your comment, or the reverse, is correct). Confirm?
I confirm that, two plugins order is reversed in my previous comment, that works indeed. If use the right order like this, it also works.
plugins: [
[
"module-resolver",
{
alias: {
"@": "./src",
},
},
],
"inline-react-svg",
],
I have configured
resolve.alias
inwebpack.config.js
:but when i use
@
toimport
a svg file,that will report an error like this image shows:when i change the
import
path,it will be ok.I changed this plugin to
@svgr/webpack
. With the samewebpack.config.js
,this problem has been solved now.Maybe that's a small bug inbabel-plugin-inline-react-svg
.😥 That's my webpack version and other package information: