Skip to content

Commit 7f73970

Browse files
authored
fix(plugin-vue): handle sourcemap with empty script code (#585)
1 parent 2e1287f commit 7f73970

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
lines changed

packages/plugin-vue/src/main.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,23 @@ export async function transformMain(
196196

197197
let resolvedMap: RawSourceMap | undefined = undefined
198198
if (options.sourceMap) {
199-
if (scriptMap && templateMap) {
200-
// if the template is inlined into the main module (indicated by the presence
201-
// of templateMap), we need to concatenate the two source maps.
202-
199+
// the mappings of the source map for the inlined template should be moved
200+
// because the position does not include the script tag part.
201+
// we also concatenate the two source maps while doing that.
202+
if (templateMap) {
203+
const from = scriptMap ?? {
204+
file: filename,
205+
sourceRoot: '',
206+
version: 3,
207+
sources: [],
208+
sourcesContent: [],
209+
names: [],
210+
mappings: '',
211+
}
203212
const gen = fromMap(
204213
// version property of result.map is declared as string
205214
// but actually it is `3`
206-
scriptMap as Omit<RawSourceMap, 'version'> as TraceEncodedSourceMap,
215+
from as Omit<RawSourceMap, 'version'> as TraceEncodedSourceMap,
207216
)
208217
const tracer = new TraceMap(
209218
// same above
@@ -231,8 +240,7 @@ export async function transformMain(
231240
// of the main module compile result, which has outdated sourcesContent.
232241
resolvedMap.sourcesContent = templateMap.sourcesContent
233242
} else {
234-
// if one of `scriptMap` and `templateMap` is empty, use the other one
235-
resolvedMap = scriptMap ?? templateMap
243+
resolvedMap = scriptMap
236244
}
237245
}
238246

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<script setup></script>
2+
<template>
3+
<p>&lt;empty-script&gt;</p>
4+
</template>

playground/vue-sourcemap/__tests__/__snapshots__/vue-sourcemap.spec.ts.snap

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,24 @@ exports[`serve:vue-sourcemap > css scoped > serve-css-scoped 1`] = `
144144
}
145145
`;
146146

147+
exports[`serve:vue-sourcemap > empty script > serve-empty-script 1`] = `
148+
{
149+
"ignoreList": [],
150+
"mappings": ";;;;wBAEE,oBAA2B,WAAxB,gBAAoB",
151+
"sources": [
152+
"EmptyScript.vue",
153+
],
154+
"sourcesContent": [
155+
"<script setup></script>
156+
<template>
157+
<p>&lt;empty-script&gt;</p>
158+
</template>
159+
",
160+
],
161+
"version": 3,
162+
}
163+
`;
164+
147165
exports[`serve:vue-sourcemap > js > serve-js 1`] = `
148166
{
149167
"ignoreList": [],
@@ -194,7 +212,8 @@ exports[`serve:vue-sourcemap > less with additionalData > serve-less-with-additi
194212

195213
exports[`serve:vue-sourcemap > no script > serve-no-script 1`] = `
196214
{
197-
"mappings": ";;;wBACE,oBAAwB,WAArB,aAAiB",
215+
"ignoreList": [],
216+
"mappings": ";;;;wBACE,oBAAwB,WAArB,aAAiB",
198217
"sources": [
199218
"NoScript.vue",
200219
],

playground/vue-sourcemap/__tests__/vue-sourcemap.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,17 @@ describe.runIf(isServe)('serve:vue-sourcemap', () => {
100100
expect(formatSourcemapForSnapshot(map)).toMatchSnapshot('serve-no-script')
101101
})
102102

103+
test('empty script', async () => {
104+
const res = await page.request.get(
105+
new URL('./EmptyScript.vue', page.url()).href,
106+
)
107+
const js = await res.text()
108+
const map = extractSourcemap(js)
109+
expect(formatSourcemapForSnapshot(map)).toMatchSnapshot(
110+
'serve-empty-script',
111+
)
112+
})
113+
103114
test('no template', async () => {
104115
const res = await page.request.get(
105116
new URL('./NoTemplate.vue', page.url()).href,

0 commit comments

Comments
 (0)