UNPKG

908 BJavaScriptView Raw
1const { minify } = require('uglify-js')
2const chalk = require('chalk')
3
4/*
5 * uglify-js is no longer maintained
6 * consider replacing with terser
7 */
8module.exports = async (files, minifyOpts, silent) => {
9 const results = minify(files, minifyOpts)
10 if (results.error) {
11 const err = Object.assign(results.error, {
12 preview: ''
13 })
14 if (typeof err.line !== 'undefined') {
15 err.preview = files[err.filename].split('\n')[err.line - 1]
16 const spaces = err.preview.match(/^\s*/)
17 err.cursor = '^'.padStart(err.col - spaces[0].length + 2, ' ')
18 if (!silent) {
19 console.log(chalk.red(`⚠️ Syntax error found by uglify in "${err.filename}", on line ${err.line}, col ${err.col}, you wrote:`))
20 console.log(err.preview.replace(/^\s*/, ' '))
21 console.log(chalk.bold.red(err.cursor))
22 }
23 }
24 return Promise.reject(err)
25 }
26 return results.code
27}