What is the reason for always including style-loader, css-loader, and vue-loader content in the webpack4 production bundle?

When building my final bundle with production mode in webpack, I noticed that the exported multiple entry compiled js files always include loaders content. How can I eliminate them and what is the reason for their inclusion?

To replicate this issue, follow these steps:

git clone https://github.com/adamchenwei/boilerplate-webpack-babel-sass-storybook-vuejs.git
git checkout step/3-prod-webpack-build-setup-basic
npm install
npm run build:prod

https://i.sstatic.net/2z0Ft.png

I would appreciate any insights or ideas on how to address this. I have already explored https://webpack.js.org/plugins/module-concatenation-plugin/ without success. It seems that there is no solution available on Stack Overflow either.

Thank you!

webpack.common.js

const path = require('path');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');

function resolve (dir) {
  return path.join(__dirname, '..', dir)
}
module.exports = {
  entry: {
    app: './src/library.js',
    HelloComp: './src/components/HelloComponent/HelloComponent.vue',
    Bye: './src/components/ByeComponent/ByeComponent.vue'
  },
  plugins: [
    new CleanWebpackPlugin(['dist']),
    new HtmlWebpackPlugin({
      title: 'Production'
    })
  ],
  output: {
    filename: '[name].js',
    path: path.resolve(__dirname, 'dist')
  },
  externals: {
    'vue': {
      root: 'vue',
      commonjs2: 'vue',
      commonjs: 'vue',
      amd: 'vue',
      umd: 'vue'
    },
    // other external dependencies...
  }
}

webpack.prod.js

const merge = require('webpack-merge');
const common = require('./webpack.common.js');
const webpack = require('webpack')
const { VueLoaderPlugin } = require('vue-loader')
const HtmlWebpackPlugin = require('html-webpack-plugin')
// other imports...

// configuration for BundleAnalyzerPlugin

module.exports = merge(common, {
  mode: 'production',
  module: {
    rules: [
      // various rules for handling different file types
    ]
  },
  plugins: [
    // various plugins including VueLoaderPlugin, HtmlWebpackPlugin, CopyWebpackPlugin, etc.
  ]
});

Answer №1

Eliminate the need for style-loader in your externals and ensure that you are generating actual CSS files from your CSS... otherwise, webpack will have to find a way to insert your styles, correct?

To achieve this, utilize the mini-css-extract-plugin.

 {
        test: /\.css$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader'
        ]
      },
      {
        test: /\.scss$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader', // translates CSS into CommonJS
          'sass-loader' // compiles Sass to CSS, using Node Sass by default
        ]
    }

Ensure to include new MiniCssExtractPlugin() in your plugins as well. This is preferably done only for production builds.

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

AngularJS: Dealing with scope loss in a directive following a promise

