|
| 1 | +import { Sequence } from './sequence'; |
| 2 | +import { createDocument } from './document'; |
| 3 | +import { TaskManager } from './task-manager'; |
| 4 | +import { Context, ComposeOption } from './types'; |
| 5 | + |
| 6 | +const DEFAULT_COMPOSE_OPTIONS: ComposeOption = { |
| 7 | + minifyIds: true, |
| 8 | + minifyClasses: true, |
| 9 | + minifyDatasets: true, |
| 10 | + minifyStyles: true, |
| 11 | + removeUnusedAttrs: false, |
| 12 | +}; |
| 13 | +/** |
| 14 | + * Minify an email body |
| 15 | + * @example |
| 16 | + * const minifier = new EmailMinifier('<html>...</html>'); |
| 17 | + * const result = await minifier.minify(); |
| 18 | + * console.log(result.minified); |
| 19 | + */ |
| 20 | +export class EmailMinifier { |
| 21 | + private emailBody: string; |
| 22 | + |
| 23 | + constructor(emailBody: string) { |
| 24 | + this.emailBody = emailBody; |
| 25 | + } |
| 26 | + /** |
| 27 | + * Compose the minify tasks |
| 28 | + */ |
| 29 | + private compose = ( |
| 30 | + context: Context, |
| 31 | + options?: ComposeOption, |
| 32 | + ): TaskManager => { |
| 33 | + const taskManager = new TaskManager(); |
| 34 | + const { |
| 35 | + minifyIds, |
| 36 | + minifyClasses, |
| 37 | + minifyDatasets, |
| 38 | + minifyStyles, |
| 39 | + removeUnusedAttrs, |
| 40 | + } = { ...DEFAULT_COMPOSE_OPTIONS, ...(options ?? {}) }; |
| 41 | + |
| 42 | + const { document } = context; |
| 43 | + |
| 44 | + document.body.querySelectorAll('*').forEach((el) => { |
| 45 | + minifyIds && taskManager.add('minify-ids', el, context); |
| 46 | + minifyClasses && taskManager.add('minify-classes', el, context); |
| 47 | + removeUnusedAttrs && |
| 48 | + taskManager.add('remove-unused-attrs', el, context, { |
| 49 | + matches: removeUnusedAttrs, |
| 50 | + }); |
| 51 | + minifyDatasets && taskManager.add('minify-dataset-attrs', el, context); |
| 52 | + }); |
| 53 | + document.head.querySelectorAll('style').forEach((el) => { |
| 54 | + taskManager.add('minify-styles', el, context, { |
| 55 | + minifyStyles, |
| 56 | + }); |
| 57 | + }); |
| 58 | + return taskManager; |
| 59 | + }; |
| 60 | + |
| 61 | + private async createContext(): Promise<Context> { |
| 62 | + const { emailBody } = this; |
| 63 | + const document = await createDocument(emailBody); |
| 64 | + return { |
| 65 | + originalEmailBody: emailBody, |
| 66 | + styles: Array.from(document.head.querySelectorAll('style')) |
| 67 | + .map((el) => el.textContent) |
| 68 | + .join('\n'), |
| 69 | + document, |
| 70 | + idSequence: new Sequence(), |
| 71 | + classSequence: new Sequence(), |
| 72 | + mapping: { |
| 73 | + ids: new Map<string, string>(), |
| 74 | + classes: new Map<string, string>(), |
| 75 | + dataSets: new Map<string, string>(), |
| 76 | + }, |
| 77 | + }; |
| 78 | + } |
| 79 | + |
| 80 | + async minify(options?: ComposeOption) { |
| 81 | + const context = await this.createContext(); |
| 82 | + const taskManager = this.compose(context, options); |
| 83 | + const { originalEmailBody, document } = context; |
| 84 | + const tasks = taskManager.runAll(); |
| 85 | + |
| 86 | + if (tasks.length === 0) { |
| 87 | + return { |
| 88 | + original: originalEmailBody, |
| 89 | + minified: null, |
| 90 | + tasks, |
| 91 | + }; |
| 92 | + } |
| 93 | + |
| 94 | + return { |
| 95 | + original: originalEmailBody, |
| 96 | + minified: document.documentElement.outerHTML, |
| 97 | + tasks, |
| 98 | + }; |
| 99 | + } |
| 100 | +} |
0 commit comments