or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-mjml-browser

Browser-compatible version of MJML framework for client-side responsive email generation

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/mjml-browser@4.15.x

To install, run

npx @tessl/cli install tessl/npm-mjml-browser@4.15.0

index.mddocs/

MJML Browser

MJML Browser is a client-side version of MJML (Mailjet Markup Language) that enables responsive email template generation directly in web browsers. It provides the full MJML compilation engine as a UMD bundle, making it possible to convert MJML markup to responsive HTML without server-side processing.

Package Information

  • Package Name: mjml-browser
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install mjml-browser

Core Imports

var mjml2html = require('mjml-browser');

UMD/Browser Global:

// Available as global 'mjml' when loaded via script tag
var result = mjml(mjmlString, options);

ES6 (if using with bundler):

import mjml2html from 'mjml-browser';

Basic Usage

var mjml2html = require('mjml-browser');

// Simple usage
var mjmlMarkup = `
<mjml>
  <mj-body>
    <mj-section>
      <mj-column>
        <mj-text>Hello World!</mj-text>
      </mj-column>
    </mj-section>
  </mj-body>
</mjml>
`;

var result = mjml2html(mjmlMarkup);
console.log(result.html); // Generated responsive HTML
console.log(result.errors); // Validation errors/warnings

Architecture

MJML Browser is built around several key architectural components that work together to transform MJML markup into responsive HTML:

  • MJML Parser: Parses MJML markup string into a structured JSON representation, handling components and attributes
  • Component System: Registry of available MJML components (mj-body, mj-section, mj-text, etc.) with rendering logic
  • Validation Engine: Validates MJML structure and component usage according to MJML rules and dependencies
  • Rendering Pipeline: Processes parsed MJML through component renderers to generate HTML output
  • CSS Processing: Handles CSS inlining, media queries, and responsive breakpoints for email client compatibility
  • Browser Compilation: Webpack bundle that packages the full MJML engine for browser environments with Node.js module mocks

The browser version maintains the same component architecture as the Node.js version but replaces filesystem operations with mocked implementations, enabling client-side email template generation while preserving full MJML functionality.

Capabilities

MJML to HTML Conversion

Core function that converts MJML markup into responsive HTML email templates.

/**
 * Converts MJML markup to responsive HTML
 * @param {string|object} mjml - MJML markup string or parsed MJML object
 * @param {object} [options={}] - Configuration options
 * @returns {object} Result object with html, json, and errors
 */
function mjml2html(mjml, options);

interface MjmlResult {
  html: string;      // Generated responsive HTML
  json: object;      // Parsed MJML object representation
  errors: Array<ValidationError>; // Validation errors and warnings
}

interface ValidationError {
  line: number;
  message: string;
  tagName: string;
  formattedMessage: string;
}

Configuration Options

Comprehensive options for customizing MJML compilation behavior.

interface MjmlOptions {
  /** Format HTML output (deprecated, use external tool) */
  beautify?: boolean;
  
  /** Custom font definitions with URLs */
  fonts?: {
    [fontName: string]: string;
  };
  
  /** Preserve HTML comments in output */
  keepComments?: boolean;
  
  /** Minify HTML output (deprecated, use external tool) */
  minify?: boolean;
  
  /** HTML minification options when minify is true */
  minifyOptions?: object;
  
  /** Ignore mj-include tags (always true in browser) */
  ignoreIncludes?: boolean;
  
  /** Options for CSS inlining with juice */
  juiceOptions?: object;
  
  /** Tags to preserve during CSS inlining */
  juicePreserveTags?: {
    [tagName: string]: any;
  };
  
  /** Custom HTML skeleton template function or string path */
  skeleton?: Function | string;
  
  /** Validation strictness: 'skip', 'soft', 'strict' */
  validationLevel?: 'skip' | 'soft' | 'strict';
  
  /** File path for error reporting */
  filePath?: string;
  
  /** Actual file path for includes (not functional in browser) */
  actualPath?: string;
  
  /** Suppress MJML v3 migration warnings */
  noMigrateWarn?: boolean;
  
  /** Custom preprocessor functions */
  preprocessors?: Array<Function>;
  
  /** Component and dependency presets */
  presets?: Array<{
    components: object;
    dependencies: object;
  }>;
  
  /** Add printer-friendly styles */
  printerSupport?: boolean;
}

Component System Integration

Access to MJML's component system for advanced usage.

/** Registry of available MJML components */
var components: object;

