Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Apr 8, 2023
1 parent 1a4967a commit 25d6172
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 17 deletions.
4 changes: 4 additions & 0 deletions packages/vitest/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ export function defineConfig(config: UserConfigExport) {
export function defineWorkspace(config: UserWorkspaceConfigExport) {
return config
}

export function defineWorkspaces(config: (string | UserWorkspaceConfigExport)[]) {
return config
}
2 changes: 1 addition & 1 deletion packages/vitest/src/node/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class Logger {
if (!workspace.browser)
return
const name = workspace.getName()
const output = name === this.ctx.config.root || name === this.ctx.config.name ? '' : ` [${name}]`
const output = (name === this.ctx.config.root || name === this.ctx.config.name) ? '' : ` [${name}]`

this.log(c.dim(c.green(` ${output} Browser runner started at http://${workspace.config.browser.api?.host || 'localhost'}:${c.bold(`${workspace.browser.config.server.port}`)}`)))
})
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/node/plugins/globalSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async function loadGlobalSetupFile(file: string, runner: ViteNodeRunner): Promis
}
}

export const GlobalSetupPlugin = (workspace: SetupInstance, logger: Logger): Plugin => {
export function GlobalSetupPlugin(workspace: SetupInstance, logger: Logger): Plugin {
let globalSetupFiles: GlobalSetupFile[]
return {
name: 'vitest:global-setup-plugin',
Expand Down
28 changes: 21 additions & 7 deletions packages/vitest/src/node/plugins/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import { CSSEnablerPlugin } from './cssEnabler'
import { EnvReplacerPlugin } from './envReplacer'
import { GlobalSetupPlugin } from './globalSetup'

export function WorkspaceVitestPlugin(workspace: VitestWorkspace) {
interface WorkspaceOptions {
root: string
}

export function WorkspaceVitestPlugin(workspace: VitestWorkspace, options: WorkspaceOptions) {
return <VitePlugin[]>[
{
name: 'vitest:workspace',
Expand Down Expand Up @@ -49,14 +53,17 @@ export function WorkspaceVitestPlugin(workspace: VitestWorkspace) {
}
}

const options = workspace.ctx.config
const testConfig = viteConfig.test || {}

const root = testConfig.root || viteConfig.root || options.root

const config: ViteConfig = {
root,
resolve: {
// by default Vite resolves `module` field, which not always a native ESM module
// setting this option can bypass that and fallback to cjs version
mainFields: [],
alias: workspace.ctx.config.alias,
alias: testConfig.alias,
conditions: ['node'],
// eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error
// @ts-ignore we support Vite ^3.0, but browserField is available in Vite ^3.2
Expand Down Expand Up @@ -85,21 +92,28 @@ export function WorkspaceVitestPlugin(workspace: VitestWorkspace) {
},
}

const classNameStrategy = (typeof options.css !== 'boolean' && options.css?.modules?.classNameStrategy) || 'stable'
const classNameStrategy = (typeof testConfig.css !== 'boolean' && testConfig.css?.modules?.classNameStrategy) || 'stable'

if (classNameStrategy !== 'scoped') {
config.css ??= {}
config.css.modules ??= {}
config.css.modules.generateScopedName = (name: string, filename: string) => {
return generateScopedClassName(classNameStrategy, name, relative(options.root, filename))!
if (config.css.modules) {
config.css.modules.generateScopedName = (name: string, filename: string) => {
return generateScopedClassName(classNameStrategy, name, relative(root, filename))!
}
}
}

return config
},
async configureServer(server) {
try {
await workspace.setServer(deepMerge({}, configDefaults, server.config.test || {}), server)
const options = deepMerge(
{},
configDefaults,
server.config.test || {},
)
await workspace.setServer(options, server)
}
catch (err) {
await workspace.ctx.logger.printError(err, true)
Expand Down
9 changes: 5 additions & 4 deletions packages/vitest/src/node/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ export async function initializeWorkspace(workspacePath: string, ctx: Vitest) {
? false
: workspacePath

const root = dirname(workspacePath)

const config: ViteInlineConfig = {
root: dirname(workspacePath),
logLevel: 'error',
configFile,
// this will make "mode" = "test" inside defineConfig
mode: ctx.config.mode || process.env.NODE_ENV,
plugins: WorkspaceVitestPlugin(workspace),
plugins: WorkspaceVitestPlugin(workspace, { root }),
}

const server = await createServer(config)
Expand Down Expand Up @@ -62,7 +64,7 @@ export class VitestWorkspace {
constructor(
public path: string,
public ctx: Vitest,
) {}
) { }

getName() {
return this.config.name || dirname(this.path)
Expand Down Expand Up @@ -236,8 +238,7 @@ export class VitestWorkspace {
},
inspect: this.ctx.config.inspect,
inspectBrk: this.ctx.config.inspectBrk,
},
this.ctx.configOverride || {} as any,
}, this.ctx.configOverride || {} as any,
) as ResolvedConfig
}

Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 25d6172

Please sign in to comment.