Webpack loader that integrates TSLint into the build process for TypeScript linting
npx @tessl/cli install tessl/npm-tslint-loader@3.6.0TSLint Loader is a webpack loader that integrates TSLint (TypeScript linter) into the webpack build process. It enables developers to automatically lint TypeScript files during compilation, catching code quality issues and style violations early in the development workflow with comprehensive configuration options.
npm install tslint tslint-loader --save-devTSLint Loader is used as a webpack loader, not directly imported in application code. It's configured in webpack configuration files.
module.exports = {
module: {
rules: [
{
test: /\.ts$/,
enforce: 'pre',
use: [
{
loader: 'tslint-loader',
options: {
// Loader configuration options
emitErrors: false,
failOnHint: true,
typeCheck: false
}
}
]
}
]
}
}module.exports = {
module: {
preLoaders: [
{
test: /\.ts$/,
loader: 'tslint-loader'
}
]
},
tslint: {
// Loader configuration options
emitErrors: false,
failOnHint: true
}
}The primary webpack loader function that processes TypeScript files through TSLint.
/**
* Main webpack loader function for TSLint integration
* @param {string} input - Source code content of the file being processed
* @param {object} map - Optional source map for the file
* @returns {void} Calls webpack callback with original source code unchanged
*/
function loader(input, map);The loader:
Comprehensive configuration system supporting inline rules, external config files, and output customization.
interface LoaderOptions {
/** Inline TSLint configuration rules */
configuration?: TSLintConfiguration;
/** Path to TSLint configuration file or false to disable */
configFile?: string | false;
/** Emit lint issues as errors instead of warnings */
emitErrors?: boolean;
/** Fail compilation when lint errors are found */
failOnHint?: boolean;
/** Enable type-checked TSLint rules */
typeCheck?: boolean;
/** Path to TypeScript configuration file for type checking */
tsConfigFile?: string;
/** Automatically fix linting errors where possible */
fix?: boolean;
/** TSLint formatter to use */
formatter?: string;
/** Directory containing custom formatters */
formattersDirectory?: string;
/** Directory containing custom TSLint rules */
rulesDirectory?: string;
/** Configuration for saving lint results to files */
fileOutput?: FileOutputOptions;
}
interface TSLintConfiguration {
rules: {
[ruleName: string]: boolean | [boolean, ...any[]];
};
[key: string]: any;
}
interface FileOutputOptions {
/** Output directory for lint reports */
dir: string;
/** File extension for reports (default: 'txt') */
ext?: string;
/** Clean output directory before writing */
clean?: boolean;
/** Content to prepend to each report file */
header?: string;
/** Content to append to each report file */
footer?: string;
}Usage Examples:
// Basic configuration with inline rules
{
loader: 'tslint-loader',
options: {
configuration: {
rules: {
quotemark: [true, 'double'],
'no-console': [false]
}
},
emitErrors: true,
failOnHint: false
}
}
// Configuration with external config file and type checking
{
loader: 'tslint-loader',
options: {
configFile: './tslint-custom.json',
typeCheck: true,
tsConfigFile: './tsconfig.json',
fix: true
}
}
// Configuration with file output for CI systems
{
loader: 'tslint-loader',
options: {
fileOutput: {
dir: './lint-reports/',
ext: 'xml',
clean: true,
header: '<?xml version="1.0" encoding="utf-8"?>\\n<checkstyle version="5.7">',
footer: '</checkstyle>'
}
}
}Built-in custom formatter optimized for webpack integration.
/**
* Custom TSLint formatter class extending AbstractFormatter
*/
class Formatter extends Lint.Formatters.AbstractFormatter {
/**
* Format lint failures for display
* @param {Array} failures - Array of TSLint failure objects
* @returns {string} Formatted output with line numbers and positions
*/
format(failures);
}The custom formatter outputs failures in the format: [line, column]: failure message
Example Output:
[8, 1]: Calls to 'console.log' are not allowed.
[15, 5]: Missing semicolon.The loader handles various error conditions:
emitErrors optionfailOnHint settingWhen failOnHint is enabled and lint errors occur, the compilation fails with detailed error information.
Enable advanced TSLint rules that require TypeScript type information:
{
loader: 'tslint-loader',
options: {
typeCheck: true,
tsConfigFile: './tsconfig.json',
configuration: {
rules: {
'for-in-array': true,
'no-unsafe-any': true,
'strict-boolean-expressions': true
}
}
}
}Use custom TSLint rules and formatters:
{
loader: 'tslint-loader',
options: {
rulesDirectory: './custom-rules/',
formatter: 'myCustomFormatter',
formattersDirectory: './custom-formatters/'
}
}Configure file output for CI systems:
{
loader: 'tslint-loader',
options: {
emitErrors: true,
failOnHint: true,
fileOutput: {
dir: './build-reports/tslint/',
ext: 'json',
clean: true
}
}
}