@@ -33,23 +33,53 @@ function getSupportedBrowsers(dir: string, isDevelopment: boolean) {
3333const getVanillaExtractCssLoaders = (
3434 options : WebpackConfigContext ,
3535 assetPrefix : string ,
36+ hasAppDir : boolean ,
3637) => {
3738 const loaders : webpack . RuleSetUseItem [ ] = [ ] ;
3839
3940 // Adopt from Next.js' getClientStyleLoader
4041 // https://github.com/vercel/next.js/blob/6e5b935fd7a61497f6854a81aec7df3a5dbf61ac/packages/next/src/build/webpack/config/blocks/css/loaders/client.ts#L3
4142 if ( ! options . isServer ) {
42- // https://github.com/vercel/next.js/blob/6e5b935fd7a61497f6854a81aec7df3a5dbf61ac/packages/next/src/build/webpack/config/blocks/css/loaders/client.ts#L44
43- // next-style-loader will mess up css order in development mode.
44- // Next.js appDir doesn't use next-style-loader either.
45- // So we always use css-loader here, to simplify things and get proper order of output CSS
46- loaders . push ( {
47- loader : NextMiniCssExtractPlugin . loader ,
48- options : {
49- publicPath : `${ assetPrefix } /_next/` ,
50- esModule : false ,
51- } ,
52- } ) ;
43+ // https://github.com/vercel/next.js/blob/6e5b935fd7a61497f6854a81aec7df3a5dbf61ac/packages/next/src/build/webpack/config/blocks/css/loaders/client.ts#L16
44+ // Keep next-style-loader for development mode in `pages/`
45+ if ( options . dev && ! hasAppDir ) {
46+ loaders . push ( {
47+ loader : 'next-style-loader' ,
48+ options : {
49+ insert : function ( element : Node ) {
50+ // By default, style-loader injects CSS into the bottom
51+ // of <head>. This causes ordering problems between dev
52+ // and prod. To fix this, we render a <noscript> tag as
53+ // an anchor for the styles to be placed before. These
54+ // styles will be applied _before_ <style jsx global>.
55+
56+ // These elements should always exist. If they do not,
57+ // this code should fail.
58+ const anchorElement = document . querySelector (
59+ '#__next_css__DO_NOT_USE__' ,
60+ ) ! ;
61+ const parentNode = anchorElement . parentNode ! ; // Normally <head>
62+
63+ // Each style tag should be placed right before our
64+ // anchor. By inserting before and not after, we do not
65+ // need to track the last inserted element.
66+ parentNode . insertBefore ( element , anchorElement ) ;
67+ } ,
68+ } ,
69+ } ) ;
70+ } else {
71+ // https://github.com/vercel/next.js/blob/6e5b935fd7a61497f6854a81aec7df3a5dbf61ac/packages/next/src/build/webpack/config/blocks/css/loaders/client.ts#L44
72+ // next-style-loader will mess up css order in development mode.
73+ // Next.js appDir doesn't use next-style-loader either.
74+ // So we always use css-loader here, to simplify things and get proper order of output CSS
75+ loaders . push ( {
76+ loader : NextMiniCssExtractPlugin . loader ,
77+ options : {
78+ publicPath : `${ assetPrefix } /_next/` ,
79+ esModule : false ,
80+ } ,
81+ } ) ;
82+ }
5383 }
5484
5585 const postcss = ( ) =>
@@ -105,7 +135,7 @@ export const createVanillaExtractPlugin = (
105135 return ( nextConfig : NextConfig = { } ) : NextConfig => ( {
106136 ...nextConfig ,
107137 webpack ( config : any , options : WebpackConfigContext ) {
108- const { dir, dev, isServer , config : resolvedNextConfig } = options ;
138+ const { dir, dev, config : resolvedNextConfig } = options ;
109139
110140 // https://github.com/vercel/next.js/blob/1fb4cad2a8329811b5ccde47217b4a6ae739124e/packages/next/build/index.ts#L336
111141 // https://github.com/vercel/next.js/blob/1fb4cad2a8329811b5ccde47217b4a6ae739124e/packages/next/build/webpack-config.ts#L626
@@ -118,11 +148,7 @@ export const createVanillaExtractPlugin = (
118148 // Skip nextConfig check since appDir is stable feature after Next.js 13.4
119149 const hasAppDir = ! ! ( findPagesDirResult && findPagesDirResult . appDir ) ;
120150
121- const outputCss = hasAppDir
122- ? // Always output css since Next.js App Router needs to collect Server CSS from React Server Components
123- true
124- : // There is no appDir, do not output css on server build
125- ! isServer ;
151+ const outputCss = true ;
126152
127153 // https://github.com/vercel/next.js/blob/6e5b935fd7a61497f6854a81aec7df3a5dbf61ac/packages/next/src/build/webpack/config/helpers.ts#L12-L21
128154 const cssRules = config . module . rules . find (
@@ -143,6 +169,7 @@ export const createVanillaExtractPlugin = (
143169 use : getVanillaExtractCssLoaders (
144170 options ,
145171 resolvedNextConfig . assetPrefix ,
172+ hasAppDir ,
146173 ) ,
147174 } ) ;
148175
0 commit comments