7
7
*/
8
8
9
9
import assert from 'assert';
10
- import { pluginName } from 'mini-css-extract-plugin';
11
10
import type { Compilation, Compiler } from 'webpack';
12
11
import { assertIsError } from '../../utils/error';
13
12
import { addError } from '../../utils/webpack-diagnostics';
@@ -30,10 +29,6 @@ export class StylesWebpackPlugin {
30
29
31
30
apply(compiler: Compiler): void {
32
31
const { entryPoints, preserveSymlinks, root } = this.options;
33
- const webpackOptions = compiler.options;
34
- const entry =
35
- typeof webpackOptions.entry === 'function' ? webpackOptions.entry() : webpackOptions.entry;
36
-
37
32
const resolver = compiler.resolverFactory.get('global-styles', {
38
33
conditionNames: ['sass', 'less', 'style'],
39
34
mainFields: ['sass', 'less', 'style', 'main', '...'],
@@ -45,32 +40,38 @@ export class StylesWebpackPlugin {
45
40
fileSystem: compiler.inputFileSystem,
46
41
});
47
42
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;
50
47
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;
54
50
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) {
61
65
assert(this.compilation, 'Compilation cannot be undefined.');
62
- addError(this.compilation, `Cannot resolve '${path}'.`);
66
+ assertIsError(error);
67
+ addError(this.compilation, error.message);
63
68
}
64
- } catch (error) {
65
- assert(this.compilation, 'Compilation cannot be undefined.');
66
- assertIsError(error);
67
- addError(this.compilation, error.message);
68
69
}
69
70
}
70
- }
71
71
72
- return entrypoints;
73
- };
72
+ return entrypoints;
73
+ };
74
+ });
74
75
75
76
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
76
77
this.compilation = compilation;
0 commit comments