From e18bf08a0fe3d6e984f18fe12cc403a67885bfa2 Mon Sep 17 00:00:00 2001 From: gtmnayan <50981692+gtm-nayan@users.noreply.github.com> Date: Tue, 29 Nov 2022 02:45:50 +0545 Subject: [PATCH 1/7] fix: glob import parsing (#10949) (#11056) closes https://github.com/vitejs/vite/issues/10949 closes https://github.com/vitejs/vite/pull/11051 --- package.json | 5 ++++ packages/vite/LICENSE.md | 29 +++++++++++++++++++ packages/vite/package.json | 1 + .../vite/src/node/plugins/importMetaGlob.ts | 13 +++------ pnpm-lock.yaml | 22 ++++++++++---- 5 files changed, 56 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 4ae32724899a1c..47f36632fc7767 100644 --- a/package.json +++ b/package.json @@ -121,6 +121,11 @@ "peerDependencies": { "postcss": "*" } + }, + "acorn-walk": { + "peerDependencies": { + "acorn": "*" + } } } } diff --git a/packages/vite/LICENSE.md b/packages/vite/LICENSE.md index d9c1270715bb5a..2a8bd0c969f14d 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 2baf81a7b9ba45..9020c73de7341d 100644 --- a/packages/vite/package.json +++ b/packages/vite/package.json @@ -79,6 +79,7 @@ "@rollup/plugin-typescript": "^8.5.0", "@rollup/pluginutils": "^4.2.1", "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 f0aac1e8dc98b5..f02265c98e293c 100644 --- a/packages/vite/src/node/plugins/importMetaGlob.ts +++ b/packages/vite/src/node/plugins/importMetaGlob.ts @@ -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' @@ -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}`) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3bdb11fc9d5ce3..caf3d36ffe550e 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: @@ -223,6 +223,7 @@ importers: '@rollup/plugin-typescript': ^8.5.0 '@rollup/pluginutils': ^4.2.1 acorn: ^8.8.1 + acorn-walk: ^8.2.0 cac: ^6.7.14 chokidar: ^3.5.3 connect: ^3.7.0 @@ -290,6 +291,7 @@ importers: '@rollup/plugin-typescript': 8.5.0_rollup@2.79.1+tslib@2.4.0 '@rollup/pluginutils': 4.2.1 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 @@ -3016,16 +3018,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==} @@ -8818,7 +8828,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 @@ -9147,6 +9157,8 @@ packages: '@types/chai': 4.3.3 '@types/chai-subset': 1.3.3 '@types/node': 17.0.42 + acorn: 8.8.1 + acorn-walk: 8.2.0_acorn@8.8.1 chai: 4.3.6 debug: 4.3.4 local-pkg: 0.4.2 From 294422defada0c34983deed034eff35d9a095f14 Mon Sep 17 00:00:00 2001 From: julienv3 Date: Mon, 28 Nov 2022 16:03:34 -0500 Subject: [PATCH 2/7] fix: import.meta.env and process.env undefined variable replacement (fix #8663) (#10958) Co-authored-by: bluwy fix https://github.com/vitejs/vite/issues/8663 --- packages/vite/src/node/plugins/define.ts | 2 +- playground/define/__tests__/define.spec.ts | 8 +++++++- playground/define/commonjs-dep/index.js | 6 +++++- playground/define/index.html | 8 +++++++- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/packages/vite/src/node/plugins/define.ts b/packages/vite/src/node/plugins/define.ts index e683ba4d0175db..30ea8171db90db 100644 --- a/packages/vite/src/node/plugins/define.ts +++ b/packages/vite/src/node/plugins/define.ts @@ -85,7 +85,7 @@ export function definePlugin(config: ResolvedConfig): Plugin { .join('|') + // Mustn't be followed by a char that can be part of an identifier // or an assignment (but allow equality operators) - ')(?![\\p{L}\\p{N}_$]|\\s*?=[^=])', + ')(?:(?<=\\.)|(?![\\p{L}\\p{N}_$]|\\s*?=[^=]))', 'gu' ) : null diff --git a/playground/define/__tests__/define.spec.ts b/playground/define/__tests__/define.spec.ts index 43787ef0adb112..8c27686bce1703 100644 --- a/playground/define/__tests__/define.spec.ts +++ b/playground/define/__tests__/define.spec.ts @@ -1,6 +1,6 @@ import { expect, test } from 'vitest' import viteConfig from '../vite.config' -import { page } from '~utils' +import { isBuild, page } from '~utils' test('string', async () => { const defines = viteConfig.define @@ -44,4 +44,10 @@ test('string', async () => { expect(await page.textContent('.define-in-dep')).toBe( defines.__STRINGIFIED_OBJ__ ) + expect(await page.textContent('.import-meta-env-undefined')).toBe( + isBuild ? '({}).UNDEFINED' : 'import.meta.env.UNDEFINED' + ) + expect(await page.textContent('.process-env-undefined')).toBe( + isBuild ? '({}).UNDEFINED' : 'process.env.UNDEFINED' + ) }) diff --git a/playground/define/commonjs-dep/index.js b/playground/define/commonjs-dep/index.js index 23e0bf1b32e32f..9be5641e04b844 100644 --- a/playground/define/commonjs-dep/index.js +++ b/playground/define/commonjs-dep/index.js @@ -1 +1,5 @@ -module.exports = { defined: __STRINGIFIED_OBJ__ } +module.exports = { + defined: __STRINGIFIED_OBJ__, + importMetaEnvUndefined: 'import.meta.env.UNDEFINED', + processEnvUndefined: 'process.env.UNDEFINED' +} diff --git a/playground/define/index.html b/playground/define/index.html index c4f4c598aba563..06b2f4f9479d38 100644 --- a/playground/define/index.html +++ b/playground/define/index.html @@ -17,6 +17,10 @@

Define

define variable in html: __EXP__

import json:

define in dep:

+

+ import.meta.env.UNDEFINED: +

+

process.env.UNDEFINED: