Turn off base64 encoding for background URLs in the Minify package

When I use the minify tool to compress my js and css files, I noticed that the files containing background:url(...) statements actually increased in size. This happened because the urls were encoded to base64.

I tried to disable this base64 encoding feature using the css-base64-images function, but it seems that it is not possible based on an issue raised back in 2016.

Here's a snippet from my package.json:

  "devDependencies": {
    "minify": "^9.1.0",
    "postcss-cli": "^10.0.0"
  }

This is a part of my code:

import { minify } from 'minify';

const myFunction = () => {
  /* some code... */

  minify(filepath).then((file) => {
                    /* some code... */
  });
}

According to an informative article, utilizing base64 for image optimization may not always be necessary. In my case, some css files have ballooned to10,000 KB from 40-50 KB post-minification, prompting me to want to deactivate base64.

Answer №1

After much experimentation, I managed to find a workaround for disabling the functionality of base64.

When using the minify function, it requires two parameters - name and userOptions.

The userOptions is then relayed to

node_modules\css-b64-images\lib\css-b64-images.js
. Within this file lies the function called replaceUrlByB64, which skips encoding to base64 if the file size exceeds the specified maxSize.

if (stat.size > options.maxSize){
      return cb(new Error('Skip ' + imageUrl + ' Exceed max size'), css);
}

My solution:

import { minify } from 'minify';

/* To avoid base64 replacement in minify, set image maxSize to 0.
   Function: replaceUrlByB64 within node_modules/css-b64-images/lib/css-b64-images.js */
const skipReplacingUrlByB64 = {
    img : {
        maxSize: 0,
    },
}

const myFunction = () => {
  /* some code... */

  minify(filepath, skipReplacingUrlByB64).then((file) => {
                    /* some code... */
  });
}

The structure of the object skipReplacingUrlByB64 mirrors how userOptions is fetched in node_modules\minify\lib\img.js

const options = {
    ...defaultOptions,
    ...userOptions?.img || {},
};

The contents of package.json remain unchanged as depicted in the original post.

  "devDependencies": {
    "minify": "^9.1.0",
    "postcss-cli": "^10.0.0"
  }

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

What could be causing the shadowbox not to display in Internet Explorer?

I am currently working on fixing a shadowbox issue in Internet Explorer. The page where the shadowbox is located can be found at this link: Here is the HTML code: <div class="hero-image"> <a href="m/abc-gardening-australia-caroli ...

Z-index problem occurring with rotateY on Safari and Chrome Mobile

https://jsfiddle.net/nxbg7rq3/ I've been struggling to get the .mask completely on top of the .screen in this example. It works fine in most browsers, but Safari and Chrome Mobile are giving me a hard time. I've experimented with various solutio ...

Achieve full height for the span tag within a div using CSS styling

I am facing a particular scenario: In a bootstrap row with 2 columns: - the first column contains a table with a button to add new rows - the second column has a label and a span (although it doesn't have to be a span) Both columns have equal hei ...

The jQuery scrollTop function seems to be malfunctioning following an ajax request

My ajax call is functioning properly, but I am experiencing an issue with the scrollTo plugin from JQuery. It is not positioning the content where I want it to be located below. //This function reveals the email body when a list item is clicked. jQue ...

The incorporation of zoom disrupts the smooth scrolling capability of the menu

My landing page has a menu that scrolls users to the selected section. However, my client prefers the page at a 90% zoom level. To accommodate this request, I added the following line of code: body { zoom:90%; } Unfortunately, when I click on a menu o ...

Checkboxes display on a separate line when rendered inlines

I'm having an issue with the rendering of my checkbox in CSS. The checkbox is currently displaying on a separate line from the text, but I would like it to be inline with the rest of the content. For reference, here is the full CSS and HTML code: htt ...

Encountering a webpack issue stating "Module not found: Error: Cannot resolve module" within the fluxible framework after re-installing the node_modules package

I encountered some issues with my fluxible project. After deleting all files in node_modules and reinstalling using npm install -d, I am now getting errors when trying to run the project with npm run dev. The command I am using is node webpack-dev-server.j ...

Tips for increasing the size of Material-ui Rating icons

I am currently utilizing npm to develop a widget. I aim to utilize the material-ui Rating component and have successfully integrated it. However, when I embed the widget on a webpage, the html font-size is set at 62.5%, causing the component to appear too ...

I noticed that the node_modules folder has mysteriously vanished from my

When I tried running npm install in the terminal of VS Code. PS D:\work\backEnd> npm install npm WARN old lockfile npm WARN old lockfile The package-lock.json file was created with an older version of npm, npm WARN old lockfile so ...

Having issues with the CSS dropdown menu not activating when hovering

I am facing an issue with a simple menu that includes a dropdown feature upon hovering. The code works perfectly fine in Fiddle, however, it fails to work when running the entire page locally on IE. Can someone please assist me? (here's my code at fi ...

Show a pop-up form when a user focuses on it using React

Hello, I have been looking for a way to create an overlay with a form that appears when a specific input field is clicked. I am trying to achieve this using React. Can someone please advise me on how to accomplish this? Here is my code import React, { Co ...

Failure to build using the spread operator is unique to the Travis CI environment

I encountered an issue when running the build command npm run build locally for my website. However, on Travis CI, it fails with the following error: > node scripts/build.js /home/travis/build/PatrickDuncan/patrickduncan.github.io/node_modules/@hapi/ho ...

"Woops! An error occurred with the message: "SassError: Could not locate the specified target selector" while working with SCSS Modules and incorporating

Currently, I am working with Next.js and utilizing SCSS Modules. To incorporate Bootstrap into my project, I opted to install it using the command npm install bootstrap. Following this installation, I went ahead and created a new file titled common.scss wi ...

Generate a fresh line within the source code

Currently, I am facing a challenge with dynamically loading CSS, JS, and other components as it appears messy when viewed from the source. Although this issue does not impact functionality, I am not satisfied with how it looks in the source code. When exam ...

Switch classes according to scrolling levels

My webpage consists of multiple sections, each occupying the full height and width of the screen and containing an image. As visitors scroll through the page, the image in the current section comes into view while the image in the previous section disappe ...

A guide on adjusting the height of UI elements in Semantic: steps for modifying

One of my current challenges involves using Semantic UI and embedding it within an iFrame tag to display a video. However, I am struggling to adjust the height of the Semantic UI element. <div className="video_box ui embed"> ...

What is the process for enabling the experimental-modules option when running an npm package's bin command?

Beginning with Node v8.5.0, the support for ES6 style modules has been introduced. import x from 'x' You can access this feature by running node using the --experimental-modules option, like so: node --experimental-modules test.mjs By utilizi ...

Is it necessary to always include all styles for jQuery UI separately in my project?

While reading various articles, I have noticed a common suggestion to explicitly bundle each jQueryUI .css file, such as the answer provided on How to add jQueryUI library in MVC 5 project?. However, upon examining the .css files within the Content/themes/ ...

What are some ways to modify the appearance of the post title table using CSS?

In this snippet of HTML code- <table style='min-width:60%;height:25px;'> <tr> <td><b><data:post.title></b></td> <td id='date-and-author' style='position: relative;bo ...

Requesting an image from the live website using a GET method

Recently, I registered a domain for my personal website. While the site is still a work in progress, I noticed that the logo image fails to display when viewed through the browser on the site. Surprisingly, it shows up perfectly fine when accessed via loca ...