Skip to content
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 next/image usage in mdx #64875

Merged
merged 6 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions packages/next/src/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,7 @@ export default async function getBaseWebpackConfig(
const shouldIncludeExternalDirs =
config.experimental.externalDir || !!config.transpilePackages

const pageExtensionsRegex = new RegExp(`(${pageExtensions.join('|')})$`)
huozhi marked this conversation as resolved.
Show resolved Hide resolved
const codeCondition = {
test: { or: [/\.(tsx|ts|js|cjs|mjs|jsx)$/, /__barrel_optimize__/] },
...(shouldIncludeExternalDirs
Expand All @@ -840,6 +841,8 @@ export default async function getBaseWebpackConfig(
},
}

const aliasCodeConditionTest = [codeCondition.test, pageExtensionsRegex]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense but why calling it alias-?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

codeCodition.test are available for both webpack alias and transform rules, I created this one to imply that we only want to apply the webpack aliases for this group.


let webpackConfig: webpack.Configuration = {
parallelism: Number(process.env.NEXT_WEBPACK_PARALLELISM) || undefined,
...(isNodeServer ? { externalsPresets: { node: true } } : {}),
Expand Down Expand Up @@ -1326,7 +1329,7 @@ export default async function getBaseWebpackConfig(
// Resolve it if it is a source code file, and it has NOT been
// opted out of bundling.
and: [
codeCondition.test,
aliasCodeConditionTest,
{
not: [optOutBundlingPackageRegex, asyncStoragesRegex],
},
Expand Down Expand Up @@ -1388,7 +1391,7 @@ export default async function getBaseWebpackConfig(
// Resolve it if it is a source code file, and it has NOT been
// opted out of bundling.
and: [
codeCondition.test,
aliasCodeConditionTest,
{
not: [optOutBundlingPackageRegex, asyncStoragesRegex],
},
Expand All @@ -1405,7 +1408,7 @@ export default async function getBaseWebpackConfig(
},
},
{
test: codeCondition.test,
test: aliasCodeConditionTest,
issuerLayer: WEBPACK_LAYERS.serverSideRendering,
resolve: {
alias: createRSCAliases(bundledReactChannel, {
Expand All @@ -1418,7 +1421,7 @@ export default async function getBaseWebpackConfig(
],
},
{
test: codeCondition.test,
test: aliasCodeConditionTest,
issuerLayer: WEBPACK_LAYERS.appPagesBrowser,
resolve: {
alias: createRSCAliases(bundledReactChannel, {
Expand Down
7 changes: 7 additions & 0 deletions test/e2e/app-dir/mdx/app/image/page.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Image from 'next/image'

# Title

This is a markdown page

<Image src="/test.jpg" alt="Next.js Logo" width={180} height={37} />
7 changes: 7 additions & 0 deletions test/e2e/app-dir/mdx/mdx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ for (const type of [
const $ = await next.render$('/')
expect($('h2').text()).toBe('This is a client component')
})

it('should work with next/image', async () => {
const $ = await next.render$('/image')
expect($('img').attr('src')).toBe(
'/_next/image?url=%2Ftest.jpg&w=384&q=75'
)
})
})

describe('pages directory', () => {
Expand Down
Binary file added test/e2e/app-dir/mdx/public/test.jpg
Loading
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 test/turbopack-build-tests-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -1793,6 +1793,7 @@
"mdx with-mdx-rs app directory should work in initial html with mdx import",
"mdx with-mdx-rs app directory should work using browser",
"mdx with-mdx-rs app directory should work using browser with mdx import",
"mdx with-mdx-rs app directory should work with next/image",
"mdx with-mdx-rs pages directory should allow overriding components",
"mdx with-mdx-rs pages directory should work in initial html",
"mdx with-mdx-rs pages directory should work in initial html with mdx import",
Expand Down
Loading