Skip to content

Add .env file support, and import.meta.env replacement - #1

Merged
FlorianRappl merged 2 commits into
FlorianRappl:mainfrom
mehrabix:add-mode-and-env-support
Jul 25, 2026
Merged

FlorianRappl merged 2 commits into
FlorianRappl:mainfrom
mehrabix:add-mode-and-env-support

Conversation

@mehrabix

@mehrabix mehrabix commented Jul 25, 2026 •

Copy link
Copy Markdown
Contributor

Types of Changes

Prerequisites

Please make sure you can check the following two boxes:

  • I have read the CONTRIBUTING document
  • My code follows the code style of this project

Contribution Type

What types of changes does your code introduce? Put an x in all the boxes that apply:

  • Bug fix (non-breaking change which fixes an issue, please reference the issue id)
  • New feature (non-breaking change which adds functionality, make sure to open an associated issue first)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • My change requires a change to the documentation
  • I have updated the documentation accordingly
  • I have added tests to cover my changes
  • All new and existing tests passed

Description

Adds developer experience features inspired by Vite:

Features

  • .env file support - Loads env variables from .env and .env.local (VITE_ prefix filtered by default)
  • import.meta.env.X replacement - Replaces env references in JS/TS with loaded values
  • --define flag - Replace global identifiers with constant expressions (e.g. --define process.env.NODE_ENV="production")
  • --alias flag - Rewrite import specifiers (e.g. --alias react=preact/compat)
  • --loader flag - Override file extension handling (e.g. --loader .svg=text)

Usage

# .env file (auto-loaded)
VITE_API_URL=https://api.example.com
VITE_APP_TITLE=My App

# Use import.meta.env in code
export const url = import.meta.env.VITE_API_URL;

# Define custom constants
npx netpack bundle src/index.html --define "__API_URL__='https://api.example.com'"

Files Changed

  • src/NetPack.Core/Graph/EnvLoader.cs - New .env file parser
  • src/NetPack.Core/Graph/BundlerContext.cs - Added EnvVars property
  • src/NetPack.Core/Graph/Traverse.cs - Env variable support
  • src/NetPack.Cli/Commands/BundleCommand.cs - .env loading + define/alias/loader flags
  • src/NetPack.Tests/CliOptionsTests.cs - Tests for define, alias, loader, import.meta.env
  • src/NetPack.Tests/EnvLoaderTests.cs - New test file for EnvLoader

Tests

All 427 tests passing.

Deferred in favor of future --preset + --define config system.
Keeps .env loading, import.meta.env, --define, --alias, --loader support.
EnvLoader now loads .env and .env.local (no mode-specific files).
All 427 tests pass.
@mehrabix
mehrabix force-pushed the add-mode-and-env-support branch from def21c8 to c36e023 Compare July 25, 2026 07:35
@mehrabix mehrabix changed the title Add --mode flag, .env file support, and import.meta.env replacement Add .env file support, and import.meta.env replacement Jul 25, 2026

@FlorianRappl FlorianRappl left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - much appreciated!

@FlorianRappl
FlorianRappl merged commit a7eabea into FlorianRappl:main Jul 25, 2026
2 checks passed
@FlorianRappl FlorianRappl added this to the 0.6.0 milestone Jul 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants