6to5 provides comprehensive CLI tools for build workflows. The command line interface supports file and directory transformation, watch mode, source maps, and extensive configuration options.
The primary 6to5 command transforms ES6+ files to ES5:
6to5 [options] <files...>6to5-node <script> [arguments] # Run Node.js with 6to5 transformation
6to5-minify <files...> # Transform and minify files
6to5-runtime # Generate runtime helpers# Transform single file
6to5 script.js --out-file script-compiled.js
# Transform directory
6to5 src --out-dir lib
# Read from stdin, write to stdout
echo "const fn = () => {};" | 6to5
# Specify input filename for stdin
echo "const fn = () => {};" | 6to5 --filename input.js# Generate external source maps
6to5 script.js --out-file script.js --source-maps
# Inline source maps
6to5 script.js --out-file script.js --source-maps-inline
# Generate source maps (filename will be automatically determined)
6to5 script.js --out-file script.js --source-maps# CommonJS (default)
6to5 script.js --out-file script.js --modules common
# AMD modules
6to5 script.js --out-file script.js --modules amd
# UMD modules
6to5 script.js --out-file script.js --modules umd
# SystemJS modules
6to5 script.js --out-file script.js --modules system
# No module transformation
6to5 script.js --out-file script.js --modules ignore# Enable specific transformers only (whitelist)
6to5 script.js --out-file script.js --whitelist es6.arrowFunctions,es6.classes
# Disable specific transformers (blacklist)
6to5 script.js --out-file script.js --blacklist es6.modules
# Enable optional transformers
6to5 script.js --out-file script.js --optional es7.comprehensions,es7.objectRestSpread
# Enable loose mode for specific transformers
6to5 script.js --out-file script.js --loose es6.classes,es6.modules# Enable experimental ES7 features
6to5 script.js --out-file script.js --experimental
# Enable playground features
6to5 script.js --out-file script.js --playground
# Use runtime helpers
6to5 script.js --out-file script.js --runtime
# React compatibility mode
6to5 script.js --out-file script.js --react-compat# Watch mode - recompile on changes
6to5 src --out-dir lib --watch
# Remove comments from output
6to5 script.js --out-file script.js --remove-comments
# Add auxiliary comment
6to5 script.js --out-file script.js --auxilary-comment "Generated by build system"# Insert module IDs
6to5 src --out-dir lib --module-ids
# Keep extensions in module IDs
6to5 src --out-dir lib --module-ids --keep-module-id-extensionsUsage: 6to5 [options] <files ...>
Options:
-t, --source-maps-inline Append sourceMappingURL comment to bottom of code
-s, --source-maps Save source map alongside the compiled code
-f, --filename [filename] Filename to use when reading from stdin [stdin]
-w, --watch Recompile files on changes
-r, --runtime Replace 6to5 declarations with references to a runtime
-e, --experimental Enable experimental support for proposed ES7 features
-p, --playground Enable playground support
-m, --modules [modules] Module formatter type to use [common]
-l, --whitelist [whitelist] Whitelist of transformers to ONLY use
-b, --blacklist [blacklist] Blacklist of transformers to NOT use
-i, --optional [list] List of optional transformers to enable
-L, --loose [list] List of transformers to enable loose mode ON
-o, --out-file [out] Compile all input files into a single file
-d, --out-dir [out] Compile an input directory of modules into an output directory
-c, --remove-comments Remove comments from the compiled code
-M, --module-ids Insert module id in modules
-R, --react-compat Makes the react transformer produce pre-v0.12 code
--keep-module-id-extensions Keep extensions when generating module ids
-a, --auxilary-comment [comment] Comment text to prepend to all auxiliary code
-V, --version Output the version number
-h, --help Output usage information# Transform single file
6to5 input.js --out-file output.js
# Transform with source maps
6to5 input.js --out-file output.js --source-maps
# Transform to AMD format
6to5 input.js --out-file output.js --modules amd# Transform entire directory
6to5 src --out-dir lib
# Transform with watch mode
6to5 src --out-dir lib --watch
# Transform with filtering
6to5 src --out-dir lib --blacklist es6.modules# Full ES6+ transformation with ES7 features
6to5 src --out-dir dist \
--modules umd \
--source-maps \
--experimental \
--optional es7.comprehensions,es7.objectRestSpread \
--runtime \
--module-ids
# Minimal transformation (only syntax changes)
6to5 src --out-dir lib \
--whitelist es6.arrowFunctions,es6.classes,es6.templateLiterals \
--loose es6.classes
# React JSX transformation
6to5 src --out-dir lib \
--optional react \
--modules common \
--react-compat# Package.json scripts
{
"scripts": {
"build": "6to5 src --out-dir lib --source-maps",
"build:watch": "6to5 src --out-dir lib --watch",
"build:production": "6to5 src --out-dir dist --modules umd --remove-comments",
"build:es7": "6to5 src --out-dir lib --experimental --optional es7.comprehensions"
}
}
# Makefile
build:
6to5 src --out-dir lib --source-maps
watch:
6to5 src --out-dir lib --watch
production:
6to5 src --out-dir dist --modules umd --remove-comments --runtime
clean:
rm -rf lib dist
.PHONY: build watch production clean#!/bin/bash
# build.sh
set -e
echo "Building JavaScript..."
# Clean previous build
rm -rf dist
# Transform ES6+ to ES5
6to5 src --out-dir dist \
--modules common \
--source-maps \
--optional es7.objectRestSpread \
--runtime \
--remove-comments
echo "Build complete!"
# Run tests on built code
node dist/test/index.jsWhile 6to5 v3.6.5 doesn't support configuration files directly, you can create wrapper scripts:
#!/bin/bash
# 6to5-config.sh
# Default options for this project
DEFAULT_OPTS="--modules common --optional es7.comprehensions --loose es6.classes"
case "$1" in
"dev")
6to5 src --out-dir lib $DEFAULT_OPTS --watch --source-maps-inline
;;
"prod")
6to5 src --out-dir dist $DEFAULT_OPTS --runtime --remove-comments
;;
"test")
6to5 test --out-dir test-compiled $DEFAULT_OPTS
;;
*)
echo "Usage: $0 {dev|prod|test}"
exit 1
;;
esac# Run Node.js script with 6to5 transformation
6to5-node app.js
# Pass Node.js flags
6to5-node --harmony app.js
# Debug mode
6to5-node --debug app.js
# With arguments
6to5-node app.js arg1 arg2# Exit on error
set -e
# Transform with error checking
if ! 6to5 src --out-dir lib; then
echo "Build failed!"
exit 1
fi
# Conditional transformation
if [ -d "src" ]; then
6to5 src --out-dir lib
else
echo "Source directory not found"
exit 1
fi# CI build script
#!/bin/bash
# Install dependencies
npm install
# Lint source code
npm run lint
# Transform ES6+ to ES5
6to5 src --out-dir lib --modules common --runtime
# Run tests on transformed code
npm test
# Check that all files were transformed
if [ ! -f "lib/index.js" ]; then
echo "Build incomplete!"
exit 1
fi
echo "CI build successful"#!/bin/bash
# deploy.sh
echo "Starting deployment build..."
# Clean and create directories
rm -rf dist
mkdir -p dist
# Transform for different environments
echo "Building for Node.js..."
6to5 src --out-dir dist/node --modules common --runtime
echo "Building for browser..."
6to5 src --out-dir dist/browser --modules umd --runtime
echo "Building minified version..."
6to5 src --out-file dist/bundle.min.js --modules umd --remove-comments --runtime
echo "Deployment build complete!"