https://github.com/codex-/swc-plugin-import-meta-env
Simple @swc plugin to transform import.meta.env to process.env
https://github.com/codex-/swc-plugin-import-meta-env
Last synced: 12 months ago
JSON representation
Simple @swc plugin to transform import.meta.env to process.env
- Host: GitHub
- URL: https://github.com/codex-/swc-plugin-import-meta-env
- Owner: Codex-
- Created: 2023-07-09T22:07:33.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-03-19T21:00:39.000Z (12 months ago)
- Last Synced: 2025-03-19T22:21:34.911Z (12 months ago)
- Language: Rust
- Homepage:
- Size: 1010 KB
- Stars: 8
- Watchers: 2
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
Awesome Lists containing this project
README
# swc-plugin-import-meta-env
> Simple plugin to transform `import.meta.env` to `process.env`
[](https://www.npmjs.com/package/swc-plugin-import-meta-env)
This `@swc` plugin provides a simple transformation from `import.meta.env` to `process.env`.
The original purpose of this was to allow `@swc` usage in a large Jest test suite while also using Vite for bundling.
## Install 🌱
```shell
npm i -D swc-plugin-import-meta-env
```
## Usage 🚀
Simply add this to the plugins field of your `.swcrc`.
```json
{
"jsc": {
"experimental": {
"plugins": [["swc-plugin-import-meta-env", {}]]
}
}
}
```
Or programmatically as an extension to your existing `.swcrc` parsing:
```js
const swcrc = JSON.parse(fs.readFileSync(".swcrc", "utf8"));
((swcrc.jsc ??= {}).experimental ??= {}).plugins = [
["swc-plugin-import-meta-env", {}],
]; // This may need updating to suit your requirements
```
## How do I populate my environment? 🤔
The purpose of this plugin currently is to keep this transformation simple. There are many tools and utilities to load `.env` files into your environment already, such as performing this during your `setupTests` phase of testing.
- [`dotenv`](https://github.com/motdotla/dotenv)
- This is what [Vite itself uses to populate the env](https://vitejs.dev/guide/env-and-mode.html#env-files), so it makes sense to also do the same here.
If there is enough demand I can investigate adding this as core functionality to this plugin.