Skip to content

Commit

Permalink
fix: silence import analysis warning
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Oct 24, 2024
1 parent 5df7414 commit 7e613b3
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/vitest/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { configDefaults, coverageConfigDefaults } from '../../defaults'
import { generateScopedClassName } from '../../integrations/css/css-modules'
import { resolveApiServerConfig } from '../config/resolveConfig'
import { Vitest } from '../core'
import { createViteLogger } from '../viteLogger'
import { createViteLogger, silenceImportViteIgnoreWarning } from '../viteLogger'
import { CoverageTransform } from './coverageTransform'
import { CSSEnablerPlugin } from './cssEnabler'
import { MocksPlugins } from './mocks'
Expand Down Expand Up @@ -140,6 +140,7 @@ export async function VitestPlugin(
allowClearScreen: false,
},
)
config.customLogger = silenceImportViteIgnoreWarning(config.customLogger)

// If "coverage.exclude" is not defined by user, add "test.include" to "coverage.exclude" automatically
if (userConfig.coverage?.enabled && !userConfig.coverage.exclude && userConfig.include && config.test) {
Expand Down
3 changes: 2 additions & 1 deletion packages/vitest/src/node/plugins/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { deepMerge } from '@vitest/utils'
import { basename, dirname, relative, resolve } from 'pathe'
import { configDefaults } from '../../defaults'
import { generateScopedClassName } from '../../integrations/css/css-modules'
import { createViteLogger } from '../viteLogger'
import { createViteLogger, silenceImportViteIgnoreWarning } from '../viteLogger'
import { CoverageTransform } from './coverageTransform'
import { CSSEnablerPlugin } from './cssEnabler'
import { MocksPlugins } from './mocks'
Expand Down Expand Up @@ -132,6 +132,7 @@ export function WorkspaceVitestPlugin(
allowClearScreen: false,
},
)
config.customLogger = silenceImportViteIgnoreWarning(config.customLogger)

return config
},
Expand Down
13 changes: 13 additions & 0 deletions packages/vitest/src/node/viteLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,16 @@ export function createViteLogger(

return logger
}

// silence warning by Vite for statically not analysizable dynamimc import
export function silenceImportViteIgnoreWarning(logger: Logger): Logger {
return {
...logger,
warn(msg, options) {
if (msg.includes('The above dynamic import cannot be analyzed by Vite')) {
return
}
logger.warn(msg, options)
},
}
}
2 changes: 1 addition & 1 deletion test/core/src/relative-import.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export function dynamicRelativeImport(file: string) {
return import(`./${file}.ts`)
return import(/* @vite-ignore */ `./${file}.ts`)
}
2 changes: 1 addition & 1 deletion test/core/test/imports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ test('dynamic import throws an error', async () => {
test('can import @vite/client', async () => {
const name = '@vite/client'
await expect(import(name)).resolves.not.toThrow()
await expect(import(`/${name}`)).resolves.not.toThrow()
await expect(import(/* @vite-ignore */ `/${name}`)).resolves.not.toThrow()
})

describe('importing special files from node_modules', async () => {
Expand Down

0 comments on commit 7e613b3

Please sign in to comment.