使用babel缩小webpack中的ES6代码

前端之家收集整理的这篇文章主要介绍了使用babel缩小webpack中的ES6代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我尝试过诸如Uglifyjs,babelli(babel-minify)之类的选项.似乎没有什么工作.Uglify会抛出这样的错误

名称预期[au680.bundle.js:147541,22]

babelli也没有缩小代码.任何人都可以使用webpack 2,babel进行es6缩小的简单例子.
可能是一个干净利落地完成工作的插件.

  1. var path = require('path');
  2. var webpack = require('webpack');
  3. var HtmlWebpackPlugin = require('html-webpack-plugin');
  4. var AppCachePlugin = require('appcache-webpack-plugin');
  5.  
  6. var appConfig= require('./config.js');
  7. console.log("appConfig is ->>>",appConfig);
  8. var appPort = appConfig.APP_PORT;//Port on which the application is running
  9.  
  10. process.noDeprecation = true;
  11. var ASSET_PATH = '/'
  12. module.exports = function(options) {
  13. var entry,jsLoaders,plugins,cssLoaders,devtool;
  14. console.log('options webconfig-->',options,'directory name',__dirname);
  15.  
  16. // If production is true
  17. if (options.prod) {
  18. console.log('production minification');
  19. // Entry
  20. entry = {
  21. 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')
  22. };
  23.  
  24. // Plugins
  25. plugins = [// Plugins for Webpack
  26. new webpack.DefinePlugin({
  27. 'process.env': {
  28. 'NODE_ENV': JSON.stringify('production')
  29. }
  30. }),// new webpack.optimize.UglifyJsPlugin({minimize: true,comments : false,compress: {
  31. // // remove warnings
  32. // warnings: false,// // Drop console statements
  33. // drop_console: true
  34. // }})
  35.  
  36. // new es3MemberExpressionLiterals(),//
  37. ];
  38.  
  39. // If app is in development
  40. } else {
  41. devtool = 'source-map';
  42. // Entry
  43. // entry = [
  44. // "webpack-dev-server/client?http://0.0.0.0:" + appPort,// Needed for hot reloading
  45. // "webpack/hot/only-dev-server",// See above
  46. // //path.resolve(__dirname,'./app') // Start with js/app.js...
  47. // path.resolve(__dirname,'./VerisInstrument/js/VerisApp')
  48. // ];
  49. // require("babel-core").transform("code",{
  50. // plugins: ["transform-object-rest-spread"]
  51. // });
  52. entry = {
  53. main: [
  54. "webpack-dev-server/client?http://0.0.0.0:" + appPort,// Needed for hot reloading
  55. "webpack/hot/only-dev-server" // See above
  56. ],//path.resolve(__dirname,'./js/app') // Start with js/app.js...
  57. veris : path.resolve(__dirname,'./VerisInstrument/js/VerisApp'),'./Common/js/infohealthcheckapp.js')
  58. };
  59. // Only plugin is the hot module replacement plugin
  60. plugins = [
  61. new webpack.DefinePlugin({
  62. 'process.env': {
  63. 'NODE_ENV': JSON.stringify('development'),}
  64. }),new webpack.HotModuleReplacementPlugin()// Make hot loading work,]
  65. }
  66.  
  67. return {
  68. devtool: devtool,entry: entry,// output: { // Compile into js/build.js
  69. // path: path.resolve(__dirname,'build'),// filename: "js/bundle.js",// publicPath : '/'
  70. // },output: { // Compile into js/build.js
  71. path: path.resolve(__dirname,filename: '[name].bundle.js',publicPath : ASSET_PATH
  72. },module: {
  73. rules: [
  74. {
  75. test: /\.js$/,// Transform all .js files required somewhere within an entry point...
  76. loader: 'babel-loader',// ...with the specified loaders...
  77. exclude: /node_modules/,options: {
  78. 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')]
  79. }
  80. // query : {
  81. // presets : ['es2015','env']
  82. // }
  83.  
  84. },{
  85. test: /\.css$/,// Transform all .css files required somewhere within an entry point...
  86. use : [
  87. {
  88. loader : 'style-loader'
  89. },{
  90. loader : 'css-loader'
  91. },{
  92. loader : 'postcss-loader'
  93. },{
  94. loader: 'sass-loader'
  95. }
  96. ] // ...with PostCSS
  97. },{
  98. test: /\.jpe?g$|\.gif$|\.png$/i,loader: "url-loader?limit=100000"
  99. },{ test: /\.(woff|woff2|eot|ttf|svg)$/,loader: 'url-loader?limit=100000' }
  100. ]
  101. },plugins: plugins,target: "web",// Make web variables accessible to webpack,e.g. window
  102. stats: false,// Don't show stats in the console
  103. node: {
  104. fs: "empty"
  105. }
  106. }
  107. }

解决方法

https://github.com/webpack/webpack/issues/2545开始:

The problem is that UglifyJS doesn’t support ES6 yet so it’s not possible to avoid that transformation yet. You can follow the progress at 07001 .

有很多解决方案;这是一对夫妇:

首先透明ES6代码,然后缩小它
为了获得最大的兼容性,使用Babel进行转换,然后使用Babel Minify(以前的Babili)缩小:

>安装babel-loaderbabel-minify-webpack-plugin

  1. npm install babel-loader babel-minify-webpack-plugin --save-dev

要么:

  1. yarn add babel-loader babel-minify-webpack-plugin --dev

>将此添加到webpack.config.js:

  1. const MinifyPlugin = require('babel-minify-webpack-plugin');
  2.  
  3. module.exports = {
  4. // ...
  5. module: {
  6. rules: [
  7. {
  8. test: /\.js$/,use: {
  9. loader: 'babel-loader',options: {
  10. presets: ['env']
  11. }
  12. }
  13. }
  14. ]
  15. },plugins: [
  16. new MinifyPlugin()
  17. ]
  18. };

或者如果您愿意,可以使用UglifyJS代替Babel Minify:

  1. const MinifyPlugin = require('uglifyjs-webpack-plugin');

无需转换即可缩小您的ES6代码
为了仅与支持您正在使用的ES6功能的浏览器兼容,请使用Babel Minify进行缩小而不进行转换:

>安装babel-minify-webpack-plugin

  1. npm install babel-minify-webpack-plugin --save-dev

要么:

  1. yarn add babel-minify-webpack-plugin --dev

>将此添加到webpack.config.js:

  1. const MinifyPlugin = require('babel-minify-webpack-plugin');
  2.  
  3. module.exports = {
  4. // ...
  5. plugins: [
  6. new MinifyPlugin()
  7. ]
  8. };

Babel Minify的默认设置对我来说很好,但您可以在这里看到更多可以自定义的选项:https://github.com/webpack-contrib/babel-minify-webpack-plugin

猜你在找的JavaScript相关文章