Skip to content

Commit

Permalink
chore: replace SourceMapConsumer with trace-mapping (#6746)
Browse files Browse the repository at this point in the history
Co-authored-by: patak-dev <matias.capeletto@gmail.com>
  • Loading branch information
TrySound and patak-dev committed Mar 8, 2022
1 parent 0ade335 commit ed4d191
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 68 deletions.
37 changes: 1 addition & 36 deletions packages/vite/LICENSE.md
Expand Up @@ -25,7 +25,7 @@ SOFTWARE.

# Licenses of bundled dependencies
The published Vite artifact additionally contains code with the following licenses:
Apache-2.0, BSD-2-Clause, BSD-3-Clause, CC0-1.0, ISC, MIT, (BSD-3-Clause OR GPL-2.0)
Apache-2.0, BSD-2-Clause, CC0-1.0, ISC, MIT, (BSD-3-Clause OR GPL-2.0)

# Bundled dependencies:
## @ampproject/remapping
Expand Down Expand Up @@ -3328,41 +3328,6 @@ Repository: lukeed/sirv

---------------------------------------

## source-map-js
License: BSD-3-Clause
By: Valentin 7rulnik Semirulnik, Nick Fitzgerald, Tobias Koppers, Duncan Beevers, Stephen Crane, Ryan Seddon, Miles Elam, Mihai Bazon, Michael Ficarra, Todd Wolfson, Alexander Solovyov, Felix Gnass, Conrad Irwin, usrbincc, David Glasser, Chase Douglas, Evan Wallace, Heather Arthur, Hugh Kennedy, Simon Lydell, Jmeas Smith, Michael Z Goddard, azu, John Gozde, Adam Kirkton, Chris Montgomery, J. Ryan Stinnett, Jack Herrington, Chris Truter, Daniel Espeset, Jamie Wong, Eddy Bruël, Hawken Rives, Gilad Peleg, djchie, Gary Ye, Nicolas Lalevée
Repository: 7rulnik/source-map-js

> Copyright (c) 2009-2011, Mozilla Foundation and contributors
> All rights reserved.
>
> Redistribution and use in source and binary forms, with or without
> modification, are permitted provided that the following conditions are met:
>
> * Redistributions of source code must retain the above copyright notice, this
> list of conditions and the following disclaimer.
>
> * Redistributions in binary form must reproduce the above copyright notice,
> this list of conditions and the following disclaimer in the documentation
> and/or other materials provided with the distribution.
>
> * Neither the names of the Mozilla Foundation nor the names of project
> contributors may be used to endorse or promote products derived from this
> software without specific prior written permission.
>
> THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
> ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
> WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
> DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
> FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
> DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
> SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
> CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
> OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
> OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
---------------------------------------

## sourcemap-codec
License: MIT
By: Rich Harris
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/package.json
Expand Up @@ -55,6 +55,7 @@
"@ampproject/remapping": "^2.1.0",
"@babel/parser": "^7.17.0",
"@babel/types": "^7.17.0",
"@jridgewell/trace-mapping": "^0.3.2",
"@rollup/plugin-alias": "^3.1.9",
"@rollup/plugin-commonjs": "^21.0.1",
"@rollup/plugin-dynamic-import-vars": "^1.4.2",
Expand Down Expand Up @@ -108,7 +109,6 @@
"resolve.exports": "^1.1.0",
"rollup-plugin-license": "^2.6.1",
"sirv": "^2.0.2",
"source-map-js": "^1.0.2",
"source-map-support": "^0.5.21",
"strip-ansi": "^6.0.1",
"terser": "^5.10.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/esbuild.ts
Expand Up @@ -15,7 +15,7 @@ import {
generateCodeFrame,
toUpperCaseDriveLetter
} from '../utils'
import type { RawSourceMap } from '@ampproject/remapping/dist/types/types'
import type { RawSourceMap } from '@ampproject/remapping'
import type { SourceMap } from 'rollup'
import type { ResolvedConfig, ViteDevServer } from '..'
import { createFilter } from '@rollup/pluginutils'
Expand Down
13 changes: 6 additions & 7 deletions packages/vite/src/node/server/pluginContainer.ts
Expand Up @@ -49,7 +49,8 @@ import type {
TransformResult
} from 'rollup'
import * as acorn from 'acorn'
import type { RawSourceMap } from '@ampproject/remapping/dist/types/types'
import type { RawSourceMap } from '@ampproject/remapping'
import { TraceMap, originalPositionFor } from '@jridgewell/trace-mapping'
import { combineSourcemaps } from '../utils'
import MagicString from 'magic-string'
import type { FSWatcher } from 'chokidar'
Expand All @@ -70,7 +71,6 @@ import type { ResolvedConfig } from '../config'
import { buildErrorMessage } from './middlewares/error'
import type { ModuleGraph } from './moduleGraph'
import { performance } from 'perf_hooks'
import { SourceMapConsumer } from 'source-map-js/lib/source-map-consumer'
import type * as postcss from 'postcss'

export interface PluginContainerOptions {
Expand Down Expand Up @@ -376,13 +376,12 @@ export async function createPluginContainer(
if (err.loc && ctx instanceof TransformContext) {
const rawSourceMap = ctx._getCombinedSourcemap()
if (rawSourceMap) {
const consumer = new SourceMapConsumer(rawSourceMap as any)
const { source, line, column } = consumer.originalPositionFor({
const traced = new TraceMap(rawSourceMap as any)
const { source, line, column } = originalPositionFor(traced, {
line: Number(err.loc.line),
column: Number(err.loc.column),
bias: SourceMapConsumer.GREATEST_LOWER_BOUND
column: Number(err.loc.column)
})
if (source) {
if (source && line != null && column != null) {
err.loc = { file: source, line, column }
}
}
Expand Down
16 changes: 6 additions & 10 deletions packages/vite/src/node/ssr/ssrStacktrace.ts
@@ -1,5 +1,4 @@
import type { RawSourceMap } from 'source-map-js'
import { SourceMapConsumer } from 'source-map-js/lib/source-map-consumer'
import { TraceMap, originalPositionFor } from '@jridgewell/trace-mapping'
import type { ModuleGraph } from '../server/moduleGraph'

let offset: number
Expand Down Expand Up @@ -32,21 +31,18 @@ export function ssrRewriteStacktrace(
return input
}

const consumer = new SourceMapConsumer(
rawSourceMap as unknown as RawSourceMap
)
const traced = new TraceMap(rawSourceMap as any)

const pos = consumer.originalPositionFor({
const pos = originalPositionFor(traced, {
line: Number(line) - offset,
column: Number(column),
bias: SourceMapConsumer.LEAST_UPPER_BOUND
column: Number(column)
})

if (!pos.source) {
if (!pos.source || pos.line == null || pos.column == null) {
return input
}

const source = `${pos.source}:${pos.line ?? 0}:${pos.column ?? 0}`
const source = `${pos.source}:${pos.line}:${pos.column}`
if (!varName || varName === 'eval') {
return ` at ${source}`
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/ssr/ssrTransform.ts
Expand Up @@ -12,7 +12,7 @@ import type {
import { extract_names as extractNames } from 'periscopic'
import { walk as eswalk } from 'estree-walker'
import { combineSourcemaps } from '../utils'
import type { RawSourceMap } from '@ampproject/remapping/dist/types/types'
import type { RawSourceMap } from '@ampproject/remapping'

type Node = _Node & {
start: number
Expand Down
5 changes: 1 addition & 4 deletions packages/vite/src/node/utils.ts
Expand Up @@ -16,10 +16,7 @@ import resolve from 'resolve'
import { builtinModules } from 'module'
import type { FSWatcher } from 'chokidar'
import remapping from '@ampproject/remapping'
import type {
DecodedSourceMap,
RawSourceMap
} from '@ampproject/remapping/dist/types/types'
import type { DecodedSourceMap, RawSourceMap } from '@ampproject/remapping'
import { performance } from 'perf_hooks'
import { parse as parseUrl, URLSearchParams } from 'url'

Expand Down
16 changes: 8 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ed4d191

Please sign in to comment.