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
Support import.meta.env
#15833
Comments
which tools support it? only Vite.js? |
To my knowledge yes. Webpack 5 has taken a big step by stepping away from Node in bundled code by removing NodeJS library polyfills from being included in bundles. Removing ViteJS also provides a powerful tool to replace CommonJS |
yeah, webpack supports |
To be honestly I am always thinking what we should use |
For example we can found many issues like immerjs/immer#557 (and workaround immerjs/immer#557 (comment)) for these packages, |
Anyway we can merge #15841 to solve problems with packages which already migrate on |
@alexander-akait Just FYI, I looked at the tc39 proposal for Footnotes |
This issue had no activity for at least three months. It's subject to automatic issue closing if there is no activity in the next 15 days. |
bump, still need a dicussion (anyway it can be done using a plugin) |
You can try to use import-meta-loader. example: // vite => webpack
import.meta.env //=> process.env
import.meta.env.MODE //=> process.env.NODE_ENV
import.meta.env.BASE_URL //=> process.env.BASE_URL
import.meta.env.PROD //=> process.env.NODE_ENV === 'production'
import.meta.env.DEV //=> process.env.NODE_ENV !== 'production'
new URL('filePath', import.meta.url).href //=> require('filePath')
import.meta.glob('filePath') //=> ... require.context('dirPath', useSubdirectories: boolean, RegExp, 'lazy') ...
import.meta.glob('filePath', { eager: true }) //=> vite3 ... require.context('dirPath', useSubdirectories: boolean, RegExp, 'sync') ...
import.meta.globEager('filePath') //=> vite2 ... require.context('dirPath', useSubdirectories: boolean, RegExp, 'sync') ... |
I'm like to have a more standard way because |
Issue was closed because of inactivity. If you think this is still a valid issue, please file a new issue with additional information. |
The modern way of doing it in node is an explicit import: import {env} from 'node:process';
console.info(env.NODE_ENV); Maybe the runtime code of webpack could export it? Using special import {env} from 'webpack:runtime';
console.info(env.NODE_ENV); Could be used for other stuff as well: import {setPublicPath} from 'webpack:runtime';
setPublicPath('/'); The only downside is that |
I'd rather encourage the explicit import as mentioned above than rely on the bundler monkey patching attributes global variables that are then used in the code, after just having been bit by the problem. The dependency on the .env attributes leads to code that fails at runtime if you build it with the wrong bundler. This is just technical debt imposed on anyone that uses a bundler with this kind of API in their projects, and that future linter authors will have to add a rule for. |
Feature request
What is the expected behavior?
import.meta.env.NODE_ENV
What is motivation or use case for adding/changing the behavior?
migrate
process.env.NODE_ENV
How should this be implemented in your opinion?
No idea
Are you willing to work on this yourself?
No idea
The text was updated successfully, but these errors were encountered: