Skip to content

Commit 5e9ec70

Browse files
authored
fix(vitePreprocess): remove dangling pure annotations (#609)
1 parent e5ecca9 commit 5e9ec70

File tree

5 files changed

+71
-1
lines changed

5 files changed

+71
-1
lines changed

.changeset/serious-sloths-grab.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): remove problematic pure annotations that could lead to wrong build output in some cases
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { getColor, getText } from '~utils';
1+
import { getColor, getText, browserLogs } from '~utils';
2+
import { expect } from 'vitest';
23

34
test('should render App', async () => {
45
expect(await getText('h1.foo')).toBe(`Hello world`);
@@ -7,3 +8,8 @@ test('should render App', async () => {
78
expect(await getColor('#foo-title')).toBe('magenta');
89
expect(await getColor('p.note')).toBe('rgb(255, 62, 0)');
910
});
11+
12+
test('should not mangle code from esbuild pure annotations', async () => {
13+
expect(browserLogs.some((log) => log.startsWith('pure test 1'))).toBe(true);
14+
expect(browserLogs.some((log) => log.startsWith('pure test 2'))).toBe(true);
15+
});

packages/e2e-tests/preprocess-with-vite/src/Foo.svelte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<script lang="ts">
22
let foo: string = 'stylus';
33
export let bla: string = 'blub';
4+
console.log('pure test 1', new Date());
5+
console.log('pure test 2');
46
</script>
57

68
<h1 id="foo-title">Styles with {foo} {bla}</h1>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { describe, it, expect } from 'vitest';
2+
import { createCompileSvelte } from '../compile';
3+
import { ResolvedOptions } from '../options';
4+
const options: ResolvedOptions = {
5+
compilerOptions: {
6+
dev: false,
7+
format: 'esm',
8+
css: 'external'
9+
},
10+
isBuild: false,
11+
isDebug: false,
12+
isProduction: false,
13+
isServe: false,
14+
root: process.cwd()
15+
};
16+
17+
describe('createCompileSvelte', () => {
18+
it('returns function', () => {
19+
const compileSvelte = createCompileSvelte(options);
20+
expect(typeof compileSvelte).toBe('function');
21+
});
22+
23+
describe('compileSvelte', async () => {
24+
it('removes dangling pure annotations', async () => {
25+
const code = `<script>
26+
const x=1;
27+
console.log('something',/* @__PURE__ */ new Date());
28+
console.log('something else');
29+
</script>
30+
<div>{x}</div>`;
31+
const compileSvelte = createCompileSvelte(options);
32+
const output = await compileSvelte(
33+
{
34+
cssId: 'svelte-xxxxx',
35+
query: undefined,
36+
raw: false,
37+
ssr: false,
38+
timestamp: Date.now(),
39+
id: 'id',
40+
filename: '/some/File.svelte',
41+
normalizedFilename: 'some/File.svelte'
42+
},
43+
code,
44+
{}
45+
);
46+
expect(output.compiled.js.code).not.toContain('/* @__PURE__ */\n');
47+
});
48+
});
49+
});

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,14 @@ const _createCompileSvelte = (makeHot: Function) => {
121121

122122
const endStat = stats?.start(filename);
123123
const compiled = compile(finalCode, finalCompileOptions);
124+
125+
// prevent dangling pure comments
126+
// see https://github.com/sveltejs/kit/issues/9492#issuecomment-1487704985
127+
// uses regex replace with whitespace to keep sourcemap/character count unmodified
128+
compiled.js.code = compiled.js.code.replace(
129+
/\/\* [@#]__PURE__ \*\/(\s*)$/gm,
130+
' $1'
131+
);
124132
if (endStat) {
125133
endStat();
126134
}

0 commit comments

Comments
 (0)