diff --git a/package.json b/package.json index 020cb5a12cd387..2f9a3b979fd4b3 100644 --- a/package.json +++ b/package.json @@ -122,6 +122,11 @@ "peerDependencies": { "postcss": "*" } + }, + "acorn-walk": { + "peerDependencies": { + "acorn": "*" + } } } } diff --git a/packages/vite/LICENSE.md b/packages/vite/LICENSE.md index 1548cbbc2444ef..fc49a93047979f 100644 --- a/packages/vite/LICENSE.md +++ b/packages/vite/LICENSE.md @@ -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 diff --git a/packages/vite/package.json b/packages/vite/package.json index 5a1af51126ec51..fca134de204f36 100644 --- a/packages/vite/package.json +++ b/packages/vite/package.json @@ -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", diff --git a/packages/vite/src/node/plugins/importMetaGlob.ts b/packages/vite/src/node/plugins/importMetaGlob.ts index a71bb20c147a75..ef313e39f6c85e 100644 --- a/packages/vite/src/node/plugins/importMetaGlob.ts +++ b/packages/vite/src/node/plugins/importMetaGlob.ts @@ -14,6 +14,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' @@ -163,15 +164,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}`) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 96b7ef388df45b..702ca2f7b7ef71 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4,7 +4,7 @@ overrides: vite: workspace:* '@vitejs/plugin-vue': workspace:* -packageExtensionsChecksum: 696422bac84dd936748019990f84746e +packageExtensionsChecksum: 2a87e01b470616d3b7def19cc0830231 importers: @@ -225,6 +225,7 @@ importers: '@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 @@ -292,6 +293,7 @@ importers: '@rollup/plugin-typescript': 9.0.2_rollup@3.3.0+tslib@2.4.1 '@rollup/pluginutils': 5.0.2_rollup@3.3.0 acorn: 8.8.1 + acorn-walk: 8.2.0_acorn@8.8.1 cac: 6.7.14 chokidar: 3.5.3 connect: 3.7.0 @@ -2953,16 +2955,24 @@ packages: resolution: {integrity: sha512-8mt+fslDufLYntIoPAaIMUe/lrbrehIiwmR3t2k9LljIzoigEPF27eLk2hy8zSGzmR/ogr7zbRKINMo1u0yh5A==} dependencies: acorn: 7.4.1 - acorn-walk: 7.2.0 + acorn-walk: 7.2.0_acorn@7.4.1 xtend: 4.0.2 - /acorn-walk/7.2.0: + /acorn-walk/7.2.0_acorn@7.4.1: resolution: {integrity: sha512-OPdCF6GsMIP+Az+aWfAAOEt2/+iVDKE7oy6lJ098aoe59oAmK76qV6Gw60SbZ8jHuG2wH058GF4pLFbYamYrVA==} engines: {node: '>=0.4.0'} + peerDependencies: + acorn: '*' + dependencies: + acorn: 7.4.1 - /acorn-walk/8.2.0: + /acorn-walk/8.2.0_acorn@8.8.1: resolution: {integrity: sha512-k+iyHEuPgSw6SbuDpGQM+06HQUa04DZ3o+F6CSzXMvvI5KMvnaEqXe+YVe555R9nn6GPt404fos4wcgpw12SDA==} engines: {node: '>=0.4.0'} + peerDependencies: + acorn: '*' + dependencies: + acorn: 8.8.1 /acorn/7.4.1: resolution: {integrity: sha512-nQyp0o1/mNdbTO1PO6kHkwSrmgZ0MT/jCCpNiwbUjGoRN4dlBhqJtoQuCnEOKzgTVwg0ZWiCoQy6SxMebQVh8A==} @@ -8590,7 +8600,7 @@ packages: '@tsconfig/node14': 1.0.1 '@tsconfig/node16': 1.0.2 acorn: 8.8.1 - acorn-walk: 8.2.0 + acorn-walk: 8.2.0_acorn@8.8.1 arg: 4.1.3 create-require: 1.1.1 diff: 4.0.2 @@ -8923,7 +8933,7 @@ packages: '@types/chai-subset': 1.3.3 '@types/node': 17.0.42 acorn: 8.8.1 - acorn-walk: 8.2.0 + acorn-walk: 8.2.0_acorn@8.8.1 chai: 4.3.6 debug: 4.3.4 local-pkg: 0.4.2