Skip to content
This repository has been archived by the owner on Dec 6, 2021. It is now read-only.

Commit

Permalink
Revert "use uglifyjs-webpack-plugin"
Browse files Browse the repository at this point in the history
This reverts commit 5f11345.
  • Loading branch information
egoist committed Dec 26, 2017
1 parent 7a2501b commit 448bf69
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 10 deletions.
27 changes: 18 additions & 9 deletions packages/poi/lib/create-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,17 +257,26 @@ module.exports = function ({
}

if (minimize) {
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')

config.plugin('minimize')
.use(UglifyJsPlugin, [{
parallel: true,
sourceMap: false,
uglifyOptions: {
compress: {
comparisons: false
}
.use(webpack.optimize.UglifyJsPlugin, [{
sourceMap: Boolean(sourceMap),
/* eslint-disable camelcase */
compressor: {
warnings: false,
conditionals: true,
unused: true,
comparisons: true,
sequences: true,
dead_code: true,
evaluate: true,
if_return: true,
join_vars: true,
negate_iife: false
},
output: {
comments: false
}
/* eslint-enable camelcase */
}])
}

Expand Down
23 changes: 23 additions & 0 deletions packages/poi/lib/webpack/handle-errors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,35 @@ function formatError(error) {
}
}

if (cannotUglifyES6(error.message)) {
const { kind, payload } = getModuleNameFromPath(error.message)
return {
type: 'uglify-error',
payload,
kind,
error
}
}

return {
type: 'unknown',
error
}
}

function cannotUglifyES6(message) {
return /from UglifyJs/.test(message) &&
/Unexpected (token|character)/.test(message)
}

function getModuleNameFromPath(str) {
const matchModule = /[/\\]node_modules[/\\]([^/\\]+)/.exec(str)
if (matchModule) return { kind: 'module', payload: matchModule[1] }

const matchFile = /\[([^:]+):[^\]]+\]/.exec(str)
if (matchFile) return { kind: 'file', payload: matchFile[1] }
}

module.exports = errors => {
errors = errors.map(formatError)
output(errors)
Expand Down
35 changes: 35 additions & 0 deletions packages/poi/lib/webpack/handle-errors/output.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const path = require('path')
const chalk = require('chalk')
const highlight = require('highlight-es')
const _ = require('lodash')
const logger = require('../../logger')

Expand Down Expand Up @@ -30,6 +31,39 @@ function moduleNotFound(errors) {
return res.join('\n')
}

function uglifyError(errors) {
if (!errors) return false
// There's always only ONE uglify-token-error
const error = errors[0]

const res = []
const { message } = error.error

if (error.kind === 'module') {
res.push(`${chalk.red('UglifyJS error')}: unexpected ES6+ code in module "${error.payload}", full error message:\n`)
res.push(chalk.dim(message))
res.push('')
res.push(
logger.tip(chalk.bold(`To fix this, try adding "${error.payload}" to "transformModules" option, eg:`), false)
)
res.push('')
res.push(highlight(`// poi.config.js
module.exports = {
transformModules: ['${error.payload}'],
// ...other config
}`))
} else if (error.kind === 'file') {
res.push(`${chalk.red('UglifyJS error')}: unexpected ES6+ code in file "${error.payload}", full error message:\n`)
res.push(chalk.dim(message))
res.push('')
res.push(
logger.tip(chalk.bold(`To fix this, please configure .babelrc to compile your app code down to ES5 or use poi-preset-babel-minify if you want to preserve ES6+ code in final bundle.`), false)
)
}

return res.join('\n')
}

function vueVersionMismatch(errors) {
if (!errors) return

Expand Down Expand Up @@ -111,6 +145,7 @@ module.exports = errors => {
errors = groupErrorsByType(errors)
run([
moduleNotFound(errors['module-not-found']),
uglifyError(errors['uglify-error']),
vueVersionMismatch(errors['vue-version-mismatch']),
babelPluginNotFound(errors['babel-plugin-not-found']),
babelPresetNotFound(errors['babel-preset-not-found']),
Expand Down
2 changes: 1 addition & 1 deletion packages/poi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"css-loader": "^0.28.1",
"extract-text-webpack-plugin": "^3.0.0",
"file-loader": "^1.1.5",
"highlight-es": "^1.0.1",
"html-webpack-plugin": "^2.28.0",
"import-local-file": "^0.2.0",
"is-ci": "^1.0.10",
Expand All @@ -72,7 +73,6 @@
"rimraf": "^2.6.1",
"strip-ansi": "^4.0.0",
"tildify": "^1.2.0",
"uglifyjs-webpack-plugin": "^1.1.4",
"update-notifier": "^2.1.0",
"url-loader": "^0.6.2",
"vue": "^2.3.3",
Expand Down

0 comments on commit 448bf69

Please sign in to comment.