I am currently facing a major issue within my Angular application. There is a Factory in my code that makes an HTTP request to retrieve spoolers and returns a promise. Spooler.prototype.load = function () { var self = this; var deferred = $q.defe ...

`Is it more effective to define an array outside of a component or inside of a component in React?`

Exterior to a unit const somevalue = [1, 2, 3, 4, 5, 6]; const Unit = () => { return ( <div> {somevalue.map((value) => <span>{value}</span>)} </div> ); }; export default Unit; Interior to a unit const Unit ...

The functionality in the React Native code that uploads images to an S3 bucket is currently failing to initiate the network request

I have been using a redux-observable epic to upload images to my AWS S3 bucket through react-native-aws3. It has been functioning smoothly for quite some time. However, recently it seems to have stopped entering the .map and .catch blocks of code. I suspec ...

Running Node.js on a cluster of Raspberry Pi devices

After completing a cluster of four nodes using Raspberry Pi by following this tutorial from the University of Southampton, I now aim to deploy a Node.js web server application on this cluster utilizing MPI or other methods. What is the most efficient way ...

Guide on changing the color of an input box based on the variable size in ReactJs!

I need help with a disabled input box that receives a variable x, which can be either < 1 or > 1. My goal is to change the background color to red if x > 1, green if x < 1, and grey if there's no value provided. Here is what I have attempt ...

Adding a <tr> tag to an HTML table using JQuery and AJAX in the context of Django framework step by step

I am currently navigating the world of Javascript, Jquery, and Ajax requests and I'm encountering a challenge with how my scripts are executing. My homepage contains a lengthy list of items (over 1200) that need to be displayed. Previously, I loaded ...

Is there a way to easily generate multiple prints with just one click?

How can I make this iframe print multiple times when the Print button is clicked? The current code only prints once. Any suggestions would be greatly appreciated. Thank you! $("#printing").on('click',function(){ var printYesNo = ...

Solving the puzzle of closing a challenging JavaScript popup with Selenium in JavaScript

Dealing with popups in Selenium can sometimes be tricky, especially when encountering unusual behavior. I recently encountered a situation where I found it difficult to close a popup window after clicking a button. Upon executing the code below: WebEleme ...

Using spline3D in combination with Vue or Nuxt: A beginner's guide

I recently started experimenting with Vue and wanted to create an interactive site using spline 3D. While there is a library for reactjs in splinetool, I couldn't find one for Vue. I attempted to import @splinetool/runtime and export it, but the 3D sc ...

html5 video: experiencing sluggish loading

Initially, I came across some topics with a similar title while searching for answers but could not find a satisfactory solution. Currently, I am experimenting with the auto-loading of HTML5 video on Android and iPad devices. It seems like auto-load featu ...

Using jQuery to loop over an array and append to the ajax function

I have a question regarding my jQuery $.ajax() post. I need to include an object (or is it a string?) in the request for each item in an array, but I'm unsure of the best approach to take. I think it might be helpful to separate the items from the dat ...

Disabling or Concealing the Timer in angularJS: A Step-by-Step Guide

In my project, I have integrated a Timer and Countdown Timer that counts down from 59 seconds to 0 seconds in reverse order using AngularJS. However, I am facing two issues that need to be resolved. Can anyone provide assistance? Issue 1: The problem lies ...

Hide a div element upon selecting an option from a dropdown menu

On iPhone 6plus and lower, I created a dropdown menu that opens when users click on "jobs" or "contact." However, after clicking on these options, the drop-down menu remains open. How can I make it hide automatically after a list item is clicked at this sp ...

What causes the while loop in a threejs render function to not refresh each frame?

I'm struggling with handling an array of 8 cubes, each only 1 pixel tall. When a button is pressed, I want them to smoothly animate to a new height using a while loop. Here's my current implementation: if (buttonPressed) { console.log(' ...

Volar and vue-tsc are producing conflicting TypeScript error messages

During the development of my project using Vite, Vue 3, and TypeScript, I have set up vue-tsc to run in watch mode. I am utilizing VS Code along with Volar. This setup has been helpful as it displays all TypeScript errors in the console as expected, but I ...

Prevent Quasar QDialog from closing when pressing the Escape key under specific conditions

I am utilizing a Quasar dialog to display content, and I want users to be able to close the dialog by pressing the Escape key. However, if a user has made edits within the dialog and hits the Escape key, I'd like to prompt them with another dialog to ...

How AngularFire automatically adds back a removed item in a Firebase array

I have been working on a way to remove an item from my $firebaseArray called "boxes". Here is the "remove" function: function remove(boxJson) { return boxes.$remove(boxJson); } Although it gets removed, I noticed that it immediately reappea ...

Having trouble locating the bootstrap import statement

Currently, I am working on a project in Angular where I have defined two styles in the angular.json file - styles.css and node_modules/bootstrap/dist/css/bootstrap.min.css. After running ng serve, it shows that it compiled successfully. However, upon ins ...

simultaneous ajax requests - encountering issues in getting a response from the initial one

I'm in the process of developing a small "ping" tool to verify the connectivity of our two servers. Here is the snippet of JavaScript code I am using: var t1, t2, t3, t4; function jsContactServers() { ajaxServerStatusWWW(); ajaxServerStatus ...

Enhancing Redux with numerous data inputs

Despite my efforts, I am facing an issue where I cannot change the values of two inputs in Redux as intended. // Within my component <Input label="name" value={initialUser.name} onChange={(e) => dispatch(onChangeInputAction(e))} name=" ...