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

Add support for the preview mode of vite #14125

Closed
vicb opened this issue Jan 4, 2023 · 11 comments · Fixed by #14412
Closed

Add support for the preview mode of vite #14125

vicb opened this issue Jan 4, 2023 · 11 comments · Fixed by #14412
Assignees
Labels
outdated scope: bundlers Issues related to webpack, rollup type: feature

Comments

@vicb
Copy link
Contributor

vicb commented Jan 4, 2023

Description

Vite supports a local web server for dev (invoked via vite) but it also supports serving the built files (invoked via vite preview)

The current serve target of the vite plugin uses the local dev server (vite).

It would be nice to add a preview target using the built files.

Motivation

The files served with vite do not match what will be served in production.

It would be nice to be able to test the actual files used in production (that is the output of the build target).

Suggested Implementation

I have tried to run vite preview from my app folder after having built the project.
I only had to to update build.outDir in vite.config.ts to make this work - that is set it to my app path. The reason for that is that this config is dynamically generated by nx/vite.

The implementation of the preview target should use that options-utils.ts to set the config.

I think it would also make sense to set the preview.proxy reusing this code

@mandarini
Copy link
Member

mandarini commented Jan 4, 2023

Hmm let me see if I get this right. Could you not see the production files by setting mode to production and also configuration to production when serving?

@mandarini mandarini added the scope: bundlers Issues related to webpack, rollup label Jan 4, 2023
@vicb
Copy link
Contributor Author

vicb commented Jan 4, 2023

mode is actually orthogonal to the matter here. The only effect of mode is to set some env variables. In that sense it is more related to the fileReplacements of Nx.

Vite does 2 main things:

  • in dev mode it bundles the dependencies with ESBuild and serves that along with a rewritten version of the app source code in ES format (for example it rewrites the import path in JS, inject scripts the html, ...)
  • in build mode it uses Rollup to bundle the app.

So those are 2 quite different things.

The dev mode is invoked via vite outside of Nx or the serve target with Nx.
The build is invoke via vite build outside of Nx or the Nx build target.
(And to be clear that's valid whatever the configuration and the mode are).

As explained above the built version is quite different from the dev version as they are built with a different set of tools. This is where the preview mode of Vite is valuable. What vite preview does is to serve the built files.

As explained in my first comment it should probably be implemented as an additional preview target depending on the build target. It would allow to test the files that are actually used in production.

Does this make sense?

A semi-related question is why the build target as "defaultConfiguration": "production". It is not consistent with what we have on the server side (i.e. an express app) so it prevent using nx run-many --target=build --projects=frontend,server without explicitly passing --configuration=....

@mandarini
Copy link
Member

Yes, makes sense! :) I would say we should add a flag in the generator to let the user choose if they want the preview target to be generated or not. Do you want to work on this?

@vicb
Copy link
Contributor Author

vicb commented Jan 4, 2023

I can probably take a look at that.

I am not sure if there should be a flag for the preview target as it's really a good practice to inspect what you will deploy in prod but we can discuss that later.

@roblebel
Copy link

roblebel commented Jan 6, 2023

I would appreciate the addition of this feature as well.

@vicb
Copy link
Contributor Author

vicb commented Jan 6, 2023

I start feeling the pressure :)
I hope to have something reviewable at the end of next week.

@mandarini mandarini self-assigned this Jan 9, 2023
@vicb
Copy link
Contributor Author

vicb commented Jan 11, 2023

Update: At this point I have a POC preview-server executor. I need to add more options and do some cleanup, probably a couple more days before I'll open a PR.

@mandarini can I submit a PR with the executor first and then a PR updating the generators? (The idea is to make each PR small enough so that they are easier to review).

@mandarini
Copy link
Member

@vicb sure, thanks! Indeed, you can make the executor PR, and push the generators in a later PR. :D :D

@mandarini
Copy link
Member

@vicb let me know if you think we can close this feature request now! :)

@vicb
Copy link
Contributor Author

vicb commented Jan 16, 2023

Maybe we can close it with my next PR that will be adding watch mode to the preview.

@github-actions
Copy link

github-actions bot commented Mar 3, 2023

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated scope: bundlers Issues related to webpack, rollup type: feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants