Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

All my code runs twice when compiled by Webpack

When I build my js bundle with webpack using webpack-dev-server my code runs twice every time. Not sure how to fix it.

Screenshot of Developer Tools console

My webpack config:

var path = require('path'); var webpack = require('webpack'); var HtmlWebpackPlugin = require('html-webpack-plugin'); var CopyWebpackPlugin = require('copy-webpack-plugin');  module.exports = {   devtool: 'cheap-eval-sourcemap',   entry: [     'webpack-dev-server/client?http://localhost:8080',     'webpack/hot/dev-server',     path.join(__dirname, '../src/main')   ],   output: {     path: path.join(__dirname, '../dist'),     filename: 'bundle.js'   },   plugins: [     new webpack.HotModuleReplacementPlugin(),     new webpack.optimize.OccurenceOrderPlugin(),     new HtmlWebpackPlugin({       template: path.join(__dirname, '../src/index.html')     }),     new CopyWebpackPlugin([       {         from: path.join(__dirname, '../assets'),         to: path.join(__dirname, '../dist/assets')       }     ])   ],   devServer: {     contentBase: path.join(__dirname, '../dist'),     outputPath: '/lol',     hot: true   },   module: {     loaders: [       {         test: /\.js$/,         loaders: ['babel-loader'],         include: path.join(__dirname, '../src')       }     ]   } }; 
like image 579
Alex Cavazos Avatar asked May 06 '16 21:05

Alex Cavazos


People also ask

Does webpack automatically minify?

In webpack 4, the bundle-level minification is enabled automatically – both in the production mode and without one. It uses the UglifyJS minifier under the hood. (If you ever need to disable minification, just use the development mode or pass false to the optimization. minimize option.)

Does webpack use multiple cores?

The file compression plug-in uglyfy-webpack-plugin on the official webpack page supports building with multiple CPU cores. This plug-in is supposed to be included in webpack by default in v1. 0.0, but now it needs to be manually installed via npm.

Does webpack increase performance?

Code Splitting Code splitting is a feature provided by webpack to split your code into multiple bundles which can be used on-demand. Code splitting results in reduced bundle size which can help us in improving the load time, hence, improving the performance.


1 Answers

in the template file you might have manually added a loading the bundle.

If you don't have the

inject: false  

option in

new HtmlWebpackPlugin({     template: path.join(__dirname, '../src/index.html') }), 

the bundle will get added again.

like image 191
Emil Perhinschi Avatar answered Sep 20 '22 19:09

Emil Perhinschi