If you want to dive right in, check out the Mailchimp Starter.
Requirements
Mailchimp requires that the zip archive contains the HTML file and all its images in the same folder. For example:Project setup
We’re starting from scratch, so let’s scaffold a new project using the Official Starter:./mailchimp-project, and select the Default Starter.
Choose Yes when prompted to Install dependencies.
Once it finishes installing dependencies, open the project folder in your favorite editor.
Structure
We’ll be organizing our templates into folders insidetemplates:
Create a template
For this written guide, we’ll be using a simplified template with a few images. See the Mailchimp Starter for a more extensive example. Createemails/template-1/index.html and paste in the following code:
emails/template-1/index.html
maizzle.png and tailwindcss.jpg images to the same folder.
Production config
This is where the magic happens. Our strategy is as follows:- for each template, create a list of the images it uses
- push that list along with some data about the template file to a queue
- after all templates have been compiled, process the queue and create the .zip archives
config.production.js to look like this:
config.production.js
static: false because we don’t want Maizzle to copy the global images folder to the dist folder. We’ll handle any global images ourselves.
Get image paths from HTML
We’ll need a way of creating a list of images that are used in a template. Createutils/getImagePaths.js and paste in the following code:
utils/getImagePaths.js
srcattributessrcsetattributesposterattributesbackgroundattributes- CSS inside
<style>tags in the<head> - inline
backgroundandbackground-imageCSS
Archiving library
There are a few libraries that can create .zip archives, but we’ll be using archiver for this guide. Install it now:Add to the queue
Let’s use theafterTransformers event to push information about each template and the images it uses to the queue variable that we defined earlier.
Modify your config.production.js to look like this:
config.production.js
Create the .zip archives
We can now process the queue and create the .zip archive for each template. We’ll use theafterBuild event for this, which is triggered after all templates have been compiled.
Modify your config.production.js to look like this:
config.production.js
Build the templates
Running thenpm run build command will now create a .zip archive for each template in the dist directory.
The archive file will have the same name as the template, so you’ll see something like this:
insignia.png has been added to both archives, even though none of the template folders include it.
Resources
- GitHub repository for this guide
- archiver library documentation