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

[Vite] fileReplacements is not working for dev-server #13288

Closed
nodegin opened this issue Nov 21, 2022 · 9 comments
Closed

[Vite] fileReplacements is not working for dev-server #13288

nodegin opened this issue Nov 21, 2022 · 9 comments
Assignees
Labels
blocked: retry with latest Retry with latest release or head. outdated scope: bundlers Issues related to webpack, rollup type: bug

Comments

@nodegin
Copy link
Contributor

nodegin commented Nov 21, 2022

Current Behavior

fileReplacements is not working for dev-server (i.e. serve command)

Expected Behavior

When specifying the fileReplacements in the project.json and running with configuration, the file should also be replaced in local dev server, not just bundling production build.

Github Repo

No response

Steps to Reproduce

  1. Add fileReplacements to the project.json
  2. Run local dev server
  3. Observe if the file is replaced

Nx Report

>  NX   Report complete - copy this into the issue template

   Node : 16.15.1
   OS   : darwin arm64
   npm  : 8.11.0
   
   nx : 15.2.0
   @nrwl/angular : Not Found
   @nrwl/cypress : 15.2.0
   @nrwl/detox : Not Found
   @nrwl/devkit : 15.2.0
   @nrwl/esbuild : Not Found
   @nrwl/eslint-plugin-nx : 15.2.0
   @nrwl/expo : Not Found
   @nrwl/express : Not Found
   @nrwl/jest : 15.2.0
   @nrwl/js : 15.2.0
   @nrwl/linter : 15.2.0
   @nrwl/nest : Not Found
   @nrwl/next : Not Found
   @nrwl/node : Not Found
   @nrwl/nx-cloud : Not Found
   @nrwl/nx-plugin : Not Found
   @nrwl/react : 15.2.0
   @nrwl/react-native : Not Found
   @nrwl/rollup : 15.2.0
   @nrwl/schematics : Not Found
   @nrwl/storybook : 15.2.0
   @nrwl/web : 15.2.0
   @nrwl/webpack : 15.2.0
   @nrwl/workspace : 15.2.0
   typescript : 4.8.4
   ---------------------------------------
   Local workspace plugins:
   ---------------------------------------
   Community plugins:
   	 @nrwl/vite: 15.2.0

Failure Logs

No response

Additional Information

No response

@FrozenPandaz FrozenPandaz added the scope: bundlers Issues related to webpack, rollup label Nov 21, 2022
@mandarini
Copy link
Member

mandarini commented Nov 21, 2022

Yep, that was fixed in 15.2.1! :D

here's the PR

@mandarini mandarini added the blocked: retry with latest Retry with latest release or head. label Nov 21, 2022
@mandarini mandarini self-assigned this Nov 21, 2022
@mandarini
Copy link
Member

here's the other issue referencing it

@nodegin
Copy link
Contributor Author

nodegin commented Nov 23, 2022

@mandarini It's a different issue I guess, I tried 15.2.1 and the issue is not fixed yet:

image

I actually have created a PR before

@goinnovise
Copy link

@nodegin Did you ever find a solution for this? I am trying to swap out environment variables found in .ts file at build time in a Nx Vite app.

@mandarini
Copy link
Member

mandarini commented Feb 3, 2023

@nodegin when I merged your PR, the fileReplacements stopped working for the vite builder, so I had to revert it.

@nodegin
Copy link
Contributor Author

nodegin commented Feb 3, 2023

@goinnovise You can do the followings with latest version of @nrwl/vite.

  1. Create .env.XXX files depends on your configurations / environments
# /apps/web/.env.staging
VITE_APP_API_URL="https://stg.myapp.com"
# /apps/web/.env.production
VITE_APP_API_URL="https://myapp.com"
  1. Edit project.json to add your MODE
# /apps/web/project.json
...
    "build": {
      "configurations": {
        "production": {
          "mode": "production",
          "sourceMap": false
        },
        "staging": {
          "mode": "staging",
          "sourceMap": true
        }
      },
      "defaultConfiguration": "staging",
      "executor": "@nrwl/vite:build",
      "options": {
        "outputPath": "dist/apps/web"
      },
      "outputs": ["{options.outputPath}"]
    },
...
  1. Finally access the env var via import.meta.env.VITE_APP_API_URL
    Note: the typing might be missing initially which you have to prepare your own .d.ts definition, otherwise VSCode will not suggest according to your .env file.
interface ImportMeta {
  env: {
    MODE: 'staging' | 'production'
    VITE_APP_API_URL: string
  }
}

Which should be able to add configs for different envs.
But if you need to replace a certain file across different envs, it is not supported anymore.

@goinnovise
Copy link

@nodegin Thanks for responding. This was the route I ended up going yesterday.

@github-actions
Copy link

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 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
blocked: retry with latest Retry with latest release or head. outdated scope: bundlers Issues related to webpack, rollup type: bug
Projects
None yet
Development

No branches or pull requests

4 participants