Skip to content

feat: Pass through vite configLoader option to createViteServer #7574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Mar 26, 2025

Conversation

janis-me
Copy link
Contributor

@janis-me janis-me commented Feb 27, 2025

Description

(this is a draft to verify the implementation. It lacks tests)

This PR

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.
This exposes the option to the CLI, passes it to `ViteInlineConfig`s where needed and adds some docstrings to InlineConfig etc.
Copy link

netlify bot commented Feb 27, 2025

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 59630b5
🔍 Latest deploy log https://app.netlify.com/sites/vitest-dev/deploys/67e3b10fb6cd050008f4efef
😎 Deploy Preview https://deploy-preview-7574--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@@ -160,6 +160,7 @@ export function resolveConfig(
}

resolved.clearScreen = resolved.clearScreen ?? viteConfig.clearScreen ?? true
resolved.configLoader = resolved.configLoader ?? viteConfig.inlineConfig.configLoader ?? 'bundle'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am actually unsure about this part. Not sure if this is needed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I commented in #7574 (review), configLoader is something meaningful before resolving config, so it's likely we don't need to care about and thus should be removed.

@janis-me janis-me changed the title Feature: Add vite config loader (FORK, WIP) feat(dev):: Pass through vite configLoader option to createViteServer Feb 28, 2025
@janis-me janis-me changed the title feat(dev):: Pass through vite configLoader option to createViteServer feat(dev): Pass through vite configLoader option to createViteServer Feb 28, 2025
@janis-me janis-me closed this Feb 28, 2025
@hi-ogawa
Copy link
Contributor

hi-ogawa commented Mar 1, 2025

Hey, thanks for the PR 👋 Not sure if it's intentional, but why close? If you want to contribute, opening a PR here is correct instead of on your fork janis-me#1.

@janis-me
Copy link
Contributor Author

janis-me commented Mar 1, 2025

Hey, thanks for the PR 👋 Not sure if it's intentional, but why close? If you want to contribute, opening a PR here is correct instead of on your fork janis-me#1.

Hey!

I closed the one to vitest, because I didn't write any tests yet and just wanted to show my "idea" first. But I can re-open It with tests if you are mostly fine with the implementation

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Mar 1, 2025

Okay, don't worry about leaving an unfinished PR here. You can make it as draft and also fine to ask for the feedback through comments. 👍

@janis-me janis-me reopened this Mar 1, 2025
@hi-ogawa hi-ogawa changed the title feat(dev): Pass through vite configLoader option to createViteServer feat: Pass through vite configLoader option to createViteServer Mar 3, 2025
Copy link
Contributor

@hi-ogawa hi-ogawa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typing might be a bit tricky. We don't want UserConfig.configLoader to be available since that means it's allowing defineConfig({ test: { configLoader: "..." } }), but that doesn't work since configLoader is used to load the config.

We probably want to add only CliOptions.configLoader and typing needs to be tweaked on createVitest and initializeProject side.

