Skip to main content
Deno 2 is finally here πŸŽ‰οΈ
Learn more

$Import πŸ¦₯ – Powerful Dynamic Import for Deno

⚑ A flexible and efficient utility for loading Deno modules dynamically β€” one file, many files, or entire folders.

Deno
Module Type


✨ Features

  • πŸ“„ Import a single file dynamically
  • πŸ“ Import all .ts, .js, etc. files in a folder
  • πŸ” Auto-detect main.ts (or custom entry) in subdirectories
  • πŸ” Handle multiple paths
  • πŸš€ Parallel loading of multiple files for optimal performance
  • 🧩 Import callback
  • πŸ“œ Filter allowed extensions
  • πŸ” Optional logging
  • 🧯 Fallback support on error

πŸ“¦ Installation

import { $Import } from "https://raw.githubusercontent.com/socle-commun/lib-core-deno/main/libs/import/mod.ts";

πŸ§ͺ Usage

const meta = import.meta.url;

// Import a single file
const mod = await $Import(meta, "./services/foo.ts");

// Import multiple files and folders
const all = await $Import(meta, [
  "./services/foo.ts",
  "./modules/",
]);

// Import with callback
await $Import(meta, "./config.ts", {
  callback: async (mod) => {
    console.log("Module loaded:", mod);
  },
});

πŸ”§ API

$Import<T>(
  metaUrl: string,
  path: string | string[],
  options?: SlothImportOptions<T>
): Promise<T>;

Options

interface SlothImportOptions<T> {
  callback?: (mod: T) => Promise<void>;
  entryFileName?: string; // default: "main.ts"
  allow?: SlothImportAllowedExtension[]; // default: ["ts"]
}

βš™οΈ Global Configuration

$Import.logging = true; // or custom logger function
$Import.fallback = (path, err) => ({ error: true });

$Import.config = {
  logging: false,
  entryFileName: "entry.ts",
  allow: ["ts"],
};

πŸ—‚ Auto-import main.ts from folders

Folder structure:

features/
β”œβ”€ feature-a/
β”‚  └─ main.ts
β”œβ”€ feature-b/
β”‚  └─ main.ts
await $Import(import.meta.url, "./features/");

Each main.ts file will be automatically imported.


πŸ”€ Supported Extensions

["ts", "js", "jsx", "tsx", "mts", "cts"]

Default to ["ts", "js"] Customizable via options.allow or config.allow.


🧱 Structure

libs/import/
β”œβ”€ mod.ts                  # Main entrypoint
β”œβ”€ config.ts               # Runtime config
β”œβ”€ types.ts                # Type declarations
β”œβ”€ constants.ts
β”œβ”€ processes/
β”‚  β”œβ”€ import-file/
β”‚  β”œβ”€ import-directory/
β”‚  β”œβ”€ resolve-path/
β”‚  β”œβ”€ handle-error/
β”‚  └─ log/
└─ _fixtures/              # Test modules

βœ… Test

deno task test

Each internal process has independent tests.


🐒 About Sloth

$Import is part of the Sloth πŸ¦₯ utility toolkit β€” designed to be minimal, lazy, and powerful for Deno developers.


πŸ›‘ License

MIT Β© 2025 Mistifiou