-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(webpack): bring back previous SVG and SVGR behavior for React pro…
…jects (nrwl#22628)
- Loading branch information
Showing
9 changed files
with
224 additions
and
81 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { | ||
cleanupProject, | ||
createFile, | ||
listFiles, | ||
newProject, | ||
readFile, | ||
runCLI, | ||
runE2ETests, | ||
uniq, | ||
updateFile, | ||
} from '@nx/e2e/utils'; | ||
|
||
describe('NextJs SVGR support', () => { | ||
beforeAll(() => { | ||
newProject({ | ||
packages: ['@nx/next'], | ||
}); | ||
}); | ||
|
||
afterAll(() => cleanupProject()); | ||
|
||
it('should allow both SVG asset and SVGR component to be used', () => { | ||
const appName = uniq('app'); | ||
runCLI( | ||
`generate @nx/next:app ${appName} --no-interactive --appDir=true --src=true` | ||
); | ||
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 app</text> | ||
</svg> | ||
` | ||
); | ||
updateFile( | ||
`apps/${appName}/src/app/page.tsx`, | ||
` | ||
import Image from 'next/image'; | ||
import svgImg, { ReactComponent as Logo } from './nx.svg'; | ||
export default async function Index() { | ||
return ( | ||
<> | ||
<Image src={svgImg} alt="Alt for SVG img tag" /> | ||
<Logo /> | ||
</> | ||
); | ||
} | ||
` | ||
); | ||
updateFile( | ||
`apps/${appName}/next.config.js`, | ||
` | ||
const { composePlugins, withNx } = require('@nx/next'); | ||
const nextConfig = { | ||
nx: { | ||
svgr: true, | ||
}, | ||
}; | ||
const plugins = [ | ||
withNx, | ||
]; | ||
module.exports = composePlugins(...plugins)(nextConfig); | ||
` | ||
); | ||
|
||
runCLI(`build ${appName}`); | ||
|
||
const pageFile = readFile(`apps/${appName}/.next/server/app/page.js`); | ||
const svgFile = listFiles(`apps/${appName}/.next/static/media`).find((f) => | ||
/nx\.[a-z0-9]+\.svg$/.test(f) | ||
); | ||
expect(`apps/${appName}/.next/static/chunks/app/${pageFile}`).toMatch( | ||
/SVG for app/ | ||
); | ||
expect(`apps/${appName}/.next/static/chunks/app/${pageFile}`).toMatch( | ||
/Alt for SVG img tag/ | ||
); | ||
expect(svgFile).toBeTruthy(); | ||
}); | ||
}); |
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,65 @@ | ||
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 app</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(/SVG for app/); | ||
expect(mainContent).toMatch(/Alt for SVG img tag/); | ||
expect(svgFile).toBeTruthy(); | ||
}, 300_000); | ||
}); |
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
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
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