Skip to content
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

feat: support resolve.alias per environment in Environment API #17583

Merged
merged 3 commits into from
Jul 19, 2024

Conversation

lazarv
Copy link
Contributor

@lazarv lazarv commented Jun 27, 2024

Description

This PR implements the suggested solution in #17582 and adds a test playground named environment-alias to check that the new alias plugin is using the resolve.alias specified per environment.

Copy link

stackblitz bot commented Jun 27, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@patak-dev
Copy link
Member

There are errors because the formatter wasn't run. It should be automatic when you commit. But you can run it manually if not.

We have a lot of playgrounds already, so we should try to avoid new ones as much as possible. Maybe you could try adding the test to the environment-react-ssr playground? Maybe later on we can have a more generic one for these tests.

@@ -40,7 +40,7 @@ export function createIdResolver(
pluginContainer = await createEnvironmentPluginContainer(
environment as Environment,
[
aliasPlugin({ entries: config.resolve.alias }), // TODO: resolve.alias per environment?
aliasPlugin({ entries: config.resolve.alias }),
Copy link
Member

Choose a reason for hiding this comment

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

This should be environment.options.resolve.alias, the config has the default values. We were thinking that we should deprecate these defaults on the ResolvedConfig and remove them later to avoid these issues.

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 kept this as a fallback and collecting the environment specific resolve.alias in the plugin, but if it's not needed, I can change this to pass it directly. Makes total sense as this part is creating a plugin container for the environment.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I see the new alias plugin. I think we don't need it. We can use the option here and keep everything else as is.

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 created the new plugin to work with resolve.alias out of the box. If you advise against that, then check my latest change. I changed the test to be a unit test and it's only using createIdResolver, but the consumer needs to handle the environment alias resolution using a custom plugin to make it work, check comment at #17583 (comment).

@lazarv
Copy link
Contributor Author

lazarv commented Jun 30, 2024

There are errors because the formatter wasn't run. It should be automatic when you commit. But you can run it manually if not.

I only see that the deadlock test is failing, which was failing before my changes too and also locally, but I will re-check the formatter.

We have a lot of playgrounds already, so we should try to avoid new ones as much as possible. Maybe you could try adding the test to the environment-react-ssr playground? Maybe later on we can have a more generic one for these tests.

I will give it a go, probably I can integrate these tests into that playground.

@lazarv
Copy link
Contributor Author

lazarv commented Jun 30, 2024

Re-run pnpm format, but no new changes. Could you please give me a link to the error?

@patak-dev
Copy link
Member

Re-run pnpm format, but no new changes. Could you please give me a link to the error?

Ah, sorry for the noise. Format is ok, nvm that.

@hi-ogawa
Copy link
Collaborator

We have a lot of playgrounds already, so we should try to avoid new ones as much as possible. Maybe you could try adding the test to the environment-react-ssr playground? Maybe later on we can have a more generic one for these tests.

I will give it a go, probably I can integrate these tests into that playground.

Maybe can we write this as unit test? We have a few tests manually target createServer and createBuilder API like below, so we could do the same for resolve.alias since it doesn't require driving web page and I think it's generally easier to author tests and faster.

test('virtual module invalidation', async () => {
const server = await createServer({

test('emitAssets', async () => {
const builder = await createBuilder({
root: resolve(__dirname, 'fixtures/emit-assets'),

I'm not sure if we have a single spec file testing both dev and build, but maybe we could add something like packages/vite/src/node/__tests__/environment.spec.ts?

@lazarv
Copy link
Contributor Author

lazarv commented Jul 1, 2024

Maybe can we write this as unit test?

Thanks @hi-ogawa very good suggestion! I implemented the test as a unit test now and yes it's much performant and also produces less noise.

@patak-dev I checked your suggested change and it works with one caveat. It seems the consumer needs to add a plugin and use the undocumented createIdResolver to resolve the id using the current environment.

let idResolver: ResolveIdFn
return {
  name: 'environment-alias-test-plugin',
  configResolved(config) {
    idResolver = createIdResolver(config, {})
  },
  async resolveId(id) {
    return await idResolver(this.environment, id)
  },
}

Copy link
Member

@patak-dev patak-dev left a comment

Choose a reason for hiding this comment

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

Sorry for the delay @lazarv! Thanks for the PR 🙏🏼

@patak-dev patak-dev merged commit 8794c49 into vitejs:v6/environment-api Jul 19, 2024
1 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants