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

Placing a pair of labels onto a bootstrap form while ensuring column alignment

I'm trying to format a form according to my UI/UX specifications, where there is a parent label and two sub-labels. Can this be achieved using Bootstrap? https://i.stack.imgur.com/J8wJZ.png However, I'm struggling to align the columns and rows ...

JavaScript multi-click navigation menu

I'm relatively new to JavaScript and I've been struggling with using multiple onClick attributes. While I have some code that works, I feel like there might be a simpler and cleaner solution to my problem. What I'm trying to achieve is a na ...

Having trouble with the jQuery each function's functionality

I am creating circular counters for surveys by generating a counter for each answer option. Currently, I am utilizing this "plugin": Issue: The problem lies in the plugin not fetching the text value from the <div> element and failing to draw coun ...

Steps for installing the most recent update of Primeng, version 7.1.0

Currently, I am working with Angular 7.1.0 and using PrimeNG version 6.1.6. I need to upgrade my PrimeNG version to 7.1.0. I tried running the command 'npm install primeng --save' but encountered an error message like... PS E:\srini_vision& ...

Arranging arrows strategically along a line and ensuring they are positioned correctly above all div elements

I'm really struggling with this issue. I have a situation where I need to center a downward pointing arrow within some divs. It should be positioned in the middle, on a brown line, and on top of everything else. The last div should also include the a ...

Is there a way to turn off the "swipe to navigate back" feature in Microsoft Edge using JavaScript or jQuery?

Working on a website that features horizontal object rotation. For instance, starting with the front view of an object followed by side, back, other side, and then back to the front. In this case, there are 24 images of an object, each taken at a 15 degre ...

Placing error notifications beneath my table rows for a more polished appearance

In the process of creating a review page for my app that showcases uploaded files, I've encountered an issue. Whenever a user uploads files with errors, I want to display a warning message below each specific file. Currently, I'm able to show the ...

Tips for fading an image as you scroll using CSS and JavaScript?

I have been dedicating my day to mastering the art of website development. However, I am facing a challenge that is proving to be quite difficult. I am looking to create a smooth transition effect where an image gradually blurs while scrolling down, and re ...

Are browser window.sessionStorage and ng libraries like ngx-webstorage considered safe to implement in Angular 4?

Seeking the optimal approach for utilizing Session Storage in Angular 4 or newer versions such as 5. The current project is making use of HTML5 window localStorage and sessionStorage. Is it advisable to implement third party libraries like angular-2-local ...

What's the issue with this HTML button not functioning properly?

I'm having an issue with a button in my code that is supposed to change the text from 'hello' to 'Goodbye', but for some reason, it's not working. I have ensured that my code is properly indented in my coding program. Below i ...

Set up npm to handle dependencies by utilizing JFrog Artifactory as a proxy for both the npm registry and GitHub repositories

I am currently facing a challenge with my build server, as it does not have internet access. In order to resolve dependencies from both github.com and registry.npmjs.org, I have set up an npm repo in Artifactory(jfrog) to act as a proxy for registry.npmjs. ...

Can a custom CSS be linked directly to the angular-cli.json file?

I'm trying to incorporate custom CSS into my angular-cli.json file, but I haven't been able to find a successful method. I attempted to use the following code: "styles": [ "styles.css", "http://site.test/mycss.css" ], However, it does ...

What is the best way to manage the package-lock.json file during deployment from git using SSH?

In my deployment process, I utilize a Git repository to check code in. By using web hooks, a deployment script is triggered on the production server. Once connected to Git via SSH and a .pem key on the server, I perform a Git pull, npm install, webpack bui ...

Function is raising an error with the wrong value or text

I am currently testing a form that I am in the process of developing. My goal is to have different values returned each time I click on a different item from the dropdown menu. If this explanation isn't clear, please take a quick look at my pen for cl ...

The image is non-adjustable to different screen sizes

Currently, I am in the process of revamping a friend's website using Next.js and Tailwind. However, I've encountered a challenge with the background image specifically on iPhones. While it appears fine on browsers like Chrome and Safari as well a ...

Dividing image styles within css

I am trying to incorporate 4 arrow images into a CSS class (up, down, right, left). I started with a basic arrow css class: .arrow { background-repeat: no-repeat; background-position: center; } Next, I created subclasses like: .arrow .up { ...

Adding CSS styles to an HTML page using JavaScript

Unfortunately, I do not have the ability to access the HTML directly as it is generated dynamically by a program. However, I do have access to the JS page that is linked to it. As an example, I am able to manipulate elements using JavaScript like so: ...

What is the best way to incorporate width adjustments in an image source for a responsive website?

Currently learning CSS and experimenting with it on an HTML page. For reference, I'm using this link. Encountering an issue with a responsive header image that adjusts size based on device (small on mobile, large on desktop). Is it possible to achiev ...

Encountering a problem while trying to install Gulp

Struggling to get Gulp up and running on my system. When I enter the command "npm install gulp" in the terminal, an error message https://i.sstatic.net/113lC.png keeps popping up. Can someone guide me through the process of installing Gulp? The internet ...

The malfunction of CSS3 effect

I designed a website and used DIV CSS to call an image. .header_bottom_area { background: url(../img/HomepagePanels.jpg)no-repeat scroll center center; max-width: 100%; width: auto; height: 911px; } I would like to have the same image as shown in ...