Skip to content

Commit

Permalink
Fix CSS imports not included in entries with a custom extension (#46571)
Browse files Browse the repository at this point in the history
`entryName` will contain the extension if it's not a normal JS entry, this causes CSS being missing in pages with a custom extension in app dir (e.g. MDX). Here we add a `.replace(/\.[^\\/.]+$/, '')` to the entry name.

Fixes NEXT-709

## Bug

- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
  • Loading branch information
shuding committed Feb 28, 2023
1 parent d49c700 commit 9edf2d3
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,12 @@ export class FlightManifestPlugin {
if (entryName?.startsWith('app/')) {
// The `key` here should be the absolute file path but without extension.
// We need to replace the separator in the entry name to match the system separator.
const key = this.appDir + entryName.slice(3).replace(/\//g, sep)
const key =
this.appDir +
entryName
.slice(3)
.replace(/\//g, sep)
.replace(/\.[^\\/.]+$/, '')
entryCSSFiles[key] = files.concat(entryCSSFiles[key] || [])
}
}
Expand Down
3 changes: 3 additions & 0 deletions test/e2e/app-dir/app-css/app/mdx/content.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import styles from './content.module.css'

export const Content = () => <h1 className={styles.root}>Hello World!</h1>
3 changes: 3 additions & 0 deletions test/e2e/app-dir/app-css/app/mdx/content.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.root {
color: red;
}
3 changes: 3 additions & 0 deletions test/e2e/app-dir/app-css/app/mdx/page.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Content } from './content'

<Content />
12 changes: 12 additions & 0 deletions test/e2e/app-dir/app-css/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ createNextDescribe(
react: 'latest',
'react-dom': 'latest',
sass: 'latest',
'@next/mdx': 'canary',
},
},
({ next, isNextDev: isDev }) => {
Expand Down Expand Up @@ -231,6 +232,17 @@ createNextDescribe(
})
})

describe('page extensions', () => {
it('should include css imported in MDX pages', async () => {
const browser = await next.browser('/mdx')
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('h1')).color`
)
).toBe('rgb(255, 0, 0)')
})
})

if (isDev) {
describe('multiple entries', () => {
it('should only inject the same style once if used by different layers', async () => {
Expand Down
1 change: 1 addition & 0 deletions test/e2e/app-dir/app-css/mdx-components.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const useMDXComponents = (components) => components
10 changes: 9 additions & 1 deletion test/e2e/app-dir/app-css/next.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
module.exports = {
const mdx = require('@next/mdx')

const withMDX = mdx()

const nextConfig = {
pageExtensions: ['ts', 'tsx', 'js', 'jsx', 'md', 'mdx'],
experimental: {
appDir: true,
mdxRs: true,
},
}

module.exports = withMDX(nextConfig)

0 comments on commit 9edf2d3

Please sign in to comment.