@roots/bud-compiler 2023.2.20 → 2023.2.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,54 +1,43 @@
1
1
  import { Service } from '@roots/bud-framework/service';
2
2
  import type { Compiler as Contract } from '@roots/bud-framework/services';
3
+ import type { ErrorWithSourceFile } from '@roots/bud-support/open';
3
4
  import type webpack from '@roots/bud-support/webpack';
4
- import type { MultiCompiler, MultiStats, WebpackError } from '@roots/bud-support/webpack';
5
+ import type { MultiCompiler, MultiStats, StatsError } from '@roots/bud-support/webpack';
5
6
  /**
6
7
  * Wepback compilation controller class
7
8
  */
8
9
  export declare class Compiler extends Service implements Contract.Service {
9
10
  /**
10
11
  * Compiler implementation
11
- * @public
12
12
  */
13
13
  implementation: typeof webpack;
14
14
  /**
15
15
  * Compiler instance
16
- * @public
17
16
  */
18
17
  instance: Contract.Service[`instance`];
19
18
  /**
20
19
  * Compilation stats
21
- * @public
22
20
  */
23
21
  stats: Contract.Service[`stats`];
24
22
  /**
25
23
  * Configuration
26
- * @public
27
24
  */
28
25
  config: Contract.Service[`config`];
29
26
  /**
30
27
  * Initiates compilation
31
- *
32
- * @returns the compiler instance
33
- *
34
- * @public
35
- * @decorator `@bind`
36
- * @decorator `@once`
37
28
  */
38
29
  compile(): Promise<MultiCompiler>;
39
30
  /**
40
31
  * Stats handler
41
- *
42
- * @public
43
- * @decorator `@bind`
44
32
  */
45
33
  onStats(stats: MultiStats): Promise<void>;
46
34
  /**
47
35
  * Compiler error event
48
- *
49
- * @public
50
- * @decorator `@bind`
51
36
  */
52
- onError(error: WebpackError): void;
37
+ onError(error: Error): Promise<void>;
38
+ /**
39
+ * Parse errors from webpack stats
40
+ */
41
+ sourceErrors(errors: Array<StatsError>): Array<ErrorWithSourceFile | StatsError>;
53
42
  }
54
43
  //# sourceMappingURL=compiler.service.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"compiler.service.d.ts","sourceRoot":"","sources":["../src/compiler.service.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,OAAO,EAAC,MAAM,8BAA8B,CAAA;AACpD,OAAO,KAAK,EAAC,QAAQ,IAAI,QAAQ,EAAC,MAAM,+BAA+B,CAAA;AAEvE,OAAO,KAAK,OAAO,MAAM,4BAA4B,CAAA;AACrD,OAAO,KAAK,EACV,aAAa,EACb,UAAU,EACV,YAAY,EACb,MAAM,4BAA4B,CAAA;AAEnC;;GAEG;AACH,qBAAa,QAAS,SAAQ,OAAQ,YAAW,QAAQ,CAAC,OAAO;IAC/D;;;OAGG;IACI,cAAc,EAAE,OAAO,OAAO,CAAA;IAErC;;;OAGG;IACI,QAAQ,EAAE,QAAQ,CAAC,OAAO,CAAC,UAAU,CAAC,CAAA;IAE7C;;;OAGG;IACI,KAAK,EAAE,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;IAEvC;;;OAGG;IACI,MAAM,EAAE,QAAQ,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAK;IAE9C;;;;;;;;OAQG;IAEU,OAAO,IAAI,OAAO,CAAC,aAAa,CAAC;IAyC9C;;;;;OAKG;IAEU,OAAO,CAAC,KAAK,EAAE,UAAU;IAgBtC;;;;;OAKG;IAEI,OAAO,CAAC,KAAK,EAAE,YAAY;CAMnC"}