/**
 * Initialize a component instance
 * @param {object} config - Component configuration
 * @returns {object|null} Component instance or null
 */
function initComponent(config);

/**
 * Register a custom component
 * @param {object} component - Component definition
 */
function registerComponent(component);

/**
 * Assign components to component registry
 * @param {object} target - Target component registry
 * @param {object} source - Source components to assign
 */
function assignComponents(target, source);

Utility Functions

Helper functions for CSS and component manipulation.

/**
 * Create responsive breakpoint CSS
 * @param {string} breakpoint - Breakpoint value
 * @returns {string} CSS media query
 */
function makeLowerBreakpoint(breakpoint);

/**
 * Add suffix to CSS class names
 * @param {string} classes - CSS classes string
 * @param {string} suffix - Suffix to add
 * @returns {string} Modified CSS classes
 */
function suffixCssClasses(classes, suffix);

/**
 * Initialize MJML component types
 * @param {object} type - Type definition
 * @returns {object} Initialized type
 */
function initializeType(type);

Component Base Classes

Base classes for creating custom MJML components.

/**
 * Base class for MJML body components
 */
class BodyComponent {
  constructor(initialDatas);
  render();
}

/**
 * Base class for MJML head components  
 */
class HeadComponent {
  constructor(initialDatas);
  handler();
}

Error Handling

Error class for validation failures in strict mode.

/**
 * Error thrown when validation fails in strict mode
 */
class ValidationError extends Error {
  constructor(message, errors);
  
  /** Array of detailed validation errors */
  errors: Array<object>;
}

Default Configuration

Built-in Fonts

{
  'Open Sans': 'https://fonts.googleapis.com/css?family=Open+Sans:300,400,500,700',
  'Droid Sans': 'https://fonts.googleapis.com/css?family=Droid+Sans:300,400,500,700',
  'Lato': 'https://fonts.googleapis.com/css?family=Lato:300,400,500,700',
  'Roboto': 'https://fonts.googleapis.com/css?family=Roboto:300,400,500,700',
  'Ubuntu': 'https://fonts.googleapis.com/css?family=Ubuntu:300,400,500,700'
}

Default Options

  • validationLevel: 'soft' (returns errors without throwing)
  • breakpoint: '480px'
  • ignoreIncludes: true (mj-include tags are not supported)
  • beautify: false
  • minify: false

Browser Limitations

The browser version of MJML provides nearly identical functionality to the Node.js version but with specific limitations due to the browser security model and environment constraints:

Unavailable Features

  • mj-include tags: Cannot include external MJML files since browsers don't have filesystem access
  • .mjmlconfig files: Configuration files cannot be loaded from the filesystem
  • Custom component loading: External component files cannot be dynamically loaded
  • File system operations: All Node.js fs module calls are replaced with browser-compatible mocks

Behavioral Differences

  • ignoreIncludes option: Always behaves as true regardless of setting
  • filePath/actualPath options: Used only for error reporting, not file resolution
  • Custom skeleton templates: String paths to skeleton files are not supported (function templates still work)
  • Component registration: Must be done programmatically at runtime rather than via configuration files

What Still Works

  • Full MJML component library: All standard MJML components (mj-text, mj-button, mj-section, etc.)
  • Custom fonts: Via URL-based font definitions in options
  • Validation: Complete MJML validation with all strictness levels
  • CSS processing: Full CSS inlining and responsive breakpoint handling
  • Component customization: Programmatic component registration and configuration

Advanced Usage Examples

Custom Fonts Configuration

var result = mjml2html(mjmlString, {
  fonts: {
    'Custom Font': 'https://fonts.example.com/custom-font.css'
  }
});

Strict Validation Mode

try {
  var result = mjml2html(mjmlString, {
    validationLevel: 'strict'
  });
} catch (error) {
  if (error instanceof ValidationError) {
    console.log('Validation errors:', error.errors);
  }
}

CSS Inlining Options

var result = mjml2html(mjmlString, {
  juiceOptions: {
    preserveImportant: true,
    removeStyleTags: false
  }
});

Component Registration

// Register a custom component
registerComponent({
  name: 'mj-custom',
  handler: function() {
    return '<div>Custom content</div>';
  }
});

// Use the custom component
var mjmlWithCustom = `
<mjml>
  <mj-body>
    <mj-section>
      <mj-column>
        <mj-custom></mj-custom>
      </mj-column>
    </mj-section>
  </mj-body>
</mjml>
`;