From d5a758adddaf993d23395df57478aa5a236368ee Mon Sep 17 00:00:00 2001 From: Tony Trinh Date: Tue, 30 Aug 2022 23:09:26 -0500 Subject: [PATCH 1/5] fix(env-replacer): don't modify string literals fix #1941 --- package.json | 3 +++ packages/vitest/src/node/plugins/envRelacer.ts | 7 +++++-- pnpm-lock.yaml | 4 +++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 544a5ff09ff6..04517f374019 100644 --- a/package.json +++ b/package.json @@ -89,5 +89,8 @@ "*.{js,ts,tsx,vue,md}": [ "eslint --fix" ] + }, + "dependencies": { + "strip-literal": "^0.4.0" } } diff --git a/packages/vitest/src/node/plugins/envRelacer.ts b/packages/vitest/src/node/plugins/envRelacer.ts index f2e7d6d9775e..ddf4b223bf60 100644 --- a/packages/vitest/src/node/plugins/envRelacer.ts +++ b/packages/vitest/src/node/plugins/envRelacer.ts @@ -1,5 +1,6 @@ import MagicString from 'magic-string' import type { Plugin } from 'vite' +import { stripLiteral } from 'strip-literal' // so people can reassign envs at runtime // import.meta.env.VITE_NAME = 'app' -> process.env.VITE_NAME = 'app' @@ -8,9 +9,11 @@ export const EnvReplacerPlugin = (): Plugin => { name: 'vitest:env-replacer', enforce: 'pre', transform(code) { - let s: MagicString | null = null + if (!/\bimport\.meta\.env\b/g.test(code)) + return null - const envs = code.matchAll(/\bimport\.meta\.env\b/g) + let s: MagicString | null = null + const envs = stripLiteral(code).matchAll(/\bimport\.meta\.env\b/g) for (const env of envs) { s ||= new MagicString(code) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b21a16313a62..6ceef8db2dec 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -46,12 +46,15 @@ importers: rollup-plugin-esbuild: ^4.10.1 rollup-plugin-license: ^2.8.1 simple-git-hooks: ^2.8.0 + strip-literal: ^0.4.0 ts-node: ^10.9.1 tsup: ^6.2.3 typescript: ^4.8.2 vite: ^3.0.9 vitest: workspace:* vue: latest + dependencies: + strip-literal: 0.4.0 devDependencies: '@antfu/eslint-config': 0.26.2_yqf6kl63nyoq5megxukfnom5rm '@antfu/ni': 0.17.2 @@ -16994,7 +16997,6 @@ packages: resolution: {integrity: sha512-ql/sBDoJOybTKSIOWrrh8kgUEMjXMwRAkZTD0EwiwxQH/6tTPkZvMIEjp0CRlpi6V5FMiJyvxeRkEi1KrGISoA==} dependencies: acorn: 8.8.0 - dev: true /style-loader/1.3.0_webpack@4.46.0: resolution: {integrity: sha512-V7TCORko8rs9rIqkSrlMfkqA63DfoGBBJmK1kKGCcSi+BWb4cqz0SRsnp4l6rU5iwOEd0/2ePv68SV22VXon4Q==} From aa3e6b0fb19a7b5146ee7528a831ade85bb7006d Mon Sep 17 00:00:00 2001 From: Tony Trinh Date: Wed, 31 Aug 2022 01:03:24 -0500 Subject: [PATCH 2/5] test(env-replacer): verify string literals are ignored --- test/core/test/env.test.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/core/test/env.test.ts b/test/core/test/env.test.ts index 67d154c358dd..c90d22526a55 100644 --- a/test/core/test/env.test.ts +++ b/test/core/test/env.test.ts @@ -43,3 +43,7 @@ test('custom env', () => { expect(process.env.CUSTOM_ENV).toBe('foo') expect(import.meta.env.CUSTOM_ENV).toBe('foo') }) + +test('ignores import.meta.env in string literals', () => { + expect('import.meta.env').toBe('import' + '.meta.env') +}) From 1303fd2968f5129b3894cccb146e0fea816dd39c Mon Sep 17 00:00:00 2001 From: Tony Trinh Date: Wed, 31 Aug 2022 01:06:33 -0500 Subject: [PATCH 3/5] chore: fix typo envRelacer.ts -> envReplacer.ts --- .../vitest/src/node/plugins/{envRelacer.ts => envReplacer.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename packages/vitest/src/node/plugins/{envRelacer.ts => envReplacer.ts} (100%) diff --git a/packages/vitest/src/node/plugins/envRelacer.ts b/packages/vitest/src/node/plugins/envReplacer.ts similarity index 100% rename from packages/vitest/src/node/plugins/envRelacer.ts rename to packages/vitest/src/node/plugins/envReplacer.ts From da8047c179d058e8035214508f360762b376c548 Mon Sep 17 00:00:00 2001 From: Tony Trinh Date: Wed, 31 Aug 2022 01:13:24 -0500 Subject: [PATCH 4/5] chore: fix typo envRelacer.ts -> envReplacer.ts --- packages/vitest/src/node/plugins/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vitest/src/node/plugins/index.ts b/packages/vitest/src/node/plugins/index.ts index 7024634b51dc..f0f02bda4475 100644 --- a/packages/vitest/src/node/plugins/index.ts +++ b/packages/vitest/src/node/plugins/index.ts @@ -4,7 +4,7 @@ import type { ResolvedConfig, UserConfig } from '../../types' import { deepMerge, ensurePackageInstalled, notNullish } from '../../utils' import { resolveApiConfig } from '../config' import { Vitest } from '../core' -import { EnvReplacerPlugin } from './envRelacer' +import { EnvReplacerPlugin } from './envReplacer' import { GlobalSetupPlugin } from './globalSetup' import { MocksPlugin } from './mock' import { CSSEnablerPlugin } from './cssEnabler' From efc2732b3b17082b9624e54c9408cbf032f6f2e3 Mon Sep 17 00:00:00 2001 From: Tony Trinh Date: Wed, 31 Aug 2022 19:43:15 -0500 Subject: [PATCH 5/5] chore: move strip-literal to correct package --- package.json | 3 --- packages/vitest/package.json | 1 + pnpm-lock.yaml | 6 +++--- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 04517f374019..544a5ff09ff6 100644 --- a/package.json +++ b/package.json @@ -89,8 +89,5 @@ "*.{js,ts,tsx,vue,md}": [ "eslint --fix" ] - }, - "dependencies": { - "strip-literal": "^0.4.0" } } diff --git a/packages/vitest/package.json b/packages/vitest/package.json index e7f8a90eb590..b669a5de033e 100644 --- a/packages/vitest/package.json +++ b/packages/vitest/package.json @@ -98,6 +98,7 @@ "chai": "^4.3.6", "debug": "^4.3.4", "local-pkg": "^0.4.2", + "strip-literal": "^0.4.0", "tinypool": "^0.2.4", "tinyspy": "^1.0.2", "vite": "^2.9.12 || ^3.0.0-0" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6ceef8db2dec..0f607f609035 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -46,15 +46,12 @@ importers: rollup-plugin-esbuild: ^4.10.1 rollup-plugin-license: ^2.8.1 simple-git-hooks: ^2.8.0 - strip-literal: ^0.4.0 ts-node: ^10.9.1 tsup: ^6.2.3 typescript: ^4.8.2 vite: ^3.0.9 vitest: workspace:* vue: latest - dependencies: - strip-literal: 0.4.0 devDependencies: '@antfu/eslint-config': 0.26.2_yqf6kl63nyoq5megxukfnom5rm '@antfu/ni': 0.17.2 @@ -791,6 +788,7 @@ importers: rollup: ~2.77.3 source-map-js: ^1.0.2 strip-ansi: ^7.0.1 + strip-literal: ^0.4.0 tinypool: ^0.2.4 tinyspy: ^1.0.2 typescript: ^4.8.2 @@ -804,6 +802,7 @@ importers: chai: 4.3.6 debug: 4.3.4 local-pkg: 0.4.2 + strip-literal: 0.4.0 tinypool: 0.2.4 tinyspy: 1.0.2 vite: 3.0.9 @@ -16607,6 +16606,7 @@ packages: /source-map/0.6.1: resolution: {integrity: sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==} engines: {node: '>=0.10.0'} + requiresBuild: true /source-map/0.7.4: resolution: {integrity: sha512-l3BikUxvPOcn5E74dZiq5BGsTb5yEwhaTSzccU6t4sDOH8NWJCstKO5QT2CvtFoK6F0saL7p9xHAqHOlCPJygA==}