How come webpack commonChunk is causing duplication of packages across my bundles?

I am aiming to generate a vendor.bundle.js file using my modules. I have set up webpack as follows:

...
  entry: {
    app: ['./src/index.js'],
    vendor: [
      'axios',
      'lodash',
      'recharts',
      'moment',
      ......
      'react-select',
      'react-tooltip',
      'react-toggle',
      'buffer',
      'jstz',
      'pikaday',
      'qrcode-js',
      'notie',
      'tippy.js',
      'react-dropzone',
      'react-dd-menu',
      'html-to-draftjs',
      'react-virtualized',
      'react-draft-wysiwyg',
      'js-search',
      'qr.js'
    ]
  },
  ...
  output: {
    path: '/Users/ben/pro/qbpanel2.0/qbpanel-2.0/qbpanel/app/assets/javascripts',
    publicPath: '/',
    filename: 'bundle.js'
  },
  ...
  plugins: [
    new BundleAnalyzerPlugin({
      analyzerPort: 9998
    }),
    new webpack.optimize.CommonsChunkPlugin({
      name: 'vendor',
      filename: 'vendor.bundle.js',
      path: '/Users/ben/pro/qbpanel2.0/qbpanel-2.0/qbpanel/app/assets/javascripts',
      publicPath: '/',
      minChunks: Infinity
    }),
...
  ]
}

I anticipate that all the packages mentioned in entry.vendor will be included in the final vendor.bundle.js file, however, some of them are present in both the bundle.js and vendor.bundle.js.

Why is this happening?

In the bundle.js, I did not expect to find tippy.js, react-virtualized, or lodash. https://i.sstatic.net/YHTZw.png In the vendor.bundle.js, there are duplicate entries from the bundle.js, which should only be here. https://i.sstatic.net/o9gUx.png

Answer №1

What version of webpack are you currently utilizing? To achieve the desired outcome, you can implement the commonsChunkPluggin

An example configuration for webpack 2 could resemble the following:

plugins: [
  new webpack.optimize.CommonsChunkPlugin({
    name: ['node-modules'],
    minChunks(module) {
      const context = module.context;
      return context && context.indexOf('node_modules') >= 0;
    },
  }),
  new webpack.optimize.CommonsChunkPlugin({
    name: ['vendor'],
    chunks: ['node-modules'],
    minChunks(module) {
      return module.context && /.*\/axios|losash|allTheModulesYouWant.*/.test(module.context);
    },
  }),
]

This approach generates a chunk containing all node modules and selectively moves some of them (specified by you) into the vendor chunk.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Display a collection of Mongoose objects in a React component

In my development stack, I rely on node js + React. The data I work with comes from mongoose and typically follows this format: { "_id": "61b711721ad6657fd07ed8ae", "url": "/esports/match/natus-vincere-vs-team-liquid- ...

Loading game resources in advance for future or immediate utilization

I'm currently developing a game UI that involves a large number of image files, totaling around 30MB in size. I've been caching these images to the disk using service workers, but some of them are quite large at 3MB each. Even when they are retri ...

Using Firebase to connect and disconnect from state in React with Re-base

I'm encountering some issues with React and Firebase while using the re-base module. When attempting to retrieve content from Firebase across different components in my app, I run into a problem. On one component/"page," which is home, I have the abi ...

What could be the root cause behind the error encountered while trying to authenticate a JWT?

I've been working on integrating a Google OAuth login feature. Once the user successfully logs in with their Google account, a JWT token is sent to this endpoint on my Express server, where it is then decoded using jsonwebtoken: app.post('/login/ ...

How to transform a file into a uInt8Array using Angular

Looking to implement a feature where I can easily upload files from Angular to PostgreSQL using a Golang API. In my Angular component, I need to convert my file into a uInt8Array. I have managed to convert the array, but it seems to be encapsulated in som ...

Encountering the start error in npm due to a missing script

Whenever I try to execute npm start, I encounter this particular error. npm ERR! Missing script: "start" npm ERR! Did you mean one of these? npm ERR! npm star # Mark your favorite packages npm ERR! npm stars # View packages marked as ...

Executing Firebase Cloud Functions to perform write operations within a database event function

Exploring the world of Firebase functions has been an exciting learning journey for me. This innovative feature is proving to be incredibly powerful and beneficial. I'm eager to utilize a function that can capture a database writing event, perform a ...

What is the best way to retrieve route data based on route name in VueJs?

When working with VueJs, I have found that I can easily access the data of the current route. However, I have encountered a situation where I need to access the data of a different route by its name. Let's say I have defined the following routes: [ ...

Modify a unique custom binding handler in such a way that it is designated using an Immediately Invoked Function Expression

I am currently working on improving a custom binding handler by converting it into an Immediately Invoked Function Expression (IIFE). Despite researching IIFE online, I am still unsure of how to make the necessary changes to my custom handler. Can someon ...

Removing an item from a table row cannot be completed due to the inability to retrieve the list_body ID necessary for deletion

I have been working on enhancing the delete button function in my table. The id linked to the list_body must be incorporated into the delete function code. I utilized some jquery methods to render the list onto the webpage. //retrieve user list information ...

"Encountering difficulties while setting up an Angular project

I am currently working on setting up an Angular project from scratch. Here are the steps I have taken so far: First, I installed Node.js Then, I proceeded to install Angular CLI using the command: npm install -g @angular/cli@latest The versions of the ...

Executing a message in Azure Service Bus using Javascript/Node.js

I am utilizing the @azure/service-bus library in a Node.js application to receive messages. The code I am using is as follows: const { ServiceBusClient } = require("@azure/service-bus"); const sbClient = new ServiceBusClient(connectionString); ...

Tips for animating a nested array using jQuery

I have a border that is 9x9 with lines, columns, and squares, similar to a Sudoku border. I want to animate it, but I encountered some issues when trying to run multiple animations simultaneously. To solve this problem, I decided to animate one array of el ...

Troubleshooting a display issue with Chrome's User Agent - Possible CSS solution?

After recently installing a WordPress theme, I am encountering an issue with a Facebook conversion box not displaying when accessing my site through Chrome. Interestingly, this problem does not occur when using Firefox. Upon inspecting the element in Chro ...

Hot reloading in React

Currently working on a React project that was initiated with create-react-app. However, I am experiencing inconsistencies with the hot reload feature. Sometimes it works for a brief period of time, then stops functioning, requiring me to restart the serv ...

Webpack is refusing to compile my code after attempting to import a mongoose model

I've been working on a React blog app that utilizes MongoDB to store posts. However, I encountered an issue where webpack fails to compile when attempting to import a Mongoose model into my NewPost component. Below are the errors I'm facing: W ...

The asp.net feature is presenting data that is not fully formed

While working with an Ajax request in JavaScript, I encountered an issue where the data being fetched is repeating in the last three fields. Upon validating the query in the database, it seems that the data retrieved by the Ajax call does not match what is ...

How can I send an array (or TList) from C# code behind to an external javascript file?

Is there a recommended method for passing a long list of IP addresses and accompanying data from C# code behind to an external JavaScript function? The list could potentially contain over 1000 items, so efficiency is key. Can you offer a concise example ...

Select specific columns from an array using Typescript

I have a collection of objects and I'm looking for a way to empower the user to choose which attributes they want to import into the database. Is there a method to map and generate a separate array containing only the selected properties for insertion ...

Develop and test a query by examining its structure, parsing it, and assessing its effectiveness

Building a query using different conditions selected from html controls in a textarea, allowing users to make modifications as needed. On the client side: a(1, 3) > 20 b(4, 5) < 90 c(3, 0) = 80 The query formed is: a(1, 3) > 20 and b(4, 5) < ...