是否有办法彻底处理与vue
捆绑在一起的webpack
组件?
在mangle.properties
设置为true
的情况下通过terser-webpack-plugin
应用重整时,并非所有属性名都被重整,例如:
location: {
lng: -.134281,
lat:51.513508,
zoom:13,
pitch:1,
bearing:60
}
成为
location:{
k:-.134281,
M:51.513508,
zoom:13,
pitch:1,
V:60
}
根据要求:Webpack配置文件的相关部分,在本例中为默认的vie-cli
配置,其中手动添加了mangle.properties
项:
minimizer: [
{
options: {
test: /\.m?js(\?.*)?$/i,
chunkFilter: () => true,
warningsFilter: () => true,
extractComments: false,
sourceMap: false,
cache: true,
cacheKeys: defaultCacheKeys => defaultCacheKeys,
parallel: true,
include: undefined,
exclude: undefined,
minify: undefined,
terserOptions: {
output: {
comments: /^\**!|@preserve|@license|@cc_on/i
},
compress: {
arrows: false,
collapse_vars: false,
comparisons: false,
computed_props: false,
hoist_funs: false,
hoist_props: false,
hoist_vars: false,
inline: false,
loops: false,
negate_iife: false,
properties: false,
reduce_funcs: false,
reduce_vars: false,
switches: false,
toplevel: false,
typeofs: false,
booleans: true,
if_return: true,
sequences: true,
unused: true,
conditionals: true,
dead_code: true,
evaluate: true
},
mangle: {
safari10: true,
properties: true
}
}
}
}
],
答案 0 :(得分:1)
这两个属性(zoom
,pitch
)恰好包含在reserved
名称列表中,请看一下UglifyJS在内部使用的默认domprops.json
文件在整容期间。
tools/domprops.json
中提供了一个默认排除文件,该文件应涵盖各种浏览器中定义的大多数标准JS和DOM属性。传递--mangle-props
domprops以禁用此功能
如果您希望保留此默认列表,则可以在插件的custom minify option中执行以下任一操作:
domprops.json
)并传入函数/过滤器以删除那些不需要的名称,{
optimization: {
minimizer: [
new TerserPlugin({
minify(file, sourceMap) {
const uglifyJsOptions = {
mangle: {
properties: {
reserved: require('your_custom_list')
}
// Or filter them
properties: {
reserved: require('uglify-js/tools/domprops.json')
.filter(name => ![
'zoom',
'pitch'
]
.includes(name))
}
}
};
return require('uglify-js').minify(file, uglifyJsOptions);
},
}),
],
},
}
此外,在执行此操作时,请注意mangle.reserved
和mangle.properties.reserved
之间的相似之处,因为后者可能是您在这里需要的。签出minify option structure。