@@ -10,11 +10,15 @@ import path from 'path';
10
10
import crypto from 'crypto' ;
11
11
import logger from '@docusaurus/logger' ;
12
12
import { BABEL_CONFIG_FILE_NAME } from '@docusaurus/utils' ;
13
- import MiniCssExtractPlugin from 'mini-css-extract-plugin' ;
14
- import webpack , { type Configuration , type RuleSetRule } from 'webpack' ;
13
+ import webpack , { type Configuration } from 'webpack' ;
15
14
import formatWebpackMessages from 'react-dev-utils/formatWebpackMessages' ;
16
15
import { importSwcJsLoaderFactory } from '../faster' ;
17
- import type { ConfigureWebpackUtils , DocusaurusConfig } from '@docusaurus/types' ;
16
+ import { getCSSExtractPlugin } from './currentBundler' ;
17
+ import type {
18
+ ConfigureWebpackUtils ,
19
+ CurrentBundler ,
20
+ DocusaurusConfig ,
21
+ } from '@docusaurus/types' ;
18
22
import type { TransformOptions } from '@babel/core' ;
19
23
20
24
export function formatStatsErrorMessage (
@@ -42,68 +46,75 @@ export function printStatsWarnings(
42
46
}
43
47
}
44
48
45
- // Utility method to get style loaders
46
- export function getStyleLoaders (
47
- isServer : boolean ,
48
- cssOptionsArg : {
49
- [ key : string ] : unknown ;
50
- } = { } ,
51
- ) : RuleSetRule [ ] {
52
- const cssOptions : { [ key : string ] : unknown } = {
53
- // TODO turn esModule on later, see https://github.com/facebook/docusaurus/pull/6424
54
- esModule : false ,
55
- ...cssOptionsArg ,
56
- } ;
49
+ export async function createStyleLoadersFactory ( {
50
+ currentBundler,
51
+ } : {
52
+ currentBundler : CurrentBundler ;
53
+ } ) : Promise < ConfigureWebpackUtils [ 'getStyleLoaders' ] > {
54
+ const CssExtractPlugin = await getCSSExtractPlugin ( { currentBundler} ) ;
57
55
58
- // On the server we don't really need to extract/emit CSS
59
- // We only need to transform CSS module imports to a styles object
60
- if ( isServer ) {
61
- return cssOptions . modules
62
- ? [
63
- {
64
- loader : require . resolve ( 'css-loader' ) ,
65
- options : cssOptions ,
66
- } ,
67
- ]
68
- : // Ignore regular CSS files
69
- [ { loader : require . resolve ( 'null-loader' ) } ] ;
70
- }
56
+ return function getStyleLoaders (
57
+ isServer : boolean ,
58
+ cssOptionsArg : {
59
+ [ key : string ] : unknown ;
60
+ } = { } ,
61
+ ) {
62
+ const cssOptions : { [ key : string ] : unknown } = {
63
+ // TODO turn esModule on later, see https://github.com/facebook/docusaurus/pull/6424
64
+ esModule : false ,
65
+ ...cssOptionsArg ,
66
+ } ;
71
67
72
- return [
73
- {
74
- loader : MiniCssExtractPlugin . loader ,
75
- options : {
76
- esModule : true ,
68
+ // On the server we don't really need to extract/emit CSS
69
+ // We only need to transform CSS module imports to a styles object
70
+ if ( isServer ) {
71
+ return cssOptions . modules
72
+ ? [
73
+ {
74
+ loader : require . resolve ( 'css-loader' ) ,
75
+ options : cssOptions ,
76
+ } ,
77
+ ]
78
+ : // Ignore regular CSS files
79
+ [ { loader : require . resolve ( 'null-loader' ) } ] ;
80
+ }
81
+
82
+ return [
83
+ {
84
+ loader : CssExtractPlugin . loader ,
85
+ options : {
86
+ esModule : true ,
87
+ } ,
88
+ } ,
89
+ {
90
+ loader : require . resolve ( 'css-loader' ) ,
91
+ options : cssOptions ,
77
92
} ,
78
- } ,
79
- {
80
- loader : require . resolve ( 'css-loader' ) ,
81
- options : cssOptions ,
82
- } ,
83
93
84
- // TODO apart for configurePostCss(), do we really need this loader?
85
- // Note: using postcss here looks inefficient/duplicate
86
- // But in practice, it's not a big deal because css-loader also uses postcss
87
- // and is able to reuse the parsed AST from postcss-loader
88
- // See https://github.com/webpack-contrib/css-loader/blob/master/src/index.js#L159
89
- {
90
- // Options for PostCSS as we reference these options twice
91
- // Adds vendor prefixing based on your specified browser support in
92
- // package.json
93
- loader : require . resolve ( 'postcss-loader' ) ,
94
- options : {
95
- postcssOptions : {
96
- // Necessary for external CSS imports to work
97
- // https://github.com/facebook/create-react-app/issues/2677
98
- ident : 'postcss' ,
99
- plugins : [
100
- // eslint-disable-next-line global-require
101
- require ( 'autoprefixer' ) ,
102
- ] ,
94
+ // TODO apart for configurePostCss(), do we really need this loader?
95
+ // Note: using postcss here looks inefficient/duplicate
96
+ // But in practice, it's not a big deal because css-loader also uses postcss
97
+ // and is able to reuse the parsed AST from postcss-loader
98
+ // See https://github.com/webpack-contrib/css-loader/blob/master/src/index.js#L159
99
+ {
100
+ // Options for PostCSS as we reference these options twice
101
+ // Adds vendor prefixing based on your specified browser support in
102
+ // package.json
103
+ loader : require . resolve ( 'postcss-loader' ) ,
104
+ options : {
105
+ postcssOptions : {
106
+ // Necessary for external CSS imports to work
107
+ // https://github.com/facebook/create-react-app/issues/2677
108
+ ident : 'postcss' ,
109
+ plugins : [
110
+ // eslint-disable-next-line global-require
111
+ require ( 'autoprefixer' ) ,
112
+ ] ,
113
+ } ,
103
114
} ,
104
115
} ,
105
- } ,
106
- ] ;
116
+ ] ;
117
+ } ;
107
118
}
108
119
109
120
export async function getCustomBabelConfigFilePath (
0 commit comments