Skip to content

Commit 8fa971e

Browse files
authored
refactor: loaderOptions separate file
1 parent b7577b4 commit 8fa971e

3 files changed

Lines changed: 26 additions & 26 deletions

File tree

src/index.ts

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import type {
1919
LogLevel,
2020
TSInstance,
2121
} from './interfaces';
22+
import { getWebpack4LoaderOptions } from './loaderUtils';
2223
import {
2324
appendSuffixesIfMatch,
2425
arrify,
@@ -29,11 +30,6 @@ import type { RawSourceMap } from 'source-map';
2930
import { SourceMapConsumer, SourceMapGenerator } from 'source-map';
3031

3132
const loaderOptionsCache: LoaderOptionsCache = {};
32-
const requireFromTsLoader = module.require.bind(module) as NodeJS.Require;
33-
type LoaderUtilsModule = {
34-
getOptions<T>(loaderContext: { query: string }): T;
35-
};
36-
let loaderUtils: LoaderUtilsModule | undefined;
3733

3834
/**
3935
* The entry point for ts-loader
@@ -241,22 +237,6 @@ function getLoaderOptions(
241237
return options;
242238
}
243239

244-
function getWebpack4LoaderOptions(
245-
loaderContext: webpack.LoaderContext<LoaderOptions>
246-
) {
247-
if (!loaderUtils) {
248-
try {
249-
loaderUtils = requireFromTsLoader('loader-utils') as LoaderUtilsModule;
250-
} catch {
251-
throw new Error(
252-
'ts-loader requires loader-utils to be installed when used with webpack 4.'
253-
);
254-
}
255-
}
256-
257-
return loaderUtils.getOptions<LoaderOptions>(loaderContext as any) || ({} as LoaderOptions);
258-
}
259-
260240
type ValidLoaderOptions = keyof LoaderOptions;
261241
const validLoaderOptions: ValidLoaderOptions[] = [
262242
'silent',

src/loader-utils.d.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/loaderUtils.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type * as webpack from 'webpack';
2+
3+
import type { LoaderOptions } from './interfaces';
4+
5+
const requireFromTsLoader = module.require.bind(module) as NodeJS.Require;
6+
type LoaderUtilsModule = {
7+
getOptions<T>(loaderContext: { query: string }): T;
8+
};
9+
let loaderUtils: LoaderUtilsModule | undefined;
10+
11+
export function getWebpack4LoaderOptions(
12+
loaderContext: webpack.LoaderContext<LoaderOptions>
13+
) {
14+
if (!loaderUtils) {
15+
try {
16+
loaderUtils = requireFromTsLoader('loader-utils') as LoaderUtilsModule;
17+
} catch {
18+
throw new Error(
19+
'ts-loader requires loader-utils to be installed when used with webpack 4.'
20+
);
21+
}
22+
}
23+
24+
return loaderUtils.getOptions<LoaderOptions>(loaderContext as any) || ({} as LoaderOptions);
25+
}

0 commit comments

Comments
 (0)