Compiling an MJML custom component. This repo builds on MJML's custom component boilerplate by demonstrating how to use the component once you've compiled it.
Brought to you by Premail.
The boilerplate repo uses a specific workflow with Babel, and older versions of MJML, but your own build's workflow doesn't need to match.
To that end: The example in this repo (Stage 2) uses the modern pipeline()
version of Gulp workflows (unlike the boilerplate example), but does not use Babel.
- In a separate folder, clone https://github.com/mjmlio/mjml-component-boilerplate
- Run
npm install
in that folder. - Create a new component, or use the existing examples.
- Run
gulp build
. - Note the compiled component file in
./lib
.
- Clone this repo.
- Run
npm install
in this folder. - Copy the compiled component from Stage 1 into
./components
. - Require and register the component in the gulp file:
// Register custom MJML components
const { registerComponent } = require('mjml-core')
const MjBasicComponent = require('./components/MjBasicComponent.js').default
registerComponent(MjBasicComponent)
- Require and register the validation (this seems to be necessary in addition to defining it in the component file itself):
const { registerDependencies } = require('mjml-validator')
registerDependencies({
// Tell the validator which tags are allowed as our component's parent
'mj-hero': ['mj-basic-component'],
'mj-column': ['mj-basic-component'],
// Tell the validator which tags are allowed as our component's children
'mj-basic-component': []
})
- Edit
./src/index.mjml
to include the custom component. - Run
gulp
to create the compiled HTML in./dist
. - Using this as an example, create and use your own custom MJML components!
- If you want to contribute your custom component back, add an
.mjmlconfig
file to your project (here's an example) and publish it to npm! Then suggest the MJML folks add it to their list of community components.
GNU Affero General Public License v3.0 or later. For the legal details of the AGPL, see the complete LICENSE.