Skip to main content

🐎 Dynamic import ponyfill!

A ponyfill for using dynamic imports in context without, like Deno Deploy, Deno compiled executables (see #1) and older browsers (see #4).

Notes

  1. The assert option does not yet work (see #3).
  2. Only the "imports" field from import maps works, the "scopes" will not.
  3. JSX will work if configured in the deno.json or deno.jsonc files, in Deno Deploy these are the only ones supported, but Deno compiled executables still have the same limitation.
  4. import.meta will be an empty object.

Example

import { importModule } from 'https://deno.land/x/import@v0.1.6/mod.ts'

if (Math.random() > 0.5) {
    await importModule('./foo.ts')
} else {
    await importModule('./bar.ts')
}

This module also exports an awesome function which evaluates code from a string containing import and exports statements.

import { importString } from 'https://deno.land/x/import@v0.1.6/mod.ts'

console.log(await importString('export const foo = "bar"'))

Options

interface ImportModuleOptions {
    /** Force the use of the ponyfill even when native dynamic import could be used. */
    force?: boolean
}

interface ImportStringOptions {
    /** The URL to use as a base for imports and exports in the string. */
    base?: URL | string
}