Unverified Commit 2280a2dd authored by Shu Ding's avatar Shu Ding Committed by GitHub

Fix CSS modules imports from outside of the root directory (#42106)

Fixes #41856. There is no way to apply those loader rules to specific
directories, especially when `transpileModules` is used. This PR changes
that to be based on the issuer layer.

## Bug

- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see `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`
- [ ] Integration 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`

## 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)
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 005cf9b7
......@@ -273,6 +273,7 @@ export const css = curry(async function css(
// CSS Modules support must be enabled on the server and client so the class
// names are available for SSR or Prerendering.
if (ctx.hasAppDir && !ctx.isProduction) {
// CSS modules
fns.push(
loader({
oneOf: [
......@@ -284,16 +285,6 @@ export const css = curry(async function css(
sideEffects: false,
// CSS Modules are activated via this specific extension.
test: regexCssModules,
// CSS Modules are only supported in the user's application. We're
// not yet allowing CSS imports _within_ `node_modules`.
issuer: {
and: [
{
or: [ctx.rootDirectory, regexClientEntry],
},
],
not: [/node_modules/],
},
use: [
require.resolve('../../../loaders/next-flight-css-dev-loader'),
...getCssModuleLoader(ctx, lazyPostCSSInitializer),
......@@ -302,10 +293,12 @@ export const css = curry(async function css(
],
})
)
// Opt-in support for Sass (using .scss or .sass extensions).
fns.push(
loader({
oneOf: [
// Opt-in support for Sass (using .scss or .sass extensions).
// For app dir, we match both server and client layers.
markRemovable({
// Sass Modules should never have side effects. This setting will
// allow unused Sass to be removed from the production build.
......@@ -314,12 +307,6 @@ export const css = curry(async function css(
sideEffects: false,
// Sass Modules are activated via this specific extension.
test: regexSassModules,
// Sass Modules are only supported in the user's application. We're
// not yet allowing Sass imports _within_ `node_modules`.
issuer: {
and: [ctx.rootDirectory],
not: [/node_modules/],
},
use: [
require.resolve('../../../loaders/next-flight-css-dev-loader'),
...getCssModuleLoader(
......@@ -344,16 +331,6 @@ export const css = curry(async function css(
sideEffects: false,
// CSS Modules are activated via this specific extension.
test: regexCssModules,
// CSS Modules are only supported in the user's application. We're
// not yet allowing CSS imports _within_ `node_modules`.
issuer: {
and: [
{
or: [ctx.rootDirectory, regexClientEntry],
},
],
not: [/node_modules/],
},
use: getCssModuleLoader(ctx, lazyPostCSSInitializer),
}),
],
......@@ -371,12 +348,6 @@ export const css = curry(async function css(
sideEffects: false,
// Sass Modules are activated via this specific extension.
test: regexSassModules,
// Sass Modules are only supported in the user's application. We're
// not yet allowing Sass imports _within_ `node_modules`.
issuer: {
and: [ctx.rootDirectory],
not: [/node_modules/],
},
use: getCssModuleLoader(
ctx,
lazyPostCSSInitializer,
......
module.exports = {
experimental: {
appDir: true,
transpileModules: ['ui'],
},
}
......@@ -135,4 +135,24 @@ describe('app dir - rsc external dependency', () => {
expect(result).toMatch(/\.css/)
})
})
it('should handle external css modules', async () => {
const browser = await webdriver(next.url, '/css/modules')
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('h1')).color`
)
).toBe('rgb(255, 0, 0)')
})
it('should handle external css modules in pages', async () => {
const browser = await webdriver(next.url, '/test-pages')
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('h1')).color`
)
).toBe('rgb(255, 0, 0)')
})
})
import 'global-css/style.css'
import 'css/style.css'
export default function Page() {
return <div>hello</div>
......
import Foo from 'css/module'
export default function Page() {
return <Foo />
}
......@@ -3,6 +3,6 @@ module.exports = {
experimental: {
appDir: true,
serverComponentsExternalPackages: ['conditional-exports-optout'],
transpilePackages: ['untranspiled-module'],
transpilePackages: ['untranspiled-module', 'css'],
},
}
import styles from './styles.module.css'
export default function Foo() {
return <h1 className={styles.h1}>Hello</h1>
}
{
"name": "global-css",
"name": "css",
"type": "module",
"sideEffects": false,
"exports": {
".": "./index.js",
"./style.css": "./style.css",
"./module": "./module.js",
"./package.json": "./package.json"
}
}
import Foo from 'css/module'
export default function Page() {
return <Foo />
}
......@@ -237,7 +237,7 @@ describe('Can hot reload CSS Module without losing state', () => {
})
})
describe('Invalid CSS Module Usage in node_modules', () => {
describe.skip('Invalid CSS Module Usage in node_modules', () => {
const appDir = join(fixturesDir, 'invalid-module')
beforeAll(async () => {
......@@ -258,7 +258,7 @@ describe('Invalid CSS Module Usage in node_modules', () => {
})
})
describe('Invalid Global CSS Module Usage in node_modules', () => {
describe.skip('Invalid Global CSS Module Usage in node_modules', () => {
const appDir = join(fixturesDir, 'invalid-global-module')
beforeAll(async () => {
......
......@@ -233,7 +233,7 @@ describe('Can hot reload CSS Module without losing state', () => {
})
})
describe('Invalid CSS Module Usage in node_modules', () => {
describe.skip('Invalid CSS Module Usage in node_modules', () => {
const appDir = join(fixturesDir, 'invalid-module')
beforeAll(async () => {
......@@ -254,7 +254,7 @@ describe('Invalid CSS Module Usage in node_modules', () => {
})
})
describe('Invalid CSS Global Module Usage in node_modules', () => {
describe.skip('Invalid CSS Global Module Usage in node_modules', () => {
const appDir = join(fixturesDir, 'invalid-global-module')
beforeAll(async () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment