FuseBox provides a comprehensive TypeScript/JavaScript transformation system with support for modern language features, JSX, custom transforms, and testing utilities. The transformation pipeline handles TypeScript compilation, modern JavaScript features, and custom code transformations.
function initCommonTransform(props: {
code: string;
compilerOptions?: ICompilerOptions;
jsx?: boolean;
props?: ISerializableTransformationContext;
transformers: Array<ITransformer>;
}): {
code: string;
requireStatementCollection: ITransformerRequireStatementCollection;
};This function applies a series of transformers to TypeScript/JavaScript code and returns the transformed code along with collected require statements for dependency analysis.
function testTransform(props: {
code: string;
compilerOptions?: ICompilerOptions;
jsx?: boolean;
props?: ISerializableTransformationContext;
transformers: Array<ITransformer>;
}): {
code: string;
requireStatementCollection: ITransformerRequireStatementCollection;
};The testTransform function is an alias for initCommonTransform specifically designed for testing and development purposes. It provides the same transformation capabilities with identical API, making it convenient for unit tests and code experimentation.
const BASE_TRANSFORMERS: Array<ITransformer>;The BASE_TRANSFORMERS constant contains the core set of transformers that handle:
interface ITransformer {
commonVisitors?: (props: ITransformerCommon) => ITransformerVisitors;
}
interface ITransformerCommon {
onRequireCallExpression: (importType: ImportType, statement: ASTNode) => void;
transformationContext: ISerializableTransformationContext;
}
interface ITransformerVisitors {
onEach?: (node: ASTNode) => ASTNode | void;
onEnter?: (node: ASTNode) => ASTNode | void;
onLeave?: (node: ASTNode) => ASTNode | void;
}interface ISerializableTransformationContext {
compilerOptions?: ICompilerOptions;
fileAbsPath?: string;
module?: {
isEntry?: boolean;
isServerEntry?: boolean;
};
packageName?: string;
packageType?: PackageType;
target?: ITarget;
}
interface ICompilerOptions {
target?: string;
module?: string;
lib?: string[];
declaration?: boolean;
outDir?: string;
rootDir?: string;
strict?: boolean;
esModuleInterop?: boolean;
skipLibCheck?: boolean;
forceConsistentCasingInFileNames?: boolean;
experimentalDecorators?: boolean;
emitDecoratorMetadata?: boolean;
jsx?: "preserve" | "react" | "react-jsx" | "react-jsxdev";
jsxFactory?: string;
jsxFragmentFactory?: string;
}enum ImportType {
REQUIRE = "require",
IMPORT = "import",
DYNAMIC_IMPORT = "dynamic_import"
}
interface ASTNode {
type: string;
start?: number;
end?: number;
loc?: {
start: { line: number; column: number };
end: { line: number; column: number };
};
[key: string]: any;
}
type ITransformerRequireStatementCollection = Array<{
importType: ImportType;
statement: ASTNode;
}>;?.) for older browsers??) for older browsersimport { initCommonTransform, testTransform, BASE_TRANSFORMERS } from "fuse-box";
const result = initCommonTransform({
code: `
interface User {
name: string;
age?: number;
}
const user: User = { name: "Alice" };
export { user };
`,
transformers: BASE_TRANSFORMERS,
compilerOptions: {
target: "ES2018",
module: "CommonJS"
}
});
console.log(result.code); // Transformed JavaScript
console.log(result.requireStatementCollection); // Collected dependenciesimport { testTransform, BASE_TRANSFORMERS } from "fuse-box";
// Use testTransform for unit tests and development
const testResult = testTransform({
code: `
enum Color { RED, GREEN, BLUE }
const color: Color = Color.RED;
`,
transformers: BASE_TRANSFORMERS
});
// Verify transformation results in tests
expect(testResult.code).toContain('var Color');
expect(testResult.requireStatementCollection).toHaveLength(0);import { initCommonTransform, BASE_TRANSFORMERS } from "fuse-box";
const result = initCommonTransform({
code: `
import React from 'react';
const Component = ({ title }: { title: string }) => {
return <div className="title">{title}</div>;
};
export default Component;
`,
jsx: true,
transformers: BASE_TRANSFORMERS,
compilerOptions: {
jsx: "react",
jsxFactory: "React.createElement"
}
});import { initCommonTransform, BASE_TRANSFORMERS } from "fuse-box";
const result = initCommonTransform({
code: sourceCode,
transformers: BASE_TRANSFORMERS,
props: {
fileAbsPath: "/path/to/file.ts",
packageName: "my-package",
target: "browser",
module: {
isEntry: true
},
compilerOptions: {
target: "ES2020",
module: "ESNext",
strict: true,
experimentalDecorators: true
}
}
});// Input TypeScript code
const tsCode = `
enum Status {
PENDING = "pending",
COMPLETED = "completed",
FAILED = "failed"
}
export { Status };
`;
const result = initCommonTransform({
code: tsCode,
transformers: BASE_TRANSFORMERS
});
// Outputs JavaScript with enum transformed to object// Input code with modern features
const modernCode = `
const user = data?.user?.profile;
const name = user?.name ?? "Unknown";
class UserService {
private cache = new Map();
async getUser(id: string) {
return this.cache.get(id) ?? await this.fetchUser(id);
}
}
`;
const result = initCommonTransform({
code: modernCode,
transformers: BASE_TRANSFORMERS,
compilerOptions: {
target: "ES5" // Transform to ES5 compatible code
}
});// Code with environment variables
const envCode = `
if (process.env.NODE_ENV === "development") {
console.log("Debug mode enabled");
}
const API_URL = process.env.API_URL || "https://api.example.com";
`;
const result = initCommonTransform({
code: envCode,
transformers: BASE_TRANSFORMERS,
props: {
target: "browser" // Will polyfill process.env for browser
}
});You can create custom transformers for specialized code transformations:
import { ITransformer, ASTNode } from "fuse-box";
const customTransformer: ITransformer = {
commonVisitors: (props) => ({
onEnter: (node: ASTNode) => {
// Custom transformation logic
if (node.type === "Identifier" && node.name === "OLD_API") {
node.name = "NEW_API";
}
}
})
};
const result = initCommonTransform({
code: sourceCode,
transformers: [...BASE_TRANSFORMERS, customTransformer]
});The transformation system is automatically integrated into FuseBox's build process:
import { fusebox } from "fuse-box";
const fuse = fusebox({
target: "browser",
entry: "src/index.ts",
compilerOptions: {
target: "ES2018",
jsx: "react",
experimentalDecorators: true
}
});
// Transformations are applied automatically during bundling
const result = await fuse.runProd();The transformation pipeline ensures that all TypeScript and modern JavaScript features are properly compiled for the target environment while maintaining source maps and preserving the original code structure where possible.