-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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: remove acorn #16238
Merged
Merged
refactor: remove acorn #16238
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
794b4f7
refactor: remove acorn from import.meta.glob plugin
sapphi-red d3e5c42
refactor: remove acorn from extractImportedBindings
sapphi-red 70f5422
refactor: remove acorn
sapphi-red a8d8ec0
chore: merge main
sapphi-red 6c4e853
refactor: fix parenthesis typo
sapphi-red 7f9c5e2
Merge branch 'main' into pr/sapphi-red/16238
bluwy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,21 +108,10 @@ | |
"overrides": { | ||
"vite": "workspace:*" | ||
}, | ||
"packageExtensions": { | ||
"acorn-walk": { | ||
"peerDependencies": { | ||
"acorn": "*" | ||
}, | ||
"peerDependenciesMeta": { | ||
"acorn": { | ||
"optional": true | ||
} | ||
} | ||
} | ||
}, | ||
"patchedDependencies": { | ||
"[email protected]": "patches/[email protected]", | ||
"[email protected]": "patches/[email protected]" | ||
"[email protected]": "patches/[email protected]", | ||
"[email protected]": "patches/[email protected]" | ||
}, | ||
"peerDependencyRules": { | ||
"allowedVersions": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,22 +4,18 @@ import { stripLiteral } from 'strip-literal' | |
import colors from 'picocolors' | ||
import type { | ||
ArrayExpression, | ||
CallExpression, | ||
Expression, | ||
Literal, | ||
MemberExpression, | ||
Node, | ||
SequenceExpression, | ||
SpreadElement, | ||
TemplateLiteral, | ||
} from 'estree' | ||
import { parseExpressionAt } from 'acorn' | ||
import type { CustomPluginOptions, RollupError } from 'rollup' | ||
import { findNodeAt } from 'acorn-walk' | ||
import type { CustomPluginOptions, RollupAstNode, RollupError } from 'rollup' | ||
import MagicString from 'magic-string' | ||
import fg from 'fast-glob' | ||
import { stringifyQuery } from 'ufo' | ||
import type { GeneralImportGlobOptions } from 'types/importGlob' | ||
import { parseAstAsync } from 'rollup/parseAst' | ||
import type { Plugin } from '../plugin' | ||
import type { ViteDevServer } from '../server' | ||
import type { ModuleNode } from '../server/moduleGraph' | ||
|
@@ -218,7 +214,7 @@ export async function parseImportGlob( | |
resolveId: IdResolver, | ||
logger?: Logger, | ||
): Promise<ParsedImportGlob[]> { | ||
let cleanCode | ||
let cleanCode: string | ||
try { | ||
cleanCode = stripLiteral(code) | ||
} catch (e) { | ||
|
@@ -236,51 +232,30 @@ export async function parseImportGlob( | |
return e | ||
} | ||
|
||
let ast: CallExpression | SequenceExpression | MemberExpression | ||
let lastTokenPos: number | undefined | ||
|
||
try { | ||
ast = parseExpressionAt(code, start, { | ||
ecmaVersion: 'latest', | ||
sourceType: 'module', | ||
ranges: true, | ||
onToken: (token) => { | ||
lastTokenPos = token.end | ||
}, | ||
}) as any | ||
} catch (e) { | ||
const _e = e as any | ||
if (_e.message && _e.message.startsWith('Unterminated string constant')) | ||
return undefined! | ||
if (lastTokenPos == null || lastTokenPos <= start) throw _e | ||
|
||
// tailing comma in object or array will make the parser think it's a comma operation | ||
// we try to parse again removing the comma | ||
try { | ||
const statement = code.slice(start, lastTokenPos).replace(/[,\s]*$/, '') | ||
ast = parseExpressionAt( | ||
' '.repeat(start) + statement, // to keep the ast position | ||
start, | ||
{ | ||
ecmaVersion: 'latest', | ||
sourceType: 'module', | ||
ranges: true, | ||
}, | ||
) as any | ||
} catch { | ||
throw _e | ||
} | ||
const end = | ||
findCorrespondingCloseParenthesisPosition( | ||
cleanCode, | ||
start + match[0].length, | ||
) + 1 | ||
Comment on lines
+235
to
+239
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previous implementation relied on acorn to find the end position of |
||
if (end <= 0) { | ||
throw err('Close parenthesis not found') | ||
} | ||
|
||
const found = findNodeAt(ast as any, start, undefined, 'CallExpression') | ||
if (!found) throw err(`Expect CallExpression, got ${ast.type}`) | ||
ast = found.node as unknown as CallExpression | ||
const statementCode = code.slice(start, end) | ||
|
||
const rootAst = (await parseAstAsync(statementCode)).body[0] | ||
if (rootAst.type !== 'ExpressionStatement') { | ||
throw err(`Expect CallExpression, got ${rootAst.type}`) | ||
} | ||
const ast = rootAst.expression | ||
if (ast.type !== 'CallExpression') { | ||
throw err(`Expect CallExpression, got ${ast.type}`) | ||
} | ||
if (ast.arguments.length < 1 || ast.arguments.length > 2) | ||
throw err(`Expected 1-2 arguments, but got ${ast.arguments.length}`) | ||
|
||
const arg1 = ast.arguments[0] as ArrayExpression | Literal | TemplateLiteral | ||
const arg2 = ast.arguments[1] as Node | undefined | ||
const arg2 = ast.arguments[1] as RollupAstNode<Node> | undefined | ||
|
||
const globs: string[] = [] | ||
|
||
|
@@ -321,14 +296,12 @@ export async function parseImportGlob( | |
) | ||
|
||
options = parseGlobOptions( | ||
code.slice(arg2.range![0], arg2.range![1]), | ||
arg2.range![0], | ||
code.slice(start + arg2.start, start + arg2.end), | ||
start + arg2.start, | ||
logger, | ||
) | ||
} | ||
|
||
const end = ast.range![1] | ||
|
||
const globsResolved = await Promise.all( | ||
globs.map((glob) => toAbsoluteGlob(glob, root, importer, resolveId)), | ||
) | ||
|
@@ -348,6 +321,34 @@ export async function parseImportGlob( | |
return (await Promise.all(tasks)).filter(Boolean) | ||
} | ||
|
||
function findCorrespondingCloseParenthesisPosition( | ||
cleanCode: string, | ||
openPos: number, | ||
) { | ||
const closePos = cleanCode.indexOf(')', openPos) | ||
if (closePos < 0) return -1 | ||
|
||
if (!cleanCode.slice(openPos, closePos).includes('(')) return closePos | ||
|
||
let remainingParenthesisCount = 0 | ||
const cleanCodeLen = cleanCode.length | ||
for (let pos = openPos; pos < cleanCodeLen; pos++) { | ||
switch (cleanCode[pos]) { | ||
case '(': { | ||
remainingParenthesisCount++ | ||
break | ||
} | ||
case ')': { | ||
remainingParenthesisCount-- | ||
if (remainingParenthesisCount <= 0) { | ||
return pos | ||
} | ||
} | ||
} | ||
} | ||
return -1 | ||
} | ||
|
||
const importPrefix = '__vite_glob_' | ||
|
||
const { basename, dirname, relative, join } = posix | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
diff --git a/package.json b/package.json | ||
index 1b8dc76afc3cf5890cc3693c2975577fd3117dd6..9ac3a4d813fda1be476bd896a8f6168b3a459e41 100644 | ||
--- a/package.json | ||
+++ b/package.json | ||
@@ -46,5 +46,6 @@ | ||
}, | ||
"bin": { | ||
"acorn": "./bin/acorn" | ||
- } | ||
+ }, | ||
+ "sideEffects": false | ||
} | ||
bluwy marked this conversation as resolved.
Show resolved
Hide resolved
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
findStaticImports
usesacorn
internally, but for our use case, it was not needed.https://github.com/unjs/mlly/blob/e5e239dea49828723879e63301f6cec5a36ee5d5/src/analyze.ts#L97-L102
https://github.com/unjs/mlly/blob/e5e239dea49828723879e63301f6cec5a36ee5d5/src/analyze.ts#L409-L432