Skip to content

Commit

Permalink
fix(webpack): bring back previous SVG and SVGR behavior for React pro…
Browse files Browse the repository at this point in the history
…jects
  • Loading branch information
jaysoo committed Apr 2, 2024
1 parent 1a82dd2 commit 6a70e04
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
64 changes: 64 additions & 0 deletions e2e/react-extensions/src/react-webpack.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import {
cleanupProject,
createFile,
listFiles,
newProject,
readFile,
runCLI,
runCLIAsync,
uniq,
updateFile,
} from '@nx/e2e/utils';

describe('Build React applications and libraries with Vite', () => {
beforeAll(() => {
newProject({
packages: ['@nx/react'],
});
});

afterAll(() => {
cleanupProject();
});

// Regression test: https://github.com/nrwl/nx/issues/21773
it('should support SVGR and SVG asset in the same project', async () => {
const appName = uniq('app');

runCLI(
`generate @nx/react:app ${appName} --bundler=webpack --compiler=babel --unitTestRunner=none --no-interactive`
);
createFile(
`apps/${appName}/src/app/nx.svg`,
`
<svg version="1.1" width="300" height="200" xmlns="http://www.w3.org/2000/svg">
<text x="150" y="125" font-size="60" text-anchor="middle" fill="white">SVG for ${appName}</text>
</svg>
`
);
updateFile(
`apps/${appName}/src/app/app.tsx`,
`
import svgImg, { ReactComponent as Logo } from './nx.svg';
export function App() {
return (
<>
<img src={svgImg} alt="Alt for SVG img tag" />
<Logo />
</>
);
}
export default App;
`
);

await runCLIAsync(`build ${appName}`);

const outFiles = listFiles(`dist/apps/${appName}`);
const mainFile = outFiles.find((f) => f.startsWith('main.'));
const mainContent = readFile(`dist/apps/${appName}/${mainFile}`);
const svgFile = outFiles.find((f) => f.endsWith('.svg'));
expect(mainContent).toMatch(/Alt for SVG img tag/);
expect(svgFile).toBeTruthy();
}, 300_000);
});
1 change: 1 addition & 0 deletions packages/react/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"babel-plugin-emotion",
"babel-plugin-styled-components",
"css-loader",
"file-loader",
"less-loader",
"react-refresh",
"rollup",
Expand Down
1 change: 1 addition & 0 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@phenomnomnominal/tsquery": "~5.0.1",
"@svgr/webpack": "^8.0.1",
"chalk": "^4.1.0",
"file-loader": "^6.2.0",
"minimatch": "9.0.3",
"tslib": "^2.3.0",
"@nx/devkit": "file:../devkit",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@ export function applyReactConfig(
if (options.svgr !== false) {
removeSvgLoaderIfPresent(config);

// TODO(v20): Remove file-loader and use `?react` querystring to differentiate between asset and SVGR.
// It should be:
// use: [{
// test: /\.svg$/i,
// type: 'asset',
// resourceQuery: /react/, // *.svg?react
// },
// {
// test: /\.svg$/i,
// issuer: /\.[jt]sx?$/,
// resourceQuery: { not: [/react/] }, // exclude react component if *.svg?react
// use: ['@svgr/webpack'],
// }],
// See:
// - SVGR: https://react-svgr.com/docs/webpack/#use-svgr-and-asset-svg-in-the-same-project
// - Vite: https://www.npmjs.com/package/vite-plugin-svgr
// - Rsbuild: https://github.com/web-infra-dev/rsbuild/pull/1783
// Note: We also need a migration for any projects that are using SVGR to convert
// `import { ReactComponent as X } from './x.svg` to
// `import X from './x.svg?react';
config.module.rules.push({
test: /\.svg$/,
issuer: /\.(js|ts|md)x?$/,
Expand All @@ -23,6 +43,12 @@ export function applyReactConfig(
ref: true,
},
},
{
loader: require.resolve('file-loader'),
options: {
name: '[name].[hash].[ext]',
},
},
],
});
}
Expand Down

0 comments on commit 6a70e04

Please sign in to comment.