feat: sourcemap for importAnalysis (#8258)

This commit is contained in:
yoho 2022-05-21 22:44:52 +08:00 committed by GitHub
parent 95297dd184
commit a4e4d39458
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 37 additions and 25 deletions

View File

@ -15,7 +15,7 @@
"scripts": {
"preinstall": "npx only-allow pnpm",
"format": "prettier --write .",
"lint": "eslint packages/*/{src,types}/** playground/**/__tests__/** scripts/**",
"lint": "eslint packages/*/{src,types}/** playground/**/__tests__/**/*.ts scripts/**",
"typecheck": "tsc -p scripts --noEmit && tsc -p playground --noEmit",
"test": "run-s test-unit test-serve test-build",
"test-serve": "vitest run -c vitest.config.e2e.ts",

View File

@ -645,7 +645,10 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
}
if (s) {
return s.toString()
return {
code: s.toString(),
map: config.build.sourcemap ? s.generateMap({ hires: true }) : null
}
} else {
return source
}

View File

@ -1,4 +1,7 @@
const modules = import.meta.glob('./*.(js|ts)', { eager: true })
const globWithAlias = import.meta.glob('@dir/al*.js', { eager: true })
// test for sourcemap
console.log('hello')
export { modules, globWithAlias }

View File

@ -6,5 +6,8 @@ export default defineConfig({
alias: {
'@dir': path.resolve(__dirname, './dir/')
}
},
build: {
sourcemap: true
}
})

View File

@ -0,0 +1,25 @@
// Vitest Snapshot v1
exports[`serve:vue-sourcemap > js 1`] = `
{
"mappings": "mIAKA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;;;;;AAGP;AACd,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;;;;;;;;;;;wBARlB,oBAAiB,WAAd,MAAU",
"sources": [
"/root/Js.vue",
],
"sourcesContent": [
"<template>
<p>&lt;js&gt;</p>
</template>
<script>
console.log('script')
</script>
<script setup>
console.log('setup')
</script>
",
],
"version": 3,
}
`;

View File

@ -22,29 +22,7 @@ describe.runIf(isServe)('serve:vue-sourcemap', () => {
const res = await page.request.get(new URL('./Js.vue', page.url()).href)
const js = await res.text()
const map = extractSourcemap(js)
expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(`
{
"mappings": "AAKA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;;;;;AAGP;AACd,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;;;;;;;;;;;wBARlB,oBAAiB,WAAd,MAAU",
"sources": [
"/root/Js.vue",
],
"sourcesContent": [
"<template>
<p>&lt;js&gt;</p>
</template>
<script>
console.log('script')
</script>
<script setup>
console.log('setup')
</script>
",
],
"version": 3,
}
`)
expect(formatSourcemapForSnapshot(map)).toMatchSnapshot()
})
test('ts', async () => {