I've setup a Svelte and Electron that uses Carbon Components. I have it working for the most part, but I'm stuck at debugging.
Here's a sample component I'm using for testing:
<script lang="ts">
import { Tile } from 'carbon-components-svelte'
let name = 'John Doe'
function test() {
console.log('You clicked me!')
}
</script>
<div>
<Tile>
<h2>{name}</h2>
<button on:click={test}>Click me</button>
</Tile>
</div>
When I load the component into a dev environment, I can't set breakpoints in Chrome's dev tools:
However, if I open my svelte.config.js
, which initially look:
import sveltePreprocess from 'svelte-preprocess'
import { optimizeImports } from "carbon-preprocess-svelte";
export default {
preprocess: [ sveltePreprocess(), optimizeImports() ]
}
And remove the optimizeImports()
call, then breakpoints work correctly.
Does anyone have any clue why this is happening? Clearly it's something to do with the carbon-preprocess-svelte
library. I created a basic project that reproduces the issue: https://github.com/troncoso/carbon-svelte-bug
TL;DR: The inline source map is kinda broken, it's a bug in
carbon-preprocess-svelte
.When you use:
import { Tile } from 'carbon-components-svelte'
, the default module resolution will import the named exportTile
from a big umbrella file atcarbon-components-svelte/lib/index.js
, which is a big minified code bundle. Clearly this is not optimal.optimizeImports
fromcarbon-preprocess-svelte
is a preprocessor that tries to optimize it by reading your import code then rewriting it into sth likeimport Tile from "carbon-components-svelte/src/Tile/Tile.svelte"
.Obviously there's some kind of bug in this preprocessor, that produces a broken source map. To be precise, it produces a source map that misses out some mapping positions. Chrome devtool relies on these mapping positions to set a breakpoint. When it's missing, it cannot set one.
When you manually rewrite the import, you skip this preprocessor, thus you avoid the wrong behavior. This is consistent to your current finding.
We might rest our case here and conclude that you should avoid using
optimizeImports
for now, and just report the bug to carbon team. Below will dive into more details.This is the inline source map of the bad case:
And here's the one of the good case:
They're base64 encoded json, if we decoded them, we get:
bad case:
good case:
Problem lies in the
mappings
field. You can make sense of both files with the source map decoder then compare by clicking the dump button. You'll see that some mapping information are simply missing in the bad case file.This article Anatomy of source maps, if it interests you, will guide you through the technical details of the js source map format (to understand the mysterious
;;OAAiB,IAAA,MAAA,+CAAA;;
part).But for now let's simply read the human-friendly dump:
And here's the dump for both files.
bad case mapping dump:
good case mapping dump: