Skip to content

Commit be73db6

Browse files
dominikgBob Fanger
andauthored
fix(vitePreprocess): use relative paths in sourcemap sources and output css dependencies (#625)
* fix: Sourcemap for "/path/to/Component.svelte" points to missing source files * refactor: more generic implementation and use unit test instead of e2e * fix: take sourceRoot into account and add unit tests for sourcemap utils * fix: use unique suffix for vite-preprocess and simplify suffix stripping * chore: fix testcase name * fix: take care of absolute path syntax differences between linux and windows * fix: make sure sourceRoot is taken into account and removed * fix: use fileURLToPath and prepend sourceRoot with single slash * fix: add dependencies to vite preprocess style output and more tests * fix: update pnpm-lock * fix: lint * fix: account for possible empty sources value and revert fileURLToPath use as it turns out there can be file:///<not-absolute-path> inputs wtf * fix: write tests in os agnostic way, add test for relative sourceroot --------- Co-authored-by: Bob Fanger <[email protected]>
1 parent 2cd6475 commit be73db6

File tree

18 files changed

+351
-28
lines changed

18 files changed

+351
-28
lines changed

.changeset/big-candles-agree.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+
fix(vitePreprocess): add dependencies to style preprocessor output

.changeset/real-jokes-help.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+
fix(vitePreprocess): use relative paths without lang suffix in sourcemaps to avoid missing source file errors.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { browserLogs, getColor, getText, isBuild } from '~utils';
2+
import { expect } from 'vitest';
3+
4+
test('should not have failed requests', async () => {
5+
browserLogs.forEach((msg) => {
6+
expect(msg).not.toMatch('404');
7+
});
8+
});
9+
10+
test('should apply css compiled from scss', async () => {
11+
expect(await getText('#test')).toBe('red');
12+
expect(await getColor('#test')).toBe('red');
13+
expect(await getText('.foo')).toBe('magenta');
14+
expect(await getColor('.foo')).toBe('magenta');
15+
});
16+
17+
if (!isBuild) {
18+
test('should generate sourcemap', async () => {
19+
const style = await getText('style');
20+
const lines = style.split(`\n`).map((l) => l.trim());
21+
const css = lines[0];
22+
const mapComment = lines[1];
23+
expect(css).toBe(
24+
'.foo.s-XsEmFtvddWTw{color:magenta}#test.s-XsEmFtvddWTw{color:red}.s-XsEmFtvddWTw{}'
25+
);
26+
const b64start = '/*# sourceMappingURL=data:application/json;base64,';
27+
const b64end = ' */';
28+
expect(mapComment.startsWith(b64start));
29+
expect(mapComment.endsWith(b64end));
30+
const map = JSON.parse(
31+
Buffer.from(mapComment.slice(b64start.length, -1 * b64end.length), 'base64').toString('utf-8')
32+
);
33+
expect(map.sources).toStrictEqual(['foo.scss', 'App.svelte']);
34+
expect(map.file).toBe('App.svelte');
35+
// we are not testing the quality of the mapping here, just that it exists.
36+
expect(map.mappings).toBeDefined();
37+
});
38+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width,initial-scale=1" />
6+
7+
<title>Svelte app</title>
8+
9+
<script type="module" src="/src/main.js"></script>
10+
</head>
11+
12+
<body></body>
13+
</html>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "e2e-tests-css-dev-sourcemap",
3+
"private": true,
4+
"version": "0.0.0",
5+
"type": "module",
6+
"scripts": {
7+
"dev": "vite",
8+
"build": "vite build",
9+
"preview": "vite preview"
10+
},
11+
"devDependencies": {
12+
"@sveltejs/vite-plugin-svelte": "workspace:^",
13+
"svelte": "^3.58.0",
14+
"vite": "^4.2.1",
15+
"sass": "^1.61.0"
16+
}
17+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<div id="test">red</div>
2+
<div class="foo">magenta</div>
3+
4+
<style lang="scss">
5+
@import './foo';
6+
#test {
7+
& {
8+
color: red;
9+
}
10+
}
11+
</style>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.foo {
2+
color: magenta;
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import App from './App.svelte';
2+
3+
const app = new App({
4+
target: document.body
5+
});
6+
7+
export default app;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/// <reference types="svelte" />
2+
/// <reference types="vite/client" />
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
2+
3+
export default {
4+
preprocess: [vitePreprocess()]
5+
};

0 commit comments

Comments
 (0)