Utilities for automatic compilation and runtime support. The registration system provides require hooks for on-the-fly transformation, while the runtime system provides helper functions needed by transformed code.
/**
* Register require hook for automatic 6to5 transformation
* @param opts - Registration options
* @returns Registration function for manual control
*/
function register(opts?: RegisterOptions): RegisterFunction;
interface RegisterOptions extends TransformOptions {
/**
* File extensions to transform
* @default [".js", ".jsx", ".es6", ".es"]
*/
extensions?: string[];
/**
* Enable require cache for transformed files
* @default true
*/
cache?: boolean;
/**
* Files/patterns to ignore (won't be transformed)
*/
ignore?: string | RegExp | (string | RegExp)[];
/**
* Files/patterns to only transform (whitelist)
*/
only?: string | RegExp | (string | RegExp)[];
/**
* Polyfill detection patterns
*/
polyfill?: boolean | string[];
}
interface RegisterFunction {
/**
* Manually transform and cache a file
* @param filename - File to transform
*/
(filename: string): void;
/**
* Registration cache
*/
cache: { [filename: string]: any };
}/**
* Check if file can be compiled by 6to5
* @param filename - File path to check
* @param altExts - Alternative extensions to accept
* @returns True if file can be compiled
*/
function canCompile(filename: string, altExts?: string[]): boolean;
/**
* Default compilable extensions
*/
canCompile.EXTENSIONS: string[]; // [".js", ".jsx", ".es6", ".es"]/**
* Generate complete 6to5 runtime code containing all helper functions
* @returns Runtime code as string
*/
function runtime(): string;/**
* Load core-js polyfills for ES6+ features
* Adds support for Promises, Set, Map, Symbol, etc.
*/
function polyfill(): void;// Register for all supported files
require("6to5/register");
// Now you can require ES6+ files directly
const MyClass = require("./my-es6-class");
const component = require("./jsx-component");require("6to5/register")({
// Transform options
modules: "common",
optional: ["es7.comprehensions", "es7.objectRestSpread"],
loose: ["es6.classes"],
// Registration-specific options
extensions: [".js", ".jsx", ".es6"],
ignore: /node_modules/,
cache: true
});
// Only ES6+ files matching patterns will be transformed
const app = require("./src/app"); // Transformed
const lodash = require("lodash"); // Not transformed (in node_modules)require("6to5/register")({
// Only transform files in src directory
only: /^src\//,
// Ignore test files
ignore: /\.test\.js$/,
experimental: true
});const register = require("6to5/register");
// Get registration function for manual control
const transform = register({
modules: "amd",
runtime: true
});
// Manually transform specific files
transform("./special-file.js");
// Access transformation cache
console.log(Object.keys(transform.cache));// Generate runtime for embedding
const to5 = require("6to5");
const runtime = to5.runtime();
// Save runtime to file
require("fs").writeFileSync("6to5-runtime.js", runtime);
// Or inject into build
const browserify = require("browserify");
browserify()
.add("./runtime-entry.js")
.transform(function(file) {
if (file === "./runtime-entry.js") {
return through(function() {
this.push(runtime);
this.push(null);
});
}
});// Load all ES6+ polyfills at application start
require("6to5/polyfill");
// Now you can use ES6+ features in older environments
const map = new Map();
const promise = Promise.resolve(42);
const symbol = Symbol("key");
// Use in browser
<script src="node_modules/6to5/polyfill.js"></script>
<script>
// ES6+ features now available
[1, 2, 3].map(x => x * 2);
</script>// Complete setup for Node.js application
require("6to5/polyfill"); // Load polyfills first
require("6to5/register")({
// Transform configuration
modules: "common",
optional: [
"es7.comprehensions",
"es7.objectRestSpread",
"es7.exponentiationOperator"
],
loose: ["es6.classes", "es6.modules"],
runtime: true,
// File handling
extensions: [".js", ".jsx", ".es6", ".es"],
ignore: [
/node_modules/,
/\.test\.js$/,
/\.spec\.js$/
],
only: /^(src|lib|app)\//,
// Performance
cache: true
});
// Application entry point
require("./src/app");// Webpack loader configuration
module.exports = {
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: "6to5-loader",
query: {
modules: "common",
optional: ["es7.comprehensions"],
runtime: true
}
}
]
}
};
// Gulp task
const gulp = require("gulp");
const to5 = require("gulp-6to5");
gulp.task("compile", function() {
return gulp.src("src/**/*.js")
.pipe(to5({
modules: "amd",
optional: ["es7.objectRestSpread"]
}))
.pipe(gulp.dest("dist"));
});// Development: transform on-the-fly
if (process.env.NODE_ENV !== "production") {
require("6to5/register")({
cache: false, // Disable cache for development
sourceMap: "inline"
});
}
// Production: use pre-compiled files
const entryPoint = process.env.NODE_ENV === "production"
? "./dist/app.js"
: "./src/app.js";
require(entryPoint);// test/setup.js
require("6to5/polyfill");
require("6to5/register")({
optional: [
"es7.comprehensions",
"es7.objectRestSpread"
],
// Include test files
extensions: [".js", ".jsx", ".test.js", ".spec.js"]
});
// mocha.opts
--require test/setup.js
--recursive test/try {
require("6to5/register")({
whitelist: ["nonexistent.transformer"]
});
} catch (err) {
if (err instanceof ReferenceError) {
console.error("Invalid transformer:", err.message);
}
}
// Handle compilation errors
process.on("uncaughtException", function(err) {
if (err.codeFrame) {
console.error("6to5 compilation error:");
console.error(err.codeFrame);
}
throw err;
});