-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Source maps & error positioning
- Loading branch information
1 parent
0bf98c5
commit 66bdeec
Showing
6 changed files
with
164 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
</head> | ||
<body> | ||
<script prerender type="module" src="/src/index.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
if (typeof window !== "undefined") { | ||
const worker = new Worker(new URL("./worker.js", import.meta.url)); | ||
|
||
worker.postMessage({ type: "init" }); | ||
} | ||
|
||
export async function prerender() { | ||
return `<h1>Simple Test Result</h1>`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
addEventListener('message', (e) => { | ||
postMessage({ type: 'init' }); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { defineConfig } from 'vite'; | ||
import { vitePrerenderPlugin } from 'vite-prerender-plugin'; | ||
|
||
export default defineConfig({ | ||
plugins: [vitePrerenderPlugin()], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { test } from 'uvu'; | ||
import * as assert from 'uvu/assert'; | ||
import path from 'node:path'; | ||
import { promises as fs } from 'node:fs'; | ||
|
||
import { setupTest, teardownTest, loadFixture, viteBuild } from './lib/lifecycle.js'; | ||
import { getOutputFile, outputFileExists, 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'); | ||
assert.is(outputChunkCode.match(/\/\/#\ssourceMappingURL=(.*)/), 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=(.*)/), 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=(.*)/)[1]; | ||
assert.ok(outDirAssets.includes(outputMap)); | ||
}); | ||
|
||
test('Should use sourcemaps to display error positioning when possible', async () => { | ||
await loadFixture('simple', env); | ||
await writeFixtureFile(env.tmp.path, 'src/index.js', ` | ||
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:2:9'); | ||
}); | ||
|
||
test.run(); | ||
|