Skip to content
This repository was archived by the owner on Dec 3, 2025. It is now read-only.

Commit c441094

Browse files
author
Martynas Žilinskas
committed
Added linters.
1 parent 6ea4cb7 commit c441094

2 files changed

Lines changed: 48 additions & 35 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import * as fs from "fs-extra";
2+
import upath from "upath";
3+
4+
// Tsconfig.
5+
export const TS_CONFIG_NAME: string = "tsconfig.json";
6+
const DEFAULT_TS_CONFIG_LOCATION: string = upath.resolve(__dirname, `../assets/${TS_CONFIG_NAME}`);
7+
8+
// TsLint.
9+
export const TSLINT_CONFIG_NAME: string = "tslint.json";
10+
const DEFAULT_TSLINT_CONFIG_LOCATION: string = upath.resolve(__dirname, `../assets/${TSLINT_CONFIG_NAME}`);
11+
12+
export function checkTsConfig(projectDirectory: string): void {
13+
const configLocation = upath.resolve(projectDirectory, TS_CONFIG_NAME);
14+
15+
if (!fs.pathExistsSync(configLocation)) {
16+
console.info(`File "${TS_CONFIG_NAME}" not found at ${configLocation}. Creating...`);
17+
fs.copySync(DEFAULT_TS_CONFIG_LOCATION, configLocation);
18+
console.info("Created.");
19+
}
20+
}
21+
22+
export function checkTslintConfig(projectDirectory: string): void {
23+
const configLocation = upath.resolve(projectDirectory, TSLINT_CONFIG_NAME);
24+
25+
if (!fs.pathExistsSync(configLocation)) {
26+
console.info(`File "${TSLINT_CONFIG_NAME}" not found at ${configLocation}. Creating...`);
27+
fs.copySync(DEFAULT_TSLINT_CONFIG_LOCATION, configLocation);
28+
console.info("Created.");
29+
}
30+
}

packages/webpack-builder-plugin-typescript/src/plugin.ts

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,49 @@ import { TsconfigPathsPlugin } from "tsconfig-paths-webpack-plugin/lib";
33
import { Options as TsconfigPathsPluginOptions } from "tsconfig-paths-webpack-plugin/lib/options";
44
import { loadTsconfig, Tsconfig } from "tsconfig-paths/lib/tsconfig-loader";
55
import upath from "upath";
6-
import * as fs from "fs-extra";
76
import { Plugin } from "@reactway/webpack-builder";
87
import { ForkTsCheckerWebpackPluginOptions } from "./plugin-options";
98
import TerserPlugin, { TerserPluginOptions } from "terser-webpack-plugin";
109

1110
import "ts-loader";
11+
import { TS_CONFIG_NAME, checkTsConfig, TSLINT_CONFIG_NAME, checkTslintConfig } from "./checkers";
12+
13+
const enum Linter {
14+
TsLint = "tslint",
15+
EsLint = "eslint"
16+
}
1217

1318
// Extensions.
1419
const TS_EXTENSION: string = ".ts";
1520
const TSX_EXTENSION: string = ".tsx";
1621
const JS_EXTENSION: string = ".js";
1722
const JSX_EXTENSION: string = ".jsx";
1823

19-
// Tsconfig.
20-
export const TS_CONFIG_NAME: string = "tsconfig.json";
21-
const DEFAULT_TS_CONFIG_LOCATION: string = upath.resolve(__dirname, `../assets/${TS_CONFIG_NAME}`);
22-
23-
// TsLint.
24-
export const TSLINT_CONFIG_NAME: string = "tslint.json";
25-
const DEFAULT_TSLINT_CONFIG_LOCATION: string = upath.resolve(__dirname, `../assets/${TSLINT_CONFIG_NAME}`);
26-
2724
interface TypeScriptPluginOptions {
2825
forkTsCheckerOptions?: Partial<ForkTsCheckerWebpackPluginOptions>;
2926
tsconfigPathsPluginOptions?: Partial<TsconfigPathsPluginOptions>;
3027
terserPluginOptions?: TerserPluginOptions;
28+
linter?: Linter;
3129
}
3230

3331
export const TypeScriptPlugin: Plugin<TypeScriptPluginOptions> = (config, projectDirectory) => {
3432
const fullTsconfigLocation = upath.resolve(projectDirectory, TS_CONFIG_NAME);
3533
let baseURLExist: boolean = false;
3634

35+
const linter = config != null && config.linter != null ? config.linter : Linter.EsLint;
36+
3737
try {
3838
checkTsConfig(projectDirectory);
3939
} catch (error) {
4040
console.error(`Failed while initiating "${TS_CONFIG_NAME}".`, error);
4141
}
4242

43-
try {
44-
checkTslintConfig(projectDirectory);
45-
} catch (error) {
46-
console.error(`Failed while initiating "${TSLINT_CONFIG_NAME}".`, error);
43+
if (linter === Linter.TsLint) {
44+
try {
45+
checkTslintConfig(projectDirectory);
46+
} catch (error) {
47+
console.error(`Failed while initiating "${TSLINT_CONFIG_NAME}".`, error);
48+
}
4749
}
4850

4951
const tsConfig: Tsconfig | undefined = loadTsconfig(fullTsconfigLocation);
@@ -63,7 +65,8 @@ export const TypeScriptPlugin: Plugin<TypeScriptPluginOptions> = (config, projec
6365
webpack.plugins.push(
6466
new ForkTsCheckerWebpackPlugin({
6567
checkSyntacticErrors: true,
66-
tslint: true,
68+
tslint: linter === Linter.TsLint ? true : undefined,
69+
eslint: linter === Linter.EsLint ? true : undefined,
6770
...forkTsConfig
6871
})
6972
);
@@ -144,7 +147,7 @@ export const TypeScriptPlugin: Plugin<TypeScriptPluginOptions> = (config, projec
144147
}
145148

146149
if (!baseURLExist && config != null && config.tsconfigPathsPluginOptions != null) {
147-
throw new Error(`Cannot add tsconfigPathsPluginOptions because baseUrl do not exist at ${TS_CONFIG_NAME}`);
150+
throw new Error(`Cannot add tsconfigPathsPluginOptions because baseUrl does not exist at ${TS_CONFIG_NAME}`);
148151
}
149152

150153
if (webpack.resolve.extensions == null) {
@@ -209,23 +212,3 @@ export const TypeScriptPlugin: Plugin<TypeScriptPluginOptions> = (config, projec
209212
return webpack;
210213
};
211214
};
212-
213-
export function checkTsConfig(projectDirectory: string): void {
214-
const configLocation = upath.resolve(projectDirectory, TS_CONFIG_NAME);
215-
216-
if (!fs.pathExistsSync(configLocation)) {
217-
console.info(`File "${TS_CONFIG_NAME}" not found at ${configLocation}. Creating...`);
218-
fs.copySync(DEFAULT_TS_CONFIG_LOCATION, configLocation);
219-
console.info("Created.");
220-
}
221-
}
222-
223-
export function checkTslintConfig(projectDirectory: string): void {
224-
const configLocation = upath.resolve(projectDirectory, TSLINT_CONFIG_NAME);
225-
226-
if (!fs.pathExistsSync(configLocation)) {
227-
console.info(`File "${TSLINT_CONFIG_NAME}" not found at ${configLocation}. Creating...`);
228-
fs.copySync(DEFAULT_TSLINT_CONFIG_LOCATION, configLocation);
229-
console.info("Created.");
230-
}
231-
}

0 commit comments

Comments
 (0)