Skip to content
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
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function definePlugin(config: ResolvedConfig): Plugin {
userDefine[key] = handleDefineValue(environment.config.define[key])

// make sure `import.meta.env` object has user define properties
if (isBuild && key.startsWith('import.meta.env.')) {
if (isBundled && key.startsWith('import.meta.env.')) {
userDefineEnv[key.slice(16)] = environment.config.define[key]
}
}
Expand Down
60 changes: 38 additions & 22 deletions playground/css/__tests__/tests.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import { readFileSync } from 'node:fs'
import { expect, test } from 'vitest'
import {
bundledDevTodo,
editFile,
findAssetFile,
getBg,
getBgColor,
getColor,
isBuild,
isBundled,
isBundledDev,
isServe,
page,
removeFile,
serverLogs,
viteTestUrl,
} from '~utils'
import { sassModuleTests, sassOtherTests, sassTest } from './sass-tests'

const droppedEditTodo = bundledDevTodo(
'An edit that lands while the page is loading or before the HMR client connects is dropped, so the update never reaches the page',
{ rolldown: [10039] },
)

// note: tests should retrieve the element at the beginning of test and reuse it
// in later assertions to ensure CSS HMR doesn't reload the page
test('imported css', async () => {
Expand All @@ -33,13 +39,11 @@ test('linked css', async () => {

expect(await getColor(linked)).toBe('blue')
expect(await getColor(atImport)).toBe('red')
})

// bundled dev drops an edit that arrives at a bad moment. Two such moments:
// while the page is loading, and before the client has connected.
// The update is then lost and never reaches the page.
// That makes the edit steps below flaky when the suite runs in parallel
// (vitejs/vite#23028)
if (isBundled) return
test.runIf(isServe)('linked css HMR', droppedEditTodo, async () => {
const linked = await page.$('.linked')
const atImport = await page.$('.linked-at-import')

editFile('linked.css', (code) => code.replace('color: blue', 'color: red'))
await expect.poll(() => getColor(linked)).toBe('red')
Expand All @@ -56,9 +60,11 @@ test('css import from js', async () => {

expect(await getColor(imported)).toBe('green')
expect(await getColor(atImport)).toBe('purple')
})

// bundled dev: same dropped update as 'linked css' above
if (isBundled) return
test.runIf(isServe)('css import from js HMR', droppedEditTodo, async () => {
const imported = await page.$('.imported')
const atImport = await page.$('.imported-at-import')

editFile('imported.css', (code) => code.replace('color: green', 'color: red'))
await expect.poll(() => getColor(imported)).toBe('red')
Expand All @@ -78,9 +84,10 @@ test('css import asset with space', async () => {
test('postcss config', async () => {
const imported = await page.$('.postcss .nesting')
expect(await getColor(imported)).toBe('pink')
})

// bundled dev: same dropped update as 'linked css' above
if (isBundled) return
test.runIf(isServe)('postcss config HMR', droppedEditTodo, async () => {
const imported = await page.$('.postcss .nesting')

editFile('imported.css', (code) => code.replace('color: pink', 'color: red'))
await expect.poll(() => getColor(imported)).toBe('red')
Expand Down Expand Up @@ -414,31 +421,40 @@ test('minify css', async () => {
expect(cssFile).not.toMatch('#ffff00b3')
})

// bundled dev turns css?url into a data: URI of the source file, without
// running postcss on it. The rule this test looks for is never produced.
// This is a real bug (vitejs/vite#22863).
// The test passes again once it is fixed, with no change to the test.
test.skipIf(isBundledDev)('?url', async () => {
expect(await getColor('.url-imported-css')).toBe('yellow')
})
test(
'?url',
bundledDevTodo(
'`?url` CSS skips the CSS pipeline and is inlined as raw bytes, so PostCSS does not run on it',
{ vite: [22863] },
),
async () => {
expect(await getColor('.url-imported-css')).toBe('yellow')
},
)

test('?raw', async () => {
const rawImportCss = await page.$('.raw-imported-css')

expect(await rawImportCss.textContent()).toBe(
readFileSync(require.resolve('../raw-imported.css'), 'utf-8'),
)
})

// bundled dev: editing a ?raw import does not reach the page (vitejs/vite#23028)
if (!isBundled) {
test.runIf(isServe)(
'?raw HMR',
bundledDevTodo(
'This edit reloads the page. An edit that lands while the page is loading is dropped, so the later HMR edits in this file become flaky',
{ rolldown: [10039] },
),
async () => {
editFile('raw-imported.css', (code) =>
code.replace('color: yellow', 'color: blue'),
)
await expect
.poll(() => page.textContent('.raw-imported-css'))
.toMatch('color: blue')
}
})
},
)

test('import css in less', async () => {
expect(await getColor('.css-in-less')).toBe('yellow')
Expand Down
16 changes: 5 additions & 11 deletions playground/dynamic-import/__tests__/dynamic-import.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
getColor,
isBuild,
isBundled,
isBundledDev,
page,
serverLogs,
} from '~utils'
Expand Down Expand Up @@ -120,16 +119,11 @@ test('should load dynamic import with vars url', async () => {
.toMatch(isBundled ? 'data:text/javascript' : '/alias/url.js')
})

// bundled dev: workers created through the dynamic-import-vars glob aren't
// bundled as worker entries yet
test.skipIf(isBundledDev)(
'should load dynamic import with vars worker',
async () => {
await expect
.poll(() => page.textContent('.dynamic-import-with-vars-worker'))
.toMatch('load worker')
},
)
test('should load dynamic import with vars worker', async () => {
await expect
.poll(() => page.textContent('.dynamic-import-with-vars-worker'))
.toMatch('load worker')
})

test('should load dynamic import with css in package', async () => {
await page.click('.pkg-css')
Expand Down
2 changes: 1 addition & 1 deletion playground/env/__tests__/env.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ test('ssr', async () => {
expect(await page.textContent('.ssr')).toBe('false')
})

test.skipIf(isBundledDev)('env object', async () => {
test('env object', async () => {
const env = JSON.parse(await page.textContent('.env-object'))
expect(env).not.toHaveProperty([
'DEPEND_ENV',
Expand Down
Loading