Skip to content

Commit 152577d

Browse files
committed
bugfix(mjml-browser): minification
- added html-minifier-terser (exc. terser) and associated dependencies - created aliases for htmlnano and terser - created minify.js file to process minification - removed alias for mjml-migrate
1 parent b5c9f66 commit 152577d

File tree

6 files changed

+223
-13
lines changed

6 files changed

+223
-13
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = function empty() {}
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
export default {
2-
async process(html) {
1+
// eslint-disable-next-line import/no-unresolved
2+
const minifyMock = require('minify')
3+
4+
module.exports = {
5+
async process(html, options = {}) {
6+
const minified = await minifyMock(html, options)
37
return {
4-
html,
8+
html: minified.result,
59
}
610
},
711
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const { minify } = require('html-minifier-terser')
2+
3+
module.exports = async (html, options = {}) => {
4+
5+
try {
6+
const safeOptions = {
7+
...options,
8+
minifyCSS: options.minifyCSS !== undefined ? options.minifyCSS : true,
9+
minifyJS: false,
10+
}
11+
12+
const defaultOptions = {
13+
collapseWhitespace: true,
14+
removeComments: true,
15+
removeOptionalTags: false,
16+
removeRedundantAttributes: true,
17+
removeScriptTypeAttributes: true,
18+
removeTagWhitespace: false,
19+
useShortDoctype: true,
20+
...safeOptions,
21+
}
22+
23+
const result = await minify(html, defaultOptions)
24+
25+
return {
26+
result,
27+
}
28+
} catch (e) {
29+
return {
30+
result: html,
31+
}
32+
}
33+
}

packages/mjml-browser/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,11 @@
3333
"uglifyjs-webpack-plugin": "^2.1.3",
3434
"webpack": "^4.36.1",
3535
"webpack-cli": "^3.3.6"
36+
},
37+
"dependencies": {
38+
"buffer": "^6.0.3",
39+
"html-minifier-terser": "^7.2.0",
40+
"path-browserify": "^1.0.1",
41+
"process": "^0.11.10"
3642
}
3743
}

packages/mjml-browser/webpack.config.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const path = require('path')
2+
const webpack = require('webpack')
23
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
34

45
module.exports = {
@@ -13,13 +14,27 @@ module.exports = {
1314
},
1415
resolve: {
1516
alias: {
16-
'path': path.resolve(__dirname, 'browser-mocks/path'),
17+
'path': 'path-browserify',
1718
'fs': path.resolve(__dirname, 'browser-mocks/fs'),
1819
'uglify-js': path.resolve(__dirname, 'browser-mocks/uglify-js'),
19-
'mjml-migrate': path.resolve(__dirname, 'browser-mocks/mjml-migrate'),
20+
'minify': path.resolve(__dirname, 'browser-mocks/minify'),
2021
'htmlnano': path.resolve(__dirname, 'browser-mocks/htmlnano'),
22+
'terser': path.resolve(__dirname, 'browser-mocks/empty'),
23+
'os': 'os-browserify/browser',
2124
},
2225
},
26+
plugins: [
27+
new webpack.ProvidePlugin({
28+
process: 'process/browser',
29+
Buffer: ['buffer', 'Buffer'],
30+
}),
31+
],
32+
node: {
33+
fs: 'empty',
34+
global: true,
35+
process: true,
36+
Buffer: true,
37+
},
2338
optimization: {
2439
minimizer: [
2540
new UglifyJsPlugin({
@@ -49,9 +64,17 @@ module.exports = {
4964
},
5065
module: {
5166
rules: [
67+
{
68+
test: /\.mjs$/,
69+
include: /node_modules/,
70+
type: 'javascript/auto',
71+
},
5272
{
5373
test: /\.js$/,
54-
exclude: path.join(__dirname, 'node_modules'),
74+
exclude: [
75+
path.join(__dirname, 'node_modules'),
76+
/html-minifier-terser/,
77+
],
5578
use: [
5679
{
5780
loader: 'babel-loader',

0 commit comments

Comments
 (0)