vue webpack压缩代码_javascript - webpack 打包 vue 压缩 js代码时报错

在使用webpack打包Vue项目时,遇到UglifyJS压缩报错,原因是未处理ES6语法。解决方案是在项目根目录添加.babelrc文件,配置 presets 为['es2015', { "modules": false }],确保Babel对ES6进行转译。" 111947893,10546839,UML建模语言详解:视图与图表总结,"['面向对象', '建模语言', '软件工程', '系统设计', 'UML图']
摘要由CSDN通过智能技术生成

问 题

'use strict'

const webpack = require('webpack')

const paths = require('./paths')

const CopyWebpackPlugin = require('copy-webpack-plugin')

const HtmlWebpackPlugin = require('html-webpack-plugin')

const ManifestPlugin = require('webpack-manifest-plugin')

const ExtractTextPlugin = require('extract-text-webpack-plugin')

const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin')

const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')

module.exports = {

entry: paths.appEntry,

output: {

pathinfo: true,

path: paths.appBuild,

filename: '[name].[hash].js'

},

bail: true,

devtool: 'source-map',

resolve: {

extensions: [

'.js', '.vue', '.scss', '.css', '.json'

],

alias: {

'src': paths.appSrc

}

},

module: {

rules: [

{

test: /\.vue$/,

loader: 'vue-loader',

options: {

extract: true,

loaders: ExtractTextPlugin.extract({

use: [

{

loader: 'css-loader',

options: {

minimize: true,

sourcMap: true

}

}, {

loader: 'sass-loader',

options: {

indentedSyntax: true,

sourcMap: true

}

}

],

fallback: 'vue-style-loader'

})

}

}, {

test: /\.js$/,

loader: 'babel-loader',

exclude: /node_modules/

}, {

test: /\.(png|jpg|gif|svg)$/,

loader: 'file-loader',

options: {

name: '[name].[ext]?[hash]'

}

}

]

},

plugins: [

new webpack.HotModuleReplacementPlugin(),

new CaseSensitivePathsPlugin(),

new webpack.DefinePlugin({

PRODUCTION: JSON.stringify(process.env.NODE_ENV === 'prod')

}),

new HtmlWebpackPlugin({

inject: true,

template: paths.appHtml,

minify: {

removeComments: true,

collapseWhitespace: true,

removeRedundantAttributes: true,

useShortDoctype: true,

removeEmptyAttributes: true,

removeStyleLinkTypeAttributes: true,

keepClosingSlash: true,

minifyJS: true,

minifyCSS: true,

minifyURLs: true

}

}),

new webpack.optimize.UglifyJsPlugin({

compress: {

warnings: false,

comparisons: false

},

output: {

comments: false

},

sourceMap: true

}),

new OptimizeCSSPlugin({

cssProcessorOptions: {

safe: true

}

}),

new ExtractTextPlugin({filename: '[name].[contenthash:8].css'}),

new ManifestPlugin({fileName: 'asset-manifest.json'}),

new CopyWebpackPlugin([

{

from: paths.appPublic,

to: paths.appBuild,

ignore: ['.*']

}

])

]

}

报错原因 好像是 uglifyjs 插件不认识 es6语法

另外 我的 css 没有独立打包成 css 文件仍和 js 揉在一起的。。。

谢谢各位大侠帮忙看看。

解决方案

你的 babel 配置是正确的。这种情况下,一个常见的可能是,你的项目根目录下遗漏了 .babelrc 文件,因而使得 babel 没有对 ES6 做转译,导致 Uglify 失败。

追加如下内容的 .babelrc 到项目根目录即可:

{

"presets": [

["es2015", { "modules": false }]

]

}

扫一扫关注IT屋

微信公众号搜索 “ IT屋 ” ,选择关注与百万开发者在一起

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值