Skip to content

Commit

Permalink
fix: terser minify in mdx schema (#38)
Browse files Browse the repository at this point in the history
* Replaced naive minify with terser minify in mdx schema

* Fix type mismatch error by ensuring return value is always a string
  • Loading branch information
afadlallah authored Feb 14, 2024
1 parent e1602b5 commit 1e0cc0c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 11 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"rollup": "^4.10.0",
"rollup-plugin-dts": "^6.1.0",
"rollup-plugin-esbuild": "^6.1.1",
"terser": "^5.27.0",
"typescript": "^5.3.3",
"unified": "^11.0.4",
"unist-util-visit": "^5.0.0",
Expand Down
52 changes: 47 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 13 additions & 6 deletions src/schemas/mdx.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { compile } from '@mdx-js/mdx'
import { minify } from 'terser'
import remarkGfm from 'remark-gfm'
import { visit } from 'unist-util-visit'

Expand Down Expand Up @@ -41,12 +42,18 @@ export const mdx = (options: MdxOptions = {}) =>

try {
const code = await compile({ value, path: file.path }, compilerOptions)
// TODO: minify output
return code
.toString()
.replace(/^"use strict";/, '')
.replace(/\s+/g, ' ')
.trim()
const minified = await minify(code.toString(), {
compress: true,
keep_classnames: true,
mangle: {
keep_fnames: true,
},
module: true,
parse: {
bare_returns: true,
},
})
return minified.code || ''
} catch (err: any) {
addIssue({ code: 'custom', message: err.message })
return value
Expand Down

0 comments on commit 1e0cc0c

Please sign in to comment.