Skip to content

Commit 987f11f

Browse files
committed
feat: add common addErrorToModule
1 parent e6a336f commit 987f11f

4 files changed

Lines changed: 26 additions & 59 deletions

File tree

src/after-compile.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
populateReverseDependencyGraph,
2020
tsLoaderSource,
2121
} from './utils';
22-
import { isWebpack5 } from './loaderUtils';
22+
import { addErrorToModule, isWebpack5 } from './loaderUtils';
2323

2424
/**
2525
* This returns a function that has options to add assets and also to provide errors to webpack
@@ -256,13 +256,7 @@ function provideErrorsToWebpack(
256256
);
257257

258258
if (!moduleHasWebpackErrors(module)) {
259-
formattedErrors.forEach(error => {
260-
if (module.addError) {
261-
module.addError(error);
262-
} else {
263-
module.errors.push(error);
264-
}
265-
});
259+
formattedErrors.forEach(error => addErrorToModule(module, error));
266260
}
267261

268262
compilation.errors.push(...formattedErrors);
@@ -322,13 +316,7 @@ function provideSolutionErrorsToWebpack(
322316
);
323317

324318
if (!moduleHasWebpackErrors(module)) {
325-
formattedErrors.forEach(error => {
326-
if (module.addError) {
327-
module.addError(error);
328-
} else {
329-
module.errors.push(error);
330-
}
331-
});
319+
formattedErrors.forEach(error => addErrorToModule(module, error));
332320
}
333321

334322
compilation.errors.push(...formattedErrors);

src/index.ts

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
} from './utils';
2929
import type { RawSourceMap } from 'source-map';
3030
import { SourceMapConsumer, SourceMapGenerator } from 'source-map';
31+
import { addErrorToModule } from './loaderUtils';
3132

3233
/**
3334
* we can only use SourceMapConsumer if the version available has a destroy method
@@ -676,32 +677,16 @@ function getTranspilationEmit(
676677
loaderContext.context
677678
);
678679

679-
errors.forEach(error => addErrorToModule(module, error));
680+
errors.forEach(error => {
681+
if (module) {
682+
addErrorToModule(module, error);
683+
}
684+
});
680685
}
681686

682687
return { outputText, sourceMapText };
683688
}
684689

685-
function addErrorToModule(
686-
module: webpack.LoaderContext<LoaderOptions>['_module'],
687-
error: webpack.WebpackError
688-
) {
689-
if (!module) {
690-
return;
691-
}
692-
693-
if (module.addError) {
694-
module.addError(error);
695-
return;
696-
}
697-
698-
const webpackModule = module as any;
699-
if (!webpackModule.errors) {
700-
webpackModule.errors = [];
701-
}
702-
webpackModule.errors.push(error);
703-
}
704-
705690
function makeSourceMap(
706691
sourceMapText: string | undefined,
707692
outputText: string,

src/instances.ts

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type typescript from 'typescript';
55
import * as webpack from 'webpack';
66

77
import { makeAfterCompile } from './after-compile';
8+
import { addErrorToModule } from './loaderUtils';
89
import { getCompiler, getCompilerOptions } from './compilerSetup';
910
import { getConfigFile, getConfigParseResult } from './config';
1011
import {
@@ -515,30 +516,12 @@ export function reportTranspileErrors(
515516
loader.context
516517
);
517518

518-
[...solutionErrors, ...errors].forEach(error =>
519-
addErrorToModule(module, error)
520-
);
521-
}
522-
}
523-
524-
function addErrorToModule(
525-
module: webpack.LoaderContext<LoaderOptions>['_module'],
526-
error: webpack.WebpackError
527-
) {
528-
if (!module) {
529-
return;
530-
}
531-
532-
if (module.addError) {
533-
module.addError(error);
534-
return;
535-
}
536-
537-
const webpackModule = module as any;
538-
if (!webpackModule.errors) {
539-
webpackModule.errors = [];
519+
[...solutionErrors, ...errors].forEach(error => {
520+
if (module) {
521+
addErrorToModule(module, error);
522+
}
523+
});
540524
}
541-
webpackModule.errors.push(error);
542525
}
543526

544527
export function buildSolutionReferences(

src/loaderUtils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,14 @@ export function getOptions(
2828

2929
return loaderUtils.getOptions<LoaderOptions>(loaderContext as any) || ({} as LoaderOptions);
3030
}
31+
32+
/**
33+
* webpack 4 and webpack 5 have different APIs for adding errors to modules. This function abstracts that away.
34+
*/
35+
export function addErrorToModule(module: webpack.Module, error: webpack.WebpackError) {
36+
if (isWebpack5) {
37+
module.addError(error);
38+
} else {
39+
module.errors.push(error);
40+
}
41+
}

0 commit comments

Comments
 (0)