1
+ {"version":3,"file":"compiler.service.d.ts","sourceRoot":"","sources":["../src/compiler.service.tsx"],"names":[],"mappings":"AAIA,OAAO,EAAC,OAAO,EAAC,MAAM,8BAA8B,CAAA;AACpD,OAAO,KAAK,EAAC,QAAQ,IAAI,QAAQ,EAAC,MAAM,+BAA+B,CAAA;AAGvE,OAAO,KAAK,EACV,mBAAmB,EAEpB,MAAM,yBAAyB,CAAA;AAGhC,OAAO,KAAK,OAAO,MAAM,4BAA4B,CAAA;AACrD,OAAO,KAAK,EACV,aAAa,EACb,UAAU,EAEV,UAAU,EACX,MAAM,4BAA4B,CAAA;AAEnC;;GAEG;AACH,qBAAa,QAAS,SAAQ,OAAQ,YAAW,QAAQ,CAAC,OAAO;IAC/D;;OAEG;IACI,cAAc,EAAE,OAAO,OAAO,CAAA;IAErC;;OAEG;IACI,QAAQ,EAAE,QAAQ,CAAC,OAAO,CAAC,UAAU,CAAC,CAAA;IAE7C;;OAEG;IACI,KAAK,EAAE,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;IAEvC;;OAEG;IACI,MAAM,EAAE,QAAQ,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAK;IAE9C;;OAEG;IAEU,OAAO,IAAI,OAAO,CAAC,aAAa,CAAC;IAqC9C;;OAEG;IAEU,OAAO,CAAC,KAAK,EAAE,UAAU;IAwDtC;;OAEG;IAEU,OAAO,CAAC,KAAK,EAAE,KAAK;IAuBjC;;OAEG;IAEI,YAAY,CACjB,MAAM,EAAE,KAAK,CAAC,UAAU,CAAC,GACxB,KAAK,CAAC,mBAAmB,GAAG,UAAU,CAAC;CAwC3C"}
@@ -1,6 +1,11 @@
1
1
  import { __decorate, __metadata } from "tslib";
2
+ import { pathToFileURL } from 'node:url';
3
+ import * as App from '@roots/bud-dashboard/app';
2
4
  import { Service } from '@roots/bud-framework/service';
3
5
  import { bind } from '@roots/bud-support/decorators';
6
+ import { duration } from '@roots/bud-support/human-readable';
7
+ import React from '@roots/bud-support/react';
8
+ import stripAnsi from '@roots/bud-support/strip-ansi';
4
9
  /**
5
10
  * Wepback compilation controller class
6
11
  */
@@ -9,18 +14,11 @@ export class Compiler extends Service {
9
14
  super(...arguments);
10
15
  /**
11
16
  * Configuration
12
- * @public
13
17
  */
14
18
  this.config = [];
15
19
  }
16
20
  /**
17
21
  * Initiates compilation
18
- *
19
- * @returns the compiler instance
20
- *
21
- * @public
22
- * @decorator `@bind`
23
- * @decorator `@once`
24
22
  */
25
23
  async compile() {
26
24
  this.implementation = await this.app.module.import(`webpack`);
@@ -35,13 +33,9 @@ export class Compiler extends Service {
35
33
  throw error;
36
34
  }
37
35
  }));
38
- try {
39
- await this.app.hooks.fire(`compiler.before`);
40
- }
41
- catch (error) {
42
- throw error;
43
- }
36
+ await this.app.hooks.fire(`compiler.before`, this.app);
44
37
  if (this.app.isCLI() && this.app.context.args.dry) {
38
+ this.app.context.logger.timeEnd(`initialize`);
45
39
  this.logger.log(`running in dry mode. exiting early.`);
46
40
  return;
47
41
  }
@@ -49,42 +43,106 @@ export class Compiler extends Service {
49
43
  this.instance = this.implementation(this.config);
50
44
  this.instance.hooks.done.tap(this.app.label, async (stats) => {
51
45
  await this.onStats(stats);
46
+ await this.app.hooks.fire(`compiler.close`, this.app);
52
47
  });
53
- this.instance.hooks.done.tap(`${this.app.label}-close`, async () => {
54
- await this.app.hooks.fire(`compiler.close`);
55
- });
56
- await this.app.hooks.fire(`compiler.after`);
48
+ await this.app.hooks.fire(`compiler.after`, this.app);
57
49
  return this.instance;
58
50
  }
