lxs24sxl / vite-plugin-html-env

A vite plugin for rewriting html
82 stars 10 forks source link

Not working when building for production #15

Closed dncore closed 2 years ago

dncore commented 2 years ago

inject an Env variable as a part of scripts tag path In the index.html file
<script src="<{ VITE_APP_DEMO_URL }>my/path/demo.js"></script> it only works for dev mode, when building, warns with info: <script src="<{ VITE_APP_DEMO_URL }>my/path/demo.js"> in "/index.html" can't be bundled without type="module" attribute after bundling, nothing changed with the script path for the index.html in dist folder, still <{ VITE_APP_DEMO_URL }>. it seems like the rollup bundler recognize the path as a file to bundle.

lxs24sxl commented 2 years ago

This warning is output by vite.

You can check the code.

  1. Are the environment variables read correctly?
  2. Are the prefix and suffix configured properly?
dncore commented 2 years ago

This warning is output by vite.

You can check the code.

  1. Are the environment variables read correctly?
  2. Are the prefix and suffix configured properly?

yes, all the environment variables got readed and prefix/suffix configured, but it only works properly when local serving

lxs24sxl commented 2 years ago

Can you provide a code snippet?

dncore commented 2 years ago

Can you provide a code snippet?

Just use the env variables as the Usage example in this repo,
the only difference is,
insert <script src="<{ VITE_APP_HOST }>***.js"></script> to html file, rather than <script src="//<{ VITE_APP_HOST }>***.js"></script> write variables in .env.xxx file,
VITE_APP_HOST should be'/'when dev serving and'https://www.my-site.com/resource/' when prod bundling.

as far as I think, this issue is caused by the conflict between my dev requirement and vite's template bundling strategy, but not the plugin. Anyway, thanks for your reply.