Skip to content

Commit dbd142a

Browse files
committed
Add CSS modules + node_modules tests
1 parent 1bfff3f commit dbd142a

File tree

15 files changed

+117
-5
lines changed

15 files changed

+117
-5
lines changed

.prettierignore_staged

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
**/.next/**
2+
**/_next/**
3+
**/dist/**

lint-staged.config.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ module.exports = {
77
.map(filename => `"${isWin ? filename : escape([filename])}"`)
88
.join(' ')
99
return [
10-
`prettier --write ${escapedFileNames}`,
11-
`eslint --max-warnings=0 --fix ${filenames.map(f => `"${f}"`).join(' ')}`,
10+
`prettier --with-node-modules --ignore-path='./.prettierignore_staged' --write ${escapedFileNames}`,
11+
`eslint --no-ignore --max-warnings=0 --fix ${filenames
12+
.map(f => `"${f}"`)
13+
.join(' ')}`,
1214
`git add ${escapedFileNames}`,
1315
]
1416
},
@@ -17,7 +19,7 @@ module.exports = {
1719
.map(filename => `"${isWin ? filename : escape([filename])}"`)
1820
.join(' ')
1921
return [
20-
`prettier --write ${escapedFileNames}`,
22+
`prettier --with-node-modules --ignore-path='./.prettierignore_staged' --write ${escapedFileNames}`,
2123
`git add ${escapedFileNames}`,
2224
]
2325
},

packages/next/build/webpack/config/blocks/css/index.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import webpack, { Configuration } from 'webpack'
55
import { loader } from '../../helpers'
66
import { ConfigurationContext, ConfigurationFn, pipe } from '../../utils'
77
import { getCssModuleLocalIdent } from './getCssModuleLocalIdent'
8-
import { getGlobalImportError } from './messages'
8+
import { getGlobalImportError, getModuleImportError } from './messages'
99
import { getPostCssPlugins } from './plugins'
1010

1111
function getClientStyleLoader({
@@ -136,6 +136,23 @@ export const css = curry(async function css(
136136
})
137137
)
138138

139+
// Throw an error for CSS Modules used outside their supported scope
140+
fns.push(
141+
loader({
142+
oneOf: [
143+
{
144+
test: /\.module\.css$/,
145+
use: {
146+
loader: 'error-loader',
147+
options: {
148+
reason: getModuleImportError(),
149+
},
150+
},
151+
},
152+
],
153+
})
154+
)
155+
139156
if (ctx.isServer) {
140157
fns.push(
141158
loader({

packages/next/build/webpack/config/blocks/css/messages.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,10 @@ export function getGlobalImportError(file: string | null) {
99
file ? file : 'pages/_app.js'
1010
)}.\nRead more: https://err.sh/next.js/global-css`
1111
}
12+
13+
export function getModuleImportError() {
14+
// TODO: Read more link
15+
return `CSS Modules ${chalk.bold(
16+
'cannot'
17+
)} be imported from within ${chalk.bold('node_modules')}.`
18+
}

test/integration/css/fixtures/invalid-module/node_modules/example/index.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/integration/css/fixtures/invalid-module/node_modules/example/index.mjs

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/integration/css/fixtures/invalid-module/node_modules/example/index.module.css

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/integration/css/fixtures/invalid-module/node_modules/example/package.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as classes from 'example'
2+
3+
function Home() {
4+
return <div>This should fail at build time {JSON.stringify(classes)}.</div>
5+
}
6+
7+
export default Home

test/integration/css/fixtures/nm-module/node_modules/example/index.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)