Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat: sourcemap for importAnalysis (#8258)
  • Loading branch information
poyoho committed May 21, 2022
1 parent 95297dd commit a4e4d39
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion packages/vite/src/node/plugins/importAnalysis.ts
Expand Up @@ -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
}
Expand Down
3 changes: 3 additions & 0 deletions playground/glob-import/dir/index.js
@@ -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 }
3 changes: 3 additions & 0 deletions playground/glob-import/vite.config.ts
Expand Up @@ -6,5 +6,8 @@ export default defineConfig({
alias: {
'@dir': path.resolve(__dirname, './dir/')
}
},
build: {
sourcemap: true
}
})
@@ -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,
}
`;
24 changes: 1 addition & 23 deletions playground/vue-sourcemap/__tests__/serve.spec.ts
Expand Up @@ -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 () => {
Expand Down

0 comments on commit a4e4d39

Please sign in to comment.