Directory in sourcemap is incorrect, impacting `vite serve` (but not Rollup)
See original GitHub issueTroubleshooting
-
Does
tsc
have the same output? If so, please explain why this is incorrect behaviorNo, due to this plugin changing
outDir
-
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 -
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
:
vite.config.js
import { defineConfig } from 'vite';
import typescriptPlugin from 'rollup-plugin-typescript2';
export default defineConfig({
plugins: [
{
...typescriptPlugin({}),
enforce: 'pre',
},
],
});
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:9
Top GitHub Comments
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.
I realized to get this working for now, I can pass:
From what I understand,
sourceRoot
(or changes to the entries insources
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?