Skip to content

Commit a6572ce

Browse files
LukeSheardjohnnyreilly
authored andcommitted
internal: remove usage of hand crafted webpack typings (#927)
* internal: remove usage of hand crafted webpack typings * patch: fix duplicate copies of node typings * update webpack typings and removed patched versions * prepare 5.4.5 release
1 parent 48626a9 commit a6572ce

File tree

18 files changed

+114
-265
lines changed

18 files changed

+114
-265
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## v5.4.5
4+
5+
* [use @types/webpack for loader typings](https://github.com/TypeStrong/ts-loader/pull/927) - thanks @LukeSheard!
6+
37
## v5.4.4
48

59
* [refactor: add common appendTsTsxSuffixesIfRequired function to instance](https://github.com/TypeStrong/ts-loader/pull/924) - thanks @johnnyreilly!

examples/react-babel-karma-gulp/yarn.lock

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
resolved "https://registry.yarnpkg.com/@types/jasmine/-/jasmine-2.6.0.tgz#997b41a27752b4850af2683bc4a8d8222c25bd02"
2323

2424
"@types/node@*":
25-
version "8.0.45"
26-
resolved "https://registry.yarnpkg.com/@types/node/-/node-8.0.45.tgz#89fad82439d5624e1b5c6b42f0f5d85136dcdecc"
25+
version "11.13.8"
26+
resolved "https://registry.yarnpkg.com/@types/node/-/node-11.13.8.tgz#e5d71173c95533be9842b2c798978f095f912aab"
27+
integrity sha512-szA3x/3miL90ZJxUCzx9haNbK5/zmPieGraZEe4WI+3srN0eGLiT22NXeMHmyhNEopn+IrxqMc7wdVwvPl8meg==
2728

2829
"@types/react-bootstrap@0.31.6":
2930
version "0.31.6"

examples/thread-loader/yarn.lock

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33

44

55
"@types/node@*":
6-
version "9.4.6"
7-
resolved "https://registry.yarnpkg.com/@types/node/-/node-9.4.6.tgz#d8176d864ee48753d053783e4e463aec86b8d82e"
6+
version "11.13.8"
7+
resolved "https://registry.yarnpkg.com/@types/node/-/node-11.13.8.tgz#e5d71173c95533be9842b2c798978f095f912aab"
8+
integrity sha512-szA3x/3miL90ZJxUCzx9haNbK5/zmPieGraZEe4WI+3srN0eGLiT22NXeMHmyhNEopn+IrxqMc7wdVwvPl8meg==
89

910
abbrev@1:
1011
version "1.1.1"

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ts-loader",
3-
"version": "5.4.4",
3+
"version": "5.4.5",
44
"description": "TypeScript loader for webpack",
55
"main": "index.js",
66
"types": "dist/types/index.d.ts",
@@ -59,8 +59,9 @@
5959
},
6060
"devDependencies": {
6161
"@types/micromatch": "^3.1.0",
62-
"@types/node": "^10.0.0",
62+
"@types/node": "*",
6363
"@types/semver": "^5.4.0",
64+
"@types/webpack": "^4.4.29",
6465
"babel": "^6.0.0",
6566
"babel-core": "^6.0.0",
6667
"babel-loader": "^7.0.0",

src/after-compile.ts

+25-20
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import * as path from 'path';
2+
import * as webpack from 'webpack';
23

34
import * as constants from './constants';
45
import { getEmitOutput } from './instances';
56
import {
67
TSFile,
78
TSFiles,
89
TSInstance,
9-
WebpackCompilation,
1010
WebpackError,
1111
WebpackModule
1212
} from './interfaces';
@@ -23,7 +23,10 @@ export function makeAfterCompile(
2323
let getCompilerOptionDiagnostics = true;
2424
let checkAllFilesForErrors = true;
2525

26-
return (compilation: WebpackCompilation, callback: () => void) => {
26+
return (
27+
compilation: webpack.compilation.Compilation,
28+
callback: () => void
29+
) => {
2730
// Don't add errors for child compilations
2831
if (compilation.compiler.isChild()) {
2932
callback();
@@ -76,7 +79,7 @@ export function makeAfterCompile(
7679
*/
7780
function provideCompilerOptionDiagnosticErrorsToWebpack(
7881
getCompilerOptionDiagnostics: boolean,
79-
compilation: WebpackCompilation,
82+
compilation: webpack.compilation.Compilation,
8083
instance: TSInstance,
8184
configFilePath: string | undefined
8285
) {
@@ -103,23 +106,25 @@ function provideCompilerOptionDiagnosticErrorsToWebpack(
103106
* this is used for quick-lookup when trying to find modules
104107
* based on filepath
105108
*/
106-
function determineModules(compilation: WebpackCompilation) {
107-
// TODO: Convert to reduce
108-
const modules = new Map<string, WebpackModule[]>();
109-
compilation.modules.forEach(module => {
110-
if (module.resource) {
111-
const modulePath = path.normalize(module.resource);
112-
const existingModules = modules.get(modulePath);
113-
if (existingModules !== undefined) {
114-
if (existingModules.indexOf(module) === -1) {
115-
existingModules.push(module);
109+
function determineModules(compilation: webpack.compilation.Compilation) {
110+
return compilation.modules.reduce<Map<string, WebpackModule[]>>(
111+
(modules, module) => {
112+
if (module.resource) {
113+
const modulePath = path.normalize(module.resource);
114+
const existingModules = modules.get(modulePath);
115+
if (existingModules !== undefined) {
116+
if (existingModules.indexOf(module) === -1) {
117+
existingModules.push(module);
118+
}
119+
} else {
120+
modules.set(modulePath, [module]);
116121
}
117-
} else {
118-
modules.set(modulePath, [module]);
119122
}
120-
}
121-
});
122-
return modules;
123+
124+
return modules;
125+
},
126+
new Map<string, WebpackModule[]>()
127+
);
123128
}
124129

125130
function determineFilesToCheckForErrors(
@@ -163,7 +168,7 @@ function determineFilesToCheckForErrors(
163168
function provideErrorsToWebpack(
164169
filesToCheckForErrors: TSFiles,
165170
filesWithErrors: TSFiles,
166-
compilation: WebpackCompilation,
171+
compilation: webpack.compilation.Compilation,
167172
modules: Map<string, WebpackModule[]>,
168173
instance: TSInstance
169174
) {
@@ -255,7 +260,7 @@ function provideErrorsToWebpack(
255260
function provideDeclarationFilesToWebpack(
256261
filesToCheckForErrors: TSFiles,
257262
instance: TSInstance,
258-
compilation: WebpackCompilation
263+
compilation: webpack.compilation.Compilation
259264
) {
260265
for (const filePath of filesToCheckForErrors.keys()) {
261266
if (filePath.match(constants.tsTsxRegex) === null) {

src/compilerSetup.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as semver from 'semver';
22
import * as typescript from 'typescript';
33

4-
import * as constants from './constants';
54
import { LoaderOptions } from './interfaces';
65
import * as logger from './logger';
76

@@ -66,9 +65,9 @@ export function getCompilerOptions(
6665
if (
6766
compilerOptions.module === undefined &&
6867
(compilerOptions.target !== undefined &&
69-
compilerOptions.target < constants.ScriptTargetES2015)
68+
compilerOptions.target < typescript.ScriptTarget.ES2015)
7069
) {
71-
compilerOptions.module = constants.ModuleKindCommonJs;
70+
compilerOptions.module = typescript.ModuleKind.CommonJS;
7271
}
7372

7473
return compilerOptions;

src/config.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { Chalk } from 'chalk';
22
import * as path from 'path';
33
import * as typescript from 'typescript';
4-
import { LoaderOptions, Webpack, WebpackError } from './interfaces';
4+
import * as webpack from 'webpack';
5+
6+
import { LoaderOptions, WebpackError } from './interfaces';
57
import * as logger from './logger';
68
import { formatErrors } from './utils';
79

@@ -13,7 +15,7 @@ interface ConfigFile {
1315
export function getConfigFile(
1416
compiler: typeof typescript,
1517
colors: Chalk,
16-
loader: Webpack,
18+
loader: webpack.loader.LoaderContext,
1719
loaderOptions: LoaderOptions,
1820
compilerCompatible: boolean,
1921
log: logger.Logger,

src/constants.ts

-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ export const LineFeed = '\n';
77
export const CarriageReturnLineFeedCode = 0;
88
export const LineFeedCode = 1;
99

10-
export const ScriptTargetES2015 = 2;
11-
12-
export const ModuleKindCommonJs = 1;
13-
1410
export const extensionRegex = /\.[^.]+$/;
1511
export const tsxRegex = /\.tsx$/i;
1612
export const tsTsxRegex = /\.ts(x?)$/i;

src/index.ts

+13-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import * as loaderUtils from 'loader-utils';
22
import * as path from 'path';
33
import * as typescript from 'typescript';
4+
import * as webpack from 'webpack';
45

56
import * as constants from './constants';
67
import { getEmitOutput, getTypeScriptInstance } from './instances';
78
import {
8-
AsyncCallback,
9-
Compiler,
109
LoaderOptions,
1110
LoaderOptionsCache,
1211
LogLevel,
1312
TSFile,
14-
TSInstance,
15-
Webpack
13+
TSInstance
1614
} from './interfaces';
1715
import {
1816
appendSuffixesIfMatch,
@@ -23,16 +21,16 @@ import {
2321
validateSourceMapOncePerProject
2422
} from './utils';
2523

26-
const webpackInstances: Compiler[] = [];
24+
const webpackInstances: webpack.Compiler[] = [];
2725
const loaderOptionsCache: LoaderOptionsCache = {};
2826

2927
/**
3028
* The entry point for ts-loader
3129
*/
32-
function loader(this: Webpack, contents: string) {
30+
function loader(this: webpack.loader.LoaderContext, contents: string) {
3331
// tslint:disable-next-line:no-unused-expression strict-boolean-expressions
3432
this.cacheable && this.cacheable();
35-
const callback = this.async();
33+
const callback = this.async() as webpack.loader.loaderCallback;
3634
const options = getLoaderOptions(this);
3735
const instanceOrError = getTypeScriptInstance(options, this);
3836

@@ -51,9 +49,9 @@ function loader(this: Webpack, contents: string) {
5149
}
5250

5351
function successLoader(
54-
loaderContext: Webpack,
52+
loaderContext: webpack.loader.LoaderContext,
5553
contents: string,
56-
callback: AsyncCallback,
54+
callback: webpack.loader.loaderCallback,
5755
options: LoaderOptions,
5856
instance: TSInstance
5957
) {
@@ -157,10 +155,10 @@ function makeSourceMapAndFinish(
157155
outputText: string | undefined,
158156
filePath: string,
159157
contents: string,
160-
loaderContext: Webpack,
158+
loaderContext: webpack.loader.LoaderContext,
161159
options: LoaderOptions,
162160
fileVersion: number,
163-
callback: AsyncCallback
161+
callback: webpack.loader.loaderCallback
164162
) {
165163
if (outputText === null || outputText === undefined) {
166164
const additionalGuidance =
@@ -198,7 +196,7 @@ function makeSourceMapAndFinish(
198196
* either retrieves loader options from the cache
199197
* or creates them, adds them to the cache and returns
200198
*/
201-
function getLoaderOptions(loaderContext: Webpack) {
199+
function getLoaderOptions(loaderContext: webpack.loader.LoaderContext) {
202200
// differentiate the TypeScript instance based on the webpack instance
203201
let webpackIndex = webpackInstances.indexOf(loaderContext._compiler);
204202
if (webpackIndex === -1) {
@@ -390,7 +388,7 @@ function getEmit(
390388
rawFilePath: string,
391389
filePath: string,
392390
instance: TSInstance,
393-
loaderContext: Webpack
391+
loaderContext: webpack.loader.LoaderContext
394392
) {
395393
const outputFiles = getEmitOutput(instance, filePath);
396394

@@ -460,7 +458,7 @@ function getTranspilationEmit(
460458
fileName: string,
461459
contents: string,
462460
instance: TSInstance,
463-
loaderContext: Webpack
461+
loaderContext: webpack.loader.LoaderContext
464462
) {
465463
const {
466464
outputText,
@@ -495,7 +493,7 @@ function makeSourceMap(
495493
outputText: string,
496494
filePath: string,
497495
contents: string,
498-
loaderContext: Webpack
496+
loaderContext: webpack.loader.LoaderContext
499497
) {
500498
if (sourceMapText === undefined) {
501499
return { output: outputText, sourceMap: undefined };

src/instances.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import chalk, { Chalk } from 'chalk';
22
import * as fs from 'fs';
33
import * as path from 'path';
44
import * as typescript from 'typescript';
5+
import * as webpack from 'webpack';
56

67
import { makeAfterCompile } from './after-compile';
78
import { getCompiler, getCompilerOptions } from './compilerSetup';
@@ -13,7 +14,6 @@ import {
1314
TSFiles,
1415
TSInstance,
1516
TSInstances,
16-
Webpack,
1717
WebpackError
1818
} from './interfaces';
1919
import * as logger from './logger';
@@ -38,7 +38,7 @@ const instances = {} as TSInstances;
3838
*/
3939
export function getTypeScriptInstance(
4040
loaderOptions: LoaderOptions,
41-
loader: Webpack
41+
loader: webpack.loader.LoaderContext
4242
): { instance?: TSInstance; error?: WebpackError } {
4343
if (instances.hasOwnProperty(loaderOptions.instance)) {
4444
const instance = instances[loaderOptions.instance];
@@ -67,7 +67,7 @@ export function getTypeScriptInstance(
6767

6868
function successfulTypeScriptInstance(
6969
loaderOptions: LoaderOptions,
70-
loader: Webpack,
70+
loader: webpack.loader.LoaderContext,
7171
log: logger.Logger,
7272
colors: Chalk,
7373
compiler: typeof typescript,

0 commit comments

Comments
 (0)