@@ -24,60 +24,14 @@ import {
24
24
SuppressExtractedTextChunksWebpackPlugin ,
25
25
} from '../plugins' ;
26
26
import { CssOptimizerPlugin } from '../plugins/css-optimizer-plugin' ;
27
+ import { StylesWebpackPlugin } from '../plugins/styles-webpack-plugin' ;
27
28
import {
28
29
assetNameTemplateFactory ,
29
30
getOutputHashFormat ,
30
31
normalizeExtraEntryPoints ,
32
+ normalizeGlobalStyles ,
31
33
} from '../utils/helpers' ;
32
34
33
- export function resolveGlobalStyles (
34
- styleEntrypoints : StyleElement [ ] ,
35
- root : string ,
36
- preserveSymlinks : boolean ,
37
- skipResolution = false ,
38
- ) : { entryPoints : Record < string , string [ ] > ; noInjectNames : string [ ] ; paths : string [ ] } {
39
- const entryPoints : Record < string , string [ ] > = { } ;
40
- const noInjectNames : string [ ] = [ ] ;
41
- const paths : string [ ] = [ ] ;
42
-
43
- if ( styleEntrypoints . length === 0 ) {
44
- return { entryPoints, noInjectNames, paths } ;
45
- }
46
-
47
- for ( const style of normalizeExtraEntryPoints ( styleEntrypoints , 'styles' ) ) {
48
- let stylesheetPath = style . input ;
49
- if ( ! skipResolution ) {
50
- stylesheetPath = path . resolve ( root , stylesheetPath ) ;
51
- if ( ! fs . existsSync ( stylesheetPath ) ) {
52
- try {
53
- stylesheetPath = require . resolve ( style . input , { paths : [ root ] } ) ;
54
- } catch { }
55
- }
56
- }
57
-
58
- if ( ! preserveSymlinks ) {
59
- stylesheetPath = fs . realpathSync ( stylesheetPath ) ;
60
- }
61
-
62
- // Add style entry points.
63
- if ( entryPoints [ style . bundleName ] ) {
64
- entryPoints [ style . bundleName ] . push ( stylesheetPath ) ;
65
- } else {
66
- entryPoints [ style . bundleName ] = [ stylesheetPath ] ;
67
- }
68
-
69
- // Add non injected styles to the list.
70
- if ( ! style . inject ) {
71
- noInjectNames . push ( style . bundleName ) ;
72
- }
73
-
74
- // Add global css paths.
75
- paths . push ( stylesheetPath ) ;
76
- }
77
-
78
- return { entryPoints, noInjectNames, paths } ;
79
- }
80
-
81
35
// eslint-disable-next-line max-lines-per-function
82
36
export function getStylesConfig ( wco : WebpackConfigOptions ) : Configuration {
83
37
const { root, projectRoot, buildOptions } = wco ;
@@ -95,14 +49,20 @@ export function getStylesConfig(wco: WebpackConfigOptions): Configuration {
95
49
buildOptions . stylePreprocessorOptions ?. includePaths ?. map ( ( p ) => path . resolve ( root , p ) ) ?? [ ] ;
96
50
97
51
// Process global styles.
98
- const {
99
- entryPoints,
100
- noInjectNames,
101
- paths : globalStylePaths ,
102
- } = resolveGlobalStyles ( buildOptions . styles , root , ! ! buildOptions . preserveSymlinks ) ;
103
- if ( noInjectNames . length > 0 ) {
104
- // Add plugin to remove hashes from lazy styles.
105
- extraPlugins . push ( new RemoveHashPlugin ( { chunkNames : noInjectNames , hashFormat } ) ) ;
52
+ if ( buildOptions . styles . length > 0 ) {
53
+ const { entryPoints, noInjectNames } = normalizeGlobalStyles ( buildOptions . styles ) ;
54
+ extraPlugins . push (
55
+ new StylesWebpackPlugin ( {
56
+ root,
57
+ entryPoints,
58
+ preserveSymlinks : buildOptions . preserveSymlinks ,
59
+ } ) ,
60
+ ) ;
61
+
62
+ if ( noInjectNames . length > 0 ) {
63
+ // Add plugin to remove hashes from lazy styles.
64
+ extraPlugins . push ( new RemoveHashPlugin ( { chunkNames : noInjectNames , hashFormat } ) ) ;
65
+ }
106
66
}
107
67
108
68
const sassImplementation = useLegacySass
@@ -319,7 +279,6 @@ export function getStylesConfig(wco: WebpackConfigOptions): Configuration {
319
279
] ;
320
280
321
281
return {
322
- entry : entryPoints ,
323
282
module : {
324
283
rules : styleLanguages . map ( ( { extensions, use } ) => ( {
325
284
test : new RegExp ( `\\.(?:${ extensions . join ( '|' ) } )$` , 'i' ) ,
@@ -330,8 +289,7 @@ export function getStylesConfig(wco: WebpackConfigOptions): Configuration {
330
289
// Global styles are only defined global styles
331
290
{
332
291
use : globalStyleLoaders ,
333
- include : globalStylePaths ,
334
- resourceQuery : { not : [ / \? n g R e s o u r c e / ] } ,
292
+ resourceQuery : / \? n g G l o b a l S t y l e / ,
335
293
} ,
336
294
// Component styles are all styles except defined global styles
337
295
{
0 commit comments