59
51
  /**
60
52
  * Stats handler
61
- *
62
- * @public
63
- * @decorator `@bind`
64
53
  */
65
54
  async onStats(stats) {
55
+ const makeNoticeTitle = (child) => this.app.label !== child.name
56
+ ? `${this.app.label} (${child.name})`
57
+ : child.name;
66
58
  this.stats = stats.toJson(this.app.hooks.filter(`build.stats`));
67
- if (this.stats.errorsCount > 0 ||
68
- this.stats.children?.some(child => child.errorsCount > 0)) {
59
+ await this.app.hooks.fire(`compiler.stats`, stats);
60
+ const statsUpdate = this.app.dashboard.update(stats);
61
+ if (stats.hasErrors()) {
69
62
  process.exitCode = 1;
63
+ this.stats.children = this.stats.children?.map(child => ({
64
+ ...child,
65
+ errors: this.sourceErrors(child.errors),
66
+ }));
67
+ this.stats.children
68
+ ?.filter(child => child.errorsCount > 0)
69
+ .forEach(child => {
70
+ const error = child.errors?.shift();
71
+ if (!error)
72
+ return;
73
+ this.app.notifier.notify({
74
+ title: makeNoticeTitle(child),
75
+ subtitle: error.file ? `Error in ${error.name}` : error.name,
76
+ message: stripAnsi(error.message),
77
+ open: error.file ? pathToFileURL(error.file) : ``,
78
+ group: `${this.app.label}-${child.name}`,
79
+ });
80
+ this.app.notifier.openEditor(error.file);
81
+ });
70
82
  }
71
- try {
72
- await this.app.dashboard.update(stats);
73
- }
74
- catch (error) {
75
- throw error;
76
- }
83
+ this.stats.children
84
+ ?.filter(child => child.errorsCount === 0)
85
+ .forEach(child => {
86
+ this.app.notifier.notify({
87
+ title: makeNoticeTitle(child),
88
+ subtitle: `Build successful`,
89
+ message: child.modules
90
+ ? `${child.modules.length} modules compiled in ${duration(child.time)}`
91
+ : `Compiled in ${duration(child.time)}`,
92
+ group: `${this.app.label}-${child.name}`,
93
+ open: this.app.server?.publicUrl.href,
94
+ });
95
+ this.app.notifier.openBrowser(this.app.server?.publicUrl.href);
96
+ });
97
+ await statsUpdate;
77
98
  }
78
99
  /**
79
100
  * Compiler error event
80
- *
81
- * @public
82
- * @decorator `@bind`
83
101
  */
84
- onError(error) {
102
+ async onError(error) {
103
+ process.exitCode = 1;
104
+ await this.app.hooks.fire(`compiler.error`, error);
85
105
  this.app.isDevelopment &&
86
106
  this.app.server.appliedMiddleware?.hot?.publish({ error });
87
- throw error;
107
+ this.app.notifier.notify({
108
+ subtitle: error.name,
109
+ message: error.message,
110
+ group: this.app.label,
111
+ });
112
+ await this.app.dashboard.renderer.once(React.createElement(App.Error, { name: "Compiler error", message: error.message, stack: error.stack }));
113
+ }
114
+ /**
115
+ * Parse errors from webpack stats
116
+ */
117
+ sourceErrors(errors) {
118
+ if (!errors || !errors.length)
119
+ return [];
120
+ try {
121
+ const parseError = (error) => {
122
+ let file;
123
+ const modules = this.stats.children.flatMap(child => child.modules);
124
+ const moduleIdent = error.moduleId ?? error.moduleName;
125
+ const module = modules.find(module => module?.id === moduleIdent || module?.name === moduleIdent);
126
+ if (!module) {
127
+ return error;
128
+ }
129
+ if (module.nameForCondition) {
130
+ file = module.nameForCondition;
131
+ }
132
+ else if (module.name) {
133
+ file = this.app.path(`@src`, module.name);
134
+ }
135
+ if (!file) {
136
+ return error;
137
+ }
138
+ return { ...error, name: module.name ?? error.name, file };
139
+ };
140
+ return errors?.map(parseError).filter(Boolean);
141
+ }
142
+ catch (error) {
143
+ this.app.warn(`error parsing errors`, error);
144
+ return [];
145
+ }
88
146
  }
89
147
  }
90
148
  __decorate([
@@ -102,7 +160,13 @@ __decorate([
102
160
  __decorate([
103
161
  bind,
104
162
  __metadata("design:type", Function),
105
- __metadata("design:paramtypes", [Function]),
106
- __metadata("design:returntype", void 0)
163
+ __metadata("design:paramtypes", [Error]),
164
+ __metadata("design:returntype", Promise)
107
165
  ], Compiler.prototype, "onError", null);
166
+ __decorate([
167
+ bind,
168
+ __metadata("design:type", Function),
169
+ __metadata("design:paramtypes", [Array]),
170
+ __metadata("design:returntype", Array)
171
+ ], Compiler.prototype, "sourceErrors", null);
108
172
  //# sourceMappingURL=compiler.service.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"compiler.service.js","sourceRoot":"","sources":["../src/compiler.service.ts"],"names":[],"mappings":";AACA,OAAO,EAAC,OAAO,EAAC,MAAM,8BAA8B,CAAA;AAEpD,OAAO,EAAC,IAAI,EAAC,MAAM,+BAA+B,CAAA;AAQlD;;GAEG;AACH,MAAM,OAAO,QAAS,SAAQ,OAAO;IAArC;;QAmBE;;;WAGG;QACI,WAAM,GAA+B,EAAE,CAAA;IAyFhD,CAAC;IAvFC;;;;;;;;OAQG;IAEU,AAAN,KAAK,CAAC,OAAO;QAClB,IAAI,CAAC,cAAc,GAAG,MAAM,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,CAAA;QAC7D,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,kBAAkB,EAAE,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,CAAA;QAEhE,IAAI,CAAC,MAAM,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,WAAW;YACjC,CAAC,CAAC,CAAC,MAAM,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;YAC/B,CAAC,CAAC,MAAM,OAAO,C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B,IAAI,GAAG,MAAM,CAAC,gBAAgB,CAAA;iBAC/B;qBAAM,IAAI,MAAM,CAAC,IAAI,EAAE;oBACtB,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC,IAAI,CAAC,CAAA;iBAC1C;gBAED,IAAI,CAAC,IAAI,EAAE;oBACT,OAAO,KAAK,CAAA;iBACb;gBAED,OAAO,EAAC,GAAG,KAAK,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,IAAI,KAAK,CAAC,IAAI,EAAE,IAAI,EAAC,CAAA;YAC1D,CAAC,CAAA;YAED,OAAO,MAAM,EAAE,GAAG,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAA;SAC/C;QAAC,OAAO,KAAK,EAAE;YACd,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,sBAAsB,EAAE,KAAK,CAAC,CAAA;YAC5C,OAAO,EAAE,CAAA;SACV;IACH,CAAC;CACF;AA1Kc;IADZ,IAAI;;;;uCAoCJ;AAMY;IADZ,IAAI;;;;uCAuDJ;AAMY;IADZ,IAAI;;qCACuB,KAAK;;uCAqBhC;AAKD;IAAC,IAAI;;qCAEK,KAAK;oCACZ,KAAK;4CAuCP"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@roots/bud-compiler",
3
3
  "description": "Compilation handler",
4
- "version": "2023.2.20",
4
+ "version": "2023.2.22",
5
5
  "homepage": "https://roots.io/bud",
6
6
  "repository": {
7
7
  "type": "git",
@@ -58,13 +58,14 @@
58
58
  }
59
59
  },
60
60
  "devDependencies": {
61
- "@roots/bud-api": "2023.2.20",
61
+ "@roots/bud-api": "2023.2.22",
62
62
  "@skypack/package-check": "0.2.2",
63
63
  "@types/node": "18.11.18"
64
64
  },
65
65
  "dependencies": {
66
- "@roots/bud-framework": "2023.2.20",
67
- "@roots/bud-support": "2023.2.20"
66
+ "@roots/bud-dashboard": "2023.2.22",
67
+ "@roots/bud-framework": "2023.2.22",
68
+ "@roots/bud-support": "2023.2.22"
68
69
  },
69
70
  "volta": {
70
71
  "extends": "../../../package.json"
@@ -0,0 +1,221 @@
1
+ import {pathToFileURL} from 'node:url'
2
+
3
+ import * as App from '@roots/bud-dashboard/app'
4
+ import type {Bud} from '@roots/bud-framework/bud'
5
+ import {Service} from '@roots/bud-framework/service'
6
+ import type {Compiler as Contract} from '@roots/bud-framework/services'
7
+ import {bind} from '@roots/bud-support/decorators'
8
+ import {duration} from '@roots/bud-support/human-readable'
9
+ import type {
10
+ ErrorWithSourceFile,
11
+ SourceFile,
12
+ } from '@roots/bud-support/open'
13
+ import React from '@roots/bud-support/react'
14
+ import stripAnsi from '@roots/bud-support/strip-ansi'
15
+ import type webpack from '@roots/bud-support/webpack'
16
+ import type {
17
+ MultiCompiler,
18
+ MultiStats,
19
+ StatsCompilation,
20
+ StatsError,
21
+ } from '@roots/bud-support/webpack'
22
+
23
+ /**
24
+ * Wepback compilation controller class
25
+ */
26
+ export class Compiler extends Service implements Contract.Service {
27
+ /**
28
+ * Compiler implementation
29
+ */
30
+ public implementation: typeof webpack
31
+
32
+ /**
33
+ * Compiler instance
34
+ */
35
+ public instance: Contract.Service[`instance`]
36
+
37
+ /**
38
+ * Compilation stats
39
+ */
40
+ public stats: Contract.Service[`stats`]
41
+
42
+ /**
43
+ * Configuration
44
+ */
45
+ public config: Contract.Service[`config`] = []
46
+
47
+ /**
48
+ * Initiates compilation
49
+ */
50
+ @bind
51
+ public async compile(): Promise<MultiCompiler> {
52
+ this.implementation = await this.app.module.import(`webpack`)
53
+ this.logger.log(`imported webpack`, this.implementation.version)
54
+
55
+ this.config = !this.app.hasChildren
56
+ ? [await this.app.build.make()]
57
+ : await Promise.all(
58
+ Object.values(this.app.children).map(async (child: Bud) => {
59
+ try {
60
+ return await child.build.make()
61
+ } catch (error) {
62
+ throw error
63
+ }
64
+ }),
65
+ )
66
+
67
+ await this.app.hooks.fire(`compiler.before`, this.app)
68
+
69
+ if (this.app.isCLI() && this.app.context.args.dry) {
70
+ this.app.context.logger.timeEnd(`initialize`)
71
+ this.logger.log(`running in dry mode. exiting early.`)
72
+ return
73
+ }
74
+
75
+ this.app.context.logger.timeEnd(`initialize`)
76
+
77
+ this.instance = this.implementation(this.config)
78
+
79
+ this.instance.hooks.done.tap(this.app.label, async (stats: any) => {
80
+ await this.onStats(stats)
81
+ await this.app.hooks.fire(`compiler.close`, this.app)
82
+ })
83
+
84
+ await this.app.hooks.fire(`compiler.after`, this.app)
85
+ return this.instance
86
+ }
87
+
88
+ /**
89
+ * Stats handler
90
+ */
91
+ @bind
92
+ public async onStats(stats: MultiStats) {
93
+ const makeNoticeTitle = (child: StatsCompilation) =>
94
+ this.app.label !== child.name
95
+ ? `${this.app.label} (${child.name})`
96
+ : child.name
97
+
98
+ this.stats = stats.toJson(this.app.hooks.filter(`build.stats`))
99
+
100
+ await this.app.hooks.fire(`compiler.stats`, stats)
101
+
102
+ const statsUpdate = this.app.dashboard.update(stats)
103
+
104
+ if (stats.hasErrors()) {
105
+ process.exitCode = 1
106
+
107
+ this.stats.children = this.stats.children?.map(child => ({
108
+ ...child,
109
+ errors: this.sourceErrors(child.errors),
110
+ }))
111
+
112
+ this.stats.children
113
+ ?.filter(child => child.errorsCount > 0)
114
+ .forEach(child => {
115
+ const error = child.errors?.shift()
116
+ if (!error) return
117
+ this.app.notifier.notify({
118
+ title: makeNoticeTitle(child),
119
+ subtitle: error.file ? `Error in ${error.name}` : error.name,
120
+ message: stripAnsi(error.message),
121
+ open: error.file ? pathToFileURL(error.file) : ``,
122
+ group: `${this.app.label}-${child.name}`,
123
+ })
124
+ this.app.notifier.openEditor(error.file)
125
+ })
126
+ }
127
+
128
+ this.stats.children
129
+ ?.filter(child => child.errorsCount === 0)
130
+ .forEach(child => {
131
+ this.app.notifier.notify({
132
+ title: makeNoticeTitle(child),
133
+ subtitle: `Build successful`,
134
+ message: child.modules
135
+ ? `${child.modules.length} modules compiled in ${duration(
136
+ child.time,
137
+ )}`
138
+ : `Compiled in ${duration(child.time)}`,
139
+ group: `${this.app.label}-${child.name}`,
140
+ open: this.app.server?.publicUrl.href,
141
+ })
142
+ this.app.notifier.openBrowser(this.app.server?.publicUrl.href)
143
+ })
144
+
145
+ await statsUpdate
146
+ }
147
+
148
+ /**
149
+ * Compiler error event
150
+ */
151
+ @bind
152
+ public async onError(error: Error) {
153
+ process.exitCode = 1
154
+
155
+ await this.app.hooks.fire(`compiler.error`, error)
156
+
157
+ this.app.isDevelopment &&
158
+ this.app.server.appliedMiddleware?.hot?.publish({error})
159
+
160
+ this.app.notifier.notify({
161
+ subtitle: error.name,
162
+ message: error.message,
163
+ group: this.app.label,
164
+ })
165
+
166
+ await this.app.dashboard.renderer.once(
167
+ <App.Error
168
+ name="Compiler error"
169
+ message={error.message}
170
+ stack={error.stack}
171
+ />,
172
+ )
173
+ }
174
+
175
+ /**
176
+ * Parse errors from webpack stats
177
+ */
178
+ @bind
179
+ public sourceErrors(
180
+ errors: Array<StatsError>,
181
+ ): Array<ErrorWithSourceFile | StatsError> {
182
+ if (!errors || !errors.length) return []
183
+
184
+ try {
185
+ const parseError = (
186
+ error: StatsError,
187
+ ): ErrorWithSourceFile | StatsError => {
188
+ let file: SourceFile[`file`] | undefined
189
+
190
+ const modules = this.stats.children.flatMap(child => child.modules)
191
+ const moduleIdent = error.moduleId ?? error.moduleName
192
+
193
+ const module = modules.find(
194
+ module =>
195
+ module?.id === moduleIdent || module?.name === moduleIdent,
196
+ )
197
+
198
+ if (!module) {
199
+ return error
200
+ }
201
+
202
+ if (module.nameForCondition) {
203
+ file = module.nameForCondition
204
+ } else if (module.name) {
205
+ file = this.app.path(`@src`, module.name)
206
+ }
207
+
208
+ if (!file) {
209
+ return error
210
+ }
211
+
212
+ return {...error, name: module.name ?? error.name, file}
213
+ }
214
+
215
+ return errors?.map(parseError).filter(Boolean)
216
+ } catch (error) {
217
+ this.app.warn(`error parsing errors`, error)
218
+ return []
219
+ }
220
+ }
221
+ }
@@ -1,127 +0,0 @@
1
- import type {Bud} from '@roots/bud-framework/bud'
2
- import {Service} from '@roots/bud-framework/service'
3
- import type {Compiler as Contract} from '@roots/bud-framework/services'
4
- import {bind} from '@roots/bud-support/decorators'
5
- import type webpack from '@roots/bud-support/webpack'
6
- import type {
7
- MultiCompiler,
8
- MultiStats,
9
- WebpackError,
10
- } from '@roots/bud-support/webpack'
11
-
12
- /**
13
- * Wepback compilation controller class
14
- */
15
- export class Compiler extends Service implements Contract.Service {
16
- /**
17
- * Compiler implementation
18
- * @public
19
- */
20
- public implementation: typeof webpack
21
-
22
- /**
23
- * Compiler instance
24
- * @public
25
- */
26
- public instance: Contract.Service[`instance`]
27
-
28
- /**
29
- * Compilation stats
30
- * @public
31
- */
32
- public stats: Contract.Service[`stats`]
33
-
34
- /**
35
- * Configuration
36
- * @public
37
- */
38
- public config: Contract.Service[`config`] = []
39
-
40
- /**
41
- * Initiates compilation
42
- *
43
- * @returns the compiler instance
44
- *
45
- * @public
46
- * @decorator `@bind`
47
- * @decorator `@once`
48
- */
49
- @bind
50
- public async compile(): Promise<MultiCompiler> {
51
- this.implementation = await this.app.module.import(`webpack`)
52
- this.logger.log(`imported webpack`, this.implementation.version)
53
-
54
- this.config = !this.app.hasChildren
55
- ? [await this.app.build.make()]
56
- : await Promise.all(
57
- Object.values(this.app.children).map(async (child: Bud) => {
58
- try {
59
- return await child.build.make()
60
- } catch (error) {
61
- throw error
62
- }
63
- }),
64
- )
65
-
66
- try {
67
- await this.app.hooks.fire(`compiler.before`)
68
- } catch (error) {
69
- throw error
70
- }
71
-
72
- if (this.app.isCLI() && this.app.context.args.dry) {
73
- this.logger.log(`running in dry mode. exiting early.`)
74
- return
75
- }
76
-
77
- this.app.context.logger.timeEnd(`initialize`)
78
-
79
- this.instance = this.implementation(this.config)
80
- this.instance.hooks.done.tap(this.app.label, async (stats: any) => {
81
- await this.onStats(stats)
82
- })
83
- this.instance.hooks.done.tap(`${this.app.label}-close`, async () => {
84
- await this.app.hooks.fire(`compiler.close`)
85
- })
86
-
87
- await this.app.hooks.fire(`compiler.after`)
88
- return this.instance
89
- }
90
-
91
- /**
92
- * Stats handler
93
- *
94
- * @public
95
- * @decorator `@bind`
96
- */
97
- @bind
98
- public async onStats(stats: MultiStats) {
99
- this.stats = stats.toJson(this.app.hooks.filter(`build.stats`))
100
- if (
101
- this.stats.errorsCount > 0 ||
102
- this.stats.children?.some(child => child.errorsCount > 0)
103
- ) {
104
- process.exitCode = 1
105
- }
106
-
107
- try {
108
- await this.app.dashboard.update(stats)
109
- } catch (error) {
110
- throw error
111
- }
112
- }
113
-
114
- /**
115
- * Compiler error event
116
- *
117
- * @public
118
- * @decorator `@bind`
119
- */
120
- @bind
121
- public onError(error: WebpackError) {
122
- this.app.isDevelopment &&
123
- this.app.server.appliedMiddleware?.hot?.publish({error})
124
-
125
- throw error
126
- }
127
- }