Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit bcf9ed9

Browse files
committed
fix: use verbose template sourcemap for better sourcemap support
1 parent 26c4a7c commit bcf9ed9

File tree

6 files changed

+66
-22
lines changed

6 files changed

+66
-22
lines changed

Diff for: .vscode/settings.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"markdownlint.config": {
3+
"no-trailing-punctuation": false
4+
}
5+
}

Diff for: examples/css-modules/rollup.config.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import vue from '../../dist/rollup-plugin-vue.esm'
22
import postcss from 'rollup-plugin-postcss'
33

4-
export default [
4+
/** @type {import('rollup').RollupOptions[]} */
5+
const config = [
56
{
67
input: 'src/App.vue',
78
output: {
89
file: 'dist/app.js',
910
format: 'esm',
10-
sourcemap: true,
11+
sourcemap: 'inline',
1112
},
1213
plugins: [
1314
vue(),
@@ -19,6 +20,10 @@ export default [
1920
}),
2021
postcss({ include: /(?<!&module=.*)\.css$/ }),
2122
],
22-
external: ['vue'],
23+
external(id) {
24+
return /(^vue$|style-inject)/.test(id)
25+
},
2326
},
2427
]
28+
29+
export default config

Diff for: examples/simple/rollup.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default [{
55
output: {
66
file: 'dist/app.js',
77
format: 'esm',
8-
sourcemap: true
8+
sourcemap: 'inline',
99
},
1010
plugins: [vue()],
1111
external: ['vue']

Diff for: package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
"@vue/compiler-sfc": "^3.0.0-alpha.3",
1414
"debug": "^4.1.1",
1515
"hash-sum": "^2.0.0",
16-
"rollup-pluginutils": "^2.8.2"
16+
"rollup-pluginutils": "^2.8.2",
17+
"sourcemap-codec": "^1.4.8"
1718
},
1819
"peerDependencies": {},
1920
"devDependencies": {
21+
"@types/debug": "^4.1.5",
2022
"@types/jest": "^24.9.0",
2123
"husky": "^4.2.0",
2224
"rollup": "^1.29.1",

Diff for: src/index.ts

+43-16
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ import {
1717
SFCTemplateCompileOptions,
1818
SFCTemplateCompileResults,
1919
} from '@vue/compiler-sfc'
20-
// @ts-ignore
2120
import createDebugger from 'debug'
2221
import hash from 'hash-sum'
2322
import { basename, relative } from 'path'
2423
import qs from 'querystring'
2524
import { Plugin, RollupError } from 'rollup'
2625
import { createFilter } from 'rollup-pluginutils'
2726
import { genCSSModulesCode } from './cssModules'
27+
import { encode } from 'sourcemap-codec'
2828

2929
const debug = createDebugger('rollup-plugin-vue')
3030

@@ -73,7 +73,6 @@ export default function PluginVue(userOptions: Partial<Options> = {}): Plugin {
7373
return undefined
7474
},
7575
load(id) {
76-
debug(`load(${id})`)
7776
const query = parseVuePartRequest(id)
7877

7978
if (query.vue) {
@@ -91,27 +90,52 @@ export default function PluginVue(userOptions: Partial<Options> = {}): Plugin {
9190
: null
9291

9392
if (block) {
94-
return {
93+
const result = {
9594
code: block.content,
96-
map: normalizeSourceMap(block.map, 'load:' + id)
95+
map: normalizeSourceMap(block.map),
96+
}
97+
98+
if (query.type === 'template') {
99+
// generate source mapping for each character.
100+
result.map.mappings = encode(
101+
result.code.split(/\r?\n/).map((line, index) => {
102+
const segments: [number, number, number, number][] = []
103+
for (let i = 0; i < line.length; ++i) {
104+
segments.push([i, 0, block.loc.start.line + index - 1, i])
105+
}
106+
107+
return segments
108+
})
109+
)
97110
}
111+
112+
debug(
113+
`load(${id})`,
114+
'\n' +
115+
result.code +
116+
'\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,' +
117+
Buffer.from(JSON.stringify(result.map), 'utf-8').toString(
118+
'base64'
119+
)
120+
)
121+
122+
return result
98123
}
99124
}
100125

101126
return undefined
102127
},
103128
async transform(code, id) {
104-
debug(`transform(${id})`)
105129
const query = parseVuePartRequest(id)
106130
if (query.vue) {
107131
const descriptor = getDescriptor(query.filename)
108132
if (query.type === 'template') {
133+
debug(`transform(${id})`)
109134
const block = descriptor.template!
110135
const result = compileTemplate({
111136
filename: query.filename,
112-
source: block.content,
137+
source: code,
113138
preprocessLang: block.lang,
114-
inMap: normalizeSourceMap(block.map!, 'transform:' + id),
115139
compiler: options.compiler,
116140
compilerOptions: options.compilerOptions,
117141
transformAssetUrls: options.transformAssetUrls,
@@ -139,9 +163,10 @@ export default function PluginVue(userOptions: Partial<Options> = {}): Plugin {
139163

140164
return {
141165
code: result.code,
142-
map: normalizeSourceMap(result.map!, id),
166+
map: normalizeSourceMap(result.map!),
143167
}
144168
} else if (query.type === 'style' && query.scoped) {
169+
debug(`transform(${id})`)
145170
const block = descriptor.styles[query.index]!
146171
const result = await compileStyleAsync({
147172
filename: query.filename,
@@ -163,11 +188,12 @@ export default function PluginVue(userOptions: Partial<Options> = {}): Plugin {
163188

164189
return {
165190
code: result.code,
166-
map: normalizeSourceMap(result.map!, id),
191+
map: normalizeSourceMap(result.map!),
167192
}
168193
}
169194
return null
170195
} else if (filter(id)) {
196+
debug(`transform(${id})`)
171197
const { descriptor, errors } = parseSFC(code, id, rootContext)
172198

173199
if (errors.length) {
@@ -184,9 +210,13 @@ export default function PluginVue(userOptions: Partial<Options> = {}): Plugin {
184210
options
185211
)
186212

213+
debug('transient .vue file:', '\n' + output + '\n')
214+
187215
return {
188216
code: output,
189-
map: null,
217+
map: {
218+
mappings: '',
219+
},
190220
}
191221
} else {
192222
return null
@@ -262,6 +292,7 @@ function parseSFC(
262292
sourceMap: true,
263293
filename: id,
264294
sourceRoot: sourceRoot,
295+
pad: 'line',
265296
})
266297

267298
cache.set(id, descriptor)
@@ -294,8 +325,8 @@ function transformVueSFC(
294325
const scriptImport = getScriptCode(descriptor, resourcePath)
295326
const stylesCode = getStyleCode(descriptor, resourcePath, id)
296327
const output = [
297-
templateImport,
298328
scriptImport,
329+
templateImport,
299330
stylesCode,
300331
`script.render = render`,
301332
]
@@ -416,13 +447,9 @@ function _(any: any) {
416447
return JSON.stringify(any)
417448
}
418449

419-
function normalizeSourceMap(map: SFCTemplateCompileResults['map'], extra?: any): any {
450+
function normalizeSourceMap(map: SFCTemplateCompileResults['map']): any {
420451
if (!map) return null as any
421452

422-
if (typeof map.mappings !== 'string') {
423-
console.log(extra, map)
424-
}
425-
426453
return {
427454
version: Number(map.version),
428455
file: map.file,

Diff for: yarn.lock

+6-1
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,11 @@
999999
resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0"
10001000
integrity sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ==
10011001

1002+
"@types/debug@^4.1.5":
1003+
version "4.1.5"
1004+
resolved "https://registry.yarnpkg.com/@types/debug/-/debug-4.1.5.tgz#b14efa8852b7768d898906613c23f688713e02cd"
1005+
integrity sha512-Q1y515GcOdTHgagaVFhHnIFQ38ygs/kmxdNpvpou+raI9UO3YZcHDngBSYKQklcKlvA7iuQlmIKbzvmxcOE9CQ==
1006+
10021007
"@types/eslint-visitor-keys@^1.0.0":
10031008
version "1.0.0"
10041009
resolved "https://registry.yarnpkg.com/@types/eslint-visitor-keys/-/eslint-visitor-keys-1.0.0.tgz#1ee30d79544ca84d68d4b3cdb0af4f205663dd2d"
@@ -5342,7 +5347,7 @@ source-map@^0.6.0, source-map@^0.6.1, source-map@~0.6.1:
53425347
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
53435348
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
53445349

5345-
sourcemap-codec@^1.4.4:
5350+
sourcemap-codec@^1.4.4, sourcemap-codec@^1.4.8:
53465351
version "1.4.8"
53475352
resolved "https://registry.yarnpkg.com/sourcemap-codec/-/sourcemap-codec-1.4.8.tgz#ea804bd94857402e6992d05a38ef1ae35a9ab4c4"
53485353
integrity sha512-9NykojV5Uih4lgo5So5dtw+f0JgJX30KCNI8gwhz2J9A15wD0Ml6tjHKwf6fTSa6fAdVBdZeNOs9eJ71qCk8vA==

0 commit comments

Comments
 (0)