Unverified Commit 18134f87 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Fix CSS handling rules (#42615)

This PR fixes (and simplifies) CSS loader rules, major things fixed with
this PR:
- (Tests added) Even if `appDir` isn't enabled, CSS/SASS/CSSM/SASSM
should still be transpiled for packages in node_modules, configured via
`transpilePackages`.
- They should be transpiled from other packages in the same monorepo
(not inside node_modules).

## 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)
parent 584d842a
import path from 'path'
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import webdriver from 'next-webdriver'
describe('transpile packages', () => {
let next: NextInstance
if ((global as any).isNextDeploy) {
it('should skip for deploy mode for now', () => {})
return
}
beforeAll(async () => {
next = await createNext({
files: new FileRef(path.join(__dirname, './npm')),
dependencies: {
react: 'latest',
'react-dom': 'latest',
sass: 'latest',
},
packageJson: {
scripts: {
setup: `cp -r ./node_modules_bak/* ./node_modules`,
build: 'yarn setup && next build',
dev: 'yarn setup && next dev',
start: 'next start',
},
},
installCommand: 'yarn',
startCommand: (global as any).isNextDev ? 'yarn dev' : 'yarn start',
buildCommand: 'yarn build',
})
})
afterAll(() => next.destroy())
const { isNextDeploy } = global as any
const isReact17 = process.env.NEXT_TEST_REACT_VERSION === '^17'
if (isNextDeploy || isReact17) {
it('should skip tests for next-deploy and react 17', () => {})
return
}
describe('css', () => {
it('should handle global css imports inside transpiled modules', async () => {
const browser = await webdriver(next.url, '/global-css')
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('body')).backgroundColor`
)
).toBe('rgb(0, 0, 255)')
})
it('should handle global scss imports inside transpiled modules', async () => {
const browser = await webdriver(next.url, '/global-scss')
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('body')).backgroundColor`
)
).toBe('rgb(0, 0, 255)')
})
it('should handle css modules imports inside transpiled modules', async () => {
const browser = await webdriver(next.url, '/css-modules')
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('h1')).backgroundColor`
)
).toBe('rgb(0, 0, 255)')
})
it('should handle scss modules imports inside transpiled modules', async () => {
const browser = await webdriver(next.url, '/scss-modules')
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('h1')).backgroundColor`
)
).toBe('rgb(0, 0, 255)')
})
})
})
module.exports = {
experimental: {
transpilePackages: ['css'],
},
}
$bg-color: blue;
body {
background: $bg-color;
}
import styles from './style.module.css'
export default styles
import styles from './style.module.scss'
export default styles
$bg-color: blue;
.div {
background: $bg-color;
}
import styles from 'css/module-css'
export default function Index() {
return <h1 className={styles.div}>Hello world!</h1>
}
import 'css/global-css'
export default function Index() {
return <div>Hello world!</div>
}
import 'css/global-scss'
export default function Index() {
return <div>Hello world!</div>
}
import styles from 'css/module-scss'
export default function Index() {
return <h1 className={styles.div}>Hello world!</h1>
}
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