如何使其工作Webpack 4树与反应应用程序摇晃

时间:2018-06-07 19:56:48

标签: reactjs webpack babel tree-shaking

我正试图让我的应用程序中的树摇动工作如何有人可以告诉我我的配置是否有错误。

- 我使用ES5进口/出口

-SideEffects:false

-UglifyJSPlugin

我尝试了很多不同的配置,但似乎我不能让它工作:( 我按照webpack文档说明进行操作,但也许我正在做一些其他问题?

以下是我的配置文件希望有人能看到我做错了什么......

  

的package.json

{
  "name": "bazuca-production",
  "sideEffects": false,
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
   ...
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.4",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "babel-plugin-transform-regenerator": "^6.26.0",
    "babel-polyfill": "^6.26.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-react": "^6.24.1",
    "compression-webpack-plugin": "^1.1.11",
    "css-loader": "^0.28.11",
    "faker": "^4.1.0",
    "file-loader": "^1.1.11",
    "html-loader": "^0.5.5",
    "html-webpack-plugin": "^3.0.7",
    "mini-css-extract-plugin": "^0.2.0",
    "react-hot-loader": "^4.1.2",
    "semantic-ui": "^2.3.1",
    "uglifyjs-webpack-plugin": "^1.2.5",
    "url-loader": "^1.0.1",
    "webpack": "^4.6.0",
    "webpack-bundle-analyzer": "^2.11.1",
    "webpack-cli": "^2.0.12",
    "webpack-dev-server": "^3.1.1",
    "webpack-jarvis": "^0.3.2",
    "webpackbar": "^2.6.1"
  },
  "dependencies": {
   ...
  }
}
  

Webpack配置

const MiniCssExtractPlugin = require("mini-css-extract-plugin");
let plugins;
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');

module.exports = env => {
  if (env.production) {
    plugins = require('./webpack/plugins/plugins.prod');
  } else {
    plugins = require('./webpack/plugins/plugins.dev');
  }
  return {
    entry: ["babel-polyfill", "./src/index.js"],
    resolve: {
      extensions: ['.js', '.jsx']
    },

    output: {
      publicPath: '/',
      filename: env.production ? "[name].[contenthash].bundle.js" : "[name].bundle.js",
      chunkFilename: env.production ? "[name].[contenthash].chunk.js" : "[name].chunk.js"
      //chunkFilename: "chunkhash].js"
    },

    module: {
      rules: [
        {
          test: /\.(js|jsx)$/,
          exclude: /node_modules/,
          use: {
            loader: "babel-loader"
          }
        },
        {
          test: /\.(png|jp(e*)g|svg)$/,
          use: [{
            loader: 'url-loader',
            options: {
              limit: 8000, // Convert images < 8kb to base64 strings
              name: 'images/[hash]-[name].[ext]'
            }
          }]
        },
        {
          test: /\.html$/,
          use: [
            {
              loader: "html-loader",
              options: { minimize: true }
            }
          ]
        },
        {
          test: /\.css$/,
          use: [MiniCssExtractPlugin.loader, "css-loader"]
        },
        /***********************************/
        {
          test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
          loader: 'url-loader?limit=10000&mimetype=application/font-woff'
        },
        {
          test: /\.(ttf|eot)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
          loader: 'file-loader'
        },
        {
          test: /\.otf(\?.*)?$/,
          use: 'file-loader?name=/fonts/[name].  [ext]&mimetype=application/font-otf'
        }
        /***********************************/
      ]
    },
    devServer: {
      historyApiFallback: true,
      disableHostCheck: true,
      // hot: true
    },
    optimization: {
      minimizer: [
        new UglifyJSPlugin({
          sourceMap: false,
          uglifyOptions: {
            ecma: 5,
            warnings: false,
            comments: false,
            beautify: false
          },
        })
      ],
      splitChunks: {
        cacheGroups: {
          commons: {
            test: /[\\/]node_modules[\\/]/,
            name: "vendors",
            chunks: "all",
            minChunks: Infinity
          }
        }
      }
    },
    plugins: plugins
  }
};
  

webpack插件

const HtmlWebPackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CompressionPlugin = require('compression-webpack-plugin');
const WebpackBar = require('webpackbar');
const DefinePlugin = require('webpack').DefinePlugin;

const Jarvis = require("webpack-jarvis");
module.exports = [new HtmlWebPackPlugin({
    template: "./src/index.html",
    filename: "./index.html",
    favicon: 'favicon.ico'
}),
new MiniCssExtractPlugin({
    filename: "[name].css",
    chunkFilename: "[id].css"
}),
new WebpackBar(),
new DefinePlugin({
    'API_URL': JSON.stringify("https://api.bazuca.com")
}),
new CompressionPlugin({
    asset: '[path].gz[query]',
    algorithm: 'gzip',
    test: /\.js$|\.css$|\.html$|\.eot?.+$|\.ttf?.+$|\.woff?.+$|\.svg?.+$/,
    threshold: 10240,
    minRatio: 0.8
}),
  

.babelrc

{
        "presets": [
            "env",
            "react",
            "stage-2"
        ],
        "plugins": [
            "transform-object-rest-spread",
            "transform-class-properties",
            "transform-regenerator",
            "react-hot-loader/babel"
        ]
    }

0 个答案:

没有答案