export interface CliOptions extends UserConfig {

Also, configLoader is only available for certain Vite version, so it would be nice if we can detect that by infering the type from import("vite").InlineConfig["configLoader"] or something.

Copy link
Contributor

@hi-ogawa hi-ogawa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed a simple test case.

@@ -160,6 +160,7 @@ export function resolveConfig(
}

resolved.clearScreen = resolved.clearScreen ?? viteConfig.clearScreen ?? true
resolved.configLoader = resolved.configLoader ?? viteConfig.inlineConfig.configLoader ?? 'bundle'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I commented in #7574 (review), configLoader is something meaningful before resolving config, so it's likely we don't need to care about and thus should be removed.

Comment on lines 804 to 808
configLoader: {
description:
'Use `bundle` to bundle the config with esbuild or `runner` (experimental) to process it on the fly (default: `bundle`)',
argument: '<loader>',
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

documentation should also mention this is only available from newer Vite version.

janis-me added 2 commits March 3, 2025 09:08
Before, it was defined in the `InlineConfig` and thus `UserConfig` interfaces, which would not have worked when the option was passed to e.g `defineConfig(...)`
@janis-me
Copy link
Contributor Author

janis-me commented Mar 3, 2025

Typing might be a bit tricky. We don't want UserConfig.configLoader to be available since that means it's allowing defineConfig({ test: { configLoader: "..." } }), but that doesn't work since configLoader is used to load the config.

Good catch! I updated the PR to only include configLoader in the CliOptions interface. This means that I needed to change the parameter of createVitest and also needed to add configLoader to InitializeProjectOptions['test']. In both cases, I used vite's InlineConfig["configLoader"] for the type. I hope those should result in never if the vite version is below 6.1.0.

I also noticed, that in browser mode, the config is loaded correctly with the ModuleRunner, but afterwards the browser mode seems to be using the default ESM loader. I probably have to adjust the browser package as well. Looking into that.

Also, thanks for adding the test! I will extend on these to also test more 'complex' configurations.

@janis-me
Copy link
Contributor Author

janis-me commented Mar 3, 2025

I also noticed, that in browser mode, the config is loaded correctly with the ModuleRunner, but afterwards the browser mode seems to be using the default ESM loader.

The issue here is, that the createBrowserServer function is passed a TestProject instance, and that instance's options object doesn't set configLoader. I am unsure how to fix this, as TestProject is used in many different ways. I can see that the TestProject class is instanciated by calling initializeProject at

projectPromises.push(concurrent(() => initializeProject(
index,
vitest,
{ ...options, root, configFile, test: { ...options.test, ...cliOverrides } },
)))

So would I need to add configLoader to the overridesOptions? cliOptions in the resolveWorkspace function are typed as UserConfig though, not CliOptions.

const overridesOptions = [
'logHeapUsage',
'allowOnly',
'sequence',
'testTimeout',
'pool',

I might not be qualified for working on this feature after all xD

hi-ogawa
hi-ogawa previously approved these changes Mar 4, 2025
Copy link
Contributor

@hi-ogawa hi-ogawa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking into it 👍 I simplified a bit by passing vite.config.inlineConfig.configLoader to child vite servers, so we don't need to track the config on our typing, but this should essentially give the desired result.

sheremet-va
sheremet-va previously approved these changes Mar 4, 2025
@janis-me
Copy link
Contributor Author

janis-me commented Mar 5, 2025

Thanks a lot for carrying me through this.

Is there something left I can help with? It seems like the test is failing for me, I can investigate why that might be.

@hi-ogawa hi-ogawa added this to the 3.1.0 milestone Mar 5, 2025
Copy link

pkg-pr-new bot commented Mar 5, 2025

@vitest/browser

npm i https://pkg.pr.new/@vitest/browser@7574

@vitest/coverage-istanbul

npm i https://pkg.pr.new/@vitest/coverage-istanbul@7574

@vitest/expect

npm i https://pkg.pr.new/@vitest/expect@7574

@vitest/coverage-v8

npm i https://pkg.pr.new/@vitest/coverage-v8@7574

@vitest/mocker

npm i https://pkg.pr.new/@vitest/mocker@7574

@vitest/pretty-format

npm i https://pkg.pr.new/@vitest/pretty-format@7574

@vitest/runner

npm i https://pkg.pr.new/@vitest/runner@7574

@vitest/snapshot

npm i https://pkg.pr.new/@vitest/snapshot@7574

@vitest/spy

npm i https://pkg.pr.new/@vitest/spy@7574

@vitest/ui

npm i https://pkg.pr.new/@vitest/ui@7574

@vitest/utils

npm i https://pkg.pr.new/@vitest/utils@7574

vite-node

npm i https://pkg.pr.new/vite-node@7574

@vitest/web-worker

npm i https://pkg.pr.new/@vitest/web-worker@7574

vitest

npm i https://pkg.pr.new/vitest@7574

@vitest/ws-client

npm i https://pkg.pr.new/@vitest/ws-client@7574

commit: 7eee6bf

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Mar 5, 2025

Is there something left I can help with? It seems like the test is failing for me, I can investigate why that might be.

This will be merged when releasing next minor 3.1. Tests passed on CI, so maybe your local setup issue? If you want to verify if this change works on your project, you may try pkg-pr-new #7574 (comment).

@sheremet-va
Copy link
Member

Can you resolve merge conflicts?

@janis-me janis-me dismissed stale reviews from sheremet-va and hi-ogawa via 59630b5 March 26, 2025 07:47
@janis-me
Copy link
Contributor Author

Done. Note that the lockfile change only consists of the lines

      '@vitest/test-dep-linked':
        specifier: link:./deps/linked
        version: link:deps/linked

which is expected. See 59630b5 for the commit. Hope it looks okay

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants