Dynamic To Static Env Transformer

1.0.0Last update Dec 17, 2024
by@priyanshuthapliyal2005

This codemod transforms import {$A} from '$env/dynamic/public'; and import {$A} from '$env/dynamic/private'; into their static counterparts, replacing them with import {$A} from '$env/static/public'; and import {$A} from '$env/static/private';, respectively. It ensures compatibility for prerendered pages and static deployments.

Note: This is a contrived example. Please modify it according to your use case.

Example

Before

// $env/dynamic/private
import { PRIVATE_API_KEY } from '$env/dynamic/private';
/** @type {import('./$types').PageServerLoad} */
export function load() {
const apiKey = PRIVATE_API_KEY;
return { apiKey };
}
// $env/dynamic/public
import { PUBLIC_API_URL } from '$env/dynamic/public';
/** @type {import('./$types').PageLoad} */
export function load() {
const apiUrl = PUBLIC_API_URL;
return { apiUrl };
}

After

// $env/static/private
import { PRIVATE_API_KEY } from '$env/static/private';
/** @type {import('./$types').PageServerLoad} */
export function load() {
const apiKey = PRIVATE_API_KEY;
return { apiKey };
}
// $env/static/public
import { PUBLIC_API_URL } from '$env/static/public';
/** @type {import('./$types').PageLoad} */
export function load() {
const apiUrl = PUBLIC_API_URL;
return { apiUrl };
}

Build custom codemods

Use AI-powered codemod studio and automate undifferentiated tasks for yourself, colleagues or the community

background illustrationGet Started Now