terser-loader
raw JSON → 2.0.3 verified Sat Apr 25 auth: no javascript
terser-loader is a webpack loader that minifies JavaScript using Terser, a modern ES6+ capable minifier. Version 2.0.3 is the latest stable release, with peer dependency on webpack ^4 || ^5. It offers options like `terserOptions` passed directly to Terser API and `stripTrailingSemicolon`. Unlike the official `terser-webpack-plugin`, this is a loader (not a plugin) integrated in webpack's module rules pipeline, useful for selective minification alongside other loaders. Low release cadence but functional for specific use cases.
Common errors
error Module not found: Error: Can't resolve 'terser' ↓
cause Missing Terser peer dependency
fix
Install terser as a dev dependency: npm install --save-dev terser
error ValidationError: Invalid options object. Terser Loader has been initialized using an options object that does not match the API schema. ↓
cause Options passed are not recognized
fix
Check terserOptions for valid Terser options; do not use options like 'output' directly on the loader.
error Error: webpack <4 is not supported ↓
cause Using terser-loader@2 with webpack 3 or lower
fix
Upgrade webpack to version 4 or 5, or install terser-loader@1
Warnings
gotcha Loader is not a plugin; it runs on each module individually, not on the final bundle. Use terser-webpack-plugin for bundle-level minification. ↓
fix If you need bundle-level minification, use terser-webpack-plugin instead.
breaking Version 2.0.0 dropped support for webpack <4. ↓
fix Upgrade webpack to ^4 || ^5, or stick to terser-loader@1 for webpack 3.
deprecated The stripTrailingSemicolon option is non-standard and may be removed in future versions. ↓
fix Use terserOptions.output.semicolon = false instead.
Install
npm install terser-loader yarn add terser-loader pnpm add terser-loader Imports
- terser-loader wrong
const TerserLoader = require('terser-loader');correctimport TerserLoader from 'terser-loader';
Quickstart
// webpack.config.js
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
module: {
rules: [
{
test: /\.js$/,
use: [
{
loader: 'terser-loader',
options: {
terserOptions: {
compress: {
drop_console: true,
},
},
stripTrailingSemicolon: true,
},
},
],
},
],
},
mode: 'production',
};