-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
[env] env file working properly, But build reported an error. #1396
Comments
See my next message....
|
OK. I found where the error message is coming from. The code is being parsed by @rollup/plugin-dynamic-import-vars, but the incoming code is already invalid. I captured the code that is being presented: https://github.com/JBusillo/Miscellaneous/blob/main/debug/before-dynamic-import-vars.js I placed the following in my source code to test: If you look at the My inkling is that the environment variable substitution is happening after whichever transform adds the This is as far as I got, but hopefully it gives someone a hint as to where the problem is. I can research this later, but the roosters are starting to crow, and I need some sleep. |
OK -- I looked into this a little more. SvelteKit invokes two builds, one for the client and one for the server, in module kit/src/core/build/index.js. The server build fails due to the internal vite plugin "vite/src/node/plugins/define.ts". It indiscriminately replaces all occurrences of the environment eye-catcher with the desired environment variable, including those within the source map. This causes the parse error as mentioned in my previous post. UPDATE: There is an open issue in Vite which addresses this: #vitejs/vite#3304 I've cloned the repo that @zhougonglai created, with a workaround. I created a separate .js file (src/lib/environmentVariables.js) that exports the environment variables. I then replaced all direct references to the variables (e.g., import.meta.env.VITE_SVELTEKIT_API_PATH and import.meta.env.VITE_SVELTEKIT_FILEPATH) to use the exported values -- in this case, envApiPath and envFilePath. The modified repo is at https://github.com/JBusillo/svelte-cms Let me know if there's anything further I can do. |
We filed vitejs/vite#3176 as our preferred solution on the server-side |
Thank you for your enthusiasm.🙏 im use js variable replace them export const file = 'https://image.xx.com/';
export const path = 'https://webapi.xxx.com'; |
Closing as duplicate of #720 |
Describe the bug:
Use env files
.env
working properly in
dev
。but on build throw error.
Stack trace
vite v2.2.4 building SSR bundle for production...✓ 8 modules transformed.
[rollup-plugin-dynamic-import-variables] Unexpected token (6:247)
file: xxxxx
> Unexpected token (6:247)
SyntaxError: Unexpected token (6:247)
the file
<script> await fetch(`${
import.meta.env.VITE_SVELTEKIT_API_PATH
}/api/news?${new URLSearchParams({
class_type: 2,
support_type: 1
})}`
).then(res => res.json());
</script>
The text was updated successfully, but these errors were encountered: