babel-preset-optimizations
raw JSON → 2.0.0 verified Sat Apr 25 auth: no javascript
Babel preset that bundles babel-minify optimisation plugins for production builds. Version 2.0.0 requires Node >=10.13.0 and ships TypeScript types. It includes plugins for constant folding, dead code elimination, guarded expressions, inline consecutive adds, undefined-to-void, and optionally simplify. Unlike full minifiers, it focuses solely on optimisations without mangling or compression, giving finer control over the output. Options allow preserving function/class names and enabling simplify or undefined-to-void transforms. It is best used alongside a separate minification step.
Common errors
error Error: Plugin/Preset files are not allowed to export objects, only functions. ↓
cause Using an outdated Babel version or wrong import syntax for the preset.
fix
Ensure Babel is version 7 and use the correct preset reference: 'babel-preset-optimizations' or short name 'optimizations'.
error Module not found: Can't resolve 'babel-preset-optimizations' ↓
cause Package not installed or not properly resolved.
fix
Run 'npm install --save-dev babel-preset-optimizations' and check node_modules.
error Unknown option: keepFnName ↓
cause Options object passed incorrectly – not wrapped in an array.
fix
Use [['optimizations', { keepFnName: true }]] instead of ['optimizations', { keepFnName: true }].
Warnings
gotcha This preset does not minify or mangle code; it only applies optimisations. Use alongside a minifier like babel-minify or terser. ↓
fix Add 'minify' or another minification preset/plugin after this one.
deprecated The simplify option changes code structure significantly and may be considered harmful for debugging. ↓
fix Only enable simplify in production builds with source maps, or avoid it.
gotcha Options must be passed in the nested array format for presets, not as a flat array. ↓
fix Use [["optimizations", { options }]] instead of ["optimizations", { options }].
breaking Version 2.0.0 drops support for Node <10.13.0. ↓
fix Upgrade Node to >=10.13.0 or stick with v1.x.
gotcha In Babel 7, preset short names may not resolve automatically; you might need the full package name. ↓
fix Use 'babel-preset-optimizations' instead of 'optimizations' if short name fails.
Install
npm install babel-preset-optimizations yarn add babel-preset-optimizations pnpm add babel-preset-optimizations Imports
- babel-preset-optimizations wrong
require('babel-preset-optimizations') as a plugincorrectmodule.exports = { presets: ['optimizations'] }; or in .babelrc: { "presets": ["optimizations"] } - options object wrong
{"presets": ["optimizations", { "keepFnName": false }]}correct{"presets": [["optimizations", { "keepFnName": false }]]} - require() in Node API wrong
const preset = require('babel-preset-optimizations').default;correctconst preset = require('babel-preset-optimizations');
Quickstart
// .babelrc or babel.config.js
module.exports = {
presets: [
['optimizations', {
keepFnName: true,
keepClassName: true,
simplify: false,
undefinedToVoid: false
}]
]
};