Skip to content

Commit a4374c1

Browse files
authored
Feat: Add test templates and write performance data (#5)
* Feat: Add test templates and write performance data
1 parent b7f3965 commit a4374c1

File tree

7 files changed

+2469
-5
lines changed

7 files changed

+2469
-5
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,14 @@ const options = {
9595

9696

9797
## Performance
98-
*TODO: Find some emails, add them to a the project, then minify and list here. Welcome to submit a PR*
9998

100-
| Email | Original Size | Minified Size | Elapsed time |
101-
|--------------------------------|-----------------|---------|------|
102-
|[Example 1](examples/emails/example1.txt)| 0kb | 0kb | 0ms |
99+
The following table shows the statistics in the Node.js environment
100+
| Email | Original Size | Minified Size | Elapsed time |
101+
| ----------------------------- | ------------- | ------------- | ------------ |
102+
|[Holiday Cheer]("examples%2Ftemplates%2FHoliday%20Cheer")|33.09kb|32.36kb|532.77ms|
103+
|[Membership Discount]("examples%2Ftemplates%2FMembership%20Discount")|104.00kb|37.97kb|94.75ms|
104+
|[Movies for Christmas]("examples%2Ftemplates%2FMovies%20for%20Christmas")|289.47kb|58.30kb|138.87ms|
105+
> The emails above are generated from [unlayer](https://dashboard.unlayer.com).
103106
104107
## License
105108
See [LICENSE](./LICENSE)

examples/templates/Holiday Cheer

Lines changed: 626 additions & 0 deletions
Large diffs are not rendered by default.

examples/templates/Membership Discount

Lines changed: 684 additions & 0 deletions
Large diffs are not rendered by default.

examples/templates/Movies for Christmas

Lines changed: 1080 additions & 0 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "email-minifier",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"keywords": [
55
"compress",
66
"compressor",
@@ -40,6 +40,7 @@
4040
}
4141
},
4242
"scripts": {
43+
"performance": "ts-node ./scripts/performance.ts",
4344
"lint": "npx eslint . --fix",
4445
"build": "npm run lint && rm -rf dist && rollup --config --bundleConfigAsCjs",
4546
"test": "npm run lint && jest",

scripts/performance.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
import { EmailMinifier } from '../lib/email-minifier';
4+
import { performance } from 'perf_hooks';
5+
6+
const source = 'https://dashboard.unlayer.com';
7+
const dir = 'examples/templates';
8+
9+
(async () => {
10+
const examples = [];
11+
const filenames = fs.readdirSync(dir);
12+
for (const filename of filenames) {
13+
const file = path.join(dir, filename);
14+
if (fs.statSync(file).isFile()) {
15+
const html = fs.readFileSync(file, 'utf-8');
16+
const t1 = performance.now();
17+
const { original, minified } = await new EmailMinifier(html).minify();
18+
const t2 = performance.now();
19+
examples.push({
20+
filename,
21+
url: file,
22+
originalSize: original.length / 1024,
23+
minifiedSize: minified.length / 1024,
24+
elapsed: t2 - t1,
25+
});
26+
}
27+
}
28+
const content = examples
29+
.sort((a, b) => a.originalSize - b.originalSize)
30+
.map(
31+
(example) =>
32+
`|[${example.filename}]("${encodeURIComponent(
33+
example.url,
34+
)}")|${example.originalSize.toFixed(
35+
2,
36+
)}kb|${example.minifiedSize.toFixed(2)}kb|${example.elapsed.toFixed(
37+
2,
38+
)}ms|`,
39+
)
40+
.join(`\n`);
41+
const sectionContent = [
42+
'The following table shows the statistics in the Node.js environment',
43+
'| Email | Original Size | Minified Size | Elapsed time |',
44+
'| ----------------------------- | ------------- | ------------- | ------------ |',
45+
content,
46+
`> The emails above are generated from [unlayer](${source}).`,
47+
].join('\n');
48+
49+
const readMe = fs.readFileSync('README.md', 'utf-8');
50+
const startMark = '## Performance';
51+
const endMark = '## License';
52+
const start = readMe.indexOf(startMark);
53+
const end = readMe.indexOf(endMark);
54+
const newReadMe =
55+
readMe.substring(0, start + startMark.length) +
56+
'\n\n' +
57+
sectionContent +
58+
'\n\n' +
59+
readMe.substring(end);
60+
readMe.substring(end);
61+
fs.writeFileSync('README.md', newReadMe, 'utf-8');
62+
})();

scripts/tsconfig.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"compilerOptions": {
3+
"esModuleInterop": true,
4+
"lib": [
5+
"ESNext"
6+
]
7+
}
8+
}

0 commit comments

Comments
 (0)