Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: template src sourcemap source #267

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/plugin-vue/src/index.ts
Expand Up @@ -291,6 +291,8 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
options.value,
this,
ssr,
filename,
!!query.src,
)
} else if (query.type === 'style') {
return transformStyle(
Expand Down
11 changes: 11 additions & 0 deletions packages/plugin-vue/src/template.ts
Expand Up @@ -17,6 +17,8 @@ export async function transformTemplateAsModule(
options: ResolvedOptions,
pluginContext: TransformPluginContext,
ssr: boolean,
filename: string,
isSrc: boolean,
): Promise<{
code: string
map: any
Expand All @@ -35,6 +37,15 @@ export async function transformTemplateAsModule(
})`
}

if (result.map && isSrc) {
const vueFileIndex = result.map.sources.findIndex(
(source) => source === descriptor.filename,
)
if (vueFileIndex >= 0) {
result.map.sources[vueFileIndex] = filename
}
}

return {
code: returnCode,
map: result.map,
Expand Down
2 changes: 2 additions & 0 deletions playground/vue-sourcemap/Main.vue
Expand Up @@ -7,6 +7,7 @@
<SassWithImport />
<Less />
<SrcImport />
<SrcImportHtml />
<NoScript />
<NoTemplate />
</template>
Expand All @@ -19,6 +20,7 @@ import Sass from './Sass.vue'
import SassWithImport from './SassWithImport.vue'
import Less from './Less.vue'
import SrcImport from './src-import/SrcImport.vue'
import SrcImportHtml from './src-import-html/SrcImportHtml.vue'
import NoScript from './NoScript.vue'
import NoTemplate from './NoTemplate.vue'
</script>
Expand Up @@ -289,6 +289,20 @@ exports[`serve:vue-sourcemap > src imported > serve-src-imported 1`] = `
}
`;

exports[`serve:vue-sourcemap > src imported html > serve-html 1`] = `
{
"mappings": ";;;wBAAA,oBAA8B,WAA3B,mBAAuB",
"sources": [
"src-import.html",
],
Comment on lines +295 to +297
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this fix, this sources was ['SrcImportHtml.vue']. So the vue file's content was replaced with the html file's content (= the content in sourcesContent)

"sourcesContent": [
"<p>&lt;src-import-html&gt;</p>
",
],
"version": 3,
}
`;

exports[`serve:vue-sourcemap > src imported sass > serve-src-imported-sass 1`] = `
{
"mappings": "AAAA;EACE;;ACCF;EACE",
Expand Down
12 changes: 12 additions & 0 deletions playground/vue-sourcemap/__tests__/vue-sourcemap.spec.ts
Expand Up @@ -91,6 +91,18 @@ describe.runIf(isServe)('serve:vue-sourcemap', () => {
)
})

test('src imported html', async () => {
const res = await page.request.get(
new URL(
'./src-import-html/src-import.html?import&vue&type=template&src=true&lang.js',
page.url(),
).href,
)
const js = await res.text()
const map = extractSourcemap(js)
expect(formatSourcemapForSnapshot(map)).toMatchSnapshot('serve-html')
})

test('no script', async () => {
const res = await page.request.get(
new URL('./NoScript.vue', page.url()).href,
Expand Down
5 changes: 5 additions & 0 deletions playground/vue-sourcemap/src-import-html/SrcImportHtml.vue
@@ -0,0 +1,5 @@
<template src="./src-import.html" />

<script setup lang="ts">
console.log('src-import-html')
</script>
1 change: 1 addition & 0 deletions playground/vue-sourcemap/src-import-html/src-import.html
@@ -0,0 +1 @@
<p>&lt;src-import-html&gt;</p>