Skip to content

Commit 52ead98

Browse files
authored
refactor: Strip map comments if user has not enabled sourcemaps (#12)
* fix: Strip sourcemap size output by default * test: Add cases for stripping & preserving sourcemap size
1 parent 3bb4cce commit 52ead98

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/plugins/prerender-plugin.js

+5
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,12 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
136136
config.customLogger = {
137137
...logger,
138138
info: (msg, options) => {
139+
if (msg.includes(' │ map:') && !userEnabledSourceMaps) {
140+
msg = msg.replace(/ map:.*/, '');
141+
}
142+
139143
loggerInfo(msg, options);
144+
140145
if (msg.includes('built in')) {
141146
loggerInfo(
142147
kl.bold(

tests/logs.test.js

+32-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { test } from 'uvu';
22
import * as assert from 'uvu/assert';
33

44
import { setupTest, teardownTest, loadFixture, viteBuildCli } from './lib/lifecycle.js';
5+
import { writeFixtureFile } from './lib/utils.js';
6+
7+
const writeConfig = async (dir, content) => writeFixtureFile(dir, 'vite.config.js', content);
58

69
let env;
710
test.before.each(async () => {
@@ -12,19 +15,45 @@ test.after.each(async () => {
1215
await teardownTest(env);
1316
});
1417

15-
test('Should support the `prerenderScript` plugin option', async () => {
18+
test('Should log which routes were prerendered & where they were discovered', async () => {
1619
await loadFixture('logs/prerendered-routes', env);
1720
const output = await viteBuildCli(env.tmp.path);
1821
await output.done;
1922

20-
const idx = output.stdout.findIndex((line) => line.includes('Prerendered'));
2123
// The prerender info is pushed as a single log line
22-
const stdout = output.stdout.slice(idx)[0];
24+
const stdout = output.stdout.find((line) => line.includes('Prerendered'));
2325

2426
assert.match(stdout, 'Prerendered 3 pages:\n');
2527
assert.match(stdout, '/\n');
2628
assert.match(stdout, '/foo [from /]\n');
2729
assert.match(stdout, '/bar [from /foo]\n');
2830
});
2931

32+
test('Should strip sourcemap sizes from logs if user has not enabled sourcemaps', async () => {
33+
await loadFixture('logs/prerendered-routes', env);
34+
const output = await viteBuildCli(env.tmp.path);
35+
await output.done;
36+
37+
const stdout = output.stdout.find((line) => /dist\/assets\/index.*\.js/.test(line));
38+
assert.not.match(stdout, '│ map:');
39+
});
40+
41+
test('Should preserve sourcemap sizes from logs if user has enabled sourcemaps', async () => {
42+
await loadFixture('logs/prerendered-routes', env);
43+
await writeConfig(env.tmp.path, `
44+
import { defineConfig } from 'vite';
45+
import { vitePrerenderPlugin } from 'vite-prerender-plugin';
46+
47+
export default defineConfig({
48+
build: { sourcemap: true },
49+
plugins: [vitePrerenderPlugin()],
50+
});
51+
`);
52+
const output = await viteBuildCli(env.tmp.path);
53+
await output.done;
54+
55+
const stdout = output.stdout.find((line) => /dist\/assets\/index.*\.js/.test(line));
56+
assert.match(stdout, '│ map:');
57+
});
58+
3059
test.run();

0 commit comments

Comments
 (0)