Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/after-compile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as path from 'path';
import type * as ts from 'typescript';
import * as webpack from 'webpack';
import type * as webpack from 'webpack';

import * as constants from './constants';
import { getEmitFromWatchHost, getEmitOutput } from './instances';
Expand Down Expand Up @@ -105,6 +105,7 @@ function provideCompilerOptionDiagnosticErrorsToWebpack(
loaderOptions,
instance.colors,
compiler,
compilation.compiler.webpack.WebpackError,
{ file: configFilePath || 'tsconfig.json' },
compilation.compiler.context
);
Expand All @@ -125,7 +126,7 @@ function determineModules(
const modules: Map<FilePathKey, webpack.Module[]> = new Map();

compilation.modules.forEach(module => {
if (module instanceof webpack.NormalModule && module.resource) {
if (module instanceof compilation.compiler.webpack.NormalModule && module.resource) {
const modulePath = filePathKeyMapper(module.resource);
const existingModules = modules.get(modulePath);
if (existingModules !== undefined) {
Expand Down Expand Up @@ -248,6 +249,7 @@ function provideErrorsToWebpack(
loaderOptions,
instance.colors,
compiler,
compilation.compiler.webpack.WebpackError,
{ module },
compilation.compiler.context
);
Expand All @@ -269,6 +271,7 @@ function provideErrorsToWebpack(
loaderOptions,
instance.colors,
compiler,
compilation.compiler.webpack.WebpackError,
{ file: fileName },
compilation.compiler.context
);
Expand Down Expand Up @@ -312,6 +315,7 @@ function provideSolutionErrorsToWebpack(
loaderOptions,
instance.colors,
compiler,
compilation.compiler.webpack.WebpackError,
{ module },
compilation.compiler.context
);
Expand All @@ -333,6 +337,7 @@ function provideSolutionErrorsToWebpack(
loaderOptions,
instance.colors,
compiler,
compilation.compiler.webpack.WebpackError,
{ file: path.resolve(perFileDiagnostics[0].file!.fileName) },
compilation.compiler.context
);
Expand All @@ -348,6 +353,7 @@ function provideSolutionErrorsToWebpack(
instance.loaderOptions,
instance.colors,
instance.compiler,
compilation.compiler.webpack.WebpackError,
{ file: 'tsconfig.json' },
compilation.compiler.context
)
Expand Down Expand Up @@ -401,7 +407,7 @@ function outputFileToAsset(
// As suggested by @JonWallsten here: https://github.com/TypeStrong/ts-loader/pull/1251#issuecomment-800032753
compilation.emitAsset(
assetPath,
new webpack.sources.RawSource(outputFile.text)
new compilation.compiler.webpack.sources.RawSource(outputFile.text)
);
}

Expand Down Expand Up @@ -462,7 +468,7 @@ function removeCompilationTSLoaderErrors(
loaderOptions: LoaderOptions
) {
compilation.errors = compilation.errors.filter(
error => error instanceof webpack.WebpackError && error.details !== tsLoaderSource(loaderOptions)
error => error instanceof compilation.compiler.webpack.WebpackError && error.details !== tsLoaderSource(loaderOptions)
);
}

Expand Down
4 changes: 3 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export function getConfigFile(
loaderOptions: LoaderOptions,
compilerCompatible: boolean,
log: logger.Logger,
compilerDetailsLogMessage: string
compilerDetailsLogMessage: string,
WebpackError: typeof webpack.WebpackError
) {
const configFilePath = findConfigFile(
compiler,
Expand All @@ -45,6 +46,7 @@ export function getConfigFile(
loaderOptions,
colors,
compiler,
WebpackError,
{ file: configFilePath },
loader.context
)[0];
Expand Down
17 changes: 12 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,31 @@ function loader(
this.cacheable && this.cacheable();
const callback = this.async();
const options = getLoaderOptions(this);
const instanceOrError = getTypeScriptInstance(options, this);

// TODO :: improve this construct
const WebpackError = this._compiler?.webpack.WebpackError!;

const instanceOrError = getTypeScriptInstance(options, this, WebpackError);

if (instanceOrError.error !== undefined) {
callback(new Error(instanceOrError.error.message));
return;
}
const instance = instanceOrError.instance!;
buildSolutionReferences(instance, this);
successLoader(this, contents, callback, instance, inputSourceMap);
successLoader(this, contents, callback, instance, WebpackError, inputSourceMap);
}

function successLoader(
loaderContext: webpack.LoaderContext<LoaderOptions>,
contents: string,
callback: ReturnType<webpack.LoaderContext<LoaderOptions>['async']>,
instance: TSInstance,
WebpackError: typeof webpack.WebpackError,
inputSourceMap?: Record<string, any>
) {
initializeInstance(loaderContext, instance);
reportTranspileErrors(instance, loaderContext);
reportTranspileErrors(instance, loaderContext, WebpackError);
const rawFilePath = path.normalize(loaderContext.resourcePath);

const filePath =
Expand All @@ -82,7 +87,7 @@ function successLoader(
instance
);
const { outputText, sourceMapText } = instance.loaderOptions.transpileOnly
? getTranspilationEmit(filePath, contents, instance, loaderContext)
? getTranspilationEmit(filePath, contents, instance, loaderContext, WebpackError)
: getEmit(rawFilePath, filePath, instance, loaderContext);

// the following function is async, which means it will immediately return and run in the "background"
Expand Down Expand Up @@ -629,7 +634,8 @@ function getTranspilationEmit(
fileName: string,
contents: string,
instance: TSInstance,
loaderContext: webpack.LoaderContext<LoaderOptions>
loaderContext: webpack.LoaderContext<LoaderOptions>,
WebpackError: typeof webpack.WebpackError,
) {
if (isReferencedFile(instance, fileName)) {
const outputFiles =
Expand Down Expand Up @@ -662,6 +668,7 @@ function getTranspilationEmit(
instance.loaderOptions,
instance.colors,
instance.compiler,
WebpackError,
{ module },
loaderContext.context
);
Expand Down
29 changes: 20 additions & 9 deletions src/instances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as chalk from 'chalk';
import * as fs from 'fs';
import * as path from 'path';
import type * as typescript from 'typescript';
import * as webpack from 'webpack';
import type * as webpack from 'webpack';

import { makeAfterCompile } from './after-compile';
import { getCompiler, getCompilerOptions } from './compilerSetup';
Expand Down Expand Up @@ -44,7 +44,8 @@ const instancesBySolutionBuilderConfigs = new Map<FilePathKey, TSInstance>();
*/
export function getTypeScriptInstance(
loaderOptions: LoaderOptions,
loader: webpack.LoaderContext<LoaderOptions>
loader: webpack.LoaderContext<LoaderOptions>,
WebpackError: typeof webpack.WebpackError
): { instance?: TSInstance; error?: webpack.WebpackError } {
const existing = getTSInstanceFromCache(
loader._compiler!,
Expand All @@ -65,7 +66,7 @@ export function getTypeScriptInstance(

if (compiler.errorMessage !== undefined) {
return {
error: makeError(loaderOptions, colors.red(compiler.errorMessage), ''),
error: makeError(WebpackError, loaderOptions, colors.red(compiler.errorMessage), ''),
};
}

Expand All @@ -76,7 +77,8 @@ export function getTypeScriptInstance(
colors,
compiler.compiler!,
compiler.compilerCompatible!,
compiler.compilerDetailsLogMessage!
compiler.compilerDetailsLogMessage!,
WebpackError
);
}

Expand Down Expand Up @@ -123,7 +125,8 @@ function successfulTypeScriptInstance(
colors: chalk.Chalk,
compiler: typeof typescript,
compilerCompatible: boolean,
compilerDetailsLogMessage: string
compilerDetailsLogMessage: string,
WebpackError: typeof webpack.WebpackError,
) {
const configFileAndPath = getConfigFile(
compiler,
Expand All @@ -132,13 +135,15 @@ function successfulTypeScriptInstance(
loaderOptions,
compilerCompatible,
log,
compilerDetailsLogMessage!
compilerDetailsLogMessage!,
WebpackError
);

if (configFileAndPath.configFileError !== undefined) {
const { message, file } = configFileAndPath.configFileError;
return {
error: makeError(
WebpackError,
loaderOptions,
colors.red('error while reading tsconfig.json:' + EOL + message),
file ?? ''
Expand Down Expand Up @@ -179,6 +184,7 @@ function successfulTypeScriptInstance(
loaderOptions,
colors,
compiler,
WebpackError,
{ file: configFilePath },
loader.context
);
Expand All @@ -187,6 +193,7 @@ function successfulTypeScriptInstance(

return {
error: makeError(
WebpackError,
loaderOptions,
colors.red('error while parsing tsconfig.json'),
configFilePath || ''
Expand Down Expand Up @@ -264,6 +271,7 @@ function successfulTypeScriptInstance(
} catch (exc) {
return {
error: makeError(
WebpackError,
loaderOptions,
colors.red(
`A file specified in tsconfig.json could not be found: ${normalizedFilePath!}`
Expand Down Expand Up @@ -324,7 +332,7 @@ function addAssetHooks(
compilation.hooks.processAssets.tap(
{
name: 'ts-loader',
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL,
stage: compilation.compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL,
},
() => {
cachedMakeAfterCompile(compilation, () => {
Expand Down Expand Up @@ -478,7 +486,8 @@ function getScriptRegexp(instance: TSInstance) {

export function reportTranspileErrors(
instance: TSInstance,
loader: webpack.LoaderContext<LoaderOptions>
loader: webpack.LoaderContext<LoaderOptions>,
WebpackError: typeof webpack.WebpackError,
) {
if (!instance.reportTranspileErrors) {
return;
Expand All @@ -489,14 +498,16 @@ export function reportTranspileErrors(
if (!instance.loaderOptions.happyPackMode) {
const solutionErrors: webpack.WebpackError[] = getSolutionErrors(
instance,
loader.context
loader.context,
WebpackError
);
const diagnostics = instance.program!.getOptionsDiagnostics();
const errors = formatErrors(
diagnostics,
instance.loaderOptions,
instance.colors,
instance.compiler,
WebpackError,
{ file: instance.configFilePath || 'tsconfig.json' },
loader.context
);
Expand Down
3 changes: 2 additions & 1 deletion src/servicesHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1163,7 +1163,7 @@ export function makeSolutionBuilderHost(
}
}

export function getSolutionErrors(instance: TSInstance, context: string) {
export function getSolutionErrors(instance: TSInstance, context: string, WebpackError: typeof webpack.WebpackError) {
const solutionErrors: webpack.WebpackError[] = [];
if (
instance.solutionBuilderHost &&
Expand All @@ -1177,6 +1177,7 @@ export function getSolutionErrors(instance: TSInstance, context: string) {
instance.loaderOptions,
instance.colors,
instance.compiler,
WebpackError,
{ file: filePath ? undefined : 'tsconfig.json' },
context
)
Expand Down
11 changes: 7 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Chalk } from 'chalk';
import * as fs from 'fs';
import micromatch from 'micromatch';
import * as path from 'path';
import * as webpack from 'webpack';
import type * as webpack from 'webpack';
import type * as typescript from 'typescript';

import constants = require('./constants');
Expand Down Expand Up @@ -45,6 +45,7 @@ export function formatErrors(
loaderOptions: LoaderOptions,
colors: Chalk,
compiler: typeof typescript,
WebpackError: typeof webpack.WebpackError,
merge: { file?: string; module?: webpack.Module },
context: string
): webpack.WebpackError[] {
Expand Down Expand Up @@ -100,7 +101,8 @@ export function formatErrors(
: loaderOptions.errorFormatter(errorInfo, colors);

const error = makeError(
loaderOptions,
WebpackError,
loaderOptions,
message,
merge.file === undefined ? errorInfo.file : merge.file,
start,
Expand Down Expand Up @@ -145,13 +147,14 @@ export function fsReadFile(
}

export function makeError(
WebpackError: typeof webpack.WebpackError,
loaderOptions: LoaderOptions,
message: string,
file: string,
location?: FileLocation,
endLocation?: FileLocation
endLocation?: FileLocation,
): webpack.WebpackError {
const error = new webpack.WebpackError(message);
const error = new WebpackError(message);
error.file = file;
error.loc =
location === undefined
Expand Down
Loading