77 */
88
99import assert from 'assert' ;
10- import { pluginName } from 'mini-css-extract-plugin' ;
1110import type { Compilation , Compiler } from 'webpack' ;
1211import { assertIsError } from '../../utils/error' ;
1312import { addError } from '../../utils/webpack-diagnostics' ;
@@ -30,10 +29,6 @@ export class StylesWebpackPlugin {
3029
3130 apply ( compiler : Compiler ) : void {
3231 const { entryPoints, preserveSymlinks, root } = this . options ;
33- const webpackOptions = compiler . options ;
34- const entry =
35- typeof webpackOptions . entry === 'function' ? webpackOptions . entry ( ) : webpackOptions . entry ;
36-
3732 const resolver = compiler . resolverFactory . get ( 'global-styles' , {
3833 conditionNames : [ 'sass' , 'less' , 'style' ] ,
3934 mainFields : [ 'sass' , 'less' , 'style' , 'main' , '...' ] ,
@@ -45,32 +40,38 @@ export class StylesWebpackPlugin {
4540 fileSystem : compiler . inputFileSystem ,
4641 } ) ;
4742
48- webpackOptions . entry = async ( ) => {
49- const entrypoints = await entry ;
43+ const webpackOptions = compiler . options ;
44+ compiler . hooks . environment . tap ( PLUGIN_NAME , ( ) => {
45+ const entry =
46+ typeof webpackOptions . entry === 'function' ? webpackOptions . entry ( ) : webpackOptions . entry ;
5047
51- for ( const [ bundleName , paths ] of Object . entries ( entryPoints ) ) {
52- entrypoints [ bundleName ] ??= { } ;
53- const entryImport = ( entrypoints [ bundleName ] . import ??= [ ] ) ;
48+ webpackOptions . entry = async ( ) => {
49+ const entrypoints = await entry ;
5450
55- for ( const path of paths ) {
56- try {
57- const resolvedPath = resolver . resolveSync ( { } , root , path ) ;
58- if ( resolvedPath ) {
59- entryImport . push ( `${ resolvedPath } ?ngGlobalStyle` ) ;
60- } else {
51+ for ( const [ bundleName , paths ] of Object . entries ( entryPoints ) ) {
52+ entrypoints [ bundleName ] ??= { } ;
53+ const entryImport = ( entrypoints [ bundleName ] . import ??= [ ] ) ;
54+
55+ for ( const path of paths ) {
56+ try {
57+ const resolvedPath = resolver . resolveSync ( { } , root , path ) ;
58+ if ( resolvedPath ) {
59+ entryImport . push ( `${ resolvedPath } ?ngGlobalStyle` ) ;
60+ } else {
61+ assert ( this . compilation , 'Compilation cannot be undefined.' ) ;
62+ addError ( this . compilation , `Cannot resolve '${ path } '.` ) ;
63+ }
64+ } catch ( error ) {
6165 assert ( this . compilation , 'Compilation cannot be undefined.' ) ;
62- addError ( this . compilation , `Cannot resolve '${ path } '.` ) ;
66+ assertIsError ( error ) ;
67+ addError ( this . compilation , error . message ) ;
6368 }
64- } catch ( error ) {
65- assert ( this . compilation , 'Compilation cannot be undefined.' ) ;
66- assertIsError ( error ) ;
67- addError ( this . compilation , error . message ) ;
6869 }
6970 }
70- }
7171
72- return entrypoints ;
73- } ;
72+ return entrypoints ;
73+ } ;
74+ } ) ;
7475
7576 compiler . hooks . thisCompilation . tap ( PLUGIN_NAME , ( compilation ) => {
7677 this . compilation = compilation ;
0 commit comments