Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MJML5.0 Replace html-minifier and js-beautify #2858

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/mjml-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [ 16.x, 18.x, 20.x]
node-version: [20.x, 22.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Run linting & tests
run: |
run: |
yarn install
yarn build
yarn lint
Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
},
"npmClient": "yarn",
"useWorkspaces": true,
"version": "4.15.3"
"version": "5.0.0-alpha.6"
}
4 changes: 2 additions & 2 deletions packages/mjml-accordion/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mjml-accordion",
"description": "mjml-accordion",
"version": "4.15.3",
"version": "5.0.0-alpha.6",
"main": "lib/index.js",
"files": [
"lib"
Expand All @@ -23,7 +23,7 @@
"dependencies": {
"@babel/runtime": "^7.23.9",
"lodash": "^4.17.21",
"mjml-core": "4.15.3"
"mjml-core": "5.0.0-alpha.6"
},
"devDependencies": {
"@babel/cli": "^7.8.4",
Expand Down
4 changes: 2 additions & 2 deletions packages/mjml-body/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mjml-body",
"description": "mjml-body",
"version": "4.15.3",
"version": "5.0.0-alpha.6",
"main": "lib/index.js",
"files": [
"lib"
Expand All @@ -23,7 +23,7 @@
"dependencies": {
"@babel/runtime": "^7.23.9",
"lodash": "^4.17.21",
"mjml-core": "4.15.3"
"mjml-core": "5.0.0-alpha.6"
},
"devDependencies": {
"@babel/cli": "^7.8.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/mjml-browser/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mjml-browser",
"description": "MJML: the only framework that makes responsive-email easy",
"version": "4.15.3",
"version": "5.0.0-alpha.6",
"main": "lib/index.js",
"files": [
"lib"
Expand Down
4 changes: 2 additions & 2 deletions packages/mjml-button/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mjml-button",
"description": "mjml-button",
"version": "4.15.3",
"version": "5.0.0-alpha.6",
"main": "lib/index.js",
"files": [
"lib"
Expand All @@ -23,7 +23,7 @@
"dependencies": {
"@babel/runtime": "^7.23.9",
"lodash": "^4.17.21",
"mjml-core": "4.15.3"
"mjml-core": "5.0.0-alpha.6"
},
"devDependencies": {
"@babel/cli": "^7.8.4",
Expand Down
4 changes: 2 additions & 2 deletions packages/mjml-carousel/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mjml-carousel",
"description": "mjml-carousel",
"version": "4.15.3",
"version": "5.0.0-alpha.6",
"main": "lib/index.js",
"files": [
"lib"
Expand All @@ -23,7 +23,7 @@
"dependencies": {
"@babel/runtime": "^7.23.9",
"lodash": "^4.17.21",
"mjml-core": "4.15.3"
"mjml-core": "5.0.0-alpha.6"
},
"devDependencies": {
"@babel/cli": "^7.8.4",
Expand Down
11 changes: 4 additions & 7 deletions packages/mjml-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mjml-cli",
"description": "MJML: the only framework that makes responsive-email easy",
"version": "4.15.3",
"version": "5.0.0-alpha.6",
"main": "bin/mjml",
"bin": {
"mjml-cli": "bin/mjml"
Expand All @@ -28,14 +28,11 @@
"@babel/runtime": "^7.23.9",
"chokidar": "^3.0.0",
"glob": "^10.3.10",
"html-minifier": "^4.0.0",
"js-beautify": "^1.6.14",
"lodash": "^4.17.21",
"minimatch": "^9.0.3",
"mjml-core": "4.15.3",
"mjml-migrate": "4.15.3",
"mjml-parser-xml": "4.15.3",
"mjml-validator": "4.15.3",
"mjml-core": "5.0.0-alpha.6",
"mjml-parser-xml": "5.0.0-alpha.6",
"mjml-validator": "5.0.0-alpha.6",
"yargs": "^17.7.2"
},
"devDependencies": {
Expand Down
36 changes: 11 additions & 25 deletions packages/mjml-cli/src/client.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import path from 'path'
import yargs from 'yargs'
import { flow, pick, isNil, negate, pickBy } from 'lodash/fp'
import { isArray, isEmpty, map, get, omit } from 'lodash'
import { html as htmlBeautify } from 'js-beautify'
import { minify as htmlMinify } from 'html-minifier'
import { isArray, isEmpty, map, get } from 'lodash'

import mjml2html, { components, initializeType } from 'mjml-core'
import migrate from 'mjml-migrate'
import validate, { dependencies } from 'mjml-validator'
import MJMLParser from 'mjml-parser-xml'

Expand Down Expand Up @@ -60,11 +57,6 @@ export default async () => {
describe: 'Compile MJML File(s)',
type: 'array',
},
m: {
alias: 'migrate',
describe: 'Migrate MJML3 File(s) (deprecated)',
type: 'array',
},
v: {
alias: 'validate',
describe: 'Run validator on File(s)',
Expand Down Expand Up @@ -212,13 +204,11 @@ export default async () => {
const convertedStream = []
const failedStream = []

inputs.forEach((i) => {
// eslint-disable-next-line guard-for-in
for (const i of inputs) {
try {
let compiled
switch (inputOpt) {
case 'm':
compiled = { html: migrate(i.mjml, { beautify: true }) }
break
case 'v': // eslint-disable-next-line no-case-declarations
const mjmlJson = MJMLParser(i.mjml, {
components,
Expand All @@ -238,20 +228,16 @@ export default async () => {
const beautify = config.beautify && config.beautify !== 'false'
const minify = config.minify && config.minify !== 'false'

compiled = mjml2html(i.mjml, {
...omit(config, ['minify', 'beautify']),
// eslint-disable-next-line no-await-in-loop
compiled = await mjml2html(i.mjml, {
...config,
minify,
beautify,
beautifyConfig,
minifyConfig,
filePath: filePath || i.file,
actualPath: i.file,
})
if (beautify) {
compiled.html = htmlBeautify(compiled.html, beautifyConfig)
}
if (minify) {
compiled.html = htmlMinify(compiled.html, {
...minifyConfig,
...config.minifyOptions,
})
}
}
}

Expand All @@ -260,7 +246,7 @@ export default async () => {
EXIT_CODE = 2
failedStream.push({ file: i.file, error: e })
}
})
}

convertedStream.forEach((s) => {
if (get(s, 'compiled.errors.length')) {
Expand Down
22 changes: 6 additions & 16 deletions packages/mjml-cli/src/commands/watchFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import { match } from 'minimatch'
import path from 'path'
import mjml2html from 'mjml-core'
import { flow, pickBy, flatMap, uniq, difference, remove } from 'lodash/fp'
import { omit } from 'lodash'
import { html as htmlBeautify } from 'js-beautify'
import { minify as htmlMinify } from 'html-minifier'

import readFile from './readFile'
import makeOutputToFile from './outputToFile'
Expand Down Expand Up @@ -51,25 +48,18 @@ export default (input, options) => {
}
const readAndCompile = flow(
(file) => ({ file, content: readFile(file).mjml }),
(args) => {
const { config, beautifyConfig, minifyConfig } = options
async (args) => {
const { config } = options
const beautify = config.beautify && config.beautify !== 'false'
const minify = config.minify && config.minify !== 'false'

const compiled = mjml2html(args.content, {
const compiled = await mjml2html(args.content, {
...config,
beautify,
minify,
filePath: args.file,
actualPath: args.file,
...omit(config, ['minify', 'beautify']),
})
if (beautify) {
compiled.html = htmlBeautify(compiled.html, beautifyConfig)
}
if (minify) {
compiled.html = htmlMinify(compiled.html, {
...minifyConfig,
...config.minifyOptions,
})
}

return {
...args,
Expand Down
4 changes: 2 additions & 2 deletions packages/mjml-column/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mjml-column",
"description": "mjml-column",
"version": "4.15.3",
"version": "5.0.0-alpha.6",
"main": "lib/index.js",
"files": [
"lib"
Expand All @@ -23,7 +23,7 @@
"dependencies": {
"@babel/runtime": "^7.23.9",
"lodash": "^4.17.21",
"mjml-core": "4.15.3"
"mjml-core": "5.0.0-alpha.6"
},
"devDependencies": {
"@babel/cli": "^7.8.4",
Expand Down
14 changes: 7 additions & 7 deletions packages/mjml-core/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mjml-core",
"description": "mjml-core",
"version": "4.15.3",
"version": "5.0.0-alpha.6",
"main": "lib/index.js",
"files": [
"lib"
Expand All @@ -25,13 +25,13 @@
"@babel/runtime": "^7.23.9",
"cheerio": "1.0.0-rc.12",
"detect-node": "^2.0.4",
"html-minifier": "^4.0.0",
"js-beautify": "^1.6.14",
"juice": "^10.0.0",
"html-crush": "^4.2.0",
"juice": "^11.0.0",
"lodash": "^4.17.21",
"mjml-migrate": "4.15.3",
"mjml-parser-xml": "4.15.3",
"mjml-validator": "4.15.3"
"mjml-parser-xml": "5.0.0-alpha.6",
"mjml-validator": "5.0.0-alpha.6",
"postcss": "^8.4.33",
"prettier": "^3.2.4"
},
"devDependencies": {
"@babel/cli": "^7.8.4",
Expand Down
36 changes: 8 additions & 28 deletions packages/mjml-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@
} from 'lodash'
import path from 'path'
import juice from 'juice'
import { html as htmlBeautify } from 'js-beautify'
import { minify as htmlMinify } from 'html-minifier'
import { load } from 'cheerio'
import prettier from 'prettier'
import { crush } from 'html-crush'

import MJMLParser from 'mjml-parser-xml'
import MJMLValidator, {
dependencies as globalDependencies,
assignDependencies,
} from 'mjml-validator'
import { handleMjml3 } from 'mjml-migrate'

import { initComponent } from './createComponent'
import globalComponents, {
Expand Down Expand Up @@ -51,7 +50,7 @@
}
}

export default function mjml2html(mjml, options = {}) {
export default async function mjml2html(mjml, options = {}) {
let content = ''
let errors = []

Expand Down Expand Up @@ -110,7 +109,7 @@
},
keepComments,
minify = false,
minifyOptions = {},

Check failure on line 112 in packages/mjml-core/src/index.js

View workflow job for this annotation

GitHub Actions / build (20.x)

'minifyOptions' is assigned a value but never used

Check failure on line 112 in packages/mjml-core/src/index.js

View workflow job for this annotation

GitHub Actions / build (22.x)

'minifyOptions' is assigned a value but never used

Check failure on line 112 in packages/mjml-core/src/index.js

View workflow job for this annotation

GitHub Actions / build (20.x)

'minifyOptions' is assigned a value but never used

Check failure on line 112 in packages/mjml-core/src/index.js

View workflow job for this annotation

GitHub Actions / build (22.x)

'minifyOptions' is assigned a value but never used
ignoreIncludes = false,
juiceOptions = {},
juicePreserveTags = null,
Expand All @@ -118,7 +117,6 @@
validationLevel = 'soft',
filePath = '.',
actualPath = '.',
noMigrateWarn = false,
preprocessors,
presets = [],
printerSupport = false,
Expand Down Expand Up @@ -148,8 +146,6 @@
})
}

mjml = handleMjml3(mjml, { noMigrateWarn })

const globalData = {
backgroundColor: '',
beforeDoctype: '',
Expand Down Expand Up @@ -397,31 +393,15 @@
content = mergeOutlookConditionnals(content)

if (beautify) {
// eslint-disable-next-line no-console
console.warn(
'"beautify" option is deprecated in mjml-core and only available in mjml cli.',
)
content = htmlBeautify(content, {
indent_size: 2,
wrap_attributes_indent_size: 2,
max_preserve_newline: 0,
preserve_newlines: false,
content = await prettier.format(content, {
parser: 'html',
printWidth: 240,
singleQuote: true,
})
}

if (minify) {
// eslint-disable-next-line no-console
console.warn(
'"minify" option is deprecated in mjml-core and only available in mjml cli.',
)

content = htmlMinify(content, {
collapseWhitespace: true,
minifyCSS: false,
caseSensitive: true,
removeEmptyAttributes: true,
...minifyOptions,
})
content = crush(content, { ...options.minifyOptions }).result
}

return {
Expand Down
Loading
Loading