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

fix: glob import parsing (#10949) #11056

Merged
merged 5 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@
"peerDependencies": {
"postcss": "*"
}
},
"acorn-walk": {
"peerDependencies": {
"acorn": "*"
}
}
}
}
Expand Down
29 changes: 29 additions & 0 deletions packages/vite/LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,35 @@ Repository: https://github.com/acornjs/acorn.git

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

## acorn-walk
License: MIT
By: Marijn Haverbeke, Ingvar Stepanyan, Adrian Heine
Repository: https://github.com/acornjs/acorn.git

> MIT License
>
> Copyright (C) 2012-2020 by various contributors (see AUTHORS)
>
> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
> in the Software without restriction, including without limitation the rights
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> copies of the Software, and to permit persons to whom the Software is
> furnished to do so, subject to the following conditions:
>
> The above copyright notice and this permission notice shall be included in
> all copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
> THE SOFTWARE.

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

## ansi-regex
License: MIT
By: Sindre Sorhus
Expand Down
1 change: 1 addition & 0 deletions packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"@rollup/plugin-typescript": "^9.0.2",
"@rollup/pluginutils": "^5.0.2",
"acorn": "^8.8.1",
"acorn-walk": "^8.2.0",
"cac": "^6.7.14",
"chokidar": "^3.5.3",
"connect": "^3.7.0",
Expand Down
13 changes: 4 additions & 9 deletions packages/vite/src/node/plugins/importMetaGlob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
TemplateLiteral
} from 'estree'
import { parseExpressionAt } from 'acorn'
import { findNodeAt } from 'acorn-walk'
import MagicString from 'magic-string'
import fg from 'fast-glob'
import { stringifyQuery } from 'ufo'
Expand Down Expand Up @@ -154,15 +155,9 @@ export async function parseImportGlob(
}
}

if (ast.type === 'SequenceExpression')
ast = ast.expressions[0] as CallExpression

// immediate property access, call expression is nested
// import.meta.glob(...)['prop']
if (ast.type === 'MemberExpression') ast = ast.object as CallExpression

if (ast.type !== 'CallExpression')
throw err(`Expect CallExpression, got ${ast.type}`)
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

if (ast.arguments.length < 1 || ast.arguments.length > 2)
throw err(`Expected 1-2 arguments, but got ${ast.arguments.length}`)
Expand Down
22 changes: 16 additions & 6 deletions pnpm-lock.yaml

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