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
8247f19f
Commit
8247f19f
authored
Dec 30, 2019
by
Joe Haddad
Committed by
JJ Kasper
Dec 30, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Defer Loading CSS Configuration (#9889)
parent
b0892eff
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
125 additions
and
9 deletions
+125
-9
webpack-config.ts
packages/next/build/webpack-config.ts
+3
-0
index.ts
packages/next/build/webpack/config/blocks/css/index.ts
+41
-4
plugins.ts
packages/next/build/webpack/config/blocks/css/plugins.ts
+5
-5
index.test.js
test/integration/css-customization/test/index.test.js
+25
-0
next.config.js
...n/css-fixtures/custom-configuration-legacy/next.config.js
+15
-0
_app.js
...on/css-fixtures/custom-configuration-legacy/pages/_app.js
+12
-0
index.js
...n/css-fixtures/custom-configuration-legacy/pages/index.js
+3
-0
postcss.config.js
...ss-fixtures/custom-configuration-legacy/postcss.config.js
+10
-0
global.css
...ss-fixtures/custom-configuration-legacy/styles/global.css
+11
-0
No files found.
packages/next/build/webpack-config.ts
View file @
8247f19f
...
...
@@ -28,6 +28,7 @@ import {
VALID_MIDDLEWARE
,
}
from
'./plugins/collect-plugins'
import
{
build
as
buildConfiguration
}
from
'./webpack/config'
import
{
__overrideCssConfiguration
}
from
'./webpack/config/blocks/css'
// @ts-ignore: JS file
import
{
pluginLoaderOptions
}
from
'./webpack/loaders/next-plugin-loader'
import
BuildManifestPlugin
from
'./webpack/plugins/build-manifest-plugin'
...
...
@@ -946,6 +947,8 @@ export default async function getBaseWebpackConfig(
e
=>
(
e
as
any
).
__next_css_remove
!==
true
)
}
}
else
{
await
__overrideCssConfiguration
(
dir
,
webpackConfig
)
}
}
...
...
packages/next/build/webpack/config/blocks/css/index.ts
View file @
8247f19f
import
curry
from
'lodash.curry'
import
path
from
'path'
import
webpack
,
{
Configuration
}
from
'webpack'
import
webpack
,
{
Configuration
,
RuleSetRule
}
from
'webpack'
import
MiniCssExtractPlugin
from
'../../../plugins/mini-css-extract-plugin'
import
{
loader
}
from
'../../helpers'
import
{
ConfigurationContext
,
ConfigurationFn
,
pipe
}
from
'../../utils'
...
...
@@ -66,6 +66,37 @@ function getClientStyleLoader({
}
}
export
async
function
__overrideCssConfiguration
(
rootDirectory
:
string
,
config
:
Configuration
)
{
const
postCssPlugins
=
await
getPostCssPlugins
(
rootDirectory
)
function
patch
(
rule
:
RuleSetRule
)
{
if
(
rule
.
options
&&
typeof
rule
.
options
===
'object'
&&
rule
.
options
[
'ident'
]
===
'__nextjs_postcss'
)
{
rule
.
options
.
plugins
=
postCssPlugins
}
else
if
(
Array
.
isArray
(
rule
.
oneOf
))
{
rule
.
oneOf
.
forEach
(
patch
)
}
else
if
(
Array
.
isArray
(
rule
.
use
))
{
rule
.
use
.
forEach
(
u
=>
{
if
(
typeof
u
===
'object'
)
{
patch
(
u
)
}
})
}
}
// TODO: remove this rule, ESLint bug
// eslint-disable-next-line no-unused-expressions
config
.
module
?.
rules
?.
forEach
(
entry
=>
{
patch
(
entry
)
})
}
export
const
css
=
curry
(
async
function
css
(
enabled
:
boolean
,
ctx
:
ConfigurationContext
,
...
...
@@ -88,7 +119,13 @@ export const css = curry(async function css(
}),
]
const
postCssPlugins
=
await
getPostCssPlugins
(
ctx
.
rootDirectory
)
const
postCssPlugins
=
await
getPostCssPlugins
(
ctx
.
rootDirectory
,
// TODO: In the future, we should stop supporting old CSS setups and
// unconditionally inject ours. When that happens, we should remove this
// function argument.
true
)
// CSS Modules support must be enabled on the server and client so the class
// names are availble for SSR or Prerendering.
fns
.
push
(
...
...
@@ -140,7 +177,7 @@ export const css = curry(async function css(
{
loader
:
require
.
resolve
(
'postcss-loader'
),
options
:
{
ident
:
'postcss'
,
ident
:
'
__nextjs_
postcss'
,
plugins
:
postCssPlugins
,
sourceMap
:
true
,
},
...
...
@@ -202,7 +239,7 @@ export const css = curry(async function css(
{
loader
:
require
.
resolve
(
'postcss-loader'
),
options
:
{
ident
:
'postcss'
,
ident
:
'
__nextjs_
postcss'
,
plugins
:
postCssPlugins
,
sourceMap
:
true
,
},
...
...
packages/next/build/webpack/config/blocks/css/plugins.ts
View file @
8247f19f
...
...
@@ -88,12 +88,12 @@ function getDefaultPlugins(): CssPluginCollection {
}
export
async
function
getPostCssPlugins
(
dir
:
string
dir
:
string
,
defaults
:
boolean
=
false
):
Promise
<
import
(
'postcss'
).
AcceptedPlugin
[]
>
{
let
config
=
await
findConfig
<
{
plugins
:
CssPluginCollection
}
>
(
dir
,
'postcss'
)
let
config
=
defaults
?
null
:
await
findConfig
<
{
plugins
:
CssPluginCollection
}
>
(
dir
,
'postcss'
)
if
(
config
==
null
)
{
config
=
{
plugins
:
getDefaultPlugins
()
}
...
...
test/integration/css-customization/test/index.test.js
View file @
8247f19f
...
...
@@ -71,6 +71,31 @@ describe('CSS Customization', () => {
})
})
describe
(
'Legacy Next-CSS Customization'
,
()
=>
{
const
appDir
=
join
(
fixturesDir
,
'custom-configuration-legacy'
)
beforeAll
(
async
()
=>
{
await
remove
(
join
(
appDir
,
'.next'
))
})
it
(
'should build successfully'
,
async
()
=>
{
await
nextBuild
(
appDir
)
})
it
(
`should've compiled and prefixed`
,
async
()
=>
{
const
cssFolder
=
join
(
appDir
,
'.next/static/chunks'
)
const
files
=
await
readdir
(
cssFolder
)
const
cssFiles
=
files
.
filter
(
f
=>
/
\.
css$/
.
test
(
f
))
expect
(
cssFiles
.
length
).
toBe
(
1
)
const
cssContent
=
await
readFile
(
join
(
cssFolder
,
cssFiles
[
0
]),
'utf8'
)
expect
(
cssContent
.
replace
(
/
\/\*
.*
?\*\/
/g
,
''
).
trim
()).
toMatchInlineSnapshot
(
`"@media (480px <= width < 768px){::placeholder{color:green}}.video{max-width:400px;max-height:300px}"`
)
})
})
describe
(
'CSS Customization Array'
,
()
=>
{
const
appDir
=
join
(
fixturesDir
,
'custom-configuration-arr'
)
...
...
test/integration/css-fixtures/custom-configuration-legacy/next.config.js
0 → 100644
View file @
8247f19f
const
withCSS
=
require
(
'@zeit/next-css'
)
module
.
exports
=
withCSS
({
onDemandEntries
:
{
// Make sure entries are not getting disposed.
maxInactiveAge
:
1000
*
60
*
60
,
},
experimental
:
{
css
:
true
,
},
webpack
(
cfg
)
{
cfg
.
devtool
=
'source-map'
return
cfg
},
})
test/integration/css-fixtures/custom-configuration-legacy/pages/_app.js
0 → 100644
View file @
8247f19f
import
React
from
'react'
import
App
from
'next/app'
import
'../styles/global.css'
class
MyApp
extends
App
{
render
()
{
const
{
Component
,
pageProps
}
=
this
.
props
return
<
Component
{...
pageProps
}
/
>
}
}
export
default
MyApp
test/integration/css-fixtures/custom-configuration-legacy/pages/index.js
0 → 100644
View file @
8247f19f
export
default
function
Home
()
{
return
<
div
/>
}
test/integration/css-fixtures/custom-configuration-legacy/postcss.config.js
0 → 100644
View file @
8247f19f
module
.
exports
=
{
// Use comments to test JSON5 support
plugins
:
[
// Test a non-standard feature that wouldn't be normally enabled
require
(
'postcss-short-size'
)({
// Add a prefix to test that configuration is passed
prefix
:
'xyz'
,
}),
],
}
test/integration/css-fixtures/custom-configuration-legacy/styles/global.css
0 → 100644
View file @
8247f19f
/* this should pass through untransformed */
@media
(
480px
<=
width
<
768px
)
{
::placeholder
{
color
:
green
;
}
}
/* this should be transformed to width/height */
.video
{
-xyz-max-size
:
400px
300px
;
}
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