Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update es-module-lexer to 1.4.0 #14937

Merged
merged 4 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
"dep-types": "link:./src/types",
"dotenv": "^16.3.1",
"dotenv-expand": "^10.0.0",
"es-module-lexer": "^1.3.1",
"es-module-lexer": "^1.4.0",
"escape-html": "^1.0.3",
"estree-walker": "^3.0.3",
"etag": "^1.8.1",
Expand Down
9 changes: 1 addition & 8 deletions packages/vite/src/node/plugins/dynamicImportVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
createFilter,
normalizePath,
parseRequest,
removeComments,
requestQuerySplitRE,
transformStableResult,
} from '../utils'
Expand Down Expand Up @@ -218,14 +217,8 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin {
s ||= new MagicString(source)
let result
try {
// When import string is using backticks, es-module-lexer `end` captures
// until the closing parenthesis, instead of the closing backtick.
// There may be inline comments between the backtick and the closing
// parenthesis, so we manually remove them for now.
// See https://github.com/guybedford/es-module-lexer/issues/118
const importSource = removeComments(source.slice(start, end)).trim()
result = await transformDynamicImport(
importSource,
source.slice(start, end),
importer,
resolve,
config.root,
Expand Down
9 changes: 7 additions & 2 deletions packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import path from 'node:path'
import { performance } from 'node:perf_hooks'
import colors from 'picocolors'
import MagicString from 'magic-string'
import type { ExportSpecifier, ImportSpecifier } from 'es-module-lexer'
import type {
ParseError as EsModuleLexerParseError,
ExportSpecifier,
ImportSpecifier,
} from 'es-module-lexer'
import { init, parse as parseImports } from 'es-module-lexer'
import { parse as parseJS } from 'acorn'
import { stripLiteral } from 'strip-literal'
Expand Down Expand Up @@ -236,7 +240,8 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
source = stripBomTag(source)
try {
;[imports, exports] = parseImports(source)
} catch (e: any) {
} catch (_e: unknown) {
const e = _e as EsModuleLexerParseError
const { message, showCodeFrame } = createParseErrorInfo(
importer,
source,
Expand Down
8 changes: 6 additions & 2 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import path from 'node:path'
import MagicString from 'magic-string'
import type { ImportSpecifier } from 'es-module-lexer'
import type {
ParseError as EsModuleLexerParseError,
ImportSpecifier,
} from 'es-module-lexer'
import { init, parse as parseImports } from 'es-module-lexer'
import type { OutputChunk, SourceMap } from 'rollup'
import colors from 'picocolors'
Expand Down Expand Up @@ -222,7 +225,8 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
let imports: readonly ImportSpecifier[] = []
try {
imports = parseImports(source)[0]
} catch (e: any) {
} catch (_e: unknown) {
const e = _e as EsModuleLexerParseError
const { message, showCodeFrame } = createParseErrorInfo(
importer,
source,
Expand Down
8 changes: 6 additions & 2 deletions packages/vite/src/node/ssr/ssrManifestPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { basename, dirname, join, relative } from 'node:path'
import { parse as parseImports } from 'es-module-lexer'
import type { ImportSpecifier } from 'es-module-lexer'
import type {
ParseError as EsModuleLexerParseError,
ImportSpecifier,
} from 'es-module-lexer'
import type { OutputChunk } from 'rollup'
import jsonStableStringify from 'json-stable-stringify'
import type { ResolvedConfig } from '..'
Expand Down Expand Up @@ -46,7 +49,8 @@ export function ssrManifestPlugin(config: ResolvedConfig): Plugin {
let imports: ImportSpecifier[] = []
try {
imports = parseImports(code)[0].filter((i) => i.n && i.d > -1)
} catch (e: any) {
} catch (_e: unknown) {
const e = _e as EsModuleLexerParseError
const loc = numberToPos(code, e.idx)
this.error({
name: e.name,
Expand Down
4 changes: 0 additions & 4 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1034,10 +1034,6 @@ export function emptyCssComments(raw: string): string {
return raw.replace(multilineCommentsRE, (s) => ' '.repeat(s.length))
}

export function removeComments(raw: string): string {
return raw.replace(multilineCommentsRE, '').replace(singlelineCommentsRE, '')
}

function backwardCompatibleWorkerPlugins(plugins: any) {
if (Array.isArray(plugins)) {
return plugins
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

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