Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
d9d9545f
Unverified
Commit
d9d9545f
authored
Jan 14, 2020
by
Joe Haddad
Committed by
GitHub
Jan 14, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Error on CSS in Custom Document (#10091)
parent
2d76c4a9
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
78 additions
and
0 deletions
+78
-0
index.ts
packages/next/build/webpack/config/blocks/css/index.ts
+23
-0
messages.ts
packages/next/build/webpack/config/blocks/css/messages.ts
+6
-0
_document.js
...n/css-fixtures/invalid-module-document/pages/_document.js
+23
-0
index.js
...ation/css-fixtures/invalid-module-document/pages/index.js
+3
-0
styles.module.css
...on/css-fixtures/invalid-module-document/styles.module.css
+3
-0
index.test.js
test/integration/css/test/index.test.js
+20
-0
No files found.
packages/next/build/webpack/config/blocks/css/index.ts
View file @
d9d9545f
...
...
@@ -6,6 +6,7 @@ import { loader, plugin } from '../../helpers'
import
{
ConfigurationContext
,
ConfigurationFn
,
pipe
}
from
'../../utils'
import
{
getCssModuleLocalIdent
}
from
'./getCssModuleLocalIdent'
import
{
getCustomDocumentError
,
getGlobalImportError
,
getGlobalModuleImportError
,
getLocalModuleImportError
,
...
...
@@ -130,6 +131,28 @@ export const css = curry(async function css(
// function argument.
true
)
// CSS cannot be imported in _document. This comes before everything because
// global CSS nor CSS modules work in said file.
fns
.
push
(
loader
({
oneOf
:
[
{
test
:
/
\.
css$/
,
// Use a loose regex so we don't have to crawl the file system to
// find the real file name (if present).
issuer
:
{
test
:
/pages
[\\/]
_document
\.
/
},
use
:
{
loader
:
'error-loader'
,
options
:
{
reason
:
getCustomDocumentError
(),
},
},
},
],
})
)
// CSS Modules support must be enabled on the server and client so the class
// names are availble for SSR or Prerendering.
fns
.
push
(
...
...
packages/next/build/webpack/config/blocks/css/messages.ts
View file @
d9d9545f
...
...
@@ -25,3 +25,9 @@ export function getLocalModuleImportError() {
'node_modules'
)}
.\nRead more: https://err.sh/next.js/css-modules-npm`
}
export
function
getCustomDocumentError
()
{
return
`CSS
${
chalk
.
bold
(
'cannot'
)}
be imported within
${
chalk
.
cyan
(
'pages/_document.js'
)}
. Please move global styles to
${
chalk
.
cyan
(
'pages/_app.js'
)}
.`
}
test/integration/css-fixtures/invalid-module-document/pages/_document.js
0 → 100644
View file @
d9d9545f
import
Document
,
{
Head
,
Html
,
Main
,
NextScript
}
from
'next/document'
import
styles
from
'../styles.module.css'
class
MyDocument
extends
Document
{
static
async
getInitialProps
(
ctx
)
{
const
initialProps
=
await
Document
.
getInitialProps
(
ctx
)
return
{
...
initialProps
}
}
render
()
{
return
(
<
Html
>
<
Head
/>
<
body
className
=
{
styles
[
'red-text'
]}
>
<
Main
/>
<
NextScript
/>
<
/body
>
<
/Html
>
)
}
}
export
default
MyDocument
test/integration/css-fixtures/invalid-module-document/pages/index.js
0 → 100644
View file @
d9d9545f
export
default
function
Home
()
{
return
<
div
>
Hello
<
/div
>
}
test/integration/css-fixtures/invalid-module-document/styles.module.css
0 → 100644
View file @
d9d9545f
.red-text
{
color
:
red
;
}
test/integration/css/test/index.test.js
View file @
d9d9545f
...
...
@@ -205,6 +205,26 @@ describe('CSS Support', () => {
})
})
describe
(
'Invalid CSS in _document'
,
()
=>
{
const
appDir
=
join
(
fixturesDir
,
'invalid-module-document'
)
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
(
'styles.module.css'
)
expect
(
stderr
).
toMatch
(
/CSS.*cannot.*be imported within.*pages
[\\/]
_document
\.
js/
)
expect
(
stderr
).
toMatch
(
/Location:.*pages
[\\/]
_document
\.
js/
)
})
})
describe
(
'Invalid Global CSS'
,
()
=>
{
const
appDir
=
join
(
fixturesDir
,
'invalid-global'
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment