-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsource-maps.test.js
More file actions
114 lines (92 loc) · 3.66 KB
/
source-maps.test.js
File metadata and controls
114 lines (92 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import path from 'node:path';
import { promises as fs } from 'node:fs';
import dedent from 'dedent';
import { setupTest, teardownTest, loadFixture, viteBuild } from './lib/lifecycle.js';
import { writeFixtureFile } from './lib/utils.js';
const writeConfig = async (dir, content) => writeFixtureFile(dir, 'vite.config.js', content);
let env;
test.before.each(async () => {
env = await setupTest();
});
test.after.each(async () => {
await teardownTest(env);
});
test('Should strip sourcemaps by default', async () => {
await loadFixture('source-maps', env);
await viteBuild(env.tmp.path);
const outDir = path.join(env.tmp.path, 'dist', 'assets');
const outDirAssets = await fs.readdir(outDir);
assert.not.ok(outDirAssets.find((f) => f.endsWith('.map')));
const outputChunk = path.join(
outDir,
outDirAssets.find((f) => /^index-.*\.js$/.test(f)),
);
const outputChunkCode = await fs.readFile(outputChunk, 'utf-8');
// Ensure arbitrary strings don't get stripped
assert.ok(outputChunkCode.match(/\/\/#\ssourceMappingURL=/));
// Ensure the sourcemap comment has been removed
assert.is(outputChunkCode.match(/^\/\/#\ssourceMappingURL=.*\.map$/m), null);
const outputAsset = path.join(
outDir,
outDirAssets.find((f) => /^worker-.*\.js$/.test(f)),
);
const outputAssetSource = await fs.readFile(outputAsset, 'utf-8');
assert.is(outputAssetSource.match(/^\/\/#\ssourceMappingURL=.*\.map$/m), null);
});
test('Should preserve sourcemaps if user has enabled them', async () => {
await loadFixture('simple', env);
await writeConfig(env.tmp.path, `
import { defineConfig } from 'vite';
import { vitePrerenderPlugin } from 'vite-prerender-plugin';
export default defineConfig({
build: { sourcemap: true },
plugins: [vitePrerenderPlugin()],
});
`);
await viteBuild(env.tmp.path);
const outDir = path.join(env.tmp.path, 'dist', 'assets');
const outDirAssets = await fs.readdir(outDir);
const outputJsFileName = outDirAssets.find((f) => f.endsWith('.js'));
assert.ok(outputJsFileName);
const outputJs = await fs.readFile(path.join(outDir, outputJsFileName), 'utf-8');
assert.match(outputJs, '//# sourceMappingURL=');
const outputMap = outputJs.match(/^\/\/#\ssourceMappingURL=(.*)\.map$/m)[1];
assert.ok(outDirAssets.includes(outputMap));
});
test('Should use sourcemaps to display error positioning for top-level throws', async () => {
await loadFixture('simple', env);
await writeFixtureFile(env.tmp.path, 'src/index.js', dedent`
document.createElement('div');
export async function prerender() {
return '<h1>Simple Test Result</h1>';
}`,
);
let message = '';
try {
await viteBuild(env.tmp.path);
} catch (error) {
message = error.message;
}
assert.match(message, 'ReferenceError: document is not defined');
assert.match(message, 'src/index.js:1:1');
});
test('Should use sourcemaps to display error positioning for throws during prerender', async () => {
await loadFixture('simple', env);
await writeFixtureFile(env.tmp.path, 'src/index.js', dedent`
export async function prerender() {
document.createElement('div');
return '<h1>Simple Test Result</h1>';
}`,
);
let message = '';
try {
await viteBuild(env.tmp.path);
} catch (error) {
message = error.message;
}
assert.match(message, 'ReferenceError: document is not defined');
assert.match(message, 'src/index.js:2:5');
});
test.run();