elm-optimize-level-2
raw JSON → 0.3.5 verified Fri May 01 auth: no javascript
A CLI tool (v0.3.5) that applies a second level of optimization to the JavaScript output of the Elm compiler, producing faster code while maintaining Elm semantics. It can be used as a drop-in replacement for `elm make --optimize` and supports an `--optimize-speed` flag for even faster code at the cost of ~5% larger bundle size. The project is experimental and may introduce breaking changes. Key differentiators: Elm-specific JS transformations (e.g., tweaking record updates), active development with community benchmarks, and no built-in minification.
Common errors
error Error [ERR_REQUIRE_ESM]: require() of ES Module ↓
cause The package is ESM-only and cannot be used with require() in CommonJS contexts.
fix
Use
import or set "type": "module" in package.json. error elm-optimize-level-2: command not found ↓
cause The package is not installed globally or not in PATH.
fix
Install globally:
npm install -g elm-optimize-level-2 or use npx elm-optimize-level-2. error elm-optimize-level-2: error: unrecognized argument: --help ↓
cause The CLI does not support --help; try --help?
fix
Check documentation or README; run without arguments to see usage (if supported).
Warnings
breaking elm-optimize-level-2 only generates JS files; it does not support HTML generation like `elm make`. ↓
fix Use a separate tool (e.g., elm-live) or serve the JS file with an HTML host.
deprecated The `--optimize-speed` flag may produce ~5% larger bundle sizes. Consider whether asset size or runtime performance is more important. ↓
fix Do not use `--optimize-speed` if bundle size is a constraint.
gotcha This package is experimental and may break your Elm code in unexpected ways. Run your test suite after each compilation. ↓
fix Only use in production after thorough testing.
gotcha Does not minify or gzip the output; you must do that separately before deployment. ↓
fix Use a minifier like Terser and gzip before deploying.
breaking Requires Node.js >= 16 and ES modules; does not support CommonJS. ↓
fix Set `"type": "module"` in your package.json or use `.mjs` extension.
Install
npm install elm-optimize-level-2 yarn add elm-optimize-level-2 pnpm add elm-optimize-level-2 Imports
- default wrong
const elmOptimizeLevel2 = require('elm-optimize-level-2')correctimport { elmOptimizeLevel2 } from 'elm-optimize-level-2' - CLI wrong
npm run elm-optimize-level-2correctnpx elm-optimize-level-2 Main.elm - types
import type { OptimizeOptions } from 'elm-optimize-level-2'
Quickstart
// Install globally
// $ npm install -g elm-optimize-level-2
// Then compile an Elm file:
// $ elm-optimize-level-2 Main.elm --output app.js
// Or with the --optimize-speed flag:
// $ elm-optimize-level-2 Main.elm --optimize-speed --output app.js
// Programmatic usage:
import elmOptimizeLevel2 from 'elm-optimize-level-2';
import { promises as fs } from 'fs';
async function compile() {
const result = await elmOptimizeLevel2({
entry: 'Main.elm',
output: 'app.js',
optimizeSpeed: false,
});
await fs.writeFile('app.js', result.code);
}
compile();