Proposal: Configurable Importing from Modules

Hello. I would like to share and propose some ideas for configuring modules' and their exported contents and functionalities. Below are indicated some of the existing ways to do this (examples 1, 2, and 3) and a proposed new syntax concept (example 4).

Example 1: Basic Import with Configuration Object

// config.js
export const config = {
  base: 'https://api.example.com'
};
// main.js
import { config } from './config.js';
import { initialize } from './module.js';

initialize(config);

Example 2: Named Imports with Configuration

// module.js
export function initialize(config) {
  console.log('Initializing with config:', config);
}

export function fetchData(endpoint) {
  // Fetch data logic
}
// main.js
import { initialize, fetchData } from './module.js';

const config = {
  base: 'https://api.example.com'
};

initialize(config);
fetchData('/data-endpoint');

Example 3: Default Import with Configuration

// module.js
export default function initialize(config) {
  console.log('Initializing with config:', config);
}
// main.js
import initialize from './module.js';

const config = {
  base: 'https://api.example.com'
};

initialize(config);

Example 4: New Syntax Concepts for Discussion

import { config } from './config.js';
import { widget } from './module.js' using config;
import { config } from './config.js';
import { widget } from './module.js' using { configuration: config };
import { widget } from './module.js' using { base: 'https://api.example.com' };

As envisioned, modules would receive these provided configuration objects, e.g., as special global values, to make use of to configure their exported contents and functionalities.

Thank you for any comments or feedback on these ideas.

I did some more searching online and found that ECMAScript 2026 already includes a with clause in its import syntax.

// Importing a module with specific conditions
import { featureA, featureB } from 'some-module' with { condition: 'value' };

I asked a search engine's AI assistant about this new language feature. It responded by providing the above example and it indicated that, in that example, that module is imported with a condition specified in that with clause. It indicated that this allows for more granular control over the import process, potentially enabling features like conditional imports based on runtime conditions.

It appears that there is a significant overlap between ECMAScript 2026's with clause, which was explained as enabling developers to import modules with conditions, and the ideas shared in the above post intending to enable developers to provide configuration...