-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
85 lines (65 loc) · 1.91 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const mix = require('laravel-mix');
const File = require('laravel-mix/src/File')
const glob = require('glob');
const juice = require("juice");
const { html: beautifyHTML } = require('js-beautify');
// email-comb is imported with a dynamic import inside register()
class Email {
register(config = {}) {
const {
enabled = mix.inProduction(),
source = 'dist',
inlineCss: juiceConfig = { enabled: false },
optimize: combConfig = { enabled: false },
beautify: beautifyConfig = { enabled: false }
} = config;
if(!enabled || (!juiceConfig.enabled && !combConfig.enabled && !beautifyConfig.enabled)) {
return;
}
// Run tasks after Webpack compiles
// https://laravel-mix.com/docs/6.0/event-hooks#run-a-function-after-webpack-compiles
mix.after(async () => {
const filePaths = glob.sync(source);
if(combConfig.enabled) {
const {comb} = await import("email-comb");
this.comb = comb;
}
filePaths.forEach(async (filePath) => {
let file = new File(filePath);
let processedHTML = file.read();
if(juiceConfig.enabled) {
processedHTML = await this.juice(processedHTML, juiceConfig);
}
if(combConfig.enabled) {
processedHTML = await this.emailComb(processedHTML, combConfig);
}
if(beautifyConfig.enabled) {
processedHTML = await this.beautify(processedHTML, beautifyConfig);
}
file.write(processedHTML)
})
});
}
juice(html, config) {
return new Promise((resolve, reject) => {
juice.juiceResources(html, config, (error, processedHTML) => {
if (error) {
console.log(error);
reject(error);
}
resolve(processedHTML)
})
})
}
emailComb(html, config) {
return new Promise(async (resolve, reject) => {
resolve(this.comb(html, config).result)
})
}
beautify(html, config) {
return new Promise(async (resolve, reject) => {
resolve(beautifyHTML(html, config));
})
}
}
mix.extend('email', new Email());