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
f67f99a2
Commit
f67f99a2
authored
Jan 22, 2020
by
Joe Haddad
Committed by
JJ Kasper
Jan 22, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extract CSS Loaders into Separate Files (#10210)
parent
6988a2e0
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
160 additions
and
124 deletions
+160
-124
index.ts
packages/next/build/webpack/config/blocks/css/index.ts
+4
-124
client.ts
...es/next/build/webpack/config/blocks/css/loaders/client.ts
+58
-0
getCssModuleLocalIdent.ts
...bpack/config/blocks/css/loaders/getCssModuleLocalIdent.ts
+0
-0
global.ts
...es/next/build/webpack/config/blocks/css/loaders/global.ts
+40
-0
index.ts
...ges/next/build/webpack/config/blocks/css/loaders/index.ts
+2
-0
modules.ts
...s/next/build/webpack/config/blocks/css/loaders/modules.ts
+56
-0
No files found.
packages/next/build/webpack/config/blocks/css/index.ts
View file @
f67f99a2
import
curry
from
'lodash.curry'
import
path
from
'path'
import
webpack
,
{
Configuration
}
from
'webpack'
import
{
Configuration
}
from
'webpack'
import
MiniCssExtractPlugin
from
'../../../plugins/mini-css-extract-plugin'
import
{
loader
,
plugin
}
from
'../../helpers'
import
{
ConfigurationContext
,
ConfigurationFn
,
pipe
}
from
'../../utils'
import
{
getCssModuleLo
calIdent
}
from
'./getCssModuleLocalIdent
'
import
{
getCssModuleLo
ader
,
getGlobalCssLoader
}
from
'./loaders
'
import
{
getCustomDocumentError
,
getGlobalImportError
,
...
...
@@ -18,62 +18,6 @@ const regexCssAll = /\.css$/
const
regexCssGlobal
=
/
(?<
!
\.
module
)\.
css$/
const
regexCssModules
=
/
\.
module
\.
css$/
function
getClientStyleLoader
({
isDevelopment
,
assetPrefix
,
}:
{
isDevelopment
:
boolean
assetPrefix
:
string
}):
webpack
.
RuleSetUseItem
{
return
isDevelopment
?
{
loader
:
require
.
resolve
(
'style-loader'
),
options
:
{
// By default, style-loader injects CSS into the bottom
// of <head>. This causes ordering problems between dev
// and prod. To fix this, we render a <noscript> tag as
// an anchor for the styles to be placed before. These
// styles will be applied _before_ <style jsx global>.
insert
:
function
(
element
:
Node
)
{
// These elements should always exist. If they do not,
// this code should fail.
var
anchorElement
=
document
.
querySelector
(
'#__next_css__DO_NOT_USE__'
)
!
var
parentNode
=
anchorElement
.
parentNode
!
// Normally <head>
// Each style tag should be placed right before our
// anchor. By inserting before and not after, we do not
// need to track the last inserted element.
parentNode
.
insertBefore
(
element
,
anchorElement
)
// Remember: this is development only code.
//
// After styles are injected, we need to remove the
// <style> tags that set `body { display: none; }`.
//
// We use `requestAnimationFrame` as a way to defer
// this operation since there may be multiple style
// tags.
;(
self
.
requestAnimationFrame
||
setTimeout
)(
function
()
{
for
(
var
x
=
document
.
querySelectorAll
(
'[data-next-hide-fouc]'
),
i
=
x
.
length
;
i
--
;
)
{
x
[
i
].
parentNode
!
.
removeChild
(
x
[
i
])
}
})
},
},
}
:
{
loader
:
MiniCssExtractPlugin
.
loader
,
options
:
{
publicPath
:
`
${
assetPrefix
}
/_next/`
},
}
}
export
const
css
=
curry
(
async
function
css
(
enabled
:
boolean
,
ctx
:
ConfigurationContext
,
...
...
@@ -145,47 +89,7 @@ export const css = curry(async function css(
include
:
[
ctx
.
rootDirectory
],
exclude
:
/node_modules/
,
},
use
:
([
// Add appropriate development more or production mode style
// loader
ctx
.
isClient
&&
getClientStyleLoader
({
isDevelopment
:
ctx
.
isDevelopment
,
assetPrefix
:
ctx
.
assetPrefix
,
}),
// Resolve CSS `@import`s and `url()`s
{
loader
:
require
.
resolve
(
'css-loader'
),
options
:
{
importLoaders
:
1
,
sourceMap
:
true
,
onlyLocals
:
ctx
.
isServer
,
modules
:
{
// Disallow global style exports so we can code-split CSS and
// not worry about loading order.
mode
:
'pure'
,
// Generate a friendly production-ready name so it's
// reasonably understandable. The same name is used for
// development.
// TODO: Consider making production reduce this to a single
// character?
getLocalIdent
:
getCssModuleLocalIdent
,
},
},
},
// Compile CSS
{
loader
:
require
.
resolve
(
'postcss-loader'
),
options
:
{
ident
:
'__nextjs_postcss'
,
plugins
:
postCssPlugins
,
sourceMap
:
true
,
},
},
]
as
webpack
.
RuleSetUseItem
[]).
filter
(
Boolean
),
use
:
getCssModuleLoader
(
ctx
,
postCssPlugins
),
},
],
})
...
...
@@ -228,31 +132,7 @@ export const css = curry(async function css(
sideEffects
:
true
,
test
:
regexCssGlobal
,
issuer
:
{
include
:
ctx
.
customAppFile
},
use
:
[
// Add appropriate development more or production mode style
// loader
getClientStyleLoader
({
isDevelopment
:
ctx
.
isDevelopment
,
assetPrefix
:
ctx
.
assetPrefix
,
}),
// Resolve CSS `@import`s and `url()`s
{
loader
:
require
.
resolve
(
'css-loader'
),
options
:
{
importLoaders
:
1
,
sourceMap
:
true
},
},
// Compile CSS
{
loader
:
require
.
resolve
(
'postcss-loader'
),
options
:
{
ident
:
'__nextjs_postcss'
,
plugins
:
postCssPlugins
,
sourceMap
:
true
,
},
},
],
use
:
getGlobalCssLoader
(
ctx
,
postCssPlugins
),
},
],
})
...
...
packages/next/build/webpack/config/blocks/css/loaders/client.ts
0 → 100644
View file @
f67f99a2
import
webpack
from
'webpack'
import
MiniCssExtractPlugin
from
'../../../../plugins/mini-css-extract-plugin'
export
function
getClientStyleLoader
({
isDevelopment
,
assetPrefix
,
}:
{
isDevelopment
:
boolean
assetPrefix
:
string
}):
webpack
.
RuleSetUseItem
{
return
isDevelopment
?
{
loader
:
require
.
resolve
(
'style-loader'
),
options
:
{
// By default, style-loader injects CSS into the bottom
// of <head>. This causes ordering problems between dev
// and prod. To fix this, we render a <noscript> tag as
// an anchor for the styles to be placed before. These
// styles will be applied _before_ <style jsx global>.
insert
:
function
(
element
:
Node
)
{
// These elements should always exist. If they do not,
// this code should fail.
var
anchorElement
=
document
.
querySelector
(
'#__next_css__DO_NOT_USE__'
)
!
var
parentNode
=
anchorElement
.
parentNode
!
// Normally <head>
// Each style tag should be placed right before our
// anchor. By inserting before and not after, we do not
// need to track the last inserted element.
parentNode
.
insertBefore
(
element
,
anchorElement
)
// Remember: this is development only code.
//
// After styles are injected, we need to remove the
// <style> tags that set `body { display: none; }`.
//
// We use `requestAnimationFrame` as a way to defer
// this operation since there may be multiple style
// tags.
;(
self
.
requestAnimationFrame
||
setTimeout
)(
function
()
{
for
(
var
x
=
document
.
querySelectorAll
(
'[data-next-hide-fouc]'
),
i
=
x
.
length
;
i
--
;
)
{
x
[
i
].
parentNode
!
.
removeChild
(
x
[
i
])
}
})
},
},
}
:
{
loader
:
MiniCssExtractPlugin
.
loader
,
options
:
{
publicPath
:
`
${
assetPrefix
}
/_next/`
},
}
}
packages/next/build/webpack/config/blocks/css/getCssModuleLocalIdent.ts
→
packages/next/build/webpack/config/blocks/css/
loaders/
getCssModuleLocalIdent.ts
View file @
f67f99a2
File moved
packages/next/build/webpack/config/blocks/css/loaders/global.ts
0 → 100644
View file @
f67f99a2
import
postcss
from
'postcss'
import
webpack
from
'webpack'
import
{
ConfigurationContext
}
from
'../../../utils'
import
{
getClientStyleLoader
}
from
'./client'
export
function
getGlobalCssLoader
(
ctx
:
ConfigurationContext
,
postCssPlugins
:
postcss
.
AcceptedPlugin
[]
):
webpack
.
RuleSetUseItem
[]
{
const
loaders
:
webpack
.
RuleSetUseItem
[]
=
[]
if
(
ctx
.
isClient
)
{
// Add appropriate development more or production mode style
// loader
loaders
.
push
(
getClientStyleLoader
({
isDevelopment
:
ctx
.
isDevelopment
,
assetPrefix
:
ctx
.
assetPrefix
,
})
)
}
// Resolve CSS `@import`s and `url()`s
loaders
.
push
({
loader
:
require
.
resolve
(
'css-loader'
),
options
:
{
importLoaders
:
1
,
sourceMap
:
true
},
})
// Compile CSS
loaders
.
push
({
loader
:
require
.
resolve
(
'postcss-loader'
),
options
:
{
ident
:
'__nextjs_postcss'
,
plugins
:
postCssPlugins
,
sourceMap
:
true
,
},
})
return
loaders
}
packages/next/build/webpack/config/blocks/css/loaders/index.ts
0 → 100644
View file @
f67f99a2
export
*
from
'./global'
export
*
from
'./modules'
packages/next/build/webpack/config/blocks/css/loaders/modules.ts
0 → 100644
View file @
f67f99a2
import
postcss
from
'postcss'
import
webpack
from
'webpack'
import
{
ConfigurationContext
}
from
'../../../utils'
import
{
getClientStyleLoader
}
from
'./client'
import
{
getCssModuleLocalIdent
}
from
'./getCssModuleLocalIdent'
export
function
getCssModuleLoader
(
ctx
:
ConfigurationContext
,
postCssPlugins
:
postcss
.
AcceptedPlugin
[]
):
webpack
.
RuleSetUseItem
[]
{
const
loaders
:
webpack
.
RuleSetUseItem
[]
=
[]
if
(
ctx
.
isClient
)
{
// Add appropriate development more or production mode style
// loader
loaders
.
push
(
getClientStyleLoader
({
isDevelopment
:
ctx
.
isDevelopment
,
assetPrefix
:
ctx
.
assetPrefix
,
})
)
}
// Resolve CSS `@import`s and `url()`s
loaders
.
push
({
loader
:
require
.
resolve
(
'css-loader'
),
options
:
{
importLoaders
:
1
,
sourceMap
:
true
,
onlyLocals
:
ctx
.
isServer
,
modules
:
{
// Disallow global style exports so we can code-split CSS and
// not worry about loading order.
mode
:
'pure'
,
// Generate a friendly production-ready name so it's
// reasonably understandable. The same name is used for
// development.
// TODO: Consider making production reduce this to a single
// character?
getLocalIdent
:
getCssModuleLocalIdent
,
},
},
})
// Compile CSS
loaders
.
push
({
loader
:
require
.
resolve
(
'postcss-loader'
),
options
:
{
ident
:
'__nextjs_postcss'
,
plugins
:
postCssPlugins
,
sourceMap
:
true
,
},
})
return
loaders
}
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