question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Directory in sourcemap is incorrect, impacting `vite serve` (but not Rollup)

See original GitHub issue

Troubleshooting

  1. Does tsc have the same output? If so, please explain why this is incorrect behavior

    No, due to this plugin changing outDir

  2. Does your Rollup plugin order match this plugin’s compatibility? If not, please elaborate

    As far as I know - I have it configured as a pre plugin with vite

  3. Can you create a minimal example that reproduces this behavior? Preferably, use this environment for your reproduction

    https://stackblitz.com/edit/vitejs-vite-rjjhlb?file=vite.config.js

What happens and why it is incorrect

When this plugin emits sourcemaps, the source map URL is incorrect, as the plugin configures typescript with a temporary output directory, and the sourcemap is generated relative to that. The sources property is modified in the .d.ts.map, but not similarly adjusted in the .js.map.

This works fine with Rollup itself, as when it merges sourcemaps it (from what I can tell) winds up only looking at the “root” sourcemap in the sourcemap chain. However, when using vite, it’s serve mode does not use rollup and instead uses @ampproject/remapping to combine sourcemaps, which relies on the sources URL being properly formed.

If you look at the script served by vite, you get:

sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJtYXBwaW5ncyI6IkFBQUEsT0FBTztBQUNQLE9BQU8sb0JBQW9CO0FBQzNCLFNBQVMsb0JBQW9CO0FBRTdCLFNBQVMsY0FBOEIsTUFBTSxFQUFHLFlBQVk7QUFBQTtBQUFBO0FBQUE7QUFBQTtBQUFBO0FBQUEsa0JBTTFDO0FBQUE7QUFBQTtBQUFBO0FBQUE7QUFBQTtBQUFBO0FBQUE7QUFBQTtBQUFBO0FBQUE7QUFZbEIsYUFBYSxTQUFTLGNBQWlDLFVBQVUsQ0FBRSIsIm5hbWVzIjpbXSwic291cmNlcyI6WyIuLi8uLi8uLi8uLi9zcmMvbWFpbi50cyJdLCJmaWxlIjoiL2hvbWUvcHJvamVjdHMvdml0ZWpzLXZpdGUtcmpqaGxiL3NyYy9tYWluLnRzIiwic291cmNlc0NvbnRlbnQiOltudWxsXX0=

which decodes to:

{"version":3,"mappings":"AAAA,OAAO;AACP,OAAO,oBAAoB;AAC3B,SAAS,oBAAoB;AAE7B,SAAS,cAA8B,MAAM,EAAG,YAAY;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,kBAM1C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAYlB,aAAa,SAAS,cAAiC,UAAU,CAAE","names":[],"sources":["../../../../src/main.ts"],"file":"/home/projects/vitejs-vite-rjjhlb/src/main.ts","sourcesContent":[null]}

The sources property should read ["../src/main.ts"]. Inserting log statements into this plugin confirms that this is what is being output from the plugin.

While technically this is a difference in behavior between vite and rollup and could arguably be a vite bug, my feeling was that this plugin should still expose accurate source URIs, especially since this is already handled for the .d.ts.map

Environment

Versions

System:
    OS: Linux 5.0 undefined
    CPU: (4) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 16.14.2 - /usr/local/bin/node
    Yarn: 1.22.10 - /bin/yarn
    npm: 7.17.0 - /bin/npm
  npmPackages:
    rollup-plugin-typescript2: ^0.32.1 => 0.32.1 
    typescript: ^4.6.4 => 4.7.4 
    vite: ^3.0.2 => 3.0.4

vite.config.js

:
import { defineConfig } from 'vite';
import typescriptPlugin from 'rollup-plugin-typescript2';

export default defineConfig({
  plugins: [
    {
      ...typescriptPlugin({}),
      enforce: 'pre',
    },
  ],
});

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:9

github_iconTop GitHub Comments

1reaction
luxaritascommented, Aug 3, 2022

Ah, my apologies if my tone was too strict on that one!

No worries at all! Appreciate the additional context, just wanted to make sure we’re on the same page which it seems we are. Thanks again for taking the time to look into this.

1reaction
luxaritascommented, Jul 30, 2022

I realized to get this working for now, I can pass:

tsconfigOverride: {
    compilerOptions: {
        sourceRoot: '../src'
    }
}

From what I understand, sourceRoot (or changes to the entries in sources itself) would always need to be resolved from the location of the output… which isn’t known during build hooks. Theoretically, you could specify an absolute path to the source files, but this would only work locally (e.g. fails when running on stackblitz). Does this mean Vite’s sourcemap merging approach would have to be changed to account for this, or is there some other way of reliably being able to determine how to resolve the source location?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring Vite
Note Vite supports using ES modules syntax in the config file even if the project is not using native Node ESM, e.g. type:...
Read more >
Rollup is not generating typescript sourcemap - Stack Overflow
I just cloned your project, ran npm run build , and got the typescript sourcemap generated just fine. It outputs bundle.js.map . What...
Read more >
rollup.js
Rollup is a module bundler for JavaScript which compiles small pieces of code into something larger and more complex, such as a library...
Read more >
vite.js:7 await import('source-map-support').then((r) => r ... - You.com
Make sure this is a Vite issue and not a framework-specific issue. ... 'chrome87', 'safari13.1' ], vite:config sourcemap: false, vite:config rollupOptions: ...
Read more >
Docs • Svelte
SvelteKit utilizes Vite to build your code and handle server-side ... npm run build will generate HTML, JS and CSS files inside the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found