diff --git a/packages/playground/legacy/__tests__/legacy.spec.ts b/packages/playground/legacy/__tests__/legacy.spec.ts index aff02741711964..267b61025c8400 100644 --- a/packages/playground/legacy/__tests__/legacy.spec.ts +++ b/packages/playground/legacy/__tests__/legacy.spec.ts @@ -5,5 +5,22 @@ test('should work', async () => { }) test('import.meta.env.LEGACY', async () => { - expect(await page.textContent('#env')).toMatch(isBuild ? 'true' : 'false') + if (isBuild) { + expect(await page.textContent('#env')).toMatch('true') + + const modernSrc = await page.$eval( + 'script[type="module-disabled"]', + (el: HTMLScriptElement) => el.src + ) + + // false after the modern chunk was executed + await page.addScriptTag({ url: modernSrc, type: 'module' }) + expect(await page.textContent('#env')).toMatch('false') + + // reload so execution of the modern chunk can't mess with other tests + await page.reload() + } else { + // always false in dev + expect(await page.textContent('#env')).toMatch('false') + } }) diff --git a/packages/playground/legacy/main.js b/packages/playground/legacy/main.js index 84409b57793631..631f589eb541d8 100644 --- a/packages/playground/legacy/main.js +++ b/packages/playground/legacy/main.js @@ -5,6 +5,13 @@ async function run() { run() -document.getElementById('env').textContent = `is legacy: ${ - import.meta.env.LEGACY -}` +let isLegacy + +// make sure that branching works despite esbuild's constant folding (#1999) +if (import.meta.env.LEGACY) { + if (import.meta.env.LEGACY === true) isLegacy = true +} else { + if (import.meta.env.LEGACY === false) isLegacy = false +} + +document.getElementById('env').textContent = `is legacy: ${isLegacy}` diff --git a/packages/playground/legacy/vite.config.js b/packages/playground/legacy/vite.config.js index d4a5e4bd481b84..0652a2860770dd 100644 --- a/packages/playground/legacy/vite.config.js +++ b/packages/playground/legacy/vite.config.js @@ -15,13 +15,13 @@ module.exports = { }, // special test only hook - // for tests, remove `