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( ...@@ -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 // CSS Modules support must be enabled on the server and client so the class
// names are available for SSR or Prerendering. // names are available for SSR or Prerendering.
if (ctx.hasAppDir && !ctx.isProduction) { if (ctx.hasAppDir && !ctx.isProduction) {
// CSS modules
fns.push( fns.push(
loader({ loader({
oneOf: [ oneOf: [
...@@ -284,16 +285,6 @@ export const css = curry(async function css( ...@@ -284,16 +285,6 @@ export const css = curry(async function css(
sideEffects: false, sideEffects: false,
// CSS Modules are activated via this specific extension. // CSS Modules are activated via this specific extension.
test: regexCssModules, 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: [ use: [
require.resolve('../../../loaders/next-flight-css-dev-loader'), require.resolve('../../../loaders/next-flight-css-dev-loader'),
...getCssModuleLoader(ctx, lazyPostCSSInitializer), ...getCssModuleLoader(ctx, lazyPostCSSInitializer),
...@@ -302,10 +293,12 @@ export const css = curry(async function css( ...@@ -302,10 +293,12 @@ export const css = curry(async function css(
], ],
}) })
) )
// Opt-in support for Sass (using .scss or .sass extensions).
fns.push( fns.push(
loader({ loader({
oneOf: [ oneOf: [
// Opt-in support for Sass (using .scss or .sass extensions). // For app dir, we match both server and client layers.
markRemovable({ markRemovable({
// Sass Modules should never have side effects. This setting will // Sass Modules should never have side effects. This setting will
// allow unused Sass to be removed from the production build. // allow unused Sass to be removed from the production build.
...@@ -314,12 +307,6 @@ export const css = curry(async function css( ...@@ -314,12 +307,6 @@ export const css = curry(async function css(
sideEffects: false, sideEffects: false,
// Sass Modules are activated via this specific extension. // Sass Modules are activated via this specific extension.
test: regexSassModules, 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: [ use: [
require.resolve('../../../loaders/next-flight-css-dev-loader'), require.resolve('../../../loaders/next-flight-css-dev-loader'),
...getCssModuleLoader( ...getCssModuleLoader(
...@@ -344,16 +331,6 @@ export const css = curry(async function css( ...@@ -344,16 +331,6 @@ export const css = curry(async function css(
sideEffects: false, sideEffects: false,
// CSS Modules are activated via this specific extension. // CSS Modules are activated via this specific extension.
test: regexCssModules, 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), use: getCssModuleLoader(ctx, lazyPostCSSInitializer),
}), }),
], ],
...@@ -371,12 +348,6 @@ export const css = curry(async function css( ...@@ -371,12 +348,6 @@ export const css = curry(async function css(
sideEffects: false, sideEffects: false,
// Sass Modules are activated via this specific extension. // Sass Modules are activated via this specific extension.
test: regexSassModules, 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( use: getCssModuleLoader(
ctx, ctx,
lazyPostCSSInitializer, lazyPostCSSInitializer,
......
module.exports = { module.exports = {
experimental: { experimental: {
appDir: true, appDir: true,
transpileModules: ['ui'],
}, },
} }
...@@ -135,4 +135,24 @@ describe('app dir - rsc external dependency', () => { ...@@ -135,4 +135,24 @@ describe('app dir - rsc external dependency', () => {
expect(result).toMatch(/\.css/) 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() { export default function Page() {
return <div>hello</div> return <div>hello</div>
......
import Foo from 'css/module'
export default function Page() {
return <Foo />
}
...@@ -3,6 +3,6 @@ module.exports = { ...@@ -3,6 +3,6 @@ module.exports = {
experimental: { experimental: {
appDir: true, appDir: true,
serverComponentsExternalPackages: ['conditional-exports-optout'], 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", "type": "module",
"sideEffects": false, "sideEffects": false,
"exports": { "exports": {
".": "./index.js", ".": "./index.js",
"./style.css": "./style.css", "./style.css": "./style.css",
"./module": "./module.js",
"./package.json": "./package.json" "./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', () => { ...@@ -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') const appDir = join(fixturesDir, 'invalid-module')
beforeAll(async () => { beforeAll(async () => {
...@@ -258,7 +258,7 @@ describe('Invalid CSS Module Usage in node_modules', () => { ...@@ -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') const appDir = join(fixturesDir, 'invalid-global-module')
beforeAll(async () => { beforeAll(async () => {
......
...@@ -233,7 +233,7 @@ describe('Can hot reload CSS Module without losing state', () => { ...@@ -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') const appDir = join(fixturesDir, 'invalid-module')
beforeAll(async () => { beforeAll(async () => {
...@@ -254,7 +254,7 @@ describe('Invalid CSS Module Usage in node_modules', () => { ...@@ -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') const appDir = join(fixturesDir, 'invalid-global-module')
beforeAll(async () => { 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