Unverified Commit 14b18da0 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Special Global CSS Error within `node_modules` (#9849)

parent 5bd58d5a
......@@ -5,7 +5,11 @@ import MiniCssExtractPlugin from '../../../plugins/mini-css-extract-plugin'
import { loader } from '../../helpers'
import { ConfigurationContext, ConfigurationFn, pipe } from '../../utils'
import { getCssModuleLocalIdent } from './getCssModuleLocalIdent'
import { getGlobalImportError, getModuleImportError } from './messages'
import {
getGlobalImportError,
getGlobalModuleImportError,
getLocalModuleImportError,
} from './messages'
import { getPostCssPlugins } from './plugins'
function getClientStyleLoader({
......@@ -156,7 +160,7 @@ export const css = curry(async function css(
use: {
loader: 'error-loader',
options: {
reason: getModuleImportError(),
reason: getLocalModuleImportError(),
},
},
},
......@@ -210,6 +214,24 @@ export const css = curry(async function css(
)
}
// Throw an error for Global CSS used inside of `node_modules`
fns.push(
loader({
oneOf: [
{
test: /\.css$/,
issuer: { include: [/node_modules/] },
use: {
loader: 'error-loader',
options: {
reason: getGlobalModuleImportError(),
},
},
},
],
})
)
// Throw an error for Global CSS used outside of our custom <App> file
fns.push(
loader({
......
......@@ -10,7 +10,14 @@ export function getGlobalImportError(file: string | null) {
)}.\nRead more: https://err.sh/next.js/global-css`
}
export function getModuleImportError() {
export function getGlobalModuleImportError() {
// TODO: Read more link
return `Global CSS ${chalk.bold(
'cannot'
)} be imported from within ${chalk.bold('node_modules')}.`
}
export function getLocalModuleImportError() {
// TODO: Read more link
return `CSS Modules ${chalk.bold(
'cannot'
......
import 'example'
function Home() {
return <div>This should fail at build time.</div>
}
export default Home
......@@ -181,6 +181,25 @@ describe('Invalid CSS Module Usage in node_modules', () => {
})
})
describe('Invalid CSS Module Usage in node_modules', () => {
const appDir = join(fixturesDir, 'invalid-global-module')
beforeAll(async () => {
await remove(join(appDir, '.next'))
})
it('should fail to build', async () => {
const { stderr } = await nextBuild(appDir, [], {
stderr: true,
})
expect(stderr).toContain('Failed to compile')
expect(stderr).toContain('node_modules/example/index.css')
expect(stderr).toMatch(
/Global CSS.*cannot.*be imported from within.*node_modules/
)
})
})
describe('Valid CSS Module Usage from within node_modules', () => {
const appDir = join(fixturesDir, 'nm-module')
......
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