Skip to content

Commit

Permalink
refactor: update es-module-lexer to 1.4.0 (#14937)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Nov 10, 2023
1 parent 6511c0e commit 374e6fd
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 35 deletions.
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
23 changes: 9 additions & 14 deletions packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ 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'
import type { Node } from 'estree'
import { findStaticImports, parseStaticImport } from 'mlly'
import { makeLegalIdentifier } from '@rollup/pluginutils'
Expand Down Expand Up @@ -76,13 +79,6 @@ const hasImportInQueryParamsRE = /[?&]import=?\b/

export const hasViteIgnoreRE = /\/\*\s*@vite-ignore\s*\*\//

const trimWhitespaceRE = /^(\s*)(\S|\S[\s\S]*\S)\s*$/
const trimWhitespaceAndComments = (code: string) => {
const cleanedCode = stripLiteral(code)
const match = trimWhitespaceRE.exec(cleanedCode)
return match ? code.slice(match[1].length, match[2].length) : code
}

const urlIsStringRE = /^(?:'.*'|".*"|`.*`)$/

const templateLiteralRE = /^\s*`(.*)`\s*$/
Expand Down Expand Up @@ -236,7 +232,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 Expand Up @@ -658,13 +655,11 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
}

if (!ssr) {
const url = trimWhitespaceAndComments(rawUrl)
if (
!urlIsStringRE.test(url) ||
isExplicitImportRequired(url.slice(1, -1))
!urlIsStringRE.test(rawUrl) ||
isExplicitImportRequired(rawUrl.slice(1, -1))
) {
needQueryInjectHelper = true
// Use rawUrl to avoid removing comments like @vite-ignore
str().overwrite(
start,
end,
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.

0 comments on commit 374e6fd

Please sign in to comment.