
Security News
Crates.io Users Targeted by Phishing Emails
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
@lifebyspot/ts-transform-inject-environment-variables
Advanced tools
Injects hardcodede environment variables at compile time
This ttsc
compliant transformer allows you to inject environment variables on your code at compile time.
This is done by replacing the initializers of variable declarations. You can learn how to do that, too!
Run npm install
to save the package in your project.
npm install -D @lifebyspot/ts-transform-inject-environment-variables
Basically, there's two main scenarios you can use to inject variables using this plugin.
First, the variables defined using process.env
as initializer.
export const API_BASE_URL = process.env.API_BASE_URL;
export let ASDASDASDASD = process.env.ASDASDASDASD;
or
export const API_BASE_URL = process.env['API_BASE_URL'];
Will be converted into:
export const API_BASE_URL = 'https://api.example.com';
export let ASDASDASDASD;
Second, the variables defined using a destructuring syntax.
export const { API_BASE_URL } = process.env;
Will be converted into:
export const { API_BASE_URL = 'https://api.example.com' } = {};
Bonus: If no environment variable is found, the variable will be uninitialized. However, it's possible to use the nullish coalescing operator or object destructuring default values to define a default value to assign when this happens.
const { HOME = '/root' } = process.env;
const PATH = process.env.PATH ?? '/bin:/usr/bin';
Will be converted to:
const { HOME = '/root' } = {};
const PATH = '/bin:/usr/bin';
Note: Do not define variables that might be undefined as
const
.
export const ASDASDASDASD = process.env.ASDASDASDASD;
This is because, when running the compiled version, would end up with an uninitialized
const
variable, which is basically a violation of Javascript's rules.
export const ASDASDASDASD; // Not valid
Add the plugin to the plugins
list on tsconfig.json
.
{
// ...
"compilerOptions": {
"plugins": [
{
"transform": "@lifebyspot/ts-transform-inject-environment-variables",
"type": "config"
}
]
}
// ...
}
Just run ttsc
against your project working directory. That it.
Just send an issue or a PR! ;)
FAQs
Injects hardcodede environment variables at compile time
The npm package @lifebyspot/ts-transform-inject-environment-variables receives a total of 3 weekly downloads. As such, @lifebyspot/ts-transform-inject-environment-variables popularity was classified as not popular.
We found that @lifebyspot/ts-transform-inject-environment-variables demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 7 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
Product
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.