我尝试过像Uglifyjs,babelli(babel-minify)这样的选项。似乎没什么用.Uglify会抛出一些错误:
预期名称[au680.bundle.js:147541,22]
babelli也没有缩小代码。任何人都可以使用webpack 2,babel进行es6缩小的简单示例。 可能是一个干净利落的插件。
var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var AppCachePlugin = require('appcache-webpack-plugin');
var appConfig= require('./config.js');
console.log("appConfig is ->>>",appConfig);
var appPort = appConfig.APP_PORT;//Port on which the application is running
process.noDeprecation = true;
var ASSET_PATH = '/'
module.exports = function(options) {
var entry, jsLoaders, plugins, cssLoaders, devtool;
console.log('options webconfig-->', options, 'directory name', __dirname);
// If production is true
if (options.prod) {
console.log('production minification');
// Entry
entry = {
veris:path.resolve(__dirname,'./VerisInstrument/js/VerisApp.js'),
au680 : path.resolve(__dirname,'./Au680Instrument/js/au680App.js'),
commondashboard:path.resolve(__dirname,'./CommonDashboard/js/CommonDashboardApp.js'),
groups:path.resolve(__dirname,'./Groups/js/GroupsApp.js'),
homepage : path.resolve(__dirname,'./HomePage/js/HomePageApp.js'),
infohealthcheck : path.resolve(__dirname,'./Common/js/infohealthcheckapp.js')
};
// Plugins
plugins = [// Plugins for Webpack
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify('production')
}
}),
// new webpack.optimize.UglifyJsPlugin({minimize: true,comments : false,compress: {
// // remove warnings
// warnings: false,
// // Drop console statements
// drop_console: true
// }})
// new es3MemberExpressionLiterals(),
//
];
// If app is in development
} else {
devtool = 'source-map';
// Entry
// entry = [
// "webpack-dev-server/client?http://0.0.0.0:" + appPort, // Needed for hot reloading
// "webpack/hot/only-dev-server", // See above
// //path.resolve(__dirname,'./app') // Start with js/app.js...
// path.resolve(__dirname,'./VerisInstrument/js/VerisApp')
// ];
// require("babel-core").transform("code", {
// plugins: ["transform-object-rest-spread"]
// });
entry = {
main: [
"webpack-dev-server/client?http://0.0.0.0:" + appPort, // Needed for hot reloading
"webpack/hot/only-dev-server" // See above
],
//path.resolve(__dirname,'./js/app') // Start with js/app.js...
veris : path.resolve(__dirname,'./VerisInstrument/js/VerisApp'),
au680 : path.resolve(__dirname,'./Au680Instrument/js/au680App.js'),
commondashboard:path.resolve(__dirname,'./CommonDashboard/js/CommonDashboardApp.js'),
groups:path.resolve(__dirname,'./Groups/js/GroupsApp.js'),
homepage : path.resolve(__dirname,'./HomePage/js/HomePageApp.js'),
infohealthcheck : path.resolve(__dirname,'./Common/js/infohealthcheckapp.js')
};
// Only plugin is the hot module replacement plugin
plugins = [
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify('development'),
}
}),
new webpack.HotModuleReplacementPlugin()// Make hot loading work,
]
}
return {
devtool: devtool,
entry: entry,
// output: { // Compile into js/build.js
// path: path.resolve(__dirname, 'build'),
// filename: "js/bundle.js",
// publicPath : '/'
// },
output: { // Compile into js/build.js
path: path.resolve(__dirname, 'build'),
filename: '[name].bundle.js',
publicPath : ASSET_PATH
},
module: {
rules: [
{
test: /\.js$/, // Transform all .js files required somewhere within an entry point...
loader: 'babel-loader', // ...with the specified loaders...
exclude: /node_modules/,
options: {
presets: ['es2015','react','stage-2','env'],
plugins: [require('babel-plugin-transform-object-rest-spread'),require('babel-plugin-transform-es2015-destructuring'),require('babel-plugin-transform-es2015-parameters')]
}
// query : {
// presets : ['es2015','react','stage-2','env']
// }
}
, {
test: /\.css$/, // Transform all .css files required somewhere within an entry point...
use : [
{
loader : 'style-loader'
},
{
loader : 'css-loader'
},
{
loader : 'postcss-loader'
},
{
loader: 'sass-loader'
}
] // ...with PostCSS
}, {
test: /\.jpe?g$|\.gif$|\.png$/i,
loader: "url-loader?limit=100000"
},
{ test: /\.(woff|woff2|eot|ttf|svg)$/,
loader: 'url-loader?limit=100000' }
]
},
plugins: plugins,
target: "web", // Make web variables accessible to webpack, e.g. window
stats: false, // Don't show stats in the console
node: {
fs: "empty"
}
}
}

答案 0 :(得分:11)
来自https://github.com/webpack/webpack/issues/2545:
问题是UglifyJS还不支持ES6,所以它还不可能避免这种转变。您可以在mishoo/UglifyJS2#448处按照进度进行操作。
有很多解决方案;这是一对夫妇:
首先透明ES6代码,然后缩小
为了获得最大的兼容性,使用Babel进行转换,然后使用Babel Minify(以前称为Babili)进行缩小:
安装babel-loader和babel-minify-webpack-plugin
npm install babel-loader babel-minify-webpack-plugin --save-dev
或者:
yarn add babel-loader babel-minify-webpack-plugin --dev
将此添加到webpack.config.js:
const MinifyPlugin = require('babel-minify-webpack-plugin');
module.exports = {
// ...
module: {
rules: [
{
test: /\.js$/,
use: {
loader: 'babel-loader',
options: {
presets: ['env']
}
}
}
]
},
plugins: [
new MinifyPlugin()
]
};
或者如果您愿意,可以使用UglifyJS代替Babel Minify:
const MinifyPlugin = require('uglifyjs-webpack-plugin');
在不转换的情况下缩小您的ES6代码
为了仅与支持您正在使用的ES6功能的浏览器兼容,请使用Babel Minify进行缩小而不进行转换:
npm install babel-minify-webpack-plugin --save-dev
或者:
yarn add babel-minify-webpack-plugin --dev
将此添加到webpack.config.js:
const MinifyPlugin = require('babel-minify-webpack-plugin');
module.exports = {
// ...
plugins: [
new MinifyPlugin()
]
};
Babel Minify的默认设置对我来说很好,但您可以在此处看到可以自定义的更多选项:https://github.com/webpack-contrib/babel-minify-webpack-plugin
答案 1 :(得分:-3)
以下是我的webpack配置文件。我正在使用webpack 2.3.1与react-router的动态路由。希望它可以帮到你。
var path = require('path');
var webpack = require('webpack');
var package=require('./package.json');
var config = require('./config');
var ManifestPlugin = require('webpack-manifest-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
var getPlugins = function(){
var pluginsList = [
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks: Infinity,
filename: 'vendor.bundle_[hash].js'
}),
new webpack.DefinePlugin({
"process.env": {
NODE_ENV: JSON.stringify("production")
}
}),
new webpack.NamedModulesPlugin(),
new ManifestPlugin({
fileName: 'build-manifest.json'
})
];
pluginsList.push(new webpack.LoaderOptionsPlugin({
minimize: true,
debug: false
}));
pluginsList.push(new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
},
output: {
comments: false
},
sourceMap: false,
minimize: true
}));
return pluginsList;
}
module.exports = {
cache: true,
output: {
path:path.join(__dirname, "dist/js"),
filename: "[name]_[hash].js",
chunkFilename:"[name]_[hash].js",
publicPath:config.envConfig.JS_ASSETS_PATH
},
devServer: {
contentBase: path.join(__dirname, "dist"),
compress: true,
port: 8080
},
entry: {
index: [
package.paths.app
],
vendor: [
'react', 'react-dom','phrontend',
'react-ga'
]
},
plugins: getPlugins(),
target: 'web',
module: {
rules: [
{
test: /.jsx?$/,
exclude: /node_modules/,
use: [{
loader: 'babel-loader',
}]
}
]
},
};