Skip to content

Commit d57bd70

Browse files
authored
fix: Improve css sourcemap accuracy (#305)
Fixes #300
1 parent bdebf4e commit d57bd70

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

.changeset/thin-swans-talk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': patch
3+
---
4+
5+
Improved CSS Source Maps when using vite's `css: { devSourcemap: true }`

packages/playground/kit-demo-app/svelte.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ const config = {
88
// Override http methods in the Todo forms
99
methodOverride: {
1010
allowed: ['PATCH', 'DELETE']
11+
},
12+
vite: {
13+
css: { devSourcemap: true }
1114
}
1215
}
1316
};

packages/vite-plugin-svelte/src/utils/preprocess.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { Preprocessor, PreprocessorGroup, Processed, ResolvedOptions } from './o
1111
import { TransformPluginContext } from 'rollup';
1212
import { log } from './log';
1313
import { buildSourceMap } from './sourcemap';
14+
import path from 'path';
1415

1516
const supportedStyleLangs = ['css', 'less', 'sass', 'scss', 'styl', 'stylus', 'postcss'];
1617

@@ -57,7 +58,7 @@ function createViteStylePreprocessor(config: ResolvedConfig): Preprocessor {
5758
)) as TransformResult;
5859
// patch sourcemap source to point back to original filename
5960
if (transformResult.map?.sources?.[0] === moduleId) {
60-
transformResult.map.sources[0] = filename;
61+
transformResult.map.sources[0] = path.basename(filename);
6162
}
6263
return {
6364
code: transformResult.code,
@@ -94,7 +95,10 @@ function createInjectScopeEverythingRulePreprocessorGroup(): PreprocessorGroup {
9495
s.append(' *{}');
9596
return {
9697
code: s.toString(),
97-
map: s.generateDecodedMap({ source: filename, hires: true })
98+
map: s.generateDecodedMap({
99+
source: filename ? path.basename(filename) : undefined,
100+
hires: true
101+
})
98102
};
99103
}
100104
};

0 commit comments

Comments
 (0)