diff --git a/packages/vite/package.json b/packages/vite/package.json index 3c007ab0631dd9..1e8079d730068e 100644 --- a/packages/vite/package.json +++ b/packages/vite/package.json @@ -79,7 +79,6 @@ "@vue/compiler-dom": "^3.1.5", "acorn": "^8.4.1", "acorn-class-fields": "^1.0.0", - "acorn-numeric-separator": "^0.3.6", "acorn-static-class-features": "^1.0.0", "brotli-size": "^4.0.0", "builtin-modules": "^3.2.0", diff --git a/packages/vite/src/node/server/pluginContainer.ts b/packages/vite/src/node/server/pluginContainer.ts index 73e962c6c0931d..37152158815ae2 100644 --- a/packages/vite/src/node/server/pluginContainer.ts +++ b/packages/vite/src/node/server/pluginContainer.ts @@ -51,7 +51,6 @@ import { } from 'rollup' import * as acorn from 'acorn' import acornClassFields from 'acorn-class-fields' -import acornNumericSeparator from 'acorn-numeric-separator' import acornStaticClassFeatures from 'acorn-static-class-features' import { RawSourceMap } from '@ampproject/remapping/dist/types/types' import { combineSourcemaps } from '../utils' @@ -116,8 +115,7 @@ type PluginContext = Omit< export let parser = acorn.Parser.extend( acornClassFields, - acornStaticClassFeatures, - acornNumericSeparator + acornStaticClassFeatures ) export async function createPluginContainer( @@ -285,10 +283,22 @@ export async function createPluginContainer( : // some rollup plugins, e.g. json, sets position instead of pos (err as any).position if (pos != null) { - err.loc = err.loc || { - file: err.id, - ...numberToPos(ctx._activeCode, pos) + let errLocation; + try { + errLocation = numberToPos(ctx._activeCode, pos); } + catch (err2) { + logger.error( + chalk.red(`Error in error handler:\n${err2.stack || err2.message}\n`), + // print extra newline to separate the two errors + { error: err2 } + ) + throw err; + } + err.loc = err.loc || { + file: err.id, + ...errLocation + }; err.frame = err.frame || generateCodeFrame(ctx._activeCode, pos) } else if (err.loc) { // css preprocessors may report errors in an included file @@ -387,8 +397,7 @@ export async function createPluginContainer( parser = acorn.Parser.extend( ...[ acornClassFields, - acornStaticClassFeatures, - acornNumericSeparator + acornStaticClassFeatures ].concat(options.acornInjectPlugins) ) } diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index d43661a44c7e85..7fa813e4293c47 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -291,7 +291,7 @@ export function numberToPos( ): { line: number; column: number } { if (typeof offset !== 'number') return offset if (offset > source.length) { - throw new Error('offset is longer than source length!') + throw new Error(`offset is longer than source length! offset ${offset} > length ${source.length}`); } const lines = source.split(splitRE) let counted = 0 diff --git a/packages/vite/types/shims.d.ts b/packages/vite/types/shims.d.ts index 8a0e94ee8ee10e..f96895bea76f6e 100644 --- a/packages/vite/types/shims.d.ts +++ b/packages/vite/types/shims.d.ts @@ -27,11 +27,6 @@ declare module 'acorn-static-class-features' { export default plugin } -declare module 'acorn-numeric-separator' { - const plugin: any - export default plugin -} - declare module 'connect-history-api-fallback' { const plugin: any export = plugin diff --git a/yarn.lock b/yarn.lock index d0ff80337a99b1..268bb0c4518565 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1654,11 +1654,6 @@ acorn-node@^1.6.1: acorn-walk "^7.0.0" xtend "^4.0.2" -acorn-numeric-separator@^0.3.6: - version "0.3.6" - resolved "https://registry.yarnpkg.com/acorn-numeric-separator/-/acorn-numeric-separator-0.3.6.tgz#af7f0abaf8e74bd9ca1117602954d0a3b75804f3" - integrity sha512-jUr5esgChu4k7VzesH/Nww3EysuyGJJcTEEiXqILUFKpO96PNyEXmK21M6nE0TSqGA1PeEg1MzgqJaoFsn9JMw== - acorn-private-class-elements@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/acorn-private-class-elements/-/acorn-private-class-elements-1.0.0.tgz#c5805bf8a46cd065dc9b3513bfebb504c88cd706"