Skip to content

Commit

Permalink
Swap JS minifier from uglify to terser
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrotherham committed Jan 16, 2023
1 parent 04a6669 commit 04bd6d1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 52 deletions.
4 changes: 3 additions & 1 deletion docs/examples/webpack/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ module.exports = ({ WEBPACK_SERVE }, { mode }) => ({
minimizer: [new TerserPlugin({
extractComments: true,
terserOptions: {
ecma: 5,
format: { comments: false },

// Compatibility workarounds
ecma: 5,
ie8: true,
safari10: true
}
Expand Down
48 changes: 10 additions & 38 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"rollup": "0.59.4",
"sassdoc": "^2.7.4",
"slash": "^5.0.0",
"uglify-js": "^3.17.4",
"terser": "^5.16.1",
"yargs-parser": "^21.1.1"
},
"devDependencies": {
Expand Down
20 changes: 8 additions & 12 deletions tasks/compile-javascripts.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { dirname, join, parse } from 'path'

import PluginError from 'plugin-error'
import { rollup } from 'rollup'
import { minify } from 'uglify-js'
import { minify } from 'terser'

import { paths, pkg } from '../config/index.js'
import { getListing } from '../lib/file-helper.js'
Expand Down Expand Up @@ -73,7 +73,7 @@ export async function compileJavaScript ([modulePath, { srcPath, destPath, minif

// Minify bundle
if (minify) {
result = minifyJavaScript(modulePath, result)
result = await minifyJavaScript(modulePath, result)

// Create directories
await mkdir(dirname(filePath), { recursive: true })
Expand All @@ -93,28 +93,24 @@ export async function compileJavaScript ([modulePath, { srcPath, destPath, minif
* @param {object} result - Generated bundle
* @param {string} result.code - Source code
* @param {import('magic-string').SourceMap} result.map - Source map
* @returns {import('uglify-js').MinifyOutput} Minifier result
* @returns {Promise<import('terser').MinifyOutput>} Minifier result
*/
export function minifyJavaScript (modulePath, result) {
const minified = minify({ [modulePath]: result.code }, {
format: { comments: false },

// Include source maps
sourceMap: {
content: result.map,
filename: result.map.file,
url: `${result.map.file}.map`,
includeSources: true
},

// Prevent unsafe polyfill changes
// https://github.com/mishoo/UglifyJS#compress-options
compress: {
inline: 2
},

// Compatibility workarounds
ecma: 5,
ie8: true,
module: false,
v8: true,
webkit: true
safari10: true
})

if (minified.error) {
Expand Down

0 comments on commit 04bd6d1

Please sign in to comment.