Skip to content

ramasilveyra/mjml-loader

Repository files navigation


MJML loader for webpack

Converts MJML files to HTML and exports them as string.

Table of Contents

Install

Node.js v6.5 or newer is required.

webpack 2 or newer is required.

Via the yarn client:

$ yarn add --dev mjml-loader webpack

Via the npm client:

$ npm install --save-dev mjml-loader webpack

Usage

Look into the official webpack loaders usage documentation.

At this moment this loader has no options.

Examples

Compile MJML file to HTML file

For transforming a MJML file to a HTML file is better to use mjml cli or gulp-mjml.

Inline the MJML result (HTML) into bundle

With this configuration:

{
  // ...
  module: {
    rules: [
      { test: /\.mjml$/, loader: 'mjml-loader' }
    ]
  }
  // ...
}

With this MJML file:

template.mjml

 <mjml>
  <mj-body>
    <mj-container>
      <mj-section>
        <mj-column>
          <mj-text>Hello World!</mj-text>
        </mj-column>
      </mj-section>
    </mj-container>
  </mj-body>
</mjml>

This entry point:

index.js

import template from './template.mjml';
console.log(template);

The console output will be:

<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<style type="text/css">
  #outlook a { padding: 0; }
  .ReadMsgBody { width: 100%; }
  .ExternalClass { width: 100%; }
  .ExternalClass * { line-height:100%; }
  body { margin: 0; padding: 0; -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; }
  table, td { border-collapse:collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; }
  img { border: 0; height: auto; line-height: 100%; outline: none; text-decoration: none; -ms-interpolation-mode: bicubic; }
  p { display: block; margin: 13px 0; }
</style>
<!--[if !mso]><!-->
<style type="text/css">
  @import url(https://fonts.googleapis.com/css?family=Ubuntu:300,400,500,700);
</style>
<style type="text/css">
  @media only screen and (max-width:480px) {
    @-ms-viewport { width:320px; }
    @viewport { width:320px; }
  }
</style>
<link href="https://fonts.googleapis.com/css?family=Ubuntu:300,400,500,700" rel="stylesheet" type="text/css">
<!--<![endif]-->
<style type="text/css">
  @media only screen and (min-width:480px) {
    .mj-column-per-100, * [aria-labelledby="mj-column-per-100"] { width:100%!important; }
  }
</style>
</head>
<body>
  <div><!--[if mso]>
      <table border="0" cellpadding="0" cellspacing="0" width="600" align="center" style="width:600px;"><tr><td>
      <![endif]--><div style="margin:0 auto;max-width:600px;"><table cellpadding="0" cellspacing="0" style="font-size:0;width:100%;" align="center" border="0"><tbody><tr><td style="text-align:center;vertical-align:top;font-size:0;padding:20px 0;"><!--[if mso]>
      <table border="0" cellpadding="0" cellspacing="0"><tr><td style="vertical-align:top;width:600px;">
      <![endif]--><div aria-labelledby="mj-column-per-100" class="mj-column-per-100" style="vertical-align:top;display:inline-block;font-size:13px;text-align:left;width:100%;"><table cellpadding="0" cellspacing="0" width="100%" border="0"><tbody><tr><td style="word-break:break-word;font-size:0;padding:10px 25px;" align="left"><div style="cursor:auto;color:#000000;font-family:Ubuntu, Helvetica, Arial, sans-serif;font-size:13px;line-height:22px;">Hello World!</div></td></tr></tbody></table></div><!--[if mso]>
      </td></tr></table>
      <![endif]--></td></tr></tbody></table></div><!--[if mso]>
      </td></tr></table>
      <![endif]-->
      <!--[if mso]>
      <table border="0" cellpadding="0" cellspacing="0" width="600" align="center" style="width:600px;"><tr><td>
      <![endif]--><div></div><!--[if mso]>
      </td></tr></table>
      <![endif]--></div>
</body>
</html>

Contribute

Feel free to dive in! Open an issue or submit PRs.

mjml-loader follows the Contributor Covenant Code of Conduct.

License

MIT © Ramiro Silveyra d'Avila