Skip to content

Commit

Permalink
esm: add import.meta.env
Browse files Browse the repository at this point in the history
  • Loading branch information
jkrems committed Sep 30, 2024
1 parent c08bb75 commit 703c545
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 0 deletions.
11 changes: 11 additions & 0 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,17 @@ added: v22.3.0

Enable exposition of [EventSource Web API][] on the global scope.

### `--experimental-import-meta-env`

<!-- YAML
added:
- REPLACEME
-->

> Stability: 1 - Experimental
Enable experimental `import.meta.env` support.

### `--experimental-import-meta-resolve`

<!-- YAML
Expand Down
14 changes: 14 additions & 0 deletions doc/api/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,20 @@ a second argument:
* `parent` {string|URL} An optional absolute parent module URL to resolve from.
**Default:** `import.meta.url`
### `import.meta.env`
<!--
added:
- REPLACEME
-->
> Stability: 1 - Experimental
This feature is only available with the `--experimental-import-meta-env`
command flag enabled.
Provides access to environment variables, just like `process.env`.
## Interoperability with CommonJS
### `import` statements
Expand Down
3 changes: 3 additions & 0 deletions doc/node.1
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ Interpret as either ES modules or CommonJS modules input via --eval or STDIN, wh
.js or extensionless files with no sibling or parent package.json;
.js or extensionless files whose nearest parent package.json lacks a "type" field, unless under node_modules.
.
.It Fl -experimental-import-meta-env
Enable experimental ES modules support for import.meta.env.
.
.It Fl -experimental-import-meta-resolve
Enable experimental ES modules support for import.meta.resolve().
.
Expand Down
5 changes: 5 additions & 0 deletions lib/internal/modules/esm/initialize_import_meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
const { getOptionValue } = require('internal/options');
const { fileURLToPath } = require('internal/url');
const { dirname } = require('path');
const experimentalImportMetaEnv = getOptionValue('--experimental-import-meta-env');
const experimentalImportMetaResolve = getOptionValue('--experimental-import-meta-resolve');

/**
Expand Down Expand Up @@ -65,6 +66,10 @@ function initializeImportMeta(meta, context, loader) {
meta.filename = filePath;
}

if (experimentalImportMetaEnv && (!loader || loader.loaderType !== 'internal')) {
meta.env = process.env;
}

if (!loader || loader.allowImportMetaResolve) {
meta.resolve = createImportMetaResolve(url, loader, experimentalImportMetaResolve);
}
Expand Down
4 changes: 4 additions & 0 deletions src/node_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
"experimental ES Module support for webassembly modules",
&EnvironmentOptions::experimental_wasm_modules,
kAllowedInEnvvar);
AddOption("--experimental-import-meta-env",
"experimental ES Module import.meta.env support",
&EnvironmentOptions::experimental_import_meta_env,
kAllowedInEnvvar);
AddOption("--experimental-import-meta-resolve",
"experimental ES Module import.meta.resolve() parentURL support",
&EnvironmentOptions::experimental_import_meta_resolve,
Expand Down
1 change: 1 addition & 0 deletions src/node_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class EnvironmentOptions : public Options {
bool experimental_global_navigator = true;
bool experimental_global_web_crypto = true;
bool experimental_wasm_modules = false;
bool experimental_import_meta_env = false;
bool experimental_import_meta_resolve = false;
std::string input_type; // Value of --input-type
std::string type; // Value of --experimental-default-type
Expand Down
22 changes: 22 additions & 0 deletions test/es-module/test-esm-import-meta-env.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Flags: --experimental-import-meta-env
import '../common/index.mjs';
import assert from 'assert/strict';
import { env } from 'process';

// Currently, the object is literally the same.
assert.equal(import.meta.env, env);
assert.equal(import.meta.env, process.env);

// It reflects changes to environment variables.
assert.equal(import.meta.env['TEST_VAR_1'], undefined);
process.env['TEST_VAR_1'] = 'x';
assert.equal(import.meta.env['TEST_VAR_1'], 'x');

// It allows modifying environment variables.
assert.equal(process.env['TEST_VAR_2'], undefined);
import.meta.env['TEST_VAR_1'] = 'x';
assert.equal(process.env['TEST_VAR_2'], 'x');

// It's possible to overwrite `import.meta.env`.
import.meta.env = 42;
assert.equal(import.meta.env, 42);

0 comments on commit 703c545

Please sign in to comment.