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(lexGlobPattern): edge case of glob import #6022

Merged
merged 2 commits into from
Dec 10, 2021
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
1 change: 1 addition & 0 deletions docs/guide/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ Note that:
- The glob patterns are treated like import specifiers: they must be either relative (start with `./`) or absolute (start with `/`, resolved relative to project root).
- The glob matching is done via `fast-glob` - check out its documentation for [supported glob patterns](https://github.com/mrmlnc/fast-glob#pattern-syntax).
- You should also be aware that glob imports do not accept variables, you need to directly pass the string pattern.
- The glob patterns cannot contain the same quote string (i.e. `'`, `"`, `` ` ``) as outer quotes, e.g. `'/Tom\'s files/**'`, use `"/Tom's files/**"` instead.

## WebAssembly

Expand Down
8 changes: 7 additions & 1 deletion packages/playground/glob-import/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

<script type="module" src="./dir/index.js"></script>
<script type="module">
const modules = import.meta.glob('/dir/**')
const modules = import.meta.glob(
'/dir/**'
// for test: annotation contain ")"
/*
* for test: annotation contain ")"
* */
)

for (const path in modules) {
modules[path]().then((mod) => {
Expand Down
34 changes: 33 additions & 1 deletion packages/vite/src/node/importGlob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,39 @@ function lexGlobPattern(code: string, pos: number): [string, number] {
throw new Error('unknown import.meta.glob lexer state')
}
}
return [pattern, code.indexOf(`)`, i) + 1]

const endIndex = getEndIndex(code, i)
return [pattern, endIndex + 1]
}

// reg without the 'g' option, only matches the first match
const multilineCommentsRE = /\/\*(.|[\r\n])*?\*\//m
const singlelineCommentsRE = /\/\/.*/

function getEndIndex(code: string, i: number): number {
const findStart = i
const endIndex = code.indexOf(`)`, findStart)
const subCode = code.substring(findStart)

const matched =
subCode.match(singlelineCommentsRE) ?? subCode.match(multilineCommentsRE)
if (!matched) {
return endIndex
}

const str = matched[0]
const index = matched.index
if (!index) {
return endIndex
}

const commentStart = findStart + index
const commentEnd = commentStart + str.length
if (endIndex > commentStart && endIndex < commentEnd) {
return getEndIndex(code, commentEnd)
} else {
return endIndex
}
}

function error(pos: number) {
Expand Down