import { importPath , resolve } from "@kosko/require" ; import { createNodeEnvironment , NodeEnvironmentOptions } from "./node" ; import debug from "../debug" ; import { mergeAsync } from "../merge" ; import { Environment } from "./types" ; import { createAsyncReducerExecutor } from "./base" ; /**
 * Returns a new `Environment` which loads environment variables using ECMAScript
 * module (ESM) `import()` function.
 */ export function createNodeESMEnvironment ( options = { } ) { return createNodeEnvironment ( { ... options , createReducerExecutor : createAsyncReducerExecutor , mergeValues : mergeAsync , requireModule : async ( env , id ) => { let path ; try { path = await resolve ( id , { extensions : env . extensions . map ( ( ext ) => `.${ ext }` ) } ) ; } catch ( err ) { if ( err . code === "MODULE_NOT_FOUND" ) { debug ( "Cannot resolve module: %s" , id ) ; return { } ; } throw err ; } try { debug ( "Importing %s" , path ) ; const mod = await importPath ( path ) ; return mod . default ; } catch ( err ) { if ( [ "ERR_MODULE_NOT_FOUND" , "MODULE_NOT_FOUND" ] . includes ( err . code ) ) { debug ( "Cannot import module: %s" , path ) ; return { } ; } throw err ; } } } ) ; }