Dynamic import ponyfill
A ponyfill for using dynamic imports in contexts without, like Deno Deploy, Deno compiled executables and older browsers (see #4).
Notes
- The
assert
option does not yet work (see #3) - JSX will work if configured in
deno.json
ordeno.jsonc
import.meta
will be{ main: false, url: '...', resolve(specifier) { return new URL(specifier, this.url).href } }
.
Example
import { importModule } from 'https://deno.land/x/import/mod.ts'
if (Math.random() > 0.5) {
await importModule('./foo.ts')
} else {
await importModule('./bar.ts')
}
This module also exports an awesome function that evaluates code from a string.
import { importString } from 'https://deno.land/x/import/mod.ts'
console.log(await importString('export const foo = "bar"'))
Options
export interface ImportModuleOptions {
/** Use of the ponyfill when native is available */
force?: boolean
}
export interface ImportStringOptions {
/** URL to use as the base for imports and exports */
base?: URL
}