Understanding the mappings in a source map: Decoding the source map keys

Consider the following scenario with a stylesheet:

.d1
    width: 10px
    .d2
        width: 20px

When viewed using the nested output style, it leads to this css code:

.d1 {
  width: 10px; }
  .d1 .d2 {
    width: 20px; }

The source map (v3) indicates the mappings as follows:

AAAA,AAAA,GAAG,CAAC;EACA,KAAK,EAAE,IAAK,GAES;EAHzB,AAEI,GAFD,CAEC,GAAG,CAAC;IACA,KAAK,EAAE,IAAK,GAAG

After decoding it using this service, the result is:

([0,0](#0)=⇒[0,0]) | ([0,0](#0)=⇒[0,0]) | ([0,3](#0)=⇒[0,3]) | ([0,4](#0)=⇒[0,4])
([1,4](#0)=⇒[1,2]) | ([1,9](#0)=⇒[1,7]) | ([1,11](#0)=⇒[1,9]) | ([1,16](#0)=⇒[1,13]) | ([3,25](#0)=⇒[1,16])
([0,0](#0)=⇒[2,2]) | ([2,4](#0)=⇒[2,2]) | ([0,3](#0)=⇒[2,5]) | ([2,4](#0)=⇒[2,6]) | ([2,7](#0)=⇒[2,9]) | ([2,8](#0)=⇒[2,10])
([3,8](#0)=⇒[3,4]) | ([3,13](#0)=⇒[3,9]) | ([3,15](#0)=⇒[3,11]) | ([3,20](#0)=⇒[3,15]) | ([3,23](#0)=⇒[3,18])

The source was generated in a similar way using node-sass (libsass).

It seems like pairs of points from the original and resulting files are being represented in the source map. For example, the last pair on the second line: ([3,25](#0)=⇒[1,16]). Is row 3 column 25 an unexpected outcome?

Is there a mistake in my approach? Or could the source map be incorrect? Any suggestions on how to generate an accurate source map would be appreciated.

Answer №1

Discover an amazing tool that is not limited to use with just libsass - find out more details here.

Remember, you'll need a file with a source map embedded as a multiline comment and the sourcesContent field in it, similar to this example:

.d1 {
  width: 10px; }
  .d1 .d2 {
    width: 20px; }

/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJtYXBwaW5ncyI6IkFBQUEsR0FBSTtFQUNBLEtBQUssRUFBRSxJQUFJO0VBQ1gsT0FBSTtJQUNBLEtBQUssRUFBRSxJQUFJIiwic291cmNlcyI6WyIxLnNjc3MiXSwic291cmNlc0NvbnRlbnQiOlsiLmQxIHtcbiAgICB3aWR0aDogMTBweDtcbiAgICAuZDIge1xuICAgICAgICB3aWR0aDogMjBweDtcbiAgICB9XG59XG4iXSwibmFtZXMiOltdLCJmaWxlIjoiMS5jc3MifQ== */

Simply paste it into the Output field, click Analyze, and you're ready to go. The content from the source map will replace whatever was originally in the Input field. Use arrow keys to navigate between mappings.

Now let's delve into how to accomplish this using ruby's sass gem:

Gemfile:

source 'https://rubygems.org'
gem 'sass'

package.json:

{
  "dependencies": {
    "convert-source-map": "^1.3.0"
  }
}

1.scss:

.d1 {
    width: 10px;
    .d2 {
        width: 20px;
    }
}

1.js:

var convert = require('convert-source-map');
var fs = require('fs');
console.log(
    convert.fromJSON(
            fs.readFileSync('1.css.map')
    ).toComment()
    .replace(/^\/\//, '/*')
    .replace(/$/, ' */')
);

Then run these commands:

$ bundle install && npm i
$ rm -f 1.css* && bundle exec sass --sourcemap=inline 1.scss 1.css
$ cat 1.css | head -n -1 && node 1.js
.d1 {
  width: 10px; }
  .d1 .d2 {
    width: 20px; }

/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJtYXBwaW5ncyI6IkFBQUEsR0FBSTtFQUNBLEtBQUssRUFBRSxJQUFJO0VBQ1gsT0FBSTtJQUNBLEtBQUssRUFBRSxJQUFJIiwic291cmNlcyI6WyIxLnNjc3MiXSwibmFtZXMiOltdLCJmaWxlIjoiMS5jc3MifQ== */

Add the result to the Output field and you're done.

The process may seem complex at first, but it's much easier than manually inspecting source maps. Trust me, I speak from experience :)

If you have a source map in JSON format, like this:

{ version: 3,
  sourceRoot: '/path/to/dir',
  sources: [ '1.css' ],
  sourcesContent: [ 'body {\n    background: #ddd;\n}\ndiv {\n    width: 100px;\n    height: 100px;\n    margin: auto;\n    background: #666;\n}\n' ],
  names: [],
  mappings: 'AAAA,AAAA,IAAI,AAAC,CACD,UAAU,CAAE,IAAI,CACnB,AACD,AAAA,GAAG,AAAC,CACA,KAAK,CAAE,KAAK,CACZ,MAAM,CAAE,KAAK,CACb,MAAM,CAAE,IAAI,CACZ,UAAU,CAAE,IAAI,CACnB' }

You can convert it into a comment like so:

var convertSourceMap = require('convert-source-map')
console.log(convertSourceMap.fromJSON(mapAsAString).toComment({multiline: true}));

P.S. Check out some other useful tools for working with source maps and get an sourcemap-codec package for added convenience.

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

Top method for concealing image within adaptable block

I am currently working on making the header slideshow responsive. I encountered an issue where I couldn't use the CSS property background with cover. Instead, I need to utilize <img> inside a div. <div class="parent"> <img src="slide ...

Creating a scoreboard layout with HTML and CSS

I am attempting to create a scoreboard in the following format: 0 - 0 Home - Away The issue I am facing is that if the length of the Home team's name is longer, it pushes the dash (-) further. I want the dash to be a ...

Ensure that the Materialize CSS modal form remains open even after submission

Is there a way to reload a form inside a materialize modal with empty fields without closing the modal after submitting? The issue is that currently, when the submit button is clicked, the modal closes and redirects. <div class="modal" id="docM ...

The div element displays a vertical scrollbar exclusively

Can you explain why the div is only displaying a vertical scrollbar? Although the canvas is larger than the div in both dimensions: div { padding-top: 2px; height: 709px; width: 889px; overflow: auto; } <div> <canvas width="1405" sty ...

Styling Your Pricing Table with a Unique Bootstrap Card Design

Today, I've been working on the bootstrap card lesson and followed along with my teacher. However, the grid markup effect isn't functioning as expected. Initially, I doubted myself and thought I had inputted something wrong, but upon closer insp ...

Tips for ensuring compatibility of CSS in IE7 and IE8

Despite using the following styles to dynamically display alternative colors in my HTML code, I am facing compatibility issues with IE7 and IE8. Surprisingly, everything works perfectly fine on IE9 and above. It seems these styles are not supported by IE7 ...

The appearance of responsive CSS is altered when deployed compared to when it is viewed on the

I'm a beginner in web development and facing an issue with my mobile CSS. The strange thing is that when I view it on localhost, everything looks great, but once deployed (tried both Heroku and GitHub), it appears distorted. Even when I make extreme c ...

Toggle class on element based on presence of class on another element

If I have 4 boxes and the user is required to choose one. I aim to assign an active class to the box that the user clicks on. However, if the user selects another box, I want to remove the class from the first clicked box and apply it to the newly clicked ...

Retrieving a completely random caption from a text file with JavaScript and showcasing it in HTML

I need help loading a random caption each time my page loads. I have a text file with a string on each line, but I'm new to HTML and JavaScript. Here's my code: <div class="centerpiece"> <h1>DEL NORTE BANQUEST</h1 ...

Is it possible to have Bootstrap 3 Navigation Tabs positioned at both the top and bottom of a

While working on a Bootstrap 3 website, I discovered that it is possible to have both a top nav-tab and bottom nav-tab section on the same page. These two distinct tab menus can control the display of the same tab content areas. However, I encountered an ...

Tips for incorporating a dynamic CSS style sheet into a standalone xhtml document

I am faced with the task of incorporating two CSS classes into a single xhtml. For instance: Take, for example, display.xhtml, which will be utilized by two different applications, each requiring its own CSS styling. This means that if display.xhtml is ...

The alignment of content in the <a> tag is not centered within a <ul> list item (horizontal menu)

I am new to web development and have recently started creating my first webpage using HTML and CSS. I'm still learning and don't have a strong understanding yet. One issue I'm facing is with centering elements on the page. I have tried usin ...

JavaScript class name modifications are not functioning as expected

Testing a sprite sheet on a small web page I created. The code for each sprite in sprites.css is structured like this... .a320_0 { top: 0px; left: 0px; width: 60px; height: 64px; background: url("images/sprites.png") no-repeat -787 ...

Cascading Style Sheets variable and Sassy CSS mixin

I'm facing a challenge involving the use of CSS variables in conjunction with my VueJs app. The goal is to make a hover effect (changing background-color) customizable by the application, while having a default value set in a nested SCSS map using the ...

The Cordova minification tool fails to compress files within the browser platform

I recently installed the cordova-minify plugin to compress the javascript code in my Cordova app. When I tried running the command: cordova build browser An output message appears: cordova-minify STARTING - minifying your js, css, html, and images. ...

Anchor elements are not enclosed by borders

I seem to be overlooking something obvious here, so my apologies in advance. I have the following HTML code and I am trying to use a CSS rule to add a border around the "third-content-wrapper" class. However, the border is only applied to the text and no ...

Steps for incorporating a font into a website

Could someone advise me on how to include the font "Burbank.otf" in my css file and apply it to text in HTML? Here is what I currently have in my HTML: <!DOCTYPE HTML> <html> <head> <link rel="stylesheet" type="text/css" href="mystyl ...

Differentiate pointer for checkbox HTML

Just starting out with html and css, and I'm working with a checkbox: <input type="checkbox" name="vehicle" value="Bike">I have a bike<br> My goal is to display a hand icon when hovering over the checkbox. Ho ...

What is the best way to zoom in on an image and make it move off the screen as I scroll through my webpage?

I am working on a website design where I would like to implement an image zoom effect as the user scrolls down. Additionally, I want the image to move to either the left or right side of the screen as it goes out of view when scrolling to the bottom. While ...

The process of setting up React in the terminal becomes tricky when the VS code editor is directing to a non-existent path

Issue with VS Code editor not recognizing existing path Recently attempted to install React using the npx command in the terminal, following steps from various tutorials on YouTube. Despite trying all suggested methods, the installation didn't succee ...