提交 f6ec1052 编写于 作者: fxy060608's avatar fxy060608

chore(uvue): 重构部分code frame 逻辑

上级 cd2697e6
...@@ -278,20 +278,24 @@ function createTryResolve( ...@@ -278,20 +278,24 @@ function createTryResolve(
return async (source: string, code: string, { ss, se }: ImportSpecifier) => { return async (source: string, code: string, { ss, se }: ImportSpecifier) => {
const resolved = await wrapResolve(resolve)(source, importer) const resolved = await wrapResolve(resolve)(source, importer)
if (!resolved) { if (!resolved) {
const { start, end } = offsetToStartAndEnd(code, ss, se) const { start, end } = offsetToStartAndEnd(code, ss, se - 1)
const consumer = new SourceMapConsumer(resolvedMap) const consumer = new SourceMapConsumer(resolvedMap)
const startPos = consumer.originalPositionFor({ const startPos = consumer.originalPositionFor({
line: start.line, line: start.line,
column: start.column, column: start.column,
}) })
if (startPos) { if (
startPos.line != null &&
startPos.column != null &&
startPos.source != null
) {
const endPos = consumer.originalPositionFor({ const endPos = consumer.originalPositionFor({
line: end.line, line: end.line,
column: end.column, column: end.column,
}) })
if (endPos) { if (endPos.line != null && endPos.column != null) {
throw createResolveError( throw createResolveError(
consumer.sourceContentFor(endPos.source), consumer.sourceContentFor(startPos.source),
createResolveErrorMsg(source, importer), createResolveErrorMsg(source, importer),
startPos as unknown as Position, startPos as unknown as Position,
endPos as unknown as Position endPos as unknown as Position
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
}, },
"dependencies": { "dependencies": {
"@ampproject/remapping": "^2.1.2", "@ampproject/remapping": "^2.1.2",
"@babel/code-frame": "^7.23.5",
"@babel/core": "^7.21.3", "@babel/core": "^7.21.3",
"@babel/parser": "^7.23.5", "@babel/parser": "^7.23.5",
"@babel/types": "^7.20.7", "@babel/types": "^7.20.7",
...@@ -48,6 +49,7 @@ ...@@ -48,6 +49,7 @@
"fs-extra": "^10.0.0", "fs-extra": "^10.0.0",
"hash-sum": "^2.0.0", "hash-sum": "^2.0.0",
"jsonc-parser": "^3.2.0", "jsonc-parser": "^3.2.0",
"lines-and-columns": "^2.0.4",
"magic-string": "^0.30.0", "magic-string": "^0.30.0",
"merge": "^2.1.1", "merge": "^2.1.1",
"mime": "^3.0.0", "mime": "^3.0.0",
...@@ -65,6 +67,7 @@ ...@@ -65,6 +67,7 @@
"gitHead": "33e807d66e1fe47e2ee08ad9c59247e37b8884da", "gitHead": "33e807d66e1fe47e2ee08ad9c59247e37b8884da",
"devDependencies": { "devDependencies": {
"@dcloudio/uni-uts-v1": "3.0.0-alpha-4000020231215001", "@dcloudio/uni-uts-v1": "3.0.0-alpha-4000020231215001",
"@types/babel__code-frame": "^7.0.6",
"@types/babel__core": "^7.1.19", "@types/babel__core": "^7.1.19",
"@types/debug": "^4.1.7", "@types/debug": "^4.1.7",
"@types/estree": "^0.0.51", "@types/estree": "^0.0.51",
...@@ -76,6 +79,7 @@ ...@@ -76,6 +79,7 @@
"@types/resolve": "^1.20.2", "@types/resolve": "^1.20.2",
"@types/sass": "^1.43.1", "@types/sass": "^1.43.1",
"@types/stylus": "^0.48.36", "@types/stylus": "^0.48.36",
"code-frame": "link:@types/@babel/code-frame",
"postcss": "^8.4.21", "postcss": "^8.4.21",
"unplugin-auto-import": "^0.16.7", "unplugin-auto-import": "^0.16.7",
"vue": "3.3.11" "vue": "3.3.11"
......
...@@ -5,7 +5,8 @@ import os from 'os' ...@@ -5,7 +5,8 @@ import os from 'os'
import path from 'path' import path from 'path'
import remapping from '@ampproject/remapping' import remapping from '@ampproject/remapping'
import type { DecodedSourceMap, RawSourceMap } from '@ampproject/remapping' import type { DecodedSourceMap, RawSourceMap } from '@ampproject/remapping'
import { SourceLocation } from '@vue/compiler-core' import { Position, SourceLocation } from '@vue/compiler-core'
import { LinesAndColumns } from 'lines-and-columns'
export function slash(p: string): string { export function slash(p: string): string {
return p.replace(/\\/g, '/') return p.replace(/\\/g, '/')
...@@ -69,7 +70,7 @@ export function offsetToStartAndEnd( ...@@ -69,7 +70,7 @@ export function offsetToStartAndEnd(
startOffset: number, startOffset: number,
endOffset: number endOffset: number
): SourceLocation { ): SourceLocation {
const lines = source.split(splitRE) const lines = new LinesAndColumns(source)
return { return {
start: offsetToLineColumnByLines(lines, startOffset), start: offsetToLineColumnByLines(lines, startOffset),
end: offsetToLineColumnByLines(lines, endOffset), end: offsetToLineColumnByLines(lines, endOffset),
...@@ -77,25 +78,23 @@ export function offsetToStartAndEnd( ...@@ -77,25 +78,23 @@ export function offsetToStartAndEnd(
} }
} }
function offsetToLineColumnByLines(lines: string[], offset: number) {
let currentOffset = 0
for (let i = 0; i < lines.length; i++) {
const lineLength = lines[i].length + 1 // Adding 1 for the newline character
if (currentOffset + lineLength > offset) {
const line = i + 1 // Line numbers start from 1
const column = offset - currentOffset // Column numbers start from 0
return { line, column, offset }
}
currentOffset += lineLength
}
return { line: lines.length, column: lines[lines.length - 1].length, offset }
}
export function offsetToLineColumn( export function offsetToLineColumn(
source: string, source: string,
offset: number offset: number
): { line: number; column: number } { ): { line: number; column: number } {
return offsetToLineColumnByLines(source.split(splitRE), offset) return offsetToLineColumnByLines(new LinesAndColumns(source), offset)
}
export function offsetToLineColumnByLines(
lines: LinesAndColumns,
offset: number
): Position {
let location = lines.locationForIndex(offset)
if (!location) {
location = lines.locationForIndex(offset)!
}
// lines-and-columns is 0-indexed while positions are 1-indexed
return { line: location.line + 1, column: location.column, offset: 0 }
} }
export function posToNumber( export function posToNumber(
...@@ -103,16 +102,16 @@ export function posToNumber( ...@@ -103,16 +102,16 @@ export function posToNumber(
pos: number | { line: number; column: number } pos: number | { line: number; column: number }
): number { ): number {
if (typeof pos === 'number') return pos if (typeof pos === 'number') return pos
const lines = source.split(splitRE) return posToNumberByLines(new LinesAndColumns(source), pos.line, pos.column)
return posToNumberByLines(lines, pos.line, pos.column)
} }
function posToNumberByLines(lines: string[], line: number, column: number) { function posToNumberByLines(
let start = 0 lines: LinesAndColumns,
for (let i = 0; i < line - 1; i++) { line: number,
start += lines[i].length + 1 column: number
} ) {
return start + column // lines-and-columns is 0-indexed while positions are 1-indexed
return lines.indexForLocation({ line: line - 1, column }) || 0
} }
export function locToStartAndEnd( export function locToStartAndEnd(
...@@ -122,7 +121,7 @@ export function locToStartAndEnd( ...@@ -122,7 +121,7 @@ export function locToStartAndEnd(
end: { line: number; column: number } end: { line: number; column: number }
} }
) { ) {
const lines = source.split(splitRE) const lines = new LinesAndColumns(source)
const start = posToNumberByLines(lines, loc.start.line, loc.start.column) const start = posToNumberByLines(lines, loc.start.line, loc.start.column)
const end = loc.end const end = loc.end
? posToNumberByLines(lines, loc.end.line, loc.end.column) ? posToNumberByLines(lines, loc.end.line, loc.end.column)
......
import type { ConfigEnv, ResolvedConfig, UserConfig } from 'vite' import type { ConfigEnv, ResolvedConfig, UserConfig } from 'vite'
import { generateCodeFrame, locToStartAndEnd } from '../plugins/vitejs/utils'
import { RollupError } from 'rollup' import { RollupError } from 'rollup'
import { CompilerError } from '@vue/compiler-sfc' import { CompilerError } from '@vue/compiler-sfc'
import { extend } from '@vue/shared' import { extend } from '@vue/shared'
import { codeFrameColumns } from '@babel/code-frame'
import { offsetToStartAndEnd } from '../plugins/vitejs/utils'
export function withSourcemap(config: ResolvedConfig) { export function withSourcemap(config: ResolvedConfig) {
if (config.command === 'serve') { if (config.command === 'serve') {
...@@ -50,14 +51,16 @@ export function createRollupError( ...@@ -50,14 +51,16 @@ export function createRollupError(
} }
if (source && source.length > 0) { if (source && source.length > 0) {
if ('offsetStart' in error && 'offsetEnd' in error) { if ('offsetStart' in error && 'offsetEnd' in error) {
rollupError.frame = generateCodeFrame( rollupError.frame = codeFrameColumns(
source, source,
error.offsetStart as number, offsetToStartAndEnd(
error.offsetEnd as number source,
error.offsetStart as number,
error.offsetEnd as number
)
).replace(/\t/g, ' ') ).replace(/\t/g, ' ')
} else { } else {
const { start, end } = locToStartAndEnd(source, error.loc) rollupError.frame = codeFrameColumns(source, error.loc).replace(
rollupError.frame = generateCodeFrame(source, start, end).replace(
/\t/g, /\t/g,
' ' ' '
) )
......
lockfileVersion: '6.0' lockfileVersion: '6.0'
settings:
autoInstallPeers: true
excludeLinksFromLockfile: false
overrides: overrides:
'@babel/plugin-transform-block-scoping': 7.19.4 '@babel/plugin-transform-block-scoping': 7.19.4
...@@ -555,6 +559,9 @@ importers: ...@@ -555,6 +559,9 @@ importers:
'@ampproject/remapping': '@ampproject/remapping':
specifier: ^2.1.2 specifier: ^2.1.2
version: 2.1.2 version: 2.1.2
'@babel/code-frame':
specifier: ^7.23.5
version: 7.23.5
'@babel/core': '@babel/core':
specifier: ^7.21.3 specifier: ^7.21.3
version: 7.21.3 version: 7.21.3
...@@ -633,6 +640,9 @@ importers: ...@@ -633,6 +640,9 @@ importers:
jsonc-parser: jsonc-parser:
specifier: ^3.2.0 specifier: ^3.2.0
version: 3.2.0 version: 3.2.0
lines-and-columns:
specifier: ^2.0.4
version: 2.0.4
magic-string: magic-string:
specifier: ^0.30.0 specifier: ^0.30.0
version: 0.30.0 version: 0.30.0
...@@ -676,6 +686,9 @@ importers: ...@@ -676,6 +686,9 @@ importers:
'@dcloudio/uni-uts-v1': '@dcloudio/uni-uts-v1':
specifier: 3.0.0-alpha-4000020231215001 specifier: 3.0.0-alpha-4000020231215001
version: link:../uni-uts-v1 version: link:../uni-uts-v1
'@types/babel__code-frame':
specifier: ^7.0.6
version: 7.0.6
'@types/babel__core': '@types/babel__core':
specifier: ^7.1.19 specifier: ^7.1.19
version: 7.1.19 version: 7.1.19
...@@ -709,6 +722,9 @@ importers: ...@@ -709,6 +722,9 @@ importers:
'@types/stylus': '@types/stylus':
specifier: ^0.48.36 specifier: ^0.48.36
version: 0.48.36 version: 0.48.36
code-frame:
specifier: link:@types/@babel/code-frame
version: link:@types/@babel/code-frame
postcss: postcss:
specifier: ^8.4.21 specifier: ^8.4.21
version: 8.4.23 version: 8.4.23
...@@ -1632,6 +1648,13 @@ packages: ...@@ -1632,6 +1648,13 @@ packages:
dependencies: dependencies:
'@babel/highlight': 7.18.6 '@babel/highlight': 7.18.6
/@babel/code-frame@7.23.5:
resolution: {integrity: sha512-CgH3s1a96LipHCmSUmYFPwY7MNx8C3avkq7i4Wl3cfa662ldtUe4VM1TPXX70pfmrlWTb6jLqTYrZyT2ZTJBgA==}
engines: {node: '>=6.9.0'}
dependencies:
'@babel/highlight': 7.23.4
chalk: 2.4.2
/@babel/compat-data@7.21.7: /@babel/compat-data@7.21.7:
resolution: {integrity: sha512-KYMqFYTaenzMK4yUtf4EW9wc4N9ef80FsbMtkwool5zpwl4YrT1SdWYSTRcT94KO4hannogdS+LxY7L+arP3gA==} resolution: {integrity: sha512-KYMqFYTaenzMK4yUtf4EW9wc4N9ef80FsbMtkwool5zpwl4YrT1SdWYSTRcT94KO4hannogdS+LxY7L+arP3gA==}
engines: {node: '>=6.9.0'} engines: {node: '>=6.9.0'}
...@@ -1641,7 +1664,7 @@ packages: ...@@ -1641,7 +1664,7 @@ packages:
engines: {node: '>=6.9.0'} engines: {node: '>=6.9.0'}
dependencies: dependencies:
'@ampproject/remapping': 2.2.1 '@ampproject/remapping': 2.2.1
'@babel/code-frame': 7.21.4 '@babel/code-frame': 7.23.5
'@babel/generator': 7.21.5 '@babel/generator': 7.21.5
'@babel/helper-compilation-targets': 7.21.5(@babel/core@7.21.3) '@babel/helper-compilation-targets': 7.21.5(@babel/core@7.21.3)
'@babel/helper-module-transforms': 7.21.5 '@babel/helper-module-transforms': 7.21.5
...@@ -1852,6 +1875,10 @@ packages: ...@@ -1852,6 +1875,10 @@ packages:
resolution: {integrity: sha512-awrNfaMtnHUr653GgGEs++LlAvW6w+DcPrOliSMXWCKo597CwL5Acf/wWdNkf/tfEQE3mjkeD1YOVZOUV/od1w==} resolution: {integrity: sha512-awrNfaMtnHUr653GgGEs++LlAvW6w+DcPrOliSMXWCKo597CwL5Acf/wWdNkf/tfEQE3mjkeD1YOVZOUV/od1w==}
engines: {node: '>=6.9.0'} engines: {node: '>=6.9.0'}
/@babel/helper-validator-identifier@7.22.20:
resolution: {integrity: sha512-Y4OZ+ytlatR8AI+8KZfKuL5urKp7qey08ha31L8b3BwewJAoJamTzyvxPR/5D+KkdJCGPq/+8TukHBlY10FX9A==}
engines: {node: '>=6.9.0'}
/@babel/helper-validator-option@7.21.0: /@babel/helper-validator-option@7.21.0:
resolution: {integrity: sha512-rmL/B8/f0mKS2baE9ZpyTcTavvEuWhTTW8amjzXNvYG4AwBsqTLikfXsEofsJEfKHf+HQVQbFOHy6o+4cnC/fQ==} resolution: {integrity: sha512-rmL/B8/f0mKS2baE9ZpyTcTavvEuWhTTW8amjzXNvYG4AwBsqTLikfXsEofsJEfKHf+HQVQbFOHy6o+4cnC/fQ==}
engines: {node: '>=6.9.0'} engines: {node: '>=6.9.0'}
...@@ -1885,6 +1912,14 @@ packages: ...@@ -1885,6 +1912,14 @@ packages:
chalk: 2.4.2 chalk: 2.4.2
js-tokens: 4.0.0 js-tokens: 4.0.0
/@babel/highlight@7.23.4:
resolution: {integrity: sha512-acGdbYSfp2WheJoJm/EBBBLh/ID8KDc64ISZ9DYtBmC8/Q204PZJLHyzeB5qMzJ5trcOkybd78M4x2KWsUq++A==}
engines: {node: '>=6.9.0'}
dependencies:
'@babel/helper-validator-identifier': 7.22.20
chalk: 2.4.2
js-tokens: 4.0.0
/@babel/parser@7.21.8: /@babel/parser@7.21.8:
resolution: {integrity: sha512-6zavDGdzG3gUqAdWvlLFfk+36RilI+Pwyuuh7HItyeScCWP3k6i8vKclAQ0bM/0y/Kz/xiwvxhMv9MgTJP5gmA==} resolution: {integrity: sha512-6zavDGdzG3gUqAdWvlLFfk+36RilI+Pwyuuh7HItyeScCWP3k6i8vKclAQ0bM/0y/Kz/xiwvxhMv9MgTJP5gmA==}
engines: {node: '>=6.0.0'} engines: {node: '>=6.0.0'}
...@@ -2698,7 +2733,7 @@ packages: ...@@ -2698,7 +2733,7 @@ packages:
resolution: {integrity: sha512-8SegXApWe6VoNw0r9JHpSteLKTpTiLZ4rMlGIm9JQ18KiCtyQiAMEazujAHrUS5flrcqYZa75ukev3P6QmUwUw==} resolution: {integrity: sha512-8SegXApWe6VoNw0r9JHpSteLKTpTiLZ4rMlGIm9JQ18KiCtyQiAMEazujAHrUS5flrcqYZa75ukev3P6QmUwUw==}
engines: {node: '>=6.9.0'} engines: {node: '>=6.9.0'}
dependencies: dependencies:
'@babel/code-frame': 7.21.4 '@babel/code-frame': 7.23.5
'@babel/parser': 7.23.6 '@babel/parser': 7.23.6
'@babel/types': 7.20.7 '@babel/types': 7.20.7
...@@ -4468,6 +4503,10 @@ packages: ...@@ -4468,6 +4503,10 @@ packages:
resolution: {integrity: sha512-ebDJ9b0e702Yr7pWgB0jzm+CX4Srzz8RcXtLJDJB+BSccqMa36uyH/zUsSYao5+BD1ytv3k3rPYCq4mAE1hsXA==} resolution: {integrity: sha512-ebDJ9b0e702Yr7pWgB0jzm+CX4Srzz8RcXtLJDJB+BSccqMa36uyH/zUsSYao5+BD1ytv3k3rPYCq4mAE1hsXA==}
dev: true dev: true
/@types/babel__code-frame@7.0.6:
resolution: {integrity: sha512-Anitqkl3+KrzcW2k77lRlg/GfLZLWXBuNgbEcIOU6M92yw42vsd3xV/Z/yAHEj8m+KUjL6bWOVOFqX8PFPJ4LA==}
dev: true
/@types/babel__core@7.1.19: /@types/babel__core@7.1.19:
resolution: {integrity: sha512-WEOTgRsbYkvA/KCsDwVEGkd7WAr1e3g31VHQ8zy5gul/V1qKullU/BU5I68X5v7V3GnB9eotmom4v5a5gjxorw==} resolution: {integrity: sha512-WEOTgRsbYkvA/KCsDwVEGkd7WAr1e3g31VHQ8zy5gul/V1qKullU/BU5I68X5v7V3GnB9eotmom4v5a5gjxorw==}
dependencies: dependencies:
...@@ -8603,6 +8642,11 @@ packages: ...@@ -8603,6 +8642,11 @@ packages:
/lines-and-columns@1.2.4: /lines-and-columns@1.2.4:
resolution: {integrity: sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg==} resolution: {integrity: sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg==}
/lines-and-columns@2.0.4:
resolution: {integrity: sha512-wM1+Z03eypVAVUCE7QdSqpVIvelbOakn1M0bPDoA4SGWPx3sNDVUiMo3L6To6WWGClB7VyXnhQ4Sn7gxiJbE6A==}
engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0}
dev: false
/lint-staged@10.5.3: /lint-staged@10.5.3:
resolution: {integrity: sha512-TanwFfuqUBLufxCc3RUtFEkFraSPNR3WzWcGF39R3f2J7S9+iF9W0KTVLfSy09lYGmZS5NDCxjNvhGMSJyFCWg==} resolution: {integrity: sha512-TanwFfuqUBLufxCc3RUtFEkFraSPNR3WzWcGF39R3f2J7S9+iF9W0KTVLfSy09lYGmZS5NDCxjNvhGMSJyFCWg==}
hasBin: true hasBin: true
...@@ -11202,7 +11246,3 @@ packages: ...@@ -11202,7 +11246,3 @@ packages:
optionalDependencies: optionalDependencies:
commander: 9.5.0 commander: 9.5.0
dev: true dev: true
settings:
autoInstallPeers: true
excludeLinksFromLockfile: